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#define_import_path bevy_pbr::view_transformations
3#import bevy_pbr::mesh_view_bindings as view_bindings
4#import bevy_pbr::prepass_bindings
6/// World space:
7/// +y is up
9/// View space:
10/// -z is forward, +x is right, +y is up
11/// Forward is from the camera position into the scene.
12/// (0.0, 0.0, -1.0) is linear distance of 1.0 in front of the camera's view relative to the camera's rotation
13/// (0.0, 1.0, 0.0) is linear distance of 1.0 above the camera's view relative to the camera's rotation
15/// NDC (normalized device coordinate):
16/// https://www.w3.org/TR/webgpu/#coordinate-systems
17/// (-1.0, -1.0) in NDC is located at the bottom-left corner of NDC
18/// (1.0, 1.0) in NDC is located at the top-right corner of NDC
19/// Z is depth where:
20/// 1.0 is near clipping plane
21/// Perspective projection: 0.0 is inf far away
22/// Orthographic projection: 0.0 is far clipping plane
24/// Clip space:
25/// This is NDC before the perspective divide, still in homogenous coordinate space.
26/// Dividing a clip space point by its w component yields a point in NDC space.
28/// UV space:
29/// 0.0, 0.0 is the top left
30/// 1.0, 1.0 is the bottom right
33// -----------------
34// TO WORLD --------
35// -----------------
37/// Convert a view space position to world space
38fn position_view_to_world(view_pos: vec3<f32>) -> vec3<f32> {
39 let world_pos = view_bindings::view.world_from_view * vec4(view_pos, 1.0);
40 return world_pos.xyz;
43/// Convert a clip space position to world space
44fn position_clip_to_world(clip_pos: vec4<f32>) -> vec3<f32> {
45 let world_pos = view_bindings::view.world_from_clip * clip_pos;
46 return world_pos.xyz;
49/// Convert a ndc space position to world space
50fn position_ndc_to_world(ndc_pos: vec3<f32>) -> vec3<f32> {
51 let world_pos = view_bindings::view.world_from_clip * vec4(ndc_pos, 1.0);
52 return world_pos.xyz / world_pos.w;
55/// Convert a view space direction to world space
56fn direction_view_to_world(view_dir: vec3<f32>) -> vec3<f32> {
57 let world_dir = view_bindings::view.world_from_view * vec4(view_dir, 0.0);
58 return world_dir.xyz;
61/// Convert a clip space direction to world space
62fn direction_clip_to_world(clip_dir: vec4<f32>) -> vec3<f32> {
63 let world_dir = view_bindings::view.world_from_clip * clip_dir;
64 return world_dir.xyz;
67// -----------------
68// TO VIEW ---------
69// -----------------
71/// Convert a world space position to view space
72fn position_world_to_view(world_pos: vec3<f32>) -> vec3<f32> {
73 let view_pos = view_bindings::view.view_from_world * vec4(world_pos, 1.0);
74 return view_pos.xyz;
77/// Convert a clip space position to view space
78fn position_clip_to_view(clip_pos: vec4<f32>) -> vec3<f32> {
79 let view_pos = view_bindings::view.view_from_clip * clip_pos;
80 return view_pos.xyz;
83/// Convert a ndc space position to view space
84fn position_ndc_to_view(ndc_pos: vec3<f32>) -> vec3<f32> {
85 let view_pos = view_bindings::view.view_from_clip * vec4(ndc_pos, 1.0);
86 return view_pos.xyz / view_pos.w;
89/// Convert a world space direction to view space
90fn direction_world_to_view(world_dir: vec3<f32>) -> vec3<f32> {
91 let view_dir = view_bindings::view.view_from_world * vec4(world_dir, 0.0);
92 return view_dir.xyz;
95/// Convert a clip space direction to view space
96fn direction_clip_to_view(clip_dir: vec4<f32>) -> vec3<f32> {
97 let view_dir = view_bindings::view.view_from_clip * clip_dir;
98 return view_dir.xyz;
101// -----------------
102// TO PREV. VIEW ---
103// -----------------
105fn position_world_to_prev_view(world_pos: vec3<f32>) -> vec3<f32> {
106 let view_pos = prepass_bindings::previous_view_uniforms.view_from_world *
107 vec4(world_pos, 1.0);
108 return view_pos.xyz;
111fn position_world_to_prev_ndc(world_pos: vec3<f32>) -> vec3<f32> {
112 let ndc_pos = prepass_bindings::previous_view_uniforms.clip_from_world *
113 vec4(world_pos, 1.0);
114 return ndc_pos.xyz / ndc_pos.w;
117// -----------------
118// TO CLIP ---------
119// -----------------
121/// Convert a world space position to clip space
122fn position_world_to_clip(world_pos: vec3<f32>) -> vec4<f32> {
123 let clip_pos = view_bindings::view.clip_from_world * vec4(world_pos, 1.0);
124 return clip_pos;
127/// Convert a view space position to clip space
128fn position_view_to_clip(view_pos: vec3<f32>) -> vec4<f32> {
129 let clip_pos = view_bindings::view.clip_from_view * vec4(view_pos, 1.0);
130 return clip_pos;
133/// Convert a world space direction to clip space
134fn direction_world_to_clip(world_dir: vec3<f32>) -> vec4<f32> {
135 let clip_dir = view_bindings::view.clip_from_world * vec4(world_dir, 0.0);
136 return clip_dir;
139/// Convert a view space direction to clip space
140fn direction_view_to_clip(view_dir: vec3<f32>) -> vec4<f32> {
141 let clip_dir = view_bindings::view.clip_from_view * vec4(view_dir, 0.0);
142 return clip_dir;
145// -----------------
146// TO NDC ----------
147// -----------------
149/// Convert a world space position to ndc space
150fn position_world_to_ndc(world_pos: vec3<f32>) -> vec3<f32> {
151 let ndc_pos = view_bindings::view.clip_from_world * vec4(world_pos, 1.0);
152 return ndc_pos.xyz / ndc_pos.w;
155/// Convert a view space position to ndc space
156fn position_view_to_ndc(view_pos: vec3<f32>) -> vec3<f32> {
157 let ndc_pos = view_bindings::view.clip_from_view * vec4(view_pos, 1.0);
158 return ndc_pos.xyz / ndc_pos.w;
161// -----------------
162// DEPTH -----------
163// -----------------
165/// Retrieve the perspective camera near clipping plane
166fn perspective_camera_near() -> f32 {
167 return view_bindings::view.clip_from_view[3][2];
170/// Convert ndc depth to linear view z.
171/// Note: Depth values in front of the camera will be negative as -z is forward
172fn depth_ndc_to_view_z(ndc_depth: f32) -> f32 {
173#ifdef VIEW_PROJECTION_PERSPECTIVE
174 return -perspective_camera_near() / ndc_depth;
175#else ifdef VIEW_PROJECTION_ORTHOGRAPHIC
176 return -(view_bindings::view.clip_from_view[3][2] - ndc_depth) / view_bindings::view.clip_from_view[2][2];
177#else
178 let view_pos = view_bindings::view.view_from_clip * vec4(0.0, 0.0, ndc_depth, 1.0);
179 return view_pos.z / view_pos.w;
180#endif
183/// Convert linear view z to ndc depth.
184/// Note: View z input should be negative for values in front of the camera as -z is forward
185fn view_z_to_depth_ndc(view_z: f32) -> f32 {
186#ifdef VIEW_PROJECTION_PERSPECTIVE
187 return -perspective_camera_near() / view_z;
188#else ifdef VIEW_PROJECTION_ORTHOGRAPHIC
189 return view_bindings::view.clip_from_view[3][2] + view_z * view_bindings::view.clip_from_view[2][2];
190#else
191 let ndc_pos = view_bindings::view.clip_from_view * vec4(0.0, 0.0, view_z, 1.0);
192 return ndc_pos.z / ndc_pos.w;
193#endif
196fn prev_view_z_to_depth_ndc(view_z: f32) -> f32 {
197#ifdef VIEW_PROJECTION_PERSPECTIVE
198 return -perspective_camera_near() / view_z;
199#else ifdef VIEW_PROJECTION_ORTHOGRAPHIC
200 return prepass_bindings::previous_view_uniforms.clip_from_view[3][2] +
201 view_z * prepass_bindings::previous_view_uniforms.clip_from_view[2][2];
202#else
203 let ndc_pos = prepass_bindings::previous_view_uniforms.clip_from_view *
204 vec4(0.0, 0.0, view_z, 1.0);
205 return ndc_pos.z / ndc_pos.w;
206#endif
209// -----------------
210// UV --------------
211// -----------------
213/// Convert ndc space xy coordinate [-1.0 .. 1.0] to uv [0.0 .. 1.0]
214fn ndc_to_uv(ndc: vec2<f32>) -> vec2<f32> {
215 return ndc * vec2(0.5, -0.5) + vec2(0.5);
218/// Convert uv [0.0 .. 1.0] coordinate to ndc space xy [-1.0 .. 1.0]
219fn uv_to_ndc(uv: vec2<f32>) -> vec2<f32> {
220 return uv * vec2(2.0, -2.0) + vec2(-1.0, 1.0);
223/// returns the (0.0, 0.0) .. (1.0, 1.0) position within the viewport for the current render target
224/// [0 .. render target viewport size] eg. [(0.0, 0.0) .. (1280.0, 720.0)] to [(0.0, 0.0) .. (1.0, 1.0)]
225fn frag_coord_to_uv(frag_coord: vec2<f32>) -> vec2<f32> {
226 return (frag_coord - view_bindings::view.viewport.xy) / view_bindings::view.viewport.zw;
229/// Convert frag coord to ndc
230fn frag_coord_to_ndc(frag_coord: vec4<f32>) -> vec3<f32> {
231 return vec3(uv_to_ndc(frag_coord_to_uv(frag_coord.xy)), frag_coord.z);
234/// Convert ndc space xy coordinate [-1.0 .. 1.0] to [0 .. render target
235/// viewport size]
236fn ndc_to_frag_coord(ndc: vec2<f32>) -> vec2<f32> {
237 return ndc_to_uv(ndc) * view_bindings::view.viewport.zw;
Contents