Skip to content

Commit

Permalink
Implement Painter's algorithm, fix Sliced
Browse files Browse the repository at this point in the history
  • Loading branch information
kvark committed Jan 5, 2020
1 parent 59a4f10 commit 53abc69
Show file tree
Hide file tree
Showing 10 changed files with 242 additions and 143 deletions.
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions config/settings.template.ron
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
// RayMipTraced (mip_count: 10, max_jumps: 25, max_steps: 100, debug: false),
// Scattered( density: (2, 2, 2) ),
// Sliced,
// Painted( density: 0.1, min_divisor: 0.5 ),
debug: (
max_vertices: 512,
collision_shapes: false,
Expand Down
29 changes: 29 additions & 0 deletions res/shader/terrain/locals.inc.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
layout(set = 1, binding = 1) uniform c_Locals {
uvec4 u_ScreenSize; // XY = size
uvec4 u_Params;
vec4 u_CamOriginDir; // XY = origin, ZW = dir
vec4 u_SampleRange; // XY = X range, ZW = y range
};

vec2 generate_scatter_pos(vec2 source_coord) {
float y;
if (true) {
float y_sqrt = mix(
sqrt(abs(u_SampleRange.z)) * sign(u_SampleRange.z),
sqrt(abs(u_SampleRange.w)) * sign(u_SampleRange.w),
source_coord.y
);
y = y_sqrt * y_sqrt * sign(y_sqrt);
} else {
y = mix(u_SampleRange.z, u_SampleRange.w, source_coord.y);
}

float x_limit = mix(
u_SampleRange.x, u_SampleRange.y,
(y - u_SampleRange.z) / (u_SampleRange.w - u_SampleRange.z)
);
float x = mix(-x_limit, x_limit, source_coord.x);

return u_CamOriginDir.xy + u_CamOriginDir.zw * y +
vec2(u_CamOriginDir.w, -u_CamOriginDir.z) * x;
}
37 changes: 37 additions & 0 deletions res/shader/terrain/paint.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//!include vs:globals.inc vs:terrain/locals.inc vs:surface.inc fs:surface.inc fs:color.inc

layout(location = 0) varying vec3 v_TexCoord;
layout(location = 1) flat varying uint v_Type;

#ifdef SHADER_VS

void main() {
float total_pixels = float(u_ScreenSize.x * u_ScreenSize.y);
uint pixel_index = uint(total_pixels * float(gl_InstanceIndex) / float(u_Params.x));

uvec2 pixel_pos = uvec2(pixel_index % u_ScreenSize.x, pixel_index / u_ScreenSize.x);
vec2 source_coord = 2.0 * vec2(pixel_pos) / vec2(u_ScreenSize.xy) - 1.0;
vec2 pos = generate_scatter_pos(source_coord);

Surface suf = get_surface(pos);
float altitude = gl_VertexIndex == 3 ? suf.high_alt :
gl_VertexIndex == 2 ? suf.low_alt + suf.delta :
gl_VertexIndex == 1 ? suf.low_alt : 0.0;

v_Type = gl_VertexIndex < 2 ? suf.low_type : suf.high_type;
v_TexCoord = vec3(suf.tex_coord, altitude / u_TextureScale.z);
gl_Position = u_ViewProj * vec4(pos, altitude, 1.0);
}
#endif //VS


#ifdef SHADER_FS
//imported: Surface, u_TextureScale, get_surface, evaluate_color

layout(location = 0) out vec4 o_Color;

void main() {
//TODO: move most of the evaluation to the vertex shader
o_Color = evaluate_color(v_Type, v_TexCoord.xy, v_TexCoord.z, 1.0);
}
#endif //FS
7 changes: 1 addition & 6 deletions res/shader/terrain/ray.glsl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//!include vs:globals.inc fs:globals.inc fs:surface.inc fs:color.inc
//!include vs:globals.inc fs:globals.inc fs:terrain/locals.inc fs:surface.inc fs:color.inc

#ifdef SHADER_VS

Expand All @@ -13,11 +13,6 @@ void main() {
#ifdef SHADER_FS
//imported: Surface, u_TextureScale, get_surface, evaluate_color

layout(set = 1, binding = 1) uniform c_Locals {
uvec4 u_ScreenSize; // XY = size
uvec4 u_Params;
};

const float
c_ReflectionVariance = 0.5,
c_ReflectionPower = 0.2;
Expand Down
7 changes: 1 addition & 6 deletions res/shader/terrain/ray_mip.glsl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//!include vs:globals.inc fs:globals.inc fs:surface.inc fs:color.inc
//!include vs:globals.inc fs:globals.inc fs:terrain/locals.inc fs:surface.inc fs:color.inc

#ifdef SHADER_VS

Expand All @@ -13,11 +13,6 @@ void main() {
#ifdef SHADER_FS
//imported: Surface, u_TextureScale, get_lod_height, get_surface, evaluate_color

layout(set = 1, binding = 1) uniform Locals {
uvec4 u_ScreenSize; // XY = size
uvec4 u_Params; // X = max mipmap level, Y = max iterations
};

#define TERRAIN_WATER 0U

const float c_Step = 0.6;
Expand Down
42 changes: 5 additions & 37 deletions res/shader/terrain/scatter.glsl
Original file line number Diff line number Diff line change
@@ -1,46 +1,11 @@
//!include cs:globals.inc cs:surface.inc cs:color.inc
//!include cs:globals.inc cs:terrain/locals.inc cs:surface.inc cs:color.inc

#ifdef SHADER_CS
//imported: Surface, get_surface, evaluate_color_id

layout(set = 1, binding = 1) uniform c_Locals {
uvec4 u_ScreenSize; // XY = size
};
layout(set = 2, binding = 0, std430) buffer Storage {
uint w_Data[];
};
layout(set = 2, binding = 1) uniform c_Scatter {
vec2 u_CamOrigin;
vec2 u_CamDir;
vec2 u_RangeY;
vec2 u_RangeX;
};


vec2 generate_pos() {
vec2 screen_coord = vec2(gl_GlobalInvocationID.xy) /
vec2(gl_NumWorkGroups.xy * gl_WorkGroupSize.xy - vec2(1));

float y;
if (true) {
float y_sqrt = mix(
sqrt(abs(u_RangeY.x)) * sign(u_RangeY.x),
sqrt(abs(u_RangeY.y)) * sign(u_RangeY.y),
screen_coord.y
);
y = y_sqrt * y_sqrt * sign(y_sqrt);
} else {
y = mix(u_RangeY.x, u_RangeY.y, screen_coord.y);
}

float x_limit = mix(
u_RangeX.x, u_RangeX.y,
(y - u_RangeY.x) / (u_RangeY.y - u_RangeY.x)
);
float x = mix(-x_limit, x_limit, screen_coord.x);

return u_CamOrigin + u_CamDir * y + vec2(u_CamDir.y, -u_CamDir.x) * x;
}

bool is_visible(vec4 p) {
return p.w > 0.0 && p.z >= 0.0 &&
Expand All @@ -62,7 +27,10 @@ void add_voxel(vec2 pos, float altitude, uint type, float lit_factor) {
}

void main() {
vec2 pos = generate_pos();
vec2 source_coord = vec2(gl_GlobalInvocationID.xy) /
vec2(gl_NumWorkGroups.xy * gl_WorkGroupSize.xy - vec2(1));
vec2 pos = generate_scatter_pos(source_coord);

Surface suf = get_surface(pos);
float base = 0.0;
float t = float(gl_GlobalInvocationID.z) / float(gl_NumWorkGroups.z * gl_WorkGroupSize.z);
Expand Down
2 changes: 1 addition & 1 deletion res/shader/terrain/slice.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ layout(location = 0) varying vec4 v_Pos;
layout(location = 0) attribute ivec4 a_Pos;

void main() {
v_Pos = vec4(ivec4(a_Pos.xy * u_TextureScale.xy, 255 - gl_InstanceIndex, 1));
v_Pos = vec4(a_Pos.xy * u_TextureScale.xy, u_TextureScale.z - float(gl_InstanceIndex + 1), 1.0);
gl_Position = u_ViewProj * v_Pos;
}
#endif //VS
Expand Down
4 changes: 4 additions & 0 deletions src/config/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ pub enum Terrain {
screen_space: bool,
},
Sliced,
Painted {
density: f32,
min_divisor: f32,
},
Scattered {
density: [u32; 3],
},
Expand Down
Loading

0 comments on commit 53abc69

Please sign in to comment.