From 67c4aa07abdf62730013939d8a5cb779bbe917da Mon Sep 17 00:00:00 2001 From: Quaternions Date: Wed, 6 Sep 2023 19:15:50 -0700 Subject: [PATCH] give the checkerboard "reflections" --- src/shader.wgsl | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/src/shader.wgsl b/src/shader.wgsl index 34236ab..caf8548 100644 --- a/src/shader.wgsl +++ b/src/shader.wgsl @@ -98,8 +98,14 @@ fn vs_entity( return result; } +struct SquareOutput { + @builtin(position) position: vec4, + @location(2) normal: vec3, + @location(3) view: vec3, + @location(4) pos: vec3, +}; @vertex -fn vs_square(@builtin(vertex_index) vertex_index: u32) -> GroundOutput { +fn vs_square(@builtin(vertex_index) vertex_index: u32) -> SquareOutput { // hacky way to draw two triangles that make a square let tmp1 = i32(vertex_index)/2-i32(vertex_index)/3; let tmp2 = i32(vertex_index)&1; @@ -109,8 +115,10 @@ fn vs_square(@builtin(vertex_index) vertex_index: u32) -> GroundOutput { 0.0 ); - var result: GroundOutput; + var result: SquareOutput; + result.normal = vec3(0.0,0.0,1.0); result.pos = (transform.transform * vec4(pos, 1.0)).xyz; + result.view = result.pos - r_data.cam_pos.xyz; result.position = r_data.proj * r_data.view * transform.transform * vec4(pos, 1.0); return result; } @@ -159,14 +167,22 @@ fn fs_ground(vertex: GroundOutput) -> @location(0) vec4 { } @fragment -fn fs_checkered(vertex: GroundOutput) -> @location(0) vec4 { +fn fs_checkered(vertex: SquareOutput) -> @location(0) vec4 { let voxel_parity: f32 = f32( u32(modulo_euclidean(vertex.pos.x,2.0)<1.0) ^ u32(modulo_euclidean(vertex.pos.y,2.0)<1.0) //^ u32(modulo_euclidean(vertex.pos.z,2.0)<1.0) ); - return vec4(vec3(1.0)*voxel_parity, 1.0); + + let incident = normalize(vertex.view); + let normal = normalize(vertex.normal); + let d = dot(normal, incident); + let reflected = incident - 2.0 * d * normal; + + let texture_color = vec3(1.0)*voxel_parity; + let reflected_color = textureSample(r_texture, r_sampler, reflected).rgb; + return vec4(mix(vec3(0.1) + 0.5 * reflected_color,texture_color,1.0-pow(1.0-abs(d),2.0)), 1.0); } @fragment -fn fs_overwrite(vertex: GroundOutput) {} \ No newline at end of file +fn fs_overwrite(vertex: SquareOutput) {} \ No newline at end of file