summaryrefslogtreecommitdiff
path: root/src/shader/instance.wgsl
diff options
context:
space:
mode:
authorKai Stevenson <kai@kaistevenson.com>2026-09-09 22:29:14 -0700
committerKai Stevenson <kai@kaistevenson.com>2026-09-10 19:11:27 -0700
commit054c967e4a06bbbf79359bae701e1b467625839c (patch)
treeb03cacbfbca32fc0b55772cd269422fa65e9803e /src/shader/instance.wgsl
parentba59c1a12f2c1ee39b32736953844e6158282c88 (diff)
lightingHEADmaster
Diffstat (limited to 'src/shader/instance.wgsl')
-rw-r--r--src/shader/instance.wgsl22
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);
}