blob: c2da83859f999950efbabbd274936dc184962559 [file] [log] [blame]
Ben Clayton73ced332022-01-21 22:38:16 +00001let shadowDepthTextureSize : f32 = 1024.0;
2
3struct Scene {
James Price3b671cb2022-03-28 14:31:22 +00004 lightViewProjMatrix : mat4x4<f32>,
5 cameraViewProjMatrix : mat4x4<f32>,
6 lightPos : vec3<f32>,
Ben Clayton73ced332022-01-21 22:38:16 +00007}
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
15struct FragmentInput {
16 @location(0)
James Price3b671cb2022-03-28 14:31:22 +000017 shadowPos : vec3<f32>,
Ben Clayton73ced332022-01-21 22:38:16 +000018 @location(1)
James Price3b671cb2022-03-28 14:31:22 +000019 fragPos : vec3<f32>,
Ben Clayton73ced332022-01-21 22:38:16 +000020 @location(2)
James Price3b671cb2022-03-28 14:31:22 +000021 fragNorm : vec3<f32>,
Ben Clayton73ced332022-01-21 22:38:16 +000022}
23
Ben Claytonc64ca232022-06-29 00:55:36 +000024const albedo : vec3<f32> = vec3<f32>(0.899999976, 0.899999976, 0.899999976);
Ben Clayton73ced332022-01-21 22:38:16 +000025
Ben Claytonc64ca232022-06-29 00:55:36 +000026const ambientFactor : f32 = 0.200000003;
Ben Clayton73ced332022-01-21 22:38:16 +000027
dan sinclairb29892b2022-06-07 13:55:34 +000028@fragment
Ben Clayton73ced332022-01-21 22:38:16 +000029fn 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}