Skip to content

Commit

Permalink
Update the old ray shader
Browse files Browse the repository at this point in the history
  • Loading branch information
kvark committed Mar 19, 2019
1 parent c944bbe commit 0ed9e33
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 14 deletions.
10 changes: 3 additions & 7 deletions data/shader/surface.inc.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,16 @@ int modulo(int a, int b) {
float get_lod_height(ivec2 ipos, int lod) {
int x = modulo(ipos.x, int(u_TextureScale.x));
int y = modulo(ipos.y, int(u_TextureScale.y));
ivec3 tc = ivec3(
x >> lod, y >> lod,
modulo((ipos.y - y) / int(u_TextureScale.y), int(u_TextureScale.w))
);
float alt = texelFetch(t_Height, tc, lod).x;
ivec2 tc = ivec2(x, y) >> lod;
float alt = texelFetch(sampler2D(t_Height, s_MainSampler), tc, lod).x;
return alt * u_TextureScale.z;
}

// The alternative version of this routine that doesn't use
// integer operations or `texelFetch`.
float get_lod_height_alt(ivec2 ipos, int lod) {
vec2 xy = (vec2(ipos.xy) + 0.5) / u_TextureScale.xy;
float z = trunc(mod(float(ipos.y) / u_TextureScale.y, u_TextureScale.w));
float alt = textureLod(t_Height, vec3(xy, z), float(lod)).x;
float alt = textureLod(sampler2D(t_Height, s_MainSampler), xy, float(lod)).x;
return alt * u_TextureScale.z;
}

Expand Down
2 changes: 2 additions & 0 deletions data/shader/terrain_ray.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ layout(set = 1, binding = 1) uniform Locals {

#define TERRAIN_WATER 0U

const float c_Step = 0.6;

layout(location = 0) out vec4 o_Color;

vec3 cast_ray_to_plane(float level, vec3 base, vec3 dir) {
Expand Down
14 changes: 8 additions & 6 deletions data/shader/terrain_ray_old.glsl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//!include fs:surface.inc fs:color.inc

uniform c_Globals {
layout(set = 0, binding = 0) uniform c_Globals {
vec4 u_CameraPos;
mat4 u_ViewProj;
mat4 u_InvViewProj;
Expand All @@ -11,18 +11,20 @@ uniform c_Globals {

#ifdef SHADER_VS

attribute vec4 a_Pos;
layout(location = 0) attribute vec4 a_Pos;

void main() {
gl_Position = u_ViewProj * a_Pos;
// convert from -1,1 Z to 0,1
gl_Position.z = 0.5 * (gl_Position.z + gl_Position.w);
}
#endif //VS


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

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

Expand All @@ -32,7 +34,7 @@ const float

#define TERRAIN_WATER 0U

out vec4 Target0;
layout(location = 0) out vec4 o_Color;


vec3 cast_ray_to_plane(float level, vec3 base, vec3 dir) {
Expand Down Expand Up @@ -85,7 +87,7 @@ Surface cast_ray_impl(
struct CastPoint {
vec3 pos;
uint type;
vec3 tex_coord;
vec2 tex_coord;
bool is_underground;
//bool is_shadowed;
};
Expand Down Expand Up @@ -180,7 +182,7 @@ void main() {
frag_color += c_ReflectionPower * ref_color;
}
}
Target0 = frag_color;
o_Color = frag_color;

vec4 target_ndc = u_ViewProj * vec4(pt.pos, 1.0);
gl_FragDepth = target_ndc.z / target_ndc.w * 0.5 + 0.5;
Expand Down
2 changes: 1 addition & 1 deletion src/render/terrain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl Context {
layout: &wgpu::PipelineLayout,
device: &wgpu::Device,
) -> wgpu::RenderPipeline {
let shaders = Shaders::new("terrain_ray", &[], device)
let shaders = Shaders::new("terrain_ray_old", &[], device)
.unwrap();
device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
layout,
Expand Down

0 comments on commit 0ed9e33

Please sign in to comment.