summaryrefslogtreecommitdiff
path: root/assets/final/shaders/renderer_3d.wgsl
diff options
context:
space:
mode:
authorskal <pascal.massimino@gmail.com>2026-02-04 11:56:03 +0100
committerskal <pascal.massimino@gmail.com>2026-02-04 11:56:03 +0100
commit2e34965e5e48175c5ee6016af1a858f7f3f02c1d (patch)
treea41ea2fdc50a18183742bffaf0705b394f2be69f /assets/final/shaders/renderer_3d.wgsl
parent0d29f340b9de8f6a14baa0a2c6f3c57b8d1458a5 (diff)
feat(gpu): Implement recursive WGSL composition and modularize shaders (Task #50)
- Updated ShaderComposer to support recursive #include "snippet_name" with cycle detection. - Extracted granular WGSL snippets: math/sdf_shapes, math/sdf_utils, render/shadows, render/scene_query, render/lighting_utils. - Refactored Renderer3D to use #include in shaders, simplifying C++ dependency lists. - Fixed WGPUShaderSourceWGSL usage on macOS to correctly handle composed shader strings. - Added comprehensive unit tests for recursive composition in test_shader_composer. - Verified system stability with test_3d_render and full test suite. - Marked Task #50 as recurrent for future code hygiene.
Diffstat (limited to 'assets/final/shaders/renderer_3d.wgsl')
-rw-r--r--assets/final/shaders/renderer_3d.wgsl44
1 files changed, 9 insertions, 35 deletions
diff --git a/assets/final/shaders/renderer_3d.wgsl b/assets/final/shaders/renderer_3d.wgsl
index 7be8d4e..b39525d 100644
--- a/assets/final/shaders/renderer_3d.wgsl
+++ b/assets/final/shaders/renderer_3d.wgsl
@@ -1,3 +1,5 @@
+#include "common_uniforms"
+
@group(0) @binding(0) var<uniform> globals: GlobalUniforms;
@group(0) @binding(1) var<storage, read> object_data: ObjectsBuffer;
@group(0) @binding(2) var noise_tex: texture_2d<f32>;
@@ -54,37 +56,10 @@ fn vs_main(@builtin(vertex_index) vertex_index: u32,
return out;
}
-fn get_dist(p: vec3<f32>, obj_type: f32) -> f32 {
- if (obj_type == 1.0) { return length(p) - 1.0; } // Unit Sphere
- if (obj_type == 2.0) { return sdBox(p, vec3<f32>(1.0)); } // Unit Box
- if (obj_type == 3.0) { return sdTorus(p, vec2<f32>(1.0, 0.4)); } // Unit Torus
- if (obj_type == 4.0) { return sdPlane(p, vec3<f32>(0.0, 1.0, 0.0), 0.0); }
- return 100.0;
-}
-
-fn map_scene(p: vec3<f32>, skip_idx: u32) -> f32 {
- var d = 1000.0;
- let count = u32(globals.params.x);
-
- for (var i = 0u; i < count; i = i + 1u) {
- if (i == skip_idx) { continue; }
- let obj = object_data.objects[i];
- let obj_type = obj.params.x;
- // Skip rasterized objects (like the floor) in the SDF map
- if (obj_type <= 0.0) { continue; }
-
- let q = (obj.inv_model * vec4<f32>(p, 1.0)).xyz;
-
- let scale_x = length(obj.model[0].xyz);
- let scale_y = length(obj.model[1].xyz);
- let scale_z = length(obj.model[2].xyz);
- // Use conservative minimum scale to avoid overstepping the distance field
- let s = min(scale_x, min(scale_y, scale_z));
-
- d = min(d, get_dist(q, obj_type) * s);
- }
- return d;
-}
+#include "render/scene_query"
+#include "render/shadows"
+#include "render/lighting_utils"
+#include "ray_box"
@fragment
fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
@@ -189,7 +164,6 @@ fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
}
let shadow = calc_shadow(p, light_dir, 0.05, 20.0, in.instance_index);
- let diffuse = max(dot(normal, light_dir), 0.0);
- let lighting = diffuse * (0.1 + 0.9 * shadow) + 0.1; // Ambient + Shadowed Diffuse
- return vec4<f32>(base_color * lighting, 1.0);
-} \ No newline at end of file
+ let lit_color = calculate_lighting(base_color, normal, p, shadow);
+ return vec4<f32>(lit_color, 1.0);
+}