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//! World space:
2//! +y is up
3//!
4//! View space:
5//! -z is forward, +x is right, +y is up
6//! Forward is from the camera position into the scene.
7//! (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
8//! (0.0, 1.0, 0.0) is linear distance of 1.0 above the camera's view relative to the camera's rotation
9//!
10//! NDC (normalized device coordinate):
11//! https://www.w3.org/TR/webgpu/#coordinate-systems
12//! (-1.0, -1.0) in NDC is located at the bottom-left corner of NDC
13//! (1.0, 1.0) in NDC is located at the top-right corner of NDC
14//! Z is depth where:
15//! 1.0 is near clipping plane
16//! Perspective projection: 0.0 is inf far away
17//! Orthographic projection: 0.0 is far clipping plane
18//!
19//! Clip space:
20//! This is NDC before the perspective divide, still in homogenous coordinate space.
21//! Dividing a clip space point by its w component yields a point in NDC space.
22//!
23//! UV space:
24//! 0.0, 0.0 is the top left
25//! 1.0, 1.0 is the bottom right
27import package::render::mesh_view_bindings as view_bindings;
28import package::prepass::bindings as prepass_bindings;
30// -----------------
31// TO WORLD --------
32// -----------------
34/// Convert a view space position to world space
35fn position_view_to_world(view_pos: vec3<f32>) -> vec3<f32> {
36 let world_pos = view_bindings::view.world_from_view * vec4(view_pos, 1.0);
37 return world_pos.xyz;
40/// Convert a clip space position to world space
41fn position_clip_to_world(clip_pos: vec4<f32>) -> vec3<f32> {
42 let world_pos = view_bindings::view.world_from_clip * clip_pos;
43 return world_pos.xyz;
46/// Convert a ndc space position to world space
47fn position_ndc_to_world(ndc_pos: vec3<f32>) -> vec3<f32> {
48 let world_pos = view_bindings::view.world_from_clip * vec4(ndc_pos, 1.0);
49 return world_pos.xyz / world_pos.w;
52/// Convert a view space direction to world space
53fn direction_view_to_world(view_dir: vec3<f32>) -> vec3<f32> {
54 let world_dir = view_bindings::view.world_from_view * vec4(view_dir, 0.0);
55 return world_dir.xyz;
58/// Convert a clip space direction to world space
59fn direction_clip_to_world(clip_dir: vec4<f32>) -> vec3<f32> {
60 let world_dir = view_bindings::view.world_from_clip * clip_dir;
61 return world_dir.xyz;
64// -----------------
65// TO VIEW ---------
66// -----------------
68/// Convert a world space position to view space
69fn position_world_to_view(world_pos: vec3<f32>) -> vec3<f32> {
70 let view_pos = view_bindings::view.view_from_world * vec4(world_pos, 1.0);
71 return view_pos.xyz;
74/// Convert a clip space position to view space
75fn position_clip_to_view(clip_pos: vec4<f32>) -> vec3<f32> {
76 let view_pos = view_bindings::view.view_from_clip * clip_pos;
77 return view_pos.xyz;
80/// Convert a ndc space position to view space
81fn position_ndc_to_view(ndc_pos: vec3<f32>) -> vec3<f32> {
82 let view_pos = view_bindings::view.view_from_clip * vec4(ndc_pos, 1.0);
83 return view_pos.xyz / view_pos.w;
86/// Convert a world space direction to view space
87fn direction_world_to_view(world_dir: vec3<f32>) -> vec3<f32> {
88 let view_dir = view_bindings::view.view_from_world * vec4(world_dir, 0.0);
89 return view_dir.xyz;
92/// Convert a clip space direction to view space
93fn direction_clip_to_view(clip_dir: vec4<f32>) -> vec3<f32> {
94 let view_dir = view_bindings::view.view_from_clip * clip_dir;
95 return view_dir.xyz;
98// -----------------
99// TO PREV. VIEW ---
100// -----------------
102fn position_world_to_prev_view(world_pos: vec3<f32>) -> vec3<f32> {
103 let view_pos = prepass_bindings::previous_view_uniforms.view_from_world *
104 vec4(world_pos, 1.0);
105 return view_pos.xyz;
108fn position_world_to_prev_ndc(world_pos: vec3<f32>) -> vec3<f32> {
109 let ndc_pos = prepass_bindings::previous_view_uniforms.clip_from_world *
110 vec4(world_pos, 1.0);
111 return ndc_pos.xyz / ndc_pos.w;
114// -----------------
115// TO CLIP ---------
116// -----------------
118/// Convert a world space position to clip space
119fn position_world_to_clip(world_pos: vec3<f32>) -> vec4<f32> {
120 let clip_pos = view_bindings::view.clip_from_world * vec4(world_pos, 1.0);
121 return clip_pos;
124/// Convert a view space position to clip space
125fn position_view_to_clip(view_pos: vec3<f32>) -> vec4<f32> {
126 let clip_pos = view_bindings::view.clip_from_view * vec4(view_pos, 1.0);
127 return clip_pos;
130/// Convert a world space direction to clip space
131fn direction_world_to_clip(world_dir: vec3<f32>) -> vec4<f32> {
132 let clip_dir = view_bindings::view.clip_from_world * vec4(world_dir, 0.0);
133 return clip_dir;
136/// Convert a view space direction to clip space
137fn direction_view_to_clip(view_dir: vec3<f32>) -> vec4<f32> {
138 let clip_dir = view_bindings::view.clip_from_view * vec4(view_dir, 0.0);
139 return clip_dir;
142// -----------------
143// TO NDC ----------
144// -----------------
146/// Convert a world space position to ndc space
147fn position_world_to_ndc(world_pos: vec3<f32>) -> vec3<f32> {
148 let ndc_pos = view_bindings::view.clip_from_world * vec4(world_pos, 1.0);
149 return ndc_pos.xyz / ndc_pos.w;
152/// Convert a view space position to ndc space
153fn position_view_to_ndc(view_pos: vec3<f32>) -> vec3<f32> {
154 let ndc_pos = view_bindings::view.clip_from_view * vec4(view_pos, 1.0);
155 return ndc_pos.xyz / ndc_pos.w;
158// -----------------
159// DEPTH -----------
160// -----------------
162/// Retrieve the perspective camera near clipping plane
163fn perspective_camera_near() -> f32 {
164 return view_bindings::view.clip_from_view[3][2];
167/// Convert ndc depth to linear view z.
168/// Note: Depth values in front of the camera will be negative as -z is forward
169fn depth_ndc_to_view_z(ndc_depth: f32) -> f32 {
170@if(VIEW_PROJECTION_PERSPECTIVE)
171 return -perspective_camera_near() / ndc_depth;
172@elif(VIEW_PROJECTION_ORTHOGRAPHIC)
173 return -(view_bindings::view.clip_from_view[3][2] - ndc_depth) / view_bindings::view.clip_from_view[2][2];
174@else {
175 let view_pos = view_bindings::view.view_from_clip * vec4(0.0, 0.0, ndc_depth, 1.0);
176 return view_pos.z / view_pos.w;
180/// Convert linear view z to ndc depth.
181/// Note: View z input should be negative for values in front of the camera as -z is forward
182fn view_z_to_depth_ndc(view_z: f32) -> f32 {
183@if(VIEW_PROJECTION_PERSPECTIVE)
184 return -perspective_camera_near() / view_z;
185@elif(VIEW_PROJECTION_ORTHOGRAPHIC)
186 return view_bindings::view.clip_from_view[3][2] + view_z * view_bindings::view.clip_from_view[2][2];
187@else {
188 let ndc_pos = view_bindings::view.clip_from_view * vec4(0.0, 0.0, view_z, 1.0);
189 return ndc_pos.z / ndc_pos.w;
193fn prev_view_z_to_depth_ndc(view_z: f32) -> f32 {
194@if(VIEW_PROJECTION_PERSPECTIVE)
195 return -perspective_camera_near() / view_z;
196@elif(VIEW_PROJECTION_ORTHOGRAPHIC)
197 return prepass_bindings::previous_view_uniforms.clip_from_view[3][2] +
198 view_z * prepass_bindings::previous_view_uniforms.clip_from_view[2][2];
199@else {
200 let ndc_pos = prepass_bindings::previous_view_uniforms.clip_from_view *
201 vec4(0.0, 0.0, view_z, 1.0);
202 return ndc_pos.z / ndc_pos.w;
206// -----------------
207// UV --------------
208// -----------------
210/// Convert ndc space xy coordinate [-1.0 .. 1.0] to uv [0.0 .. 1.0]
211fn ndc_to_uv(ndc: vec2<f32>) -> vec2<f32> {
212 return ndc * vec2(0.5, -0.5) + vec2(0.5);
215/// Convert uv [0.0 .. 1.0] coordinate to ndc space xy [-1.0 .. 1.0]
216fn uv_to_ndc(uv: vec2<f32>) -> vec2<f32> {
217 return uv * vec2(2.0, -2.0) + vec2(-1.0, 1.0);
220/// returns the (0.0, 0.0) .. (1.0, 1.0) position within the viewport for the current render target
221/// [0 .. render target viewport size] eg. [(0.0, 0.0) .. (1280.0, 720.0)] to [(0.0, 0.0) .. (1.0, 1.0)]
222fn frag_coord_to_uv(frag_coord: vec2<f32>) -> vec2<f32> {
223 return (frag_coord - view_bindings::view.viewport.xy) / view_bindings::view.viewport.zw;
226/// Convert frag coord to ndc
227fn frag_coord_to_ndc(frag_coord: vec4<f32>) -> vec3<f32> {
228 return vec3(uv_to_ndc(frag_coord_to_uv(frag_coord.xy)), frag_coord.z);
231/// Convert ndc space xy coordinate [-1.0 .. 1.0] to [0 .. render target
232/// viewport size]
233fn ndc_to_frag_coord(ndc: vec2<f32>) -> vec2<f32> {
234 return ndc_to_uv(ndc) * view_bindings::view.viewport.zw;
Contents