Search across packages, versions, files, declarations, and comments. Use multiple terms to narrow results (AND), and add @vertex, @fragment, or @compute to filter by shader stage.
1#import bevy_render::view::View
2#import bevy_pbr::utils::coords_to_viewport_uv
4struct SkyboxUniforms {
5 brightness: f32,
6 transform: mat4x4<f32>,
7#ifdef SIXTEEN_BYTE_ALIGNMENT
8 _wasm_padding_8b: u32,
9 _wasm_padding_12b: u32,
10 _wasm_padding_16b: u32,
11#endif
14@group(0) @binding(0) var skybox: texture_cube<f32>;
15@group(0) @binding(1) var skybox_sampler: sampler;
16@group(0) @binding(2) var<uniform> view: View;
17@group(0) @binding(3) var<uniform> uniforms: SkyboxUniforms;
19fn coords_to_ray_direction(position: vec2<f32>, viewport: vec4<f32>) -> vec3<f32> {
20 // Using world positions of the fragment and camera to calculate a ray direction
21 // breaks down at large translations. This code only needs to know the ray direction.
22 // The ray direction is along the direction from the camera to the fragment position.
23 // In view space, the camera is at the origin, so the view space ray direction is
24 // along the direction of the fragment position - (0,0,0) which is just the
25 // fragment position.
26 // Use the position on the near clipping plane to avoid -inf world position
27 // because the far plane of an infinite reverse projection is at infinity.
28 let view_position_homogeneous = view.view_from_clip * vec4(
29 coords_to_viewport_uv(position, viewport) * vec2(2.0, -2.0) + vec2(-1.0, 1.0),
30 1.0,
31 1.0,
32 );
34 // Transforming the view space ray direction by the skybox transform matrix, it is
35 // equivalent to rotating the skybox itself.
36 var view_ray_direction = view_position_homogeneous.xyz / view_position_homogeneous.w;
37 view_ray_direction = (view.world_from_view * vec4(view_ray_direction, 0.0)).xyz;
39 // Transforming the view space ray direction by the view matrix, transforms the
40 // direction to world space. Note that the w element is set to 0.0, as this is a
41 // vector direction, not a position, That causes the matrix multiplication to ignore
42 // the translations from the view matrix.
43 let ray_direction = (uniforms.transform * vec4(view_ray_direction, 0.0)).xyz;
45 return normalize(ray_direction);
48struct VertexOutput {
49 @builtin(position) position: vec4<f32>,
50};
52// 3 | 2.
53// 2 | : `.
54// 1 | x-----x.
55// 0 | | s | `.
56// -1 | 0-----x.....1
57// +---------------
58// -1 0 1 2 3
59//
60// The axes are clip-space x and y. The region marked s is the visible region.
61// The digits in the corners of the right-angled triangle are the vertex
62// indices.
63@vertex
64fn skybox_vertex(@builtin(vertex_index) vertex_index: u32) -> VertexOutput {
65 // See the explanation above for how this works.
66 let clip_position = vec2(
67 f32(vertex_index & 1u),
68 f32((vertex_index >> 1u) & 1u),
69 ) * 4.0 - vec2(1.0);
71 return VertexOutput(vec4(clip_position, 0.0, 1.0));
74@fragment
75fn skybox_fragment(in: VertexOutput) -> @location(0) vec4<f32> {
76 let ray_direction = coords_to_ray_direction(in.position.xy, view.viewport);
78 // Cube maps are left-handed so we negate the z coordinate.
79 let out = textureSample(skybox, skybox_sampler, ray_direction * vec3(1.0, 1.0, -1.0));
80 return vec4(out.rgb * uniforms.brightness, out.a);
Contents