diff options
Diffstat (limited to 'src/shader/instance.wgsl')
| -rw-r--r-- | src/shader/instance.wgsl | 22 |
1 files changed, 21 insertions, 1 deletions
diff --git a/src/shader/instance.wgsl b/src/shader/instance.wgsl index a549801..4b921ab 100644 --- a/src/shader/instance.wgsl +++ b/src/shader/instance.wgsl @@ -1,8 +1,16 @@ +const AMBIENT_LIGHT = vec4f(0.4, 0.4, 0.4, 1.0); +const LIGHT_RESOLUTION = 1.0; + struct Camera { scale: vec2f, centre: vec2f, }; +struct Lighting { + origin: vec2f, + _padding: vec2f, +}; + struct Instance { centre: vec2f, cos_sin: vec2f, @@ -15,6 +23,9 @@ struct Instance { @group(0) @binding(1) var<storage, read> palette: array<vec4f>; @group(0) @binding(2) var<storage, read> instances: array<Instance>; @group(0) @binding(3) var<storage, read> cells: array<u32>; +@group(0) @binding(4) var lighting: texture_2d_array<f32>; +@group(0) @binding(5) var light_sampler: sampler; +@group(0) @binding(6) var<uniform> light_params: Lighting; struct VertexOutput { @builtin(position) clip_position: vec4f, @@ -67,5 +78,14 @@ fn fs_main(in: VertexOutput) -> @location(0) vec4f { discard; } - return palette[material]; + var total = vec3f(0.0); + let light_dims = vec2f(textureDimensions(lighting)); + let light_texel = (in.world - light_params.origin) * LIGHT_RESOLUTION; + let light_uv = light_texel / light_dims; + for (var d = 0u; d < 4u; d++) { + total += textureSample(lighting, light_sampler, light_uv, d).rgb; + } + let light = vec4f(total.r, total.g, total.b, 1.0); + + return palette[material] * (light + AMBIENT_LIGHT); } |
