diff options
| author | skal <pascal.massimino@gmail.com> | 2026-02-28 11:50:13 +0100 |
|---|---|---|
| committer | skal <pascal.massimino@gmail.com> | 2026-02-28 11:50:13 +0100 |
| commit | b9c2a0394343ff3586880d118b7d549b3e748cad (patch) | |
| tree | bfc437f805c6b7344951107df8c7cd69a7ec421f /src/effects/rotating_cube.wgsl | |
| parent | 21d8a0b86ceda19812e9869a72e49c56c90ae3da (diff) | |
refactor(effects): co-locate effect WGSL shaders with their .h/.cc in src/effects/
Move 13 effect-specific shaders from workspaces/main/shaders/ to src/effects/
so each effect's .h, .cc, and .wgsl are together. Update assets.txt paths in
both main and test workspaces. Update EFFECT_WORKFLOW.md to reflect new location.
Shared/reusable snippets remain in src/shaders/.
handoff(Gemini): shaders moved; src/effects/ now has .h, .cc, and .wgsl per effect.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Diffstat (limited to 'src/effects/rotating_cube.wgsl')
| -rw-r--r-- | src/effects/rotating_cube.wgsl | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/src/effects/rotating_cube.wgsl b/src/effects/rotating_cube.wgsl new file mode 100644 index 0000000..0c75a13 --- /dev/null +++ b/src/effects/rotating_cube.wgsl @@ -0,0 +1,89 @@ +// Rotating cube shader v2 (simplified, no masking) + +struct Uniforms { + view_proj: mat4x4f, + inv_view_proj: mat4x4f, + camera_pos_time: vec4f, + params: vec4f, + resolution: vec2f, + aspect_ratio: f32, + _pad: f32, +}; + +struct ObjectData { + model: mat4x4f, + inv_model: mat4x4f, + color: vec4f, + params: vec4f, +}; + +@group(0) @binding(0) var<uniform> uniforms: Uniforms; +@group(0) @binding(1) var<storage, read> object: ObjectData; + +struct VSOut { + @builtin(position) pos: vec4f, + @location(0) world_pos: vec3f, + @location(1) normal: vec3f, +}; + +// Cube vertices (hardcoded) +fn get_cube_vertex(vid: u32) -> vec3f { + let positions = array<vec3f, 36>( + // Front face + vec3f(-1, -1, 1), vec3f( 1, -1, 1), vec3f( 1, 1, 1), + vec3f(-1, -1, 1), vec3f( 1, 1, 1), vec3f(-1, 1, 1), + // Back face + vec3f( 1, -1, -1), vec3f(-1, -1, -1), vec3f(-1, 1, -1), + vec3f( 1, -1, -1), vec3f(-1, 1, -1), vec3f( 1, 1, -1), + // Right face + vec3f( 1, -1, 1), vec3f( 1, -1, -1), vec3f( 1, 1, -1), + vec3f( 1, -1, 1), vec3f( 1, 1, -1), vec3f( 1, 1, 1), + // Left face + vec3f(-1, -1, -1), vec3f(-1, -1, 1), vec3f(-1, 1, 1), + vec3f(-1, -1, -1), vec3f(-1, 1, 1), vec3f(-1, 1, -1), + // Top face + vec3f(-1, 1, 1), vec3f( 1, 1, 1), vec3f( 1, 1, -1), + vec3f(-1, 1, 1), vec3f( 1, 1, -1), vec3f(-1, 1, -1), + // Bottom face + vec3f(-1, -1, -1), vec3f( 1, -1, -1), vec3f( 1, -1, 1), + vec3f(-1, -1, -1), vec3f( 1, -1, 1), vec3f(-1, -1, 1) + ); + return positions[vid]; +} + +fn get_cube_normal(vid: u32) -> vec3f { + let face_id = vid / 6u; + let normals = array<vec3f, 6>( + vec3f( 0, 0, 1), // Front + vec3f( 0, 0, -1), // Back + vec3f( 1, 0, 0), // Right + vec3f(-1, 0, 0), // Left + vec3f( 0, 1, 0), // Top + vec3f( 0, -1, 0) // Bottom + ); + return normals[face_id]; +} + +@vertex fn vs_main(@builtin(vertex_index) vid: u32) -> VSOut { + let local_pos = get_cube_vertex(vid); + let local_normal = get_cube_normal(vid); + + let world_pos = object.model * vec4f(local_pos, 1.0); + let world_normal = normalize((object.model * vec4f(local_normal, 0.0)).xyz); + + let clip_pos = uniforms.view_proj * world_pos; + + return VSOut(clip_pos, world_pos.xyz, world_normal); +} + +@fragment fn fs_main(@location(0) world_pos: vec3f, @location(1) normal: vec3f) -> @location(0) vec4f { + let N = normalize(normal); + let light_dir = normalize(vec3f(1.0, 1.0, 1.0)); + let diffuse = max(dot(N, light_dir), 0.0); + + let ambient = 0.3; + let lighting = ambient + diffuse * 0.7; + + let color = object.color.rgb * lighting; + return vec4f(color, 1.0); +} |
