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
5/// World space:
6/// +y is up
8/// View space:
9/// -z is forward, +x is right, +y is up
10/// Forward is from the camera position into the scene.
11/// (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
12/// (0.0, 1.0, 0.0) is linear distance of 1.0 above the camera's view relative to the camera's rotation
14/// NDC (normalized device coordinate):
15/// https://www.w3.org/TR/webgpu/#coordinate-systems
16/// (-1.0, -1.0) in NDC is located at the bottom-left corner of NDC
17/// (1.0, 1.0) in NDC is located at the top-right corner of NDC
18/// Z is depth where:
19/// 1.0 is near clipping plane
20/// Perspective projection: 0.0 is inf far away
21/// Orthographic projection: 0.0 is far clipping plane
23/// UV space:
24/// 0.0, 0.0 is the top left
25/// 1.0, 1.0 is the bottom right
28// -----------------
29// TO WORLD --------
30// -----------------
32/// Convert a view space position to world space
33fn position_view_to_world(view_pos: vec3<f32>) -> vec3<f32> {
34 let world_pos = view_bindings::view.world_from_view * vec4(view_pos, 1.0);
35 return world_pos.xyz;
38/// Convert a clip space position to world space
39fn position_clip_to_world(clip_pos: vec4<f32>) -> vec3<f32> {
40 let world_pos = view_bindings::view.world_from_clip * clip_pos;
41 return world_pos.xyz;
44/// Convert a ndc space position to world space
45fn position_ndc_to_world(ndc_pos: vec3<f32>) -> vec3<f32> {
46 let world_pos = view_bindings::view.world_from_clip * vec4(ndc_pos, 1.0);
47 return world_pos.xyz / world_pos.w;
50/// Convert a view space direction to world space
51fn direction_view_to_world(view_dir: vec3<f32>) -> vec3<f32> {
52 let world_dir = view_bindings::view.world_from_view * vec4(view_dir, 0.0);
53 return world_dir.xyz;
56/// Convert a clip space direction to world space
57fn direction_clip_to_world(clip_dir: vec4<f32>) -> vec3<f32> {
58 let world_dir = view_bindings::view.world_from_clip * clip_dir;
59 return world_dir.xyz;
62// -----------------
63// TO VIEW ---------
64// -----------------
66/// Convert a world space position to view space
67fn position_world_to_view(world_pos: vec3<f32>) -> vec3<f32> {
68 let view_pos = view_bindings::view.view_from_world * vec4(world_pos, 1.0);
69 return view_pos.xyz;
72/// Convert a clip space position to view space
73fn position_clip_to_view(clip_pos: vec4<f32>) -> vec3<f32> {
74 let view_pos = view_bindings::view.view_from_clip * clip_pos;
75 return view_pos.xyz;
78/// Convert a ndc space position to view space
79fn position_ndc_to_view(ndc_pos: vec3<f32>) -> vec3<f32> {
80 let view_pos = view_bindings::view.view_from_clip * vec4(ndc_pos, 1.0);
81 return view_pos.xyz / view_pos.w;
84/// Convert a world space direction to view space
85fn direction_world_to_view(world_dir: vec3<f32>) -> vec3<f32> {
86 let view_dir = view_bindings::view.view_from_world * vec4(world_dir, 0.0);
87 return view_dir.xyz;
90/// Convert a clip space direction to view space
91fn direction_clip_to_view(clip_dir: vec4<f32>) -> vec3<f32> {
92 let view_dir = view_bindings::view.view_from_clip * clip_dir;
93 return view_dir.xyz;
96// -----------------
97// TO CLIP ---------
98// -----------------
100/// Convert a world space position to clip space
101fn position_world_to_clip(world_pos: vec3<f32>) -> vec4<f32> {
102 let clip_pos = view_bindings::view.clip_from_world * vec4(world_pos, 1.0);
103 return clip_pos;
106/// Convert a view space position to clip space
107fn position_view_to_clip(view_pos: vec3<f32>) -> vec4<f32> {
108 let clip_pos = view_bindings::view.clip_from_view * vec4(view_pos, 1.0);
109 return clip_pos;
112/// Convert a world space direction to clip space
113fn direction_world_to_clip(world_dir: vec3<f32>) -> vec4<f32> {
114 let clip_dir = view_bindings::view.clip_from_world * vec4(world_dir, 0.0);
115 return clip_dir;
118/// Convert a view space direction to clip space
119fn direction_view_to_clip(view_dir: vec3<f32>) -> vec4<f32> {
120 let clip_dir = view_bindings::view.clip_from_view * vec4(view_dir, 0.0);
121 return clip_dir;
124// -----------------
125// TO NDC ----------
126// -----------------
128/// Convert a world space position to ndc space
129fn position_world_to_ndc(world_pos: vec3<f32>) -> vec3<f32> {
130 let ndc_pos = view_bindings::view.clip_from_world * vec4(world_pos, 1.0);
131 return ndc_pos.xyz / ndc_pos.w;
134/// Convert a view space position to ndc space
135fn position_view_to_ndc(view_pos: vec3<f32>) -> vec3<f32> {
136 let ndc_pos = view_bindings::view.clip_from_view * vec4(view_pos, 1.0);
137 return ndc_pos.xyz / ndc_pos.w;
140// -----------------
141// DEPTH -----------
142// -----------------
144/// Retrieve the perspective camera near clipping plane
145fn perspective_camera_near() -> f32 {
146 return view_bindings::view.clip_from_view[3][2];
149/// Convert ndc depth to linear view z.
150/// Note: Depth values in front of the camera will be negative as -z is forward
151fn depth_ndc_to_view_z(ndc_depth: f32) -> f32 {
152#ifdef VIEW_PROJECTION_PERSPECTIVE
153 return -perspective_camera_near() / ndc_depth;
154#else ifdef VIEW_PROJECTION_ORTHOGRAPHIC
155 return -(view_bindings::view.clip_from_view[3][2] - ndc_depth) / view_bindings::view.clip_from_view[2][2];
156#else
157 let view_pos = view_bindings::view.view_from_clip * vec4(0.0, 0.0, ndc_depth, 1.0);
158 return view_pos.z / view_pos.w;
159#endif
162/// Convert linear view z to ndc depth.
163/// Note: View z input should be negative for values in front of the camera as -z is forward
164fn view_z_to_depth_ndc(view_z: f32) -> f32 {
165#ifdef VIEW_PROJECTION_PERSPECTIVE
166 return -perspective_camera_near() / view_z;
167#else ifdef VIEW_PROJECTION_ORTHOGRAPHIC
168 return view_bindings::view.clip_from_view[3][2] + view_z * view_bindings::view.clip_from_view[2][2];
169#else
170 let ndc_pos = view_bindings::view.clip_from_view * vec4(0.0, 0.0, view_z, 1.0);
171 return ndc_pos.z / ndc_pos.w;
172#endif
175// -----------------
176// UV --------------
177// -----------------
179/// Convert ndc space xy coordinate [-1.0 .. 1.0] to uv [0.0 .. 1.0]
180fn ndc_to_uv(ndc: vec2<f32>) -> vec2<f32> {
181 return ndc * vec2(0.5, -0.5) + vec2(0.5);
184/// Convert uv [0.0 .. 1.0] coordinate to ndc space xy [-1.0 .. 1.0]
185fn uv_to_ndc(uv: vec2<f32>) -> vec2<f32> {
186 return uv * vec2(2.0, -2.0) + vec2(-1.0, 1.0);
189/// returns the (0.0, 0.0) .. (1.0, 1.0) position within the viewport for the current render target
190/// [0 .. render target viewport size] eg. [(0.0, 0.0) .. (1280.0, 720.0)] to [(0.0, 0.0) .. (1.0, 1.0)]
191fn frag_coord_to_uv(frag_coord: vec2<f32>) -> vec2<f32> {
192 return (frag_coord - view_bindings::view.viewport.xy) / view_bindings::view.viewport.zw;
195/// Convert frag coord to ndc
196fn frag_coord_to_ndc(frag_coord: vec4<f32>) -> vec3<f32> {
197 return vec3(uv_to_ndc(frag_coord_to_uv(frag_coord.xy)), frag_coord.z);
200/// Convert ndc space xy coordinate [-1.0 .. 1.0] to [0 .. render target
201/// viewport size]
202fn ndc_to_frag_coord(ndc: vec2<f32>) -> vec2<f32> {
203 return ndc_to_uv(ndc) * view_bindings::view.viewport.zw;
Contents