Ben Clayton | 73ced33 | 2022-01-21 22:38:16 +0000 | [diff] [blame] | 1 | let shadowDepthTextureSize : f32 = 1024.0; |
| 2 | |
| 3 | struct Scene { |
James Price | 3b671cb | 2022-03-28 14:31:22 +0000 | [diff] [blame] | 4 | lightViewProjMatrix : mat4x4<f32>, |
| 5 | cameraViewProjMatrix : mat4x4<f32>, |
| 6 | lightPos : vec3<f32>, |
Ben Clayton | 73ced33 | 2022-01-21 22:38:16 +0000 | [diff] [blame] | 7 | } |
| 8 | |
| 9 | @group(0) @binding(0) var<uniform> scene : Scene; |
| 10 | |
| 11 | @group(0) @binding(1) var shadowMap : texture_depth_2d; |
| 12 | |
| 13 | @group(0) @binding(2) var shadowSampler : sampler_comparison; |
| 14 | |
| 15 | struct FragmentInput { |
| 16 | @location(0) |
James Price | 3b671cb | 2022-03-28 14:31:22 +0000 | [diff] [blame] | 17 | shadowPos : vec3<f32>, |
Ben Clayton | 73ced33 | 2022-01-21 22:38:16 +0000 | [diff] [blame] | 18 | @location(1) |
James Price | 3b671cb | 2022-03-28 14:31:22 +0000 | [diff] [blame] | 19 | fragPos : vec3<f32>, |
Ben Clayton | 73ced33 | 2022-01-21 22:38:16 +0000 | [diff] [blame] | 20 | @location(2) |
James Price | 3b671cb | 2022-03-28 14:31:22 +0000 | [diff] [blame] | 21 | fragNorm : vec3<f32>, |
Ben Clayton | 73ced33 | 2022-01-21 22:38:16 +0000 | [diff] [blame] | 22 | } |
| 23 | |
Ben Clayton | c64ca23 | 2022-06-29 00:55:36 +0000 | [diff] [blame] | 24 | const albedo : vec3<f32> = vec3<f32>(0.899999976, 0.899999976, 0.899999976); |
Ben Clayton | 73ced33 | 2022-01-21 22:38:16 +0000 | [diff] [blame] | 25 | |
Ben Clayton | c64ca23 | 2022-06-29 00:55:36 +0000 | [diff] [blame] | 26 | const ambientFactor : f32 = 0.200000003; |
Ben Clayton | 73ced33 | 2022-01-21 22:38:16 +0000 | [diff] [blame] | 27 | |
dan sinclair | b29892b | 2022-06-07 13:55:34 +0000 | [diff] [blame] | 28 | @fragment |
Ben Clayton | 73ced33 | 2022-01-21 22:38:16 +0000 | [diff] [blame] | 29 | fn main(input : FragmentInput) -> @location(0) vec4<f32> { |
| 30 | var visibility : f32 = 0.0; |
| 31 | let oneOverShadowDepthTextureSize = (1.0 / shadowDepthTextureSize); |
| 32 | for(var y : i32 = -1; (y <= 1); y = (y + 1)) { |
| 33 | for(var x : i32 = -1; (x <= 1); x = (x + 1)) { |
| 34 | let offset : vec2<f32> = vec2<f32>((f32(x) * oneOverShadowDepthTextureSize), (f32(y) * oneOverShadowDepthTextureSize)); |
| 35 | visibility = (visibility + textureSampleCompare(shadowMap, shadowSampler, (input.shadowPos.xy + offset), (input.shadowPos.z - 0.007))); |
| 36 | } |
| 37 | } |
| 38 | visibility = (visibility / 9.0); |
| 39 | let lambertFactor : f32 = max(dot(normalize((scene.lightPos - input.fragPos)), input.fragNorm), 0.0); |
| 40 | let lightingFactor : f32 = min((ambientFactor + (visibility * lambertFactor)), 1.0); |
| 41 | return vec4<f32>((lightingFactor * albedo), 1.0); |
| 42 | } |