diff options
Diffstat (limited to 'workspaces/test')
56 files changed, 1760 insertions, 0 deletions
diff --git a/workspaces/test/assets.txt b/workspaces/test/assets.txt new file mode 100644 index 0000000..2be2c9d --- /dev/null +++ b/workspaces/test/assets.txt @@ -0,0 +1,62 @@ +# Asset Name, Compression Type, Filename/Placeholder, Description + +# --- Drum & Percussion Samples --- +KICK_1, NONE, assets/music/KICK_606.spec, "606 Kick" +KICK_2, NONE, assets/music/KICK_90S_2.spec, "90s Kick" +SNARE_1, NONE, assets/music/SNARE_808.spec, "808 Snare" +SNARE_2, NONE, assets/music/SNARE_909_TUNE_8.spec, "909 Snare" +SNARE_3, NONE, assets/music/SNARE_BLUE_ROOM.spec, "Snare Blue Room" +HIHAT_1, NONE, assets/music/HIHAT_CLOSED_DMX.spec, "DMX Closed Hi-hat" +HIHAT_2, NONE, assets/music/HIHAT_CLOSED_DUFF.spec, "Duff Closed Hi-hat" +HIHAT_3, NONE, assets/music/HIHAT_CLOSED_ER_1.spec, "ER-1 Closed Hi-hat" +CRASH_1, NONE, assets/music/CRASH_DMX.spec, "DMX Crash" +RIDE_1, NONE, assets/music/RIDE_CUP_1.spec, "Ride Cymbal" +SPLASH_1, NONE, assets/music/SPLASH_GROUNDED.spec, "Splash Cymbal" + +# --- Melodic Samples --- +BASS_1, NONE, assets/music/BASS_GUITAR_FEEL.spec, "Bass Guitar" +BASS_2, NONE, assets/music/BASS_SYNTH_1.spec, "Synth Bass 1" +BASS_3, NONE, assets/music/SYNTH_BASS_DISTORT.spec, "Distorted Synth Bass" + +# --- Procedural Textures --- +NOISE_TEX, PROC(gen_noise, 1234, 16), _, "Procedural noise texture for bump mapping" + +# --- WGSL Shaders & Snippets --- +SHADER_RENDERER_3D, NONE, shaders/renderer_3d.wgsl, "Hybrid 3D Renderer Shader" +SHADER_COMMON_UNIFORMS, NONE, shaders/common_uniforms.wgsl, "Common Uniforms Snippet" +SHADER_SDF_PRIMITIVES, NONE, shaders/sdf_primitives.wgsl, "SDF Primitives Snippet" +SHADER_LIGHTING, NONE, shaders/lighting.wgsl, "Lighting Snippet" +SHADER_RAY_BOX, NONE, shaders/ray_box.wgsl, "Ray-Box Intersection Snippet" +SHADER_RAY_TRIANGLE, NONE, shaders/ray_triangle.wgsl, "Ray-Triangle Intersection Snippet (Möller-Trumbore)" +SHADER_MAIN, NONE, shaders/main_shader.wgsl, "Main Heptagon Shader" +SHADER_PARTICLE_COMPUTE, NONE, shaders/particle_compute.wgsl, "Particle Compute Shader" +SHADER_PARTICLE_RENDER, NONE, shaders/particle_render.wgsl, "Particle Render Shader" +SHADER_PASSTHROUGH, NONE, shaders/passthrough.wgsl, "Passthrough Shader" +SHADER_ELLIPSE, NONE, shaders/ellipse.wgsl, "Ellipse Shader" +SHADER_PARTICLE_SPRAY_COMPUTE, NONE, shaders/particle_spray_compute.wgsl, "Particle Spray Compute" +SHADER_GAUSSIAN_BLUR, NONE, shaders/gaussian_blur.wgsl, "Gaussian Blur Shader" +SHADER_SOLARIZE, NONE, shaders/solarize.wgsl, "Solarize Shader" +SHADER_DISTORT, NONE, shaders/distort.wgsl, "Distort Shader" +SHADER_CHROMA_ABERRATION, NONE, shaders/chroma_aberration.wgsl, "Chroma Aberration Shader" +SHADER_VISUAL_DEBUG, NONE, shaders/visual_debug.wgsl, "Visual Debug Shader" +SHADER_SKYBOX, NONE, shaders/skybox.wgsl, "Skybox background shader" +SHADER_MATH_SDF_SHAPES, NONE, shaders/math/sdf_shapes.wgsl, "SDF Shapes Snippet" +SHADER_MATH_SDF_UTILS, NONE, shaders/math/sdf_utils.wgsl, "SDF Utils Snippet" +SHADER_MATH_COMMON_UTILS, NONE, shaders/math/common_utils.wgsl, "Common Math Utils" +SHADER_MATH_NOISE, NONE, shaders/math/noise.wgsl, "RNG and Noise Functions" +SHADER_RENDER_SHADOWS, NONE, shaders/render/shadows.wgsl, "Shadows Snippet" +SHADER_RENDER_SCENE_QUERY_BVH, NONE, shaders/render/scene_query_bvh.wgsl, "Scene Query Snippet (BVH)" +SHADER_RENDER_SCENE_QUERY_LINEAR, NONE, shaders/render/scene_query_linear.wgsl, "Scene Query Snippet (Linear)" +SHADER_RENDER_LIGHTING_UTILS, NONE, shaders/render/lighting_utils.wgsl, "Lighting Utils Snippet" +SHADER_MESH, NONE, shaders/mesh_render.wgsl, "Mesh Rasterization Shader" +MESH_CUBE, NONE, test_mesh.obj, "A simple cube mesh" +DODECAHEDRON, NONE, dodecahedron.obj, "A dodecahedron mesh" +SHADER_VIGNETTE, NONE, shaders/vignette.wgsl, "Vignette Shader" +SHADER_COMPUTE_GEN_NOISE, NONE, shaders/compute/gen_noise.wgsl, "GPU Noise Compute Shader" +SHADER_COMPUTE_GEN_PERLIN, NONE, shaders/compute/gen_perlin.wgsl, "GPU Perlin Noise Compute Shader" +SHADER_COMPUTE_GEN_GRID, NONE, shaders/compute/gen_grid.wgsl, "GPU Grid Compute Shader" +SHADER_COMPUTE_GEN_BLEND, NONE, shaders/compute/gen_blend.wgsl, "GPU Blend Composite Shader" +SHADER_COMPUTE_GEN_MASK, NONE, shaders/compute/gen_mask.wgsl, "GPU Mask Composite Shader" +CIRCLE_MASK_COMPUTE_SHADER, NONE, shaders/circle_mask_compute.wgsl, "Circle mask compute shader" +CIRCLE_MASK_RENDER_SHADER, NONE, shaders/circle_mask_render.wgsl, "Circle mask render shader" +MASKED_CUBE_SHADER, NONE, shaders/masked_cube.wgsl, "Masked cube shader" diff --git a/workspaces/test/assets/music/BASS_GUITAR_FEEL.spec b/workspaces/test/assets/music/BASS_GUITAR_FEEL.spec Binary files differnew file mode 100644 index 0000000..54f49a6 --- /dev/null +++ b/workspaces/test/assets/music/BASS_GUITAR_FEEL.spec diff --git a/workspaces/test/assets/music/BASS_SYNTH_1.spec b/workspaces/test/assets/music/BASS_SYNTH_1.spec Binary files differnew file mode 100644 index 0000000..33bc0a0 --- /dev/null +++ b/workspaces/test/assets/music/BASS_SYNTH_1.spec diff --git a/workspaces/test/assets/music/CRASH_DMX.spec b/workspaces/test/assets/music/CRASH_DMX.spec Binary files differnew file mode 100644 index 0000000..45ee52d --- /dev/null +++ b/workspaces/test/assets/music/CRASH_DMX.spec diff --git a/workspaces/test/assets/music/HIHAT_CLOSED_DMX.spec b/workspaces/test/assets/music/HIHAT_CLOSED_DMX.spec Binary files differnew file mode 100644 index 0000000..8fce1d2 --- /dev/null +++ b/workspaces/test/assets/music/HIHAT_CLOSED_DMX.spec diff --git a/workspaces/test/assets/music/HIHAT_CLOSED_DUFF.spec b/workspaces/test/assets/music/HIHAT_CLOSED_DUFF.spec Binary files differnew file mode 100644 index 0000000..f738271 --- /dev/null +++ b/workspaces/test/assets/music/HIHAT_CLOSED_DUFF.spec diff --git a/workspaces/test/assets/music/HIHAT_CLOSED_ER_1.spec b/workspaces/test/assets/music/HIHAT_CLOSED_ER_1.spec Binary files differnew file mode 100644 index 0000000..bb03f5e --- /dev/null +++ b/workspaces/test/assets/music/HIHAT_CLOSED_ER_1.spec diff --git a/workspaces/test/assets/music/KICK_606.spec b/workspaces/test/assets/music/KICK_606.spec Binary files differnew file mode 100644 index 0000000..10af84a --- /dev/null +++ b/workspaces/test/assets/music/KICK_606.spec diff --git a/workspaces/test/assets/music/KICK_90S_2.spec b/workspaces/test/assets/music/KICK_90S_2.spec Binary files differnew file mode 100644 index 0000000..126409e --- /dev/null +++ b/workspaces/test/assets/music/KICK_90S_2.spec diff --git a/workspaces/test/assets/music/RIDE_CUP_1.spec b/workspaces/test/assets/music/RIDE_CUP_1.spec Binary files differnew file mode 100644 index 0000000..78867c1 --- /dev/null +++ b/workspaces/test/assets/music/RIDE_CUP_1.spec diff --git a/workspaces/test/assets/music/SNARE_808.spec b/workspaces/test/assets/music/SNARE_808.spec Binary files differnew file mode 100644 index 0000000..2923c3c --- /dev/null +++ b/workspaces/test/assets/music/SNARE_808.spec diff --git a/workspaces/test/assets/music/SNARE_909_TUNE_8.spec b/workspaces/test/assets/music/SNARE_909_TUNE_8.spec Binary files differnew file mode 100644 index 0000000..4693c05 --- /dev/null +++ b/workspaces/test/assets/music/SNARE_909_TUNE_8.spec diff --git a/workspaces/test/assets/music/SNARE_BLUE_ROOM.spec b/workspaces/test/assets/music/SNARE_BLUE_ROOM.spec Binary files differnew file mode 100644 index 0000000..b24baa7 --- /dev/null +++ b/workspaces/test/assets/music/SNARE_BLUE_ROOM.spec diff --git a/workspaces/test/assets/music/SPLASH_GROUNDED.spec b/workspaces/test/assets/music/SPLASH_GROUNDED.spec Binary files differnew file mode 100644 index 0000000..a919be4 --- /dev/null +++ b/workspaces/test/assets/music/SPLASH_GROUNDED.spec diff --git a/workspaces/test/assets/music/SYNTH_BASS_DISTORT.spec b/workspaces/test/assets/music/SYNTH_BASS_DISTORT.spec Binary files differnew file mode 100644 index 0000000..33bc0a0 --- /dev/null +++ b/workspaces/test/assets/music/SYNTH_BASS_DISTORT.spec diff --git a/workspaces/test/music.track b/workspaces/test/music.track new file mode 100644 index 0000000..6ae5c67 --- /dev/null +++ b/workspaces/test/music.track @@ -0,0 +1,36 @@ +# Minimal drum beat for audio/visual sync testing +# Pattern: kick-snare-kick-snare, crash every 4th bar +# Includes NOTE_A4 (440 Hz) at start of each bar for testing +# +# TIMING: Unit-less (1 unit = 4 beats at 120 BPM = 2 seconds) +# Pattern events use unit-less time (0.0-1.0 for 4-beat pattern) +# Score triggers use unit-less time + +SAMPLE ASSET_KICK_1 +SAMPLE ASSET_SNARE_1 +SAMPLE ASSET_CRASH_1 + +PATTERN drums_basic LENGTH 1.0 + 0.00, ASSET_KICK_1, 1.0, 0.0 + 0.00, NOTE_A4, 0.5, 0.0 + 0.25, ASSET_SNARE_1, 0.9, 0.0 + 0.50, ASSET_KICK_1, 1.0, 0.0 + 0.75, ASSET_SNARE_1, 0.9, 0.0 + +PATTERN drums_with_crash LENGTH 1.0 + 0.00, ASSET_KICK_1, 1.0, 0.0 + 0.00, ASSET_CRASH_1, 0.85, 0.0 + 0.00, NOTE_A4, 0.5, 0.0 + 0.25, ASSET_SNARE_1, 0.9, 0.0 + 0.50, ASSET_KICK_1, 1.0, 0.0 + 0.75, ASSET_SNARE_1, 0.9, 0.0 + +SCORE + 0.0, drums_basic + 1.0, drums_basic + 2.0, drums_with_crash + 3.0, drums_basic + 4.0, drums_basic + 5.0, drums_basic + 6.0, drums_with_crash + 7.0, drums_basic diff --git a/workspaces/test/shaders/chroma_aberration.wgsl b/workspaces/test/shaders/chroma_aberration.wgsl new file mode 100644 index 0000000..6c942b7 --- /dev/null +++ b/workspaces/test/shaders/chroma_aberration.wgsl @@ -0,0 +1,35 @@ +@group(0) @binding(0) var smplr: sampler; +@group(0) @binding(1) var txt: texture_2d<f32>; + +#include "common_uniforms" +struct ChromaAberrationParams { + offset_scale: f32, + angle: f32, +}; + +@group(0) @binding(2) var<uniform> uniforms: CommonUniforms; +@group(0) @binding(3) var<uniform> params: ChromaAberrationParams; + +@vertex fn vs_main(@builtin(vertex_index) i: u32) -> @builtin(position) vec4<f32> { + var pos = array<vec2<f32>, 3>( + vec2<f32>(-1, -1), + vec2<f32>(3, -1), + vec2<f32>(-1, 3) + ); + return vec4<f32>(pos[i], 0.0, 1.0); +} + +@fragment fn fs_main(@builtin(position) p: vec4<f32>) -> @location(0) vec4<f32> { + let uv = p.xy / uniforms.resolution; + + // Compute offset magnitude and direction + let offset_mag = params.offset_scale * uniforms.audio_intensity; + let offset_dir = vec2<f32>(cos(params.angle), sin(params.angle)); + let offset = offset_mag * offset_dir; + + // Sample RGB channels with chromatic aberration + let r = textureSample(txt, smplr, uv + offset).r; + let g = textureSample(txt, smplr, uv).g; + let b = textureSample(txt, smplr, uv - offset).b; + return vec4<f32>(r, g, b, 1.0); +} diff --git a/workspaces/test/shaders/circle_mask_compute.wgsl b/workspaces/test/shaders/circle_mask_compute.wgsl new file mode 100644 index 0000000..484d3dd --- /dev/null +++ b/workspaces/test/shaders/circle_mask_compute.wgsl @@ -0,0 +1,35 @@ +// Circle mask compute shader +// Generates a circular mask (1.0 inside, 0.0 outside) + +#include "common_uniforms" +struct CircleMaskParams { + radius: f32, + _pad0: f32, + _pad1: f32, + _pad2: f32, +}; + +@group(0) @binding(0) var<uniform> uniforms: CommonUniforms; +@group(0) @binding(1) var<uniform> params: CircleMaskParams; + +struct VSOutput { + @builtin(position) position: vec4<f32>, +}; + +@vertex fn vs_main(@builtin(vertex_index) i: u32) -> VSOutput { + var pos = array<vec2<f32>, 3>( + vec2<f32>(-1, -1), vec2<f32>(3, -1), vec2<f32>(-1, 3)); + return VSOutput(vec4<f32>(pos[i], 0.0, 1.0)); +} + +@fragment fn fs_main(@builtin(position) p: vec4<f32>) -> @location(0) vec4<f32> { + let uv = p.xy / uniforms.resolution; + let center = vec2<f32>(0.5, 0.5); + let aspect_corrected_uv = (uv - center) * vec2<f32>(uniforms.aspect_ratio, 1.0); + let dist = length(aspect_corrected_uv); + + let edge_width = 0.01; + let mask = smoothstep(params.radius + edge_width, params.radius - edge_width, dist); + + return vec4<f32>(mask, mask, mask, 1.0); +} diff --git a/workspaces/test/shaders/circle_mask_render.wgsl b/workspaces/test/shaders/circle_mask_render.wgsl new file mode 100644 index 0000000..cfa002e --- /dev/null +++ b/workspaces/test/shaders/circle_mask_render.wgsl @@ -0,0 +1,30 @@ +// Circle mask render shader +// Samples mask and draws green outside the circle + +@group(0) @binding(0) var mask_tex: texture_2d<f32>; +@group(0) @binding(1) var mask_sampler: sampler; + +#include "common_uniforms" + +@group(0) @binding(2) var<uniform> uniforms: CommonUniforms; + +struct VSOutput { + @builtin(position) position: vec4<f32>, +}; + +@vertex fn vs_main(@builtin(vertex_index) i: u32) -> VSOutput { + var pos = array<vec2<f32>, 3>( + vec2<f32>(-1, -1), vec2<f32>(3, -1), vec2<f32>(-1, 3)); + return VSOutput(vec4<f32>(pos[i], 0.0, 1.0)); +} + +@fragment fn fs_main(@builtin(position) p: vec4<f32>) -> @location(0) vec4<f32> { + let uv = p.xy / uniforms.resolution; + let mask_value = textureSample(mask_tex, mask_sampler, uv).r; + + if (mask_value > 0.5) { + discard; + } + + return vec4<f32>(0.0, 1.0, 0.0, 1.0); +} diff --git a/workspaces/test/shaders/common_uniforms.wgsl b/workspaces/test/shaders/common_uniforms.wgsl new file mode 100644 index 0000000..ce1be53 --- /dev/null +++ b/workspaces/test/shaders/common_uniforms.wgsl @@ -0,0 +1,25 @@ +struct CommonUniforms { + resolution: vec2<f32>, + _pad0: f32, + _pad1: f32, + aspect_ratio: f32, + time: f32, + beat: f32, + audio_intensity: f32, +}; +struct GlobalUniforms { + view_proj: mat4x4<f32>, + inv_view_proj: mat4x4<f32>, + camera_pos_time: vec4<f32>, + params: vec4<f32>, + resolution: vec2<f32>, +}; +struct ObjectData { + model: mat4x4<f32>, + inv_model: mat4x4<f32>, + color: vec4<f32>, + params: vec4<f32>, +}; +struct ObjectsBuffer { + objects: array<ObjectData>, +};
\ No newline at end of file diff --git a/workspaces/test/shaders/compute/gen_blend.wgsl b/workspaces/test/shaders/compute/gen_blend.wgsl new file mode 100644 index 0000000..9fc9e1e --- /dev/null +++ b/workspaces/test/shaders/compute/gen_blend.wgsl @@ -0,0 +1,29 @@ +// This file is part of the 64k demo project. +// GPU composite shader: Blend two textures. + +struct BlendParams { + width: u32, + height: u32, + blend_factor: f32, + _pad0: f32, +} + +@group(0) @binding(0) var output_tex: texture_storage_2d<rgba8unorm, write>; +@group(0) @binding(1) var<uniform> params: BlendParams; +@group(0) @binding(2) var input_a: texture_2d<f32>; +@group(0) @binding(3) var input_b: texture_2d<f32>; +@group(0) @binding(4) var tex_sampler: sampler; + +@compute @workgroup_size(8, 8, 1) +fn main(@builtin(global_invocation_id) id: vec3<u32>) { + if (id.x >= params.width || id.y >= params.height) { return; } + + let uv = vec2<f32>(f32(id.x) / f32(params.width), + f32(id.y) / f32(params.height)); + + let color_a = textureSampleLevel(input_a, tex_sampler, uv, 0.0); + let color_b = textureSampleLevel(input_b, tex_sampler, uv, 0.0); + let blended = mix(color_a, color_b, params.blend_factor); + + textureStore(output_tex, id.xy, blended); +} diff --git a/workspaces/test/shaders/compute/gen_grid.wgsl b/workspaces/test/shaders/compute/gen_grid.wgsl new file mode 100644 index 0000000..cc5e189 --- /dev/null +++ b/workspaces/test/shaders/compute/gen_grid.wgsl @@ -0,0 +1,24 @@ +// GPU procedural grid pattern generator. +// Simple grid lines with configurable spacing and thickness. + +struct GridParams { + width: u32, + height: u32, + grid_size: u32, + thickness: u32, +} + +@group(0) @binding(0) var output_tex: texture_storage_2d<rgba8unorm, write>; +@group(0) @binding(1) var<uniform> params: GridParams; + +@compute @workgroup_size(8, 8, 1) +fn main(@builtin(global_invocation_id) id: vec3<u32>) { + if (id.x >= params.width || id.y >= params.height) { return; } + + let on_line = (id.x % params.grid_size) < params.thickness || + (id.y % params.grid_size) < params.thickness; + + let val = select(0.0, 1.0, on_line); + + textureStore(output_tex, id.xy, vec4<f32>(val, val, val, 1.0)); +} diff --git a/workspaces/test/shaders/compute/gen_mask.wgsl b/workspaces/test/shaders/compute/gen_mask.wgsl new file mode 100644 index 0000000..1ce9f52 --- /dev/null +++ b/workspaces/test/shaders/compute/gen_mask.wgsl @@ -0,0 +1,27 @@ +// This file is part of the 64k demo project. +// GPU composite shader: Multiply texture A by texture B (masking). + +struct MaskParams { + width: u32, + height: u32, +} + +@group(0) @binding(0) var output_tex: texture_storage_2d<rgba8unorm, write>; +@group(0) @binding(1) var<uniform> params: MaskParams; +@group(0) @binding(2) var input_a: texture_2d<f32>; +@group(0) @binding(3) var input_b: texture_2d<f32>; +@group(0) @binding(4) var tex_sampler: sampler; + +@compute @workgroup_size(8, 8, 1) +fn main(@builtin(global_invocation_id) id: vec3<u32>) { + if (id.x >= params.width || id.y >= params.height) { return; } + + let uv = vec2<f32>(f32(id.x) / f32(params.width), + f32(id.y) / f32(params.height)); + + let color_a = textureSampleLevel(input_a, tex_sampler, uv, 0.0); + let mask_b = textureSampleLevel(input_b, tex_sampler, uv, 0.0); + let masked = color_a * mask_b; + + textureStore(output_tex, id.xy, masked); +} diff --git a/workspaces/test/shaders/compute/gen_noise.wgsl b/workspaces/test/shaders/compute/gen_noise.wgsl new file mode 100644 index 0000000..5c0babd --- /dev/null +++ b/workspaces/test/shaders/compute/gen_noise.wgsl @@ -0,0 +1,26 @@ +// GPU procedural noise texture generator. +// Uses compute shader for parallel texture generation. + +#include "math/noise" + +struct NoiseParams { + width: u32, + height: u32, + seed: f32, + frequency: f32, +} + +@group(0) @binding(0) var output_tex: texture_storage_2d<rgba8unorm, write>; +@group(0) @binding(1) var<uniform> params: NoiseParams; + +@compute @workgroup_size(8, 8, 1) +fn main(@builtin(global_invocation_id) id: vec3<u32>) { + if (id.x >= params.width || id.y >= params.height) { return; } + + let uv = vec2<f32>(f32(id.x) / f32(params.width), + f32(id.y) / f32(params.height)); + let p = uv * params.frequency + params.seed; + let noise = noise_2d(p); + + textureStore(output_tex, id.xy, vec4<f32>(noise, noise, noise, 1.0)); +} diff --git a/workspaces/test/shaders/compute/gen_perlin.wgsl b/workspaces/test/shaders/compute/gen_perlin.wgsl new file mode 100644 index 0000000..73816d6 --- /dev/null +++ b/workspaces/test/shaders/compute/gen_perlin.wgsl @@ -0,0 +1,44 @@ +// GPU procedural Perlin noise texture generator. +// Fractional Brownian Motion using value noise. + +#include "math/noise" + +struct PerlinParams { + width: u32, + height: u32, + seed: f32, + frequency: f32, + amplitude: f32, + amplitude_decay: f32, + octaves: u32, + _pad0: f32, // Padding for alignment +} + +@group(0) @binding(0) var output_tex: texture_storage_2d<rgba8unorm, write>; +@group(0) @binding(1) var<uniform> params: PerlinParams; + +@compute @workgroup_size(8, 8, 1) +fn main(@builtin(global_invocation_id) id: vec3<u32>) { + if (id.x >= params.width || id.y >= params.height) { return; } + + let uv = vec2<f32>(f32(id.x) / f32(params.width), + f32(id.y) / f32(params.height)); + + var value = 0.0; + var amplitude = params.amplitude; + var frequency = params.frequency; + var total_amp = 0.0; + + for (var o: u32 = 0u; o < params.octaves; o++) { + let p = uv * frequency + params.seed; + value += noise_2d(p) * amplitude; + total_amp += amplitude; + frequency *= 2.0; + amplitude *= params.amplitude_decay; + } + + value /= total_amp; + let clamped = clamp(value, 0.0, 1.0); + + textureStore(output_tex, id.xy, vec4<f32>(clamped, clamped, clamped, 1.0)); +} diff --git a/workspaces/test/shaders/distort.wgsl b/workspaces/test/shaders/distort.wgsl new file mode 100644 index 0000000..5d35129 --- /dev/null +++ b/workspaces/test/shaders/distort.wgsl @@ -0,0 +1,27 @@ +@group(0) @binding(0) var smplr: sampler; +@group(0) @binding(1) var txt: texture_2d<f32>; + +#include "common_uniforms" + +struct DistortParams { + strength: f32, + speed: f32, +}; + +@group(0) @binding(2) var<uniform> uniforms: CommonUniforms; +@group(0) @binding(3) var<uniform> params: DistortParams; + +@vertex fn vs_main(@builtin(vertex_index) i: u32) -> @builtin(position) vec4<f32> { + var pos = array<vec2<f32>, 3>( + vec2<f32>(-1, -1), + vec2<f32>(3, -1), + vec2<f32>(-1, 3) + ); + return vec4<f32>(pos[i], 0.0, 1.0); +} + +@fragment fn fs_main(@builtin(position) p: vec4<f32>) -> @location(0) vec4<f32> { + let uv = p.xy / uniforms.resolution; + let dist = params.strength * uniforms.audio_intensity * sin(uv.y * 20.0 + uniforms.time * params.speed * 5.0); + return textureSample(txt, smplr, uv + vec2<f32>(dist, 0.0)); +} diff --git a/workspaces/test/shaders/ellipse.wgsl b/workspaces/test/shaders/ellipse.wgsl new file mode 100644 index 0000000..05dfcfc --- /dev/null +++ b/workspaces/test/shaders/ellipse.wgsl @@ -0,0 +1,51 @@ +#include "common_uniforms" + +@group(0) @binding(0) var<uniform> uniforms: CommonUniforms; + +@vertex fn vs_main(@builtin(vertex_index) i: u32) -> @builtin(position) vec4<f32> { + var pos = array<vec2<f32>, 3>( + vec2<f32>(-1.0, -1.0), + vec2<f32>(3.0, -1.0), + vec2<f32>(-1.0, 3.0) + ); + return vec4<f32>(pos[i], 0.0, 1.0); +} + +fn sdEllipse(p: vec2<f32>, ab: vec2<f32>) -> f32 { + var p_abs = abs(p); + if (p_abs.x > p_abs.y) { + p_abs = vec2<f32>(p_abs.y, p_abs.x); + } + let l = ab.y * ab.y - ab.x * ab.x; + let m = ab.x * p_abs.x / l; + let n = ab.y * p_abs.y / l; + let m2 = m * m; + let n2 = n * n; + let c = (m2 + n2 - 1.0) / 3.0; + let c3 = c * c * c; + let d = c3 + m2 * n2; + let g = m + m * n2; + var co: f32; + if (d < 0.0) { + let h = acos((c3 + m2 * n2 * 2.0) / c3) / 3.0; + let s = cos(h); + let t = sin(h) * sqrt(3.0); + co = (sqrt(-c * (s + t * 2.0) + m2) + sign(l) * sqrt(-c * (s - t * 2.0) + m2) + abs(g) / (sqrt(-c * (s + t * 2.0) + m2) * sqrt(-c * (s - t * 2.0) + m2)) - m) / 2.0; + } else { + let h = 2.0 * m * n * sqrt(d); + let s = sign(c3 + m2 * n2 + h) * pow(abs(c3 + m2 * n2 + h), 1.0 / 3.0); + let u = sign(c3 + m2 * n2 - h) * pow(abs(c3 + m2 * n2 - h), 1.0 / 3.0); + let rx = -s - u + m2 * 2.0; + let ry = (s - u) * sqrt(3.0); + co = (ry / sqrt(sqrt(rx * rx + ry * ry) - rx) + 2.0 * g / sqrt(rx * rx + ry * ry) - m) / 2.0; + } + let si = sqrt(max(0.0, 1.0 - co * co)); + return length(p_abs - vec2<f32>(ab.x * co, ab.y * si)) * sign(p_abs.y * ab.x * co - p_abs.x * ab.y * si); +} + +@fragment fn fs_main(@builtin(position) p: vec4<f32>) -> @location(0) vec4<f32> { + let uv = (p.xy / uniforms.resolution - 0.5) * 2.0; + let movement = vec2<f32>(sin(uniforms.time * 0.7), cos(uniforms.time * 0.5)); + let d = sdEllipse((uv * vec2<f32>(uniforms.aspect_ratio, 1.0)) - movement, vec2<f32>(0.5, 0.3) * (1.0 + uniforms.beat * 0.2)); + return mix(vec4<f32>(0.2, 0.8, 0.4, 1.0), vec4<f32>(0.0), smoothstep(0.0, 0.01, d)); +} diff --git a/workspaces/test/shaders/gaussian_blur.wgsl b/workspaces/test/shaders/gaussian_blur.wgsl new file mode 100644 index 0000000..02156f7 --- /dev/null +++ b/workspaces/test/shaders/gaussian_blur.wgsl @@ -0,0 +1,36 @@ +@group(0) @binding(0) var smplr: sampler; +@group(0) @binding(1) var txt: texture_2d<f32>; + +#include "common_uniforms" +struct GaussianBlurParams { + strength: f32, + _pad: f32, +}; + +@group(0) @binding(2) var<uniform> uniforms: CommonUniforms; +@group(0) @binding(3) var<uniform> params: GaussianBlurParams; + +@vertex fn vs_main(@builtin(vertex_index) i: u32) -> @builtin(position) vec4<f32> { + var pos = array<vec2<f32>, 3>( + vec2<f32>(-1, -1), + vec2<f32>(3, -1), + vec2<f32>(-1, 3) + ); + return vec4<f32>(pos[i], 0.0, 1.0); +} + +@fragment fn fs_main(@builtin(position) p: vec4<f32>) -> @location(0) vec4<f32> { + let uv = p.xy / uniforms.resolution; + var res = vec4<f32>(0.0); + + // Parameterized strength + dramatic beat pulsation + let pulse = 0.5 + uniforms.audio_intensity * 2.0; // Pulsate between 0.5x and 2.5x with beat + let size = params.strength * pulse; + + for (var x: f32 = -2.0; x <= 2.0; x += 1.0) { + for (var y: f32 = -2.0; y <= 2.0; y += 1.0) { + res += textureSample(txt, smplr, uv + vec2<f32>(x, y) * size / uniforms.resolution.x); + } + } + return res / 25.0; +} diff --git a/workspaces/test/shaders/lighting.wgsl b/workspaces/test/shaders/lighting.wgsl new file mode 100644 index 0000000..ac2142b --- /dev/null +++ b/workspaces/test/shaders/lighting.wgsl @@ -0,0 +1,24 @@ +fn get_normal_basic(p: vec3<f32>, obj_params: vec4<f32>) -> vec3<f32> { + let obj_type = obj_params.x; + if (obj_type == 1.0) { return normalize(p); } + let e = vec2<f32>(0.001, 0.0); + return normalize(vec3<f32>( + get_dist(p + e.xyy, obj_params) - get_dist(p - e.xyy, obj_params), + get_dist(p + e.yxy, obj_params) - get_dist(p - e.yxy, obj_params), + get_dist(p + e.yyx, obj_params) - get_dist(p - e.yyx, obj_params) + )); +} + +fn calc_shadow(ro: vec3<f32>, rd: vec3<f32>, tmin: f32, tmax: f32, skip_idx: u32) -> f32 { + var res = 1.0; + var t = tmin; + if (t < 0.05) { t = 0.05; } + for (var i = 0; i < 32; i = i + 1) { + let h = map_scene(ro + rd * t, skip_idx); + if (h < 0.001) { return 0.0; } + res = min(res, 16.0 * h / t); + t = t + clamp(h, 0.02, 0.4); + if (t > tmax) { break; } + } + return clamp(res, 0.0, 1.0); +} diff --git a/workspaces/test/shaders/main_shader.wgsl b/workspaces/test/shaders/main_shader.wgsl new file mode 100644 index 0000000..ab0278c --- /dev/null +++ b/workspaces/test/shaders/main_shader.wgsl @@ -0,0 +1,25 @@ +#include "common_uniforms" + +@group(0) @binding(0) var<uniform> uniforms: CommonUniforms; + +@vertex fn vs_main(@builtin(vertex_index) i: u32) -> @builtin(position) vec4<f32> { + let PI = 3.14159265; + let num_sides = 7.0; + let scale = 0.5 + 0.3 * uniforms.audio_intensity; + let tri_idx = f32(i / 3u); + let sub_idx = i % 3u; + if (sub_idx == 0u) { + return vec4<f32>(0.0, 0.0, 0.0, 1.0); + } + let angle = (tri_idx + f32(sub_idx - 1u)) * 2.0 * PI / num_sides + uniforms.time * 0.5; + return vec4<f32>(scale * cos(angle) / uniforms.aspect_ratio, scale * sin(angle), 0.0, 1.0); +} + +@fragment fn fs_main() -> @location(0) vec4<f32> { + let h = uniforms.time * 2.0 + uniforms.audio_intensity * 3.0; + let r = sin(h) * 0.5 + 0.5; + let g = sin(h + 2.0) * 0.9 + 0.3; + let b = sin(h + 4.0) * 0.5 + 0.5; + let boost = uniforms.audio_intensity * 0.5; + return vec4<f32>(r + boost, g + boost, b + boost, 1.0); +} diff --git a/workspaces/test/shaders/masked_cube.wgsl b/workspaces/test/shaders/masked_cube.wgsl new file mode 100644 index 0000000..5e673a3 --- /dev/null +++ b/workspaces/test/shaders/masked_cube.wgsl @@ -0,0 +1,159 @@ +// Masked cube shader - based on renderer_3d.wgsl with mask sampling +#include "common_uniforms" +#include "math/common_utils" +#include "math/sdf_utils" + +@group(0) @binding(0) var<uniform> globals: GlobalUniforms; +@group(0) @binding(1) var<storage, read> object_data: ObjectsBuffer; +@group(0) @binding(3) var noise_tex: texture_2d<f32>; +@group(0) @binding(4) var noise_sampler: sampler; + +@group(1) @binding(0) var mask_tex: texture_2d<f32>; +@group(1) @binding(1) var mask_sampler: sampler; + +struct VertexOutput { + @builtin(position) position: vec4<f32>, + @location(0) local_pos: vec3<f32>, + @location(1) color: vec4<f32>, + @location(2) @interpolate(flat) instance_index: u32, + @location(3) world_pos: vec3<f32>, + @location(4) transformed_normal: vec3<f32>, +}; + +@vertex +fn vs_main(@builtin(vertex_index) vertex_index: u32, + @builtin(instance_index) instance_index: u32) -> VertexOutput { + var pos = array<vec3<f32>, 36>( + vec3(-1.0, -1.0, 1.0), vec3( 1.0, -1.0, 1.0), vec3( 1.0, 1.0, 1.0), + vec3(-1.0, -1.0, 1.0), vec3( 1.0, 1.0, 1.0), vec3(-1.0, 1.0, 1.0), + vec3(-1.0, -1.0, -1.0), vec3(-1.0, 1.0, -1.0), vec3( 1.0, 1.0, -1.0), + vec3(-1.0, -1.0, -1.0), vec3( 1.0, 1.0, -1.0), vec3( 1.0, -1.0, -1.0), + vec3(-1.0, 1.0, -1.0), vec3(-1.0, 1.0, 1.0), vec3( 1.0, 1.0, 1.0), + vec3(-1.0, 1.0, -1.0), vec3( 1.0, 1.0, 1.0), vec3( 1.0, 1.0, -1.0), + vec3(-1.0, -1.0, -1.0), vec3( 1.0, -1.0, -1.0), vec3( 1.0, -1.0, 1.0), + vec3(-1.0, -1.0, -1.0), vec3( 1.0, -1.0, 1.0), vec3(-1.0, -1.0, 1.0), + vec3( 1.0, -1.0, -1.0), vec3( 1.0, 1.0, -1.0), vec3( 1.0, 1.0, 1.0), + vec3( 1.0, -1.0, -1.0), vec3( 1.0, 1.0, 1.0), vec3( 1.0, -1.0, 1.0), + vec3(-1.0, -1.0, -1.0), vec3(-1.0, -1.0, 1.0), vec3(-1.0, 1.0, 1.0), + vec3(-1.0, -1.0, -1.0), vec3(-1.0, 1.0, 1.0), vec3(-1.0, 1.0, -1.0) + ); + + var p = pos[vertex_index]; + let obj = object_data.objects[instance_index]; + let world_pos = obj.model * vec4<f32>(p, 1.0); + let clip_pos = globals.view_proj * world_pos; + + var out: VertexOutput; + out.position = clip_pos; + out.local_pos = p; + out.color = obj.color; + out.instance_index = instance_index; + out.world_pos = world_pos.xyz; + out.transformed_normal = normalize(vec3<f32>(0.0, 1.0, 0.0)); + + return out; +} + +#include "render/scene_query_mode" +#include "render/shadows" +#include "render/lighting_utils" +#include "ray_box" + +struct FragmentOutput { + @location(0) color: vec4<f32>, + @builtin(frag_depth) depth: f32, +}; + +@fragment +fn fs_main(in: VertexOutput) -> FragmentOutput { + let screen_uv = in.position.xy / globals.resolution; + let mask_value = textureSample(mask_tex, mask_sampler, screen_uv).r; + + if (mask_value < 0.5) { + discard; + } + + let obj = object_data.objects[in.instance_index]; + let obj_type = obj.params.x; + + var p: vec3<f32>; + var normal: vec3<f32>; + var base_color = in.color.rgb; + let light_dir = normalize(vec3<f32>(1.0, 1.0, 1.0)); + + let ray_origin = globals.camera_pos_time.xyz; + let ray_dir = normalize(in.world_pos - ray_origin); + let inv_model = obj.inv_model; + + let local_origin = (inv_model * vec4<f32>(ray_origin, 1.0)).xyz; + let local_dir = normalize((inv_model * vec4<f32>(ray_dir, 0.0)).xyz); + + let bounds = ray_box_intersection(local_origin, local_dir, vec3<f32>(1.0)); + if (!bounds.hit) { + discard; + } + + let t_start = bounds.t_entry; + let t_end = bounds.t_exit; + + var t_march = t_start; + let max_steps = 128; + var hit = false; + var local_p = vec3<f32>(0.0); + + for (var step = 0; step < max_steps; step++) { + local_p = local_origin + t_march * local_dir; + let d = sdBox(local_p, vec3<f32>(1.0)); + + if (d < 0.001) { + hit = true; + break; + } + + t_march += max(d * 0.5, 0.001); + if (t_march > t_end) { + break; + } + } + + if (!hit) { + discard; + } + + p = local_p; + let eps = 0.001; + normal = normalize(vec3<f32>( + sdBox(p + vec3<f32>(eps, 0.0, 0.0), vec3<f32>(1.0)) - + sdBox(p - vec3<f32>(eps, 0.0, 0.0), vec3<f32>(1.0)), + sdBox(p + vec3<f32>(0.0, eps, 0.0), vec3<f32>(1.0)) - + sdBox(p - vec3<f32>(0.0, eps, 0.0), vec3<f32>(1.0)), + sdBox(p + vec3<f32>(0.0, 0.0, eps), vec3<f32>(1.0)) - + sdBox(p - vec3<f32>(0.0, 0.0, eps), vec3<f32>(1.0)) + )); + + let world_p = (obj.model * vec4<f32>(p, 1.0)).xyz; + let world_normal = normalize((obj.model * vec4<f32>(normal, 0.0)).xyz); + + let bump_strength = 0.3; + let bump_scale = 4.0; + let noise_uv = world_p.xy * bump_scale; + let noise_val = textureSample(noise_tex, noise_sampler, noise_uv).r; + let bump_offset = (noise_val - 0.5) * bump_strength; + + let bumped_normal = normalize(world_normal + vec3<f32>(bump_offset)); + + let diffuse = max(dot(bumped_normal, light_dir), 0.0); + let ambient = 0.3; + let lighting = ambient + diffuse * 0.7; + + let final_color = base_color * lighting; + + let clip_p = globals.view_proj * vec4<f32>(world_p, 1.0); + let depth = clip_p.z / clip_p.w; + + var out: FragmentOutput; + out.color = vec4<f32>(final_color, 1.0); + out.depth = depth; + + return out; +} diff --git a/workspaces/test/shaders/math/common_utils.wgsl b/workspaces/test/shaders/math/common_utils.wgsl new file mode 100644 index 0000000..7131216 --- /dev/null +++ b/workspaces/test/shaders/math/common_utils.wgsl @@ -0,0 +1,36 @@ +// Common utility functions for WGSL shaders. +// Reduces duplication across renderer_3d, mesh_render, etc. + +// Constants +const PI: f32 = 3.14159265359; +const TAU: f32 = 6.28318530718; + +// Transform normal from local to world space using inverse model matrix +fn transform_normal(inv_model: mat4x4<f32>, normal_local: vec3<f32>) -> vec3<f32> { + let normal_matrix = mat3x3<f32>(inv_model[0].xyz, inv_model[1].xyz, inv_model[2].xyz); + return normalize(normal_matrix * normal_local); +} + +// Spherical UV mapping (sphere or any radial surface) +// Returns UV in [0,1] range +fn spherical_uv(p: vec3<f32>) -> vec2<f32> { + let u = atan2(p.x, p.z) / TAU + 0.5; + let v = acos(clamp(p.y / length(p), -1.0, 1.0)) / PI; + return vec2<f32>(u, v); +} + +// Spherical UV from direction vector (for skybox, etc.) +fn spherical_uv_from_dir(dir: vec3<f32>) -> vec2<f32> { + let u = atan2(dir.z, dir.x) / TAU + 0.5; + let v = asin(clamp(dir.y, -1.0, 1.0)) / PI + 0.5; + return vec2<f32>(u, v); +} + +// Grid pattern for procedural texturing (checkerboard-like) +fn grid_pattern(uv: vec2<f32>) -> f32 { + let grid = 0.5 + 0.5 * sin(uv.x * PI) * sin(uv.y * PI); + return smoothstep(0.45, 0.55, grid); +} + +// NOTE: calc_sdf_normal_bumped() removed - too specialized, depends on get_dist() +// from scene_query snippets. Keep bump mapping code inline in shaders that use it. diff --git a/workspaces/test/shaders/math/noise.wgsl b/workspaces/test/shaders/math/noise.wgsl new file mode 100644 index 0000000..9f99e4a --- /dev/null +++ b/workspaces/test/shaders/math/noise.wgsl @@ -0,0 +1,147 @@ +// Random number generation and noise functions for WGSL shaders. +// Collection of hash functions and noise generators. + +// ============================================ +// Hash Functions (Float Input) +// ============================================ + +// Hash: f32 -> f32 +// Fast fractional hash for floats +fn hash_1f(x: f32) -> f32 { + var v = fract(x * 0.3351); + v *= v + 33.33; + v *= v + v; + return fract(v); +} + +// Hash: vec2<f32> -> f32 +// 2D coordinate to single hash value +fn hash_2f(p: vec2<f32>) -> f32 { + var h = dot(p, vec2<f32>(127.1, 311.7)); + return fract(sin(h) * 43758.5453123); +} + +// Hash: vec2<f32> -> vec2<f32> +// 2D coordinate to 2D hash (from Shadertoy 4djSRW) +fn hash_2f_2f(p: vec2<f32>) -> vec2<f32> { + var p3 = fract(vec3<f32>(p.x, p.y, p.x) * vec3<f32>(0.1021, 0.1013, 0.0977)); + p3 += dot(p3, p3.yzx + 33.33); + return fract((p3.xx + p3.yz) * p3.zy); +} + +// Hash: vec3<f32> -> f32 +// 3D coordinate to single hash value +fn hash_3f(p: vec3<f32>) -> f32 { + var h = dot(p, vec3<f32>(127.1, 311.7, 74.7)); + return fract(sin(h) * 43758.5453123); +} + +// Hash: vec3<f32> -> vec3<f32> +// 3D coordinate to 3D hash +fn hash_3f_3f(p: vec3<f32>) -> vec3<f32> { + var v = fract(p); + v += dot(v, v.yxz + 32.41); + return fract((v.xxy + v.yzz) * v.zyx); +} + +// ============================================ +// Hash Functions (Integer Input) +// ============================================ + +// Hash: u32 -> f32 +// Integer hash with bit operations (high quality) +fn hash_1u(p: u32) -> f32 { + var P = (p << 13u) ^ p; + P = P * (P * P * 15731u + 789221u) + 1376312589u; + return bitcast<f32>((P >> 9u) | 0x3f800000u) - 1.0; +} + +// Hash: u32 -> vec2<f32> +fn hash_1u_2f(p: u32) -> vec2<f32> { + return vec2<f32>(hash_1u(p), hash_1u(p + 1423u)); +} + +// Hash: u32 -> vec3<f32> +fn hash_1u_3f(p: u32) -> vec3<f32> { + return vec3<f32>(hash_1u(p), hash_1u(p + 1423u), hash_1u(p + 124453u)); +} + +// ============================================ +// Noise Functions +// ============================================ + +// Value Noise: 2D +// Interpolated grid noise using smoothstep +fn noise_2d(p: vec2<f32>) -> f32 { + let i = floor(p); + let f = fract(p); + let u = f * f * (3.0 - 2.0 * f); + let n0 = hash_2f(i + vec2<f32>(0.0, 0.0)); + let n1 = hash_2f(i + vec2<f32>(1.0, 0.0)); + let n2 = hash_2f(i + vec2<f32>(0.0, 1.0)); + let n3 = hash_2f(i + vec2<f32>(1.0, 1.0)); + let ix0 = mix(n0, n1, u.x); + let ix1 = mix(n2, n3, u.x); + return mix(ix0, ix1, u.y); +} + +// Value Noise: 3D +fn noise_3d(p: vec3<f32>) -> f32 { + let i = floor(p); + let f = fract(p); + let u = f * f * (3.0 - 2.0 * f); + let n000 = hash_3f(i + vec3<f32>(0.0, 0.0, 0.0)); + let n100 = hash_3f(i + vec3<f32>(1.0, 0.0, 0.0)); + let n010 = hash_3f(i + vec3<f32>(0.0, 1.0, 0.0)); + let n110 = hash_3f(i + vec3<f32>(1.0, 1.0, 0.0)); + let n001 = hash_3f(i + vec3<f32>(0.0, 0.0, 1.0)); + let n101 = hash_3f(i + vec3<f32>(1.0, 0.0, 1.0)); + let n011 = hash_3f(i + vec3<f32>(0.0, 1.0, 1.0)); + let n111 = hash_3f(i + vec3<f32>(1.0, 1.0, 1.0)); + let ix00 = mix(n000, n100, u.x); + let ix10 = mix(n010, n110, u.x); + let ix01 = mix(n001, n101, u.x); + let ix11 = mix(n011, n111, u.x); + let iy0 = mix(ix00, ix10, u.y); + let iy1 = mix(ix01, ix11, u.y); + return mix(iy0, iy1, u.z); +} + +// ============================================ +// Special Functions +// ============================================ + +// Gyroid function (periodic triply-orthogonal minimal surface) +// Useful for procedural patterns and cellular structures +fn gyroid(p: vec3<f32>) -> f32 { + return abs(0.04 + dot(sin(p), cos(p.zxy))); +} + +// Fractional Brownian Motion (FBM) 2D +// Multi-octave noise for natural-looking variation +fn fbm_2d(p: vec2<f32>, octaves: i32) -> f32 { + var value = 0.0; + var amplitude = 0.5; + var frequency = 1.0; + var pos = p; + for (var i = 0; i < octaves; i++) { + value += amplitude * noise_2d(pos * frequency); + frequency *= 2.0; + amplitude *= 0.5; + } + return value; +} + +// Fractional Brownian Motion (FBM) 3D +fn fbm_3d(p: vec3<f32>, octaves: i32) -> f32 { + var value = 0.0; + var amplitude = 0.5; + var frequency = 1.0; + var pos = p; + for (var i = 0; i < octaves; i++) { + value += amplitude * noise_3d(pos * frequency); + frequency *= 2.0; + amplitude *= 0.5; + } + return value; +} diff --git a/workspaces/test/shaders/math/sdf_shapes.wgsl b/workspaces/test/shaders/math/sdf_shapes.wgsl new file mode 100644 index 0000000..31bbe2d --- /dev/null +++ b/workspaces/test/shaders/math/sdf_shapes.wgsl @@ -0,0 +1,14 @@ +fn sdSphere(p: vec3<f32>, r: f32) -> f32 { + return length(p) - r; +} +fn sdBox(p: vec3<f32>, b: vec3<f32>) -> f32 { + let q = abs(p) - b; + return length(max(q, vec3<f32>(0.0))) + min(max(q.x, max(q.y, q.z)), 0.0); +} +fn sdTorus(p: vec3<f32>, t: vec2<f32>) -> f32 { + let q = vec2<f32>(length(p.xz) - t.x, p.y); + return length(q) - t.y; +} +fn sdPlane(p: vec3<f32>, n: vec3<f32>, h: f32) -> f32 { + return dot(p, n) + h; +} diff --git a/workspaces/test/shaders/math/sdf_utils.wgsl b/workspaces/test/shaders/math/sdf_utils.wgsl new file mode 100644 index 0000000..660a4ce --- /dev/null +++ b/workspaces/test/shaders/math/sdf_utils.wgsl @@ -0,0 +1,115 @@ +fn get_normal_basic(p: vec3<f32>, obj_params: vec4<f32>) -> vec3<f32> { + let obj_type = obj_params.x; + if (obj_type == 1.0) { return normalize(p); } + let e = vec2<f32>(0.001, 0.0); + return normalize(vec3<f32>( + get_dist(p + e.xyy, obj_params) - get_dist(p - e.xyy, obj_params), + get_dist(p + e.yxy, obj_params) - get_dist(p - e.yxy, obj_params), + get_dist(p + e.yyx, obj_params) - get_dist(p - e.yyx, obj_params) + )); +} + +// Optimized normal estimation using tetrahedron pattern (4 SDF evals instead of 6). +// Slightly less accurate than central differences but faster. +// Uses tetrahedral gradient approximation with corners at (±1, ±1, ±1). +fn get_normal_fast(p: vec3<f32>, obj_params: vec4<f32>) -> vec3<f32> { + let obj_type = obj_params.x; + if (obj_type == 1.0) { return normalize(p); } + let eps = 0.0001; + let k = vec2<f32>(1.0, -1.0); + return normalize( + k.xyy * get_dist(p + k.xyy * eps, obj_params) + + k.yyx * get_dist(p + k.yyx * eps, obj_params) + + k.yxy * get_dist(p + k.yxy * eps, obj_params) + + k.xxx * get_dist(p + k.xxx * eps, obj_params) + ); +} + +// Bump-mapped normal using central differences (6 samples: SDF + texture). +// High quality, suitable for detailed surfaces with displacement mapping. +// Note: Requires spherical_uv() function and get_dist() to be available in calling context. +fn get_normal_bump( + p: vec3<f32>, + obj_params: vec4<f32>, + noise_tex: texture_2d<f32>, + noise_sampler: sampler, + disp_strength: f32 +) -> vec3<f32> { + let e = vec2<f32>(0.005, 0.0); + + let q_x1 = p + e.xyy; + let uv_x1 = spherical_uv(q_x1); + let h_x1 = textureSample(noise_tex, noise_sampler, uv_x1).r; + let d_x1 = get_dist(q_x1, obj_params) - disp_strength * h_x1; + + let q_x2 = p - e.xyy; + let uv_x2 = spherical_uv(q_x2); + let h_x2 = textureSample(noise_tex, noise_sampler, uv_x2).r; + let d_x2 = get_dist(q_x2, obj_params) - disp_strength * h_x2; + + let q_y1 = p + e.yxy; + let uv_y1 = spherical_uv(q_y1); + let h_y1 = textureSample(noise_tex, noise_sampler, uv_y1).r; + let d_y1 = get_dist(q_y1, obj_params) - disp_strength * h_y1; + + let q_y2 = p - e.yxy; + let uv_y2 = spherical_uv(q_y2); + let h_y2 = textureSample(noise_tex, noise_sampler, uv_y2).r; + let d_y2 = get_dist(q_y2, obj_params) - disp_strength * h_y2; + + let q_z1 = p + e.yyx; + let uv_z1 = spherical_uv(q_z1); + let h_z1 = textureSample(noise_tex, noise_sampler, uv_z1).r; + let d_z1 = get_dist(q_z1, obj_params) - disp_strength * h_z1; + + let q_z2 = p - e.yyx; + let uv_z2 = spherical_uv(q_z2); + let h_z2 = textureSample(noise_tex, noise_sampler, uv_z2).r; + let d_z2 = get_dist(q_z2, obj_params) - disp_strength * h_z2; + + return normalize(vec3<f32>(d_x1 - d_x2, d_y1 - d_y2, d_z1 - d_z2)); +} + +// Optimized bump-mapped normal using tetrahedron pattern (4 samples instead of 6). +// 33% faster than get_normal_bump(), slightly less accurate. +// Suitable for real-time rendering with displacement mapping. +fn get_normal_bump_fast( + p: vec3<f32>, + obj_params: vec4<f32>, + noise_tex: texture_2d<f32>, + noise_sampler: sampler, + disp_strength: f32 +) -> vec3<f32> { + let eps = 0.0005; + let k = vec2<f32>(1.0, -1.0); + + let q1 = p + k.xyy * eps; + let uv1 = spherical_uv(q1); + let h1 = textureSample(noise_tex, noise_sampler, uv1).r; + let d1 = get_dist(q1, obj_params) - disp_strength * h1; + + let q2 = p + k.yyx * eps; + let uv2 = spherical_uv(q2); + let h2 = textureSample(noise_tex, noise_sampler, uv2).r; + let d2 = get_dist(q2, obj_params) - disp_strength * h2; + + let q3 = p + k.yxy * eps; + let uv3 = spherical_uv(q3); + let h3 = textureSample(noise_tex, noise_sampler, uv3).r; + let d3 = get_dist(q3, obj_params) - disp_strength * h3; + + let q4 = p + k.xxx * eps; + let uv4 = spherical_uv(q4); + let h4 = textureSample(noise_tex, noise_sampler, uv4).r; + let d4 = get_dist(q4, obj_params) - disp_strength * h4; + + return normalize(k.xyy * d1 + k.yyx * d2 + k.yxy * d3 + k.xxx * d4); +} + +// Distance to an Axis-Aligned Bounding Box +fn aabb_sdf(p: vec3<f32>, min_p: vec3<f32>, max_p: vec3<f32>) -> f32 { + let center = (min_p + max_p) * 0.5; + let extent = (max_p - min_p) * 0.5; + let q = abs(p - center) - extent; + return length(max(q, vec3<f32>(0.0))) + min(max(q.x, max(q.y, q.z)), 0.0); +} diff --git a/workspaces/test/shaders/mesh_render.wgsl b/workspaces/test/shaders/mesh_render.wgsl new file mode 100644 index 0000000..7390b06 --- /dev/null +++ b/workspaces/test/shaders/mesh_render.wgsl @@ -0,0 +1,58 @@ +#include "common_uniforms" +#include "math/common_utils" + +@group(0) @binding(0) var<uniform> globals: GlobalUniforms; +@group(0) @binding(1) var<storage, read> object_data: ObjectsBuffer; + +// Binding 2 is reserved for BVH (not used here but matches layout for simplicity) + +@group(0) @binding(3) var noise_tex: texture_2d<f32>; +@group(0) @binding(4) var noise_sampler: sampler; +@group(0) @binding(5) var sky_tex: texture_2d<f32>; + +struct VertexInput { + @location(0) position: vec3<f32>, + @location(1) normal: vec3<f32>, + @location(2) uv: vec2<f32>, +}; + +struct VertexOutput { + @builtin(position) clip_pos: vec4<f32>, + @location(0) world_pos: vec3<f32>, + @location(1) normal: vec3<f32>, + @location(2) uv: vec2<f32>, + @location(3) color: vec4<f32>, + @location(4) @interpolate(flat) instance_index: u32, +}; + +@vertex +fn vs_main(in: VertexInput, @builtin(instance_index) instance_index: u32) -> VertexOutput { + let obj = object_data.objects[instance_index]; + let world_pos = obj.model * vec4<f32>(in.position, 1.0); + + var out: VertexOutput; + out.clip_pos = globals.view_proj * world_pos; + out.world_pos = world_pos.xyz; + + // Transform normal from local to world space + out.normal = transform_normal(obj.inv_model, in.normal); + + out.uv = in.uv; + out.color = obj.color; + out.instance_index = instance_index; + return out; +} + +#include "render/scene_query_mode" +#include "render/shadows" +#include "render/lighting_utils" + +@fragment +fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> { + let light_dir = normalize(vec3<f32>(1.0, 1.0, 1.0)); + + let shadow = calc_shadow(in.world_pos, light_dir, 0.05, 20.0, in.instance_index); + let lit_color = calculate_lighting(in.color.rgb, in.normal, in.world_pos, shadow); + + return vec4<f32>(lit_color, in.color.a); +}
\ No newline at end of file diff --git a/workspaces/test/shaders/particle_compute.wgsl b/workspaces/test/shaders/particle_compute.wgsl new file mode 100644 index 0000000..ae513c8 --- /dev/null +++ b/workspaces/test/shaders/particle_compute.wgsl @@ -0,0 +1,30 @@ +struct Particle { + pos: vec4<f32>, + vel: vec4<f32>, + rot: vec4<f32>, + color: vec4<f32>, +}; + +#include "common_uniforms" + +@group(0) @binding(0) var<storage, read_write> particles: array<Particle>; +@group(0) @binding(1) var<uniform> uniforms: CommonUniforms; + +@compute @workgroup_size(64) +fn main(@builtin(global_invocation_id) id: vec3<u32>) { + let i = id.x; + if (i >= arrayLength(&particles)) { + return; + } + var p = particles[i]; + let new_pos = p.pos.xyz + p.vel.xyz * 0.016; + p.pos = vec4<f32>(new_pos, p.pos.w); + p.vel.y = p.vel.y - 0.01 * (1.0 + uniforms.audio_intensity * 5.0); + p.rot.x = p.rot.x + p.rot.y * 0.016; + if (p.pos.y < -1.5) { + p.pos.y = 1.5; + p.pos.x = (f32(i % 100u) / 50.0) - 1.0 + (uniforms.audio_intensity * 0.5); + p.vel.y = 0.0; + } + particles[i] = p; +} diff --git a/workspaces/test/shaders/particle_render.wgsl b/workspaces/test/shaders/particle_render.wgsl new file mode 100644 index 0000000..6a2b636 --- /dev/null +++ b/workspaces/test/shaders/particle_render.wgsl @@ -0,0 +1,52 @@ +struct Particle { + pos: vec4<f32>, + vel: vec4<f32>, + rot: vec4<f32>, + color: vec4<f32>, +}; + +#include "common_uniforms" + +@group(0) @binding(0) var<storage, read> particles: array<Particle>; +@group(0) @binding(1) var<uniform> uniforms: CommonUniforms; + +struct VSOut { + @builtin(position) pos: vec4<f32>, + @location(0) color: vec4<f32>, + @location(1) uv: vec2<f32>, +}; + +@vertex fn vs_main(@builtin(vertex_index) vi: u32, @builtin(instance_index) ii: u32) -> VSOut { + let p = particles[ii]; + let size = 0.02 + p.pos.z * 0.01 + uniforms.audio_intensity * 0.02; + var offsets = array<vec2<f32>, 6>( + vec2<f32>(-1, -1), + vec2<f32>(1, -1), + vec2<f32>(-1, 1), + vec2<f32>(-1, 1), + vec2<f32>(1, -1), + vec2<f32>(1, 1) + ); + let offset = offsets[vi]; + let c = cos(p.rot.x); + let s = sin(p.rot.x); + let rotated_offset = vec2<f32>(offset.x * c - offset.y * s, offset.x * s + offset.y * c); + let pos = vec2<f32>(p.pos.x + rotated_offset.x * size / uniforms.aspect_ratio, p.pos.y + rotated_offset.y * size); + + // Fade based on lifetime (p.pos.w goes from 1.0 to 0.0) + let lifetime_fade = p.pos.w; + let color_with_fade = vec4<f32>(p.color.rgb * (0.5 + 0.5 * uniforms.audio_intensity), p.color.a * lifetime_fade); + + return VSOut(vec4<f32>(pos, 0.0, 1.0), color_with_fade, offset); +} + +@fragment fn fs_main(@location(0) color: vec4<f32>, @location(1) uv: vec2<f32>) -> @location(0) vec4<f32> { + // Calculate distance from center for circular shape + let dist = length(uv); + + // Smooth circular falloff (1.0 at center, 0.0 at edge) + let circle_alpha = smoothstep(1.0, 0.5, dist); + + // Apply circular fade to alpha channel + return vec4<f32>(color.rgb, color.a * circle_alpha); +} diff --git a/workspaces/test/shaders/particle_spray_compute.wgsl b/workspaces/test/shaders/particle_spray_compute.wgsl new file mode 100644 index 0000000..a4041f2 --- /dev/null +++ b/workspaces/test/shaders/particle_spray_compute.wgsl @@ -0,0 +1,35 @@ +struct Particle { + pos: vec4<f32>, + vel: vec4<f32>, + rot: vec4<f32>, + color: vec4<f32>, +}; + +#include "common_uniforms" + +@group(0) @binding(0) var<storage, read_write> particles: array<Particle>; +@group(0) @binding(1) var<uniform> uniforms: CommonUniforms; + +fn hash(p: f32) -> f32 { + return fract(sin(p) * 43758.5453); +} + +@compute @workgroup_size(64) +fn main(@builtin(global_invocation_id) id: vec3<u32>) { + let i = id.x; + if (i >= arrayLength(&particles)) { + return; + } + var p = particles[i]; + if (p.pos.w <= 0.0) { + let r = hash(f32(i) + uniforms.time); + let angle = r * 6.28318; + p.pos = vec4<f32>(0.0, 0.0, 0.0, 1.0); + p.vel = vec4<f32>(cos(angle), sin(angle), 0.0, 0.0) * (0.5 + hash(r) * 0.5) * (1.0 + uniforms.audio_intensity * 2.0); + p.color = vec4<f32>(hash(r + 0.1), hash(r + 0.2), 1.0, 1.0); + } + let new_pos = p.pos.xyz + p.vel.xyz * 0.016; + p.pos = vec4<f32>(new_pos, p.pos.w - 0.01 * (1.0 + uniforms.beat)); + p.vel.y = p.vel.y - 0.01; + particles[i] = p; +} diff --git a/workspaces/test/shaders/passthrough.wgsl b/workspaces/test/shaders/passthrough.wgsl new file mode 100644 index 0000000..266e231 --- /dev/null +++ b/workspaces/test/shaders/passthrough.wgsl @@ -0,0 +1,18 @@ +@group(0) @binding(0) var smplr: sampler; +@group(0) @binding(1) var txt: texture_2d<f32>; + +#include "common_uniforms" +@group(0) @binding(2) var<uniform> uniforms: CommonUniforms; + +@vertex fn vs_main(@builtin(vertex_index) i: u32) -> @builtin(position) vec4<f32> { + var pos = array<vec2<f32>, 3>( + vec2<f32>(-1, -1), + vec2<f32>(3, -1), + vec2<f32>(-1, 3) + ); + return vec4<f32>(pos[i], 0.0, 1.0); +} + +@fragment fn fs_main(@builtin(position) p: vec4<f32>) -> @location(0) vec4<f32> { + return textureSample(txt, smplr, p.xy / uniforms.resolution); +} diff --git a/workspaces/test/shaders/ray_box.wgsl b/workspaces/test/shaders/ray_box.wgsl new file mode 100644 index 0000000..d56ea1b --- /dev/null +++ b/workspaces/test/shaders/ray_box.wgsl @@ -0,0 +1,16 @@ +struct RayBounds { + t_entry: f32, + t_exit: f32, + hit: bool, +}; + +fn ray_box_intersection(ro: vec3<f32>, rd: vec3<f32>, extent: vec3<f32>) -> RayBounds { + let inv_rd = 1.0 / rd; + let t0 = (-extent - ro) * inv_rd; + let t1 = (extent - ro) * inv_rd; + let tmin_vec = min(t0, t1); + let tmax_vec = max(t0, t1); + let t_entry = max(0.0, max(tmin_vec.x, max(tmin_vec.y, tmin_vec.z))); + let t_exit = min(tmax_vec.x, min(tmax_vec.y, tmax_vec.z)); + return RayBounds(t_entry, t_exit, t_entry <= t_exit); +} diff --git a/workspaces/test/shaders/ray_triangle.wgsl b/workspaces/test/shaders/ray_triangle.wgsl new file mode 100644 index 0000000..13341c8 --- /dev/null +++ b/workspaces/test/shaders/ray_triangle.wgsl @@ -0,0 +1,30 @@ +// This file is part of the 64k demo project. +// Möller-Trumbore ray-triangle intersection algorithm. +// Reference: "Fast, Minimum Storage Ray-Triangle Intersection" + +struct TriangleHit { + uv: vec2<f32>, + z: f32, + N: vec3<f32>, + hit: bool, +}; + +fn ray_triangle_intersection( + orig: vec3<f32>, + dir: vec3<f32>, + p0: vec3<f32>, + p1: vec3<f32>, + p2: vec3<f32> +) -> TriangleHit { + let d10 = p1 - p0; + let d20 = p2 - p0; + let N = cross(d10, d20); + let det = -dot(dir, N); + let invdet = 1.0 / det; + let d0 = orig - p0; + let nd = cross(d0, dir); + let uv = vec2<f32>(dot(d20, nd), -dot(d10, nd)) * invdet; + let z = dot(d0, N) * invdet; + let hit = det > 0.0 && z >= 0.0 && uv.x >= 0.0 && uv.y >= 0.0 && (uv.x + uv.y) < 1.0; + return TriangleHit(uv, z, N, hit); +} diff --git a/workspaces/test/shaders/render/lighting_utils.wgsl b/workspaces/test/shaders/render/lighting_utils.wgsl new file mode 100644 index 0000000..d2fd2e2 --- /dev/null +++ b/workspaces/test/shaders/render/lighting_utils.wgsl @@ -0,0 +1,6 @@ +fn calculate_lighting(color: vec3<f32>, normal: vec3<f32>, pos: vec3<f32>, shadow: f32) -> vec3<f32> { + let light_dir = normalize(vec3<f32>(1.0, 1.0, 1.0)); + let diffuse = max(dot(normal, light_dir), 0.0); + let lighting = diffuse * (0.1 + 0.9 * shadow) + 0.1; // Ambient + Shadowed Diffuse + return color * lighting; +} diff --git a/workspaces/test/shaders/render/scene_query_bvh.wgsl b/workspaces/test/shaders/render/scene_query_bvh.wgsl new file mode 100644 index 0000000..3e6f895 --- /dev/null +++ b/workspaces/test/shaders/render/scene_query_bvh.wgsl @@ -0,0 +1,67 @@ +#include "math/sdf_shapes" +#include "math/sdf_utils" + +struct BVHNode { + min: vec3<f32>, + left_idx: i32, + max: vec3<f32>, + obj_idx_or_right: i32, +}; + +@group(0) @binding(2) var<storage, read> bvh_nodes: array<BVHNode>; + +fn get_dist(p: vec3<f32>, obj_params: vec4<f32>) -> f32 { + let obj_type = obj_params.x; + 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); } + if (obj_type == 5.0) { return sdBox(p, obj_params.yzw); } // MESH AABB + return 100.0; +} + +fn map_scene(p: vec3<f32>, skip_idx: u32) -> f32 { + var d = 1000.0; + var stack: array<i32, 32>; + var stack_ptr = 0; + + if (arrayLength(&bvh_nodes) > 0u) { + stack[stack_ptr] = 0; + stack_ptr++; + } + + while (stack_ptr > 0) { + stack_ptr--; + let node_idx = stack[stack_ptr]; + let node = bvh_nodes[node_idx]; + + if (aabb_sdf(p, node.min, node.max) < d) { + if (node.left_idx < 0) { // Leaf + let obj_idx = u32(node.obj_idx_or_right); + if (obj_idx == skip_idx) { continue; } + let obj = object_data.objects[obj_idx]; + let q = (obj.inv_model * vec4<f32>(p, 1.0)).xyz; + + // Extract scale factors from the model matrix + let sx = length(obj.model[0].xyz); + let sy = length(obj.model[1].xyz); + let sz = length(obj.model[2].xyz); + + var s = min(sx, min(sy, sz)); + if (obj.params.x == 4.0) { + s = sy; // Plane normal is (0,1,0) in local space + } + + d = min(d, get_dist(q, obj.params) * s); + } else { // Internal + if (stack_ptr < 31) { + stack[stack_ptr] = node.left_idx; + stack_ptr++; + stack[stack_ptr] = node.obj_idx_or_right; + stack_ptr++; + } + } + } + } + return d; +} diff --git a/workspaces/test/shaders/render/scene_query_linear.wgsl b/workspaces/test/shaders/render/scene_query_linear.wgsl new file mode 100644 index 0000000..0497a40 --- /dev/null +++ b/workspaces/test/shaders/render/scene_query_linear.wgsl @@ -0,0 +1,56 @@ +#include "math/sdf_shapes" +#include "math/sdf_utils" + +fn get_dist(p: vec3<f32>, obj_params: vec4<f32>) -> f32 { + let obj_type = obj_params.x; + 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); } + if (obj_type == 5.0) { return sdBox(p, obj_params.yzw); } // MESH AABB + return 100.0; +} + +fn map_scene(p: vec3<f32>, skip_idx: u32) -> f32 { + + var d = 1000.0; + + let num_objects = arrayLength(&object_data.objects); + + for (var i = 0u; i < num_objects; i++) { + + if (i == skip_idx) { continue; } + + let obj = object_data.objects[i]; + + let q = (obj.inv_model * vec4<f32>(p, 1.0)).xyz; + + + + // Extract scale factors from the model matrix + + let sx = length(obj.model[0].xyz); + + let sy = length(obj.model[1].xyz); + + let sz = length(obj.model[2].xyz); + + + + var s = min(sx, min(sy, sz)); + + if (obj.params.x == 4.0) { + + s = sy; // Plane normal is (0,1,0) in local space + + } + + + + d = min(d, get_dist(q, obj.params) * s); + + } + + return d; + +} diff --git a/workspaces/test/shaders/render/shadows.wgsl b/workspaces/test/shaders/render/shadows.wgsl new file mode 100644 index 0000000..7cba089 --- /dev/null +++ b/workspaces/test/shaders/render/shadows.wgsl @@ -0,0 +1,13 @@ +fn calc_shadow(ro: vec3<f32>, rd: vec3<f32>, tmin: f32, tmax: f32, skip_idx: u32) -> f32 { + var res = 1.0; + var t = tmin; + if (t < 0.05) { t = 0.05; } + for (var i = 0; i < 32; i = i + 1) { + let h = map_scene(ro + rd * t, skip_idx); + if (h < 0.001) { return 0.0; } + res = min(res, 16.0 * h / t); + t = t + clamp(h, 0.02, 0.4); + if (t > tmax) { break; } + } + return clamp(res, 0.0, 1.0); +} diff --git a/workspaces/test/shaders/renderer_3d.wgsl b/workspaces/test/shaders/renderer_3d.wgsl new file mode 100644 index 0000000..d3b0bae --- /dev/null +++ b/workspaces/test/shaders/renderer_3d.wgsl @@ -0,0 +1,167 @@ +#include "common_uniforms" +#include "math/common_utils" +#include "math/sdf_utils" + +@group(0) @binding(0) var<uniform> globals: GlobalUniforms; +@group(0) @binding(1) var<storage, read> object_data: ObjectsBuffer; + +// Binding 2 is reserved for BVH buffer when enabled + +@group(0) @binding(3) var noise_tex: texture_2d<f32>; +@group(0) @binding(4) var noise_sampler: sampler; +@group(0) @binding(5) var sky_tex: texture_2d<f32>; + +struct VertexOutput { + @builtin(position) position: vec4<f32>, + @location(0) local_pos: vec3<f32>, + @location(1) color: vec4<f32>, + @location(2) @interpolate(flat) instance_index: u32, + @location(3) world_pos: vec3<f32>, + @location(4) transformed_normal: vec3<f32>, +}; + +@vertex +fn vs_main(@builtin(vertex_index) vertex_index: u32, + @builtin(instance_index) instance_index: u32) -> VertexOutput { + + var pos = array<vec3<f32>, 36>( + vec3(-1.0, -1.0, 1.0), vec3( 1.0, -1.0, 1.0), vec3( 1.0, 1.0, 1.0), + vec3(-1.0, -1.0, 1.0), vec3( 1.0, 1.0, 1.0), vec3(-1.0, 1.0, 1.0), + vec3(-1.0, -1.0, -1.0), vec3(-1.0, 1.0, -1.0), vec3( 1.0, 1.0, -1.0), + vec3(-1.0, -1.0, -1.0), vec3( 1.0, 1.0, -1.0), vec3( 1.0, -1.0, -1.0), + vec3(-1.0, 1.0, -1.0), vec3(-1.0, 1.0, 1.0), vec3( 1.0, 1.0, 1.0), + vec3(-1.0, 1.0, -1.0), vec3( 1.0, 1.0, 1.0), vec3( 1.0, 1.0, -1.0), + vec3(-1.0, -1.0, -1.0), vec3( 1.0, -1.0, -1.0), vec3( 1.0, -1.0, 1.0), + vec3(-1.0, -1.0, -1.0), vec3( 1.0, -1.0, 1.0), vec3(-1.0, -1.0, 1.0), + vec3( 1.0, -1.0, -1.0), vec3( 1.0, 1.0, -1.0), vec3( 1.0, 1.0, 1.0), + vec3( 1.0, -1.0, -1.0), vec3( 1.0, 1.0, 1.0), vec3( 1.0, -1.0, 1.0), + vec3(-1.0, -1.0, -1.0), vec3(-1.0, -1.0, 1.0), vec3(-1.0, 1.0, 1.0), + vec3(-1.0, -1.0, -1.0), vec3(-1.0, 1.0, 1.0), vec3(-1.0, 1.0, -1.0) + ); + + var p = pos[vertex_index]; + let obj = object_data.objects[instance_index]; + let obj_type = obj.params.x; + + if (obj_type == 5.0) { // MESH + // For meshes, we use the actual vertex data, not proxy geometry. + // The position here is a placeholder, the real mesh data is handled by mesh_pipeline_. + var out: VertexOutput; + out.position = vec4<f32>(0.0, 0.0, 2.0, 1.0); // Outside far plane, so it's not rendered by this pipeline. + return out; + } + + // Tight fit for Torus proxy hull (major radius 1.0, minor 0.4) + if (obj_type == 3.0) { + p.x = p.x * 1.5; + p.z = p.z * 1.5; + p.y = p.y * 0.5; + } + + let world_pos = obj.model * vec4<f32>(p, 1.0); + let clip_pos = globals.view_proj * world_pos; + + var out: VertexOutput; + out.position = clip_pos; + out.local_pos = p; + out.color = obj.color; + out.instance_index = instance_index; + out.world_pos = world_pos.xyz; + + // For SDF primitives, we don't use vertex normals - they are computed analytically in the fragment shader. + // This field is only used by the mesh pipeline (mesh_render.wgsl), not this SDF pipeline. + out.transformed_normal = normalize(vec3<f32>(0.0, 1.0, 0.0)); // Placeholder + + return out; +} + +#include "render/scene_query_mode" +#include "render/shadows" +#include "render/lighting_utils" +#include "ray_box" + +struct FragmentOutput { + @location(0) color: vec4<f32>, + @builtin(frag_depth) depth: f32, +}; + +@fragment +fn fs_main(in: VertexOutput) -> FragmentOutput { + let obj = object_data.objects[in.instance_index]; + let obj_type = obj.params.x; + + var p: vec3<f32>; + var normal: vec3<f32>; + var base_color = in.color.rgb; + let light_dir = normalize(vec3<f32>(1.0, 1.0, 1.0)); + + if (obj_type <= 0.0) { // Raster path (legacy or generic) + p = in.world_pos; + // Use the transformed normal passed from the vertex shader for rasterized objects + normal = normalize(in.transformed_normal); + + // Apply grid pattern to floor + let uv = p.xz * 0.5; + let grid_val = grid_pattern(uv); + base_color = base_color * (0.5 + 0.5 * grid_val); + } else { // SDF path + let ro_world = globals.camera_pos_time.xyz; + let rd_world = normalize(in.world_pos - ro_world); + + // Ray-Box Intersection in local space to find tight bounds + let ro_local = (obj.inv_model * vec4<f32>(ro_world, 1.0)).xyz; + let rd_local = normalize((obj.inv_model * vec4<f32>(rd_world, 0.0)).xyz); + + // Proxy box extent (matches vs_main) + // MESHES use obj.params.yzw for extent + var extent = vec3<f32>(1.0); + if (obj.params.x == 3.0) { extent = vec3<f32>(1.5, 0.5, 1.5); } // Torus + else if (obj.params.x == 5.0) { extent = obj.params.yzw; } // MESH extent + + let bounds = ray_box_intersection(ro_local, rd_local, extent); + + if (!bounds.hit) { discard; } + + var t = bounds.t_entry; + var hit = false; + for (var i = 0; i < 64; i = i + 1) { + let q = ro_local + rd_local * t; + let d_local = get_dist(q, obj.params); + if (d_local < 0.0005) { hit = true; break; } + t = t + d_local; + if (t > bounds.t_exit) { break; } + } + if (!hit) { discard; } + + let q_hit = ro_local + rd_local * t; + p = (obj.model * vec4<f32>(q_hit, 1.0)).xyz; // Correct world position + + // Calculate normal with bump mapping (using utility function) + let disp_strength = 0.05; + let n_local = get_normal_bump(q_hit, obj.params, noise_tex, noise_sampler, disp_strength); + normal = transform_normal(obj.inv_model, n_local); + + // Apply texture to SDF color + if (in.instance_index == 0u || obj_type == 4.0) { // Floor (index 0) or PLANE + let uv_grid = p.xz * 0.5; + let grid_val = grid_pattern(uv_grid); + base_color = base_color * (0.5 + 0.5 * grid_val); + } else { + let uv_hit = spherical_uv(q_hit); + let tex_val = textureSample(noise_tex, noise_sampler, uv_hit).r; + base_color = base_color * (0.7 + 0.3 * tex_val); + } + } + + let shadow = calc_shadow(p, light_dir, 0.05, 20.0, in.instance_index); + let lit_color = calculate_lighting(base_color, normal, p, shadow); + + var out: FragmentOutput; + out.color = vec4<f32>(lit_color, 1.0); + + // Calculate and write correct depth + let clip_pos = globals.view_proj * vec4<f32>(p, 1.0); + out.depth = clip_pos.z / clip_pos.w; + + return out; +}
\ No newline at end of file diff --git a/workspaces/test/shaders/sdf_primitives.wgsl b/workspaces/test/shaders/sdf_primitives.wgsl new file mode 100644 index 0000000..31bbe2d --- /dev/null +++ b/workspaces/test/shaders/sdf_primitives.wgsl @@ -0,0 +1,14 @@ +fn sdSphere(p: vec3<f32>, r: f32) -> f32 { + return length(p) - r; +} +fn sdBox(p: vec3<f32>, b: vec3<f32>) -> f32 { + let q = abs(p) - b; + return length(max(q, vec3<f32>(0.0))) + min(max(q.x, max(q.y, q.z)), 0.0); +} +fn sdTorus(p: vec3<f32>, t: vec2<f32>) -> f32 { + let q = vec2<f32>(length(p.xz) - t.x, p.y); + return length(q) - t.y; +} +fn sdPlane(p: vec3<f32>, n: vec3<f32>, h: f32) -> f32 { + return dot(p, n) + h; +} diff --git a/workspaces/test/shaders/skybox.wgsl b/workspaces/test/shaders/skybox.wgsl new file mode 100644 index 0000000..31bea3b --- /dev/null +++ b/workspaces/test/shaders/skybox.wgsl @@ -0,0 +1,42 @@ +#include "common_uniforms" +#include "math/common_utils" + +@group(0) @binding(0) var sky_tex: texture_2d<f32>; +@group(0) @binding(1) var sky_sampler: sampler; +@group(0) @binding(2) var<uniform> globals: GlobalUniforms; + +struct VertexOutput { + @builtin(position) position: vec4<f32>, + @location(0) uv: vec2<f32>, +}; + +@vertex +fn vs_main(@builtin(vertex_index) vertex_index: u32) -> VertexOutput { + var pos = array<vec2<f32>, 3>( + vec2<f32>(-1.0, -1.0), + vec2<f32>( 3.0, -1.0), + vec2<f32>(-1.0, 3.0) + ); + + var out: VertexOutput; + out.position = vec4<f32>(pos[vertex_index], 0.0, 1.0); + out.uv = vec2<f32>(pos[vertex_index].x * 0.5 + 0.5, 1.0 - (pos[vertex_index].y * 0.5 + 0.5)); + return out; +} + +@fragment +fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> { + // Convert UV to NDC + let ndc_x = in.uv.x * 2.0 - 1.0; + let ndc_y = (1.0 - in.uv.y) * 2.0 - 1.0; // Un-flip Y for NDC (Y-up) + + // Unproject to find world direction + // We want the direction from camera to the far plane at this pixel + let clip_pos = vec4<f32>(ndc_x, ndc_y, 1.0, 1.0); + let world_pos_h = globals.inv_view_proj * clip_pos; + let world_pos = world_pos_h.xyz / world_pos_h.w; + + let ray_dir = normalize(world_pos - globals.camera_pos_time.xyz); + let uv = spherical_uv_from_dir(ray_dir); + return textureSample(sky_tex, sky_sampler, uv); +}
\ No newline at end of file diff --git a/workspaces/test/shaders/solarize.wgsl b/workspaces/test/shaders/solarize.wgsl new file mode 100644 index 0000000..de15dfc --- /dev/null +++ b/workspaces/test/shaders/solarize.wgsl @@ -0,0 +1,53 @@ +@group(0) @binding(0) var smplr: sampler; +@group(0) @binding(1) var txt: texture_2d<f32>; + +#include "common_uniforms" + +@group(0) @binding(2) var<uniform> uniforms: CommonUniforms; + +@vertex fn vs_main(@builtin(vertex_index) i: u32) -> @builtin(position) vec4<f32> { + var pos = array<vec2<f32>, 3>( + vec2<f32>(-1, -1), + vec2<f32>(3, -1), + vec2<f32>(-1, 3) + ); + return vec4<f32>(pos[i], 0.0, 1.0); +} + +@fragment fn fs_main(@builtin(position) p: vec4<f32>) -> @location(0) vec4<f32> { + let uv = p.xy / uniforms.resolution; + var col = textureSample(txt, smplr, uv); + + // Patterns are 2 seconds long (120 BPM, 4 beats) + let pattern_num = u32(uniforms.time / 2.0); + let is_even_pattern = (pattern_num % 2u) == 0u; + + // Reduced threshold range for subtler effect + let thr = 0.4 + 0.15 * sin(uniforms.time); + let solarize_strength = 0.4; // Reduced from 1.0 for less saturation + + if (is_even_pattern) { + // Even patterns: Subtle red-orange tint + if (col.r < thr) { + col.r = mix(col.r, 1.0 - col.r, solarize_strength); + } + if (col.g < thr) { + col.g = mix(col.g, 1.0 - col.g * 0.7, solarize_strength * 0.7); + } + if (col.b < thr) { + col.b = mix(col.b, col.b * 0.5, solarize_strength); + } + } else { + // Odd patterns: Subtle blue-cyan tint + if (col.r < thr) { + col.r = mix(col.r, col.r * 0.6, solarize_strength); + } + if (col.g < thr) { + col.g = mix(col.g, 1.0 - col.g * 0.8, solarize_strength * 0.8); + } + if (col.b < thr) { + col.b = mix(col.b, 1.0 - col.b, solarize_strength); + } + } + return col; +} diff --git a/workspaces/test/shaders/test_snippet_a.wgsl b/workspaces/test/shaders/test_snippet_a.wgsl new file mode 100644 index 0000000..732973d --- /dev/null +++ b/workspaces/test/shaders/test_snippet_a.wgsl @@ -0,0 +1,4 @@ +// test_snippet_a.wgsl +fn snippet_a() -> f32 { + return 1.0; +} diff --git a/workspaces/test/shaders/test_snippet_b.wgsl b/workspaces/test/shaders/test_snippet_b.wgsl new file mode 100644 index 0000000..071346e --- /dev/null +++ b/workspaces/test/shaders/test_snippet_b.wgsl @@ -0,0 +1,4 @@ +// test_snippet_b.wgsl +fn snippet_b() -> f32 { + return 2.0; +} diff --git a/workspaces/test/shaders/vignette.wgsl b/workspaces/test/shaders/vignette.wgsl new file mode 100644 index 0000000..b129883 --- /dev/null +++ b/workspaces/test/shaders/vignette.wgsl @@ -0,0 +1,32 @@ +@group(0) @binding(0) var input_sampler: sampler; +@group(0) @binding(1) var input_tex: texture_2d<f32>; +#include "common_uniforms" + +struct VignetteParams { + radius: f32, + softness: f32, +}; + +@group(0) @binding(2) var<uniform> common_uniforms: CommonUniforms; +@group(0) @binding(3) var<uniform> params: VignetteParams; + +@vertex +fn vs_main(@builtin(vertex_index) vertex_idx: u32) -> @builtin(position) vec4<f32> { + var pos = array<vec2<f32>, 3>( + vec2<f32>(-1.0, -1.0), + vec2<f32>(3.0, -1.0), + vec2<f32>(-1.0, 3.0) + ); + return vec4<f32>(pos[vertex_idx], 0.0, 1.0); +} + +@fragment +fn fs_main(@builtin(position) pos: vec4<f32>) -> @location(0) vec4<f32> { + let uv = pos.xy / common_uniforms.resolution; + let color = textureSample(input_tex, input_sampler, uv); + + let d = distance(uv, vec2<f32>(0.5, 0.5)); + let vignette = smoothstep(params.radius, params.radius - params.softness, d); + + return vec4<f32>(color.rgb * mix(1.0, vignette, common_uniforms.audio_intensity), color.a); +}
\ No newline at end of file diff --git a/workspaces/test/shaders/visual_debug.wgsl b/workspaces/test/shaders/visual_debug.wgsl new file mode 100644 index 0000000..63e1f13 --- /dev/null +++ b/workspaces/test/shaders/visual_debug.wgsl @@ -0,0 +1,31 @@ +struct GlobalUniforms { + view_proj: mat4x4<f32>, + inv_view_proj: mat4x4<f32>, + camera_pos_time: vec4<f32>, + params: vec4<f32>, + resolution: vec2<f32>, +}; +@group(0) @binding(0) var<uniform> uniforms : GlobalUniforms; + +struct VertexInput { + @location(0) position : vec3<f32>, + @location(1) color : vec3<f32>, +} + +struct VertexOutput { + @builtin(position) position : vec4<f32>, + @location(0) color : vec3<f32>, +} + +@vertex +fn vs_main(in : VertexInput) -> VertexOutput { + var out : VertexOutput; + out.position = uniforms.view_proj * vec4<f32>(in.position, 1.0); + out.color = in.color; + return out; +} + +@fragment +fn fs_main(in : VertexOutput) -> @location(0) vec4<f32> { + return vec4<f32>(in.color, 1.0); +} diff --git a/workspaces/test/timeline.seq b/workspaces/test/timeline.seq new file mode 100644 index 0000000..6dc26ca --- /dev/null +++ b/workspaces/test/timeline.seq @@ -0,0 +1,7 @@ +# Minimal timeline for audio/visual sync testing +# BPM 120 (set in test_demo.track) + +SEQUENCE 0.0 0 "Main Loop" + EFFECT + FlashEffect 0.0 16.0 + +END_DEMO 32b diff --git a/workspaces/test/workspace.cfg b/workspaces/test/workspace.cfg new file mode 100644 index 0000000..af822c5 --- /dev/null +++ b/workspaces/test/workspace.cfg @@ -0,0 +1,18 @@ +[workspace] +name = "Test Demo" +description = "Test and validation demo" +version = "1.0" + +[build] +target = "test_demo" +timeline = "timeline.seq" +music = "music.track" +assets = "assets.txt" +asset_dirs = ["assets/", "../common/"] +shader_dirs = ["shaders/", "../common/shaders/"] + +[options] +width = 1280 +height = 720 +duration = -1 +bpm = 120 |
