diff options
Diffstat (limited to 'assets/final/shaders')
| -rw-r--r-- | assets/final/shaders/common_uniforms.wgsl | 3 | ||||
| -rw-r--r-- | assets/final/shaders/skybox.wgsl | 22 |
2 files changed, 21 insertions, 4 deletions
diff --git a/assets/final/shaders/common_uniforms.wgsl b/assets/final/shaders/common_uniforms.wgsl index cefa3b2..4b5cf33 100644 --- a/assets/final/shaders/common_uniforms.wgsl +++ b/assets/final/shaders/common_uniforms.wgsl @@ -1,5 +1,6 @@ struct GlobalUniforms { view_proj: mat4x4<f32>, + inv_view_proj: mat4x4<f32>, camera_pos_time: vec4<f32>, params: vec4<f32>, resolution: vec2<f32>, @@ -12,4 +13,4 @@ struct ObjectData { }; struct ObjectsBuffer { objects: array<ObjectData>, -}; +};
\ No newline at end of file diff --git a/assets/final/shaders/skybox.wgsl b/assets/final/shaders/skybox.wgsl index 8347a5a..6becc1a 100644 --- a/assets/final/shaders/skybox.wgsl +++ b/assets/final/shaders/skybox.wgsl @@ -22,6 +22,22 @@ fn vs_main(@builtin(vertex_index) vertex_index: u32) -> VertexOutput { } @fragment -fn fs_main(@builtin(position) frag_pos: vec4<f32>) -> @location(0) vec4<f32> { - return textureSample(sky_tex, sky_sampler, frag_pos.xy / globals.resolution); -} +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); + + // Spherical Mapping + let u = atan2(ray_dir.z, ray_dir.x) / 6.28318 + 0.5; + let v = asin(clamp(ray_dir.y, -1.0, 1.0)) / 3.14159 + 0.5; + + return textureSample(sky_tex, sky_sampler, vec2<f32>(u, v)); +}
\ No newline at end of file |
