|
| 1 | +#define_import_path bevy_pbr::view_transformations |
| 2 | + |
| 3 | +#import bevy_pbr::mesh_view_bindings as view_bindings |
| 4 | + |
| 5 | +/// World space: |
| 6 | +/// +y is up |
| 7 | + |
| 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 |
| 13 | + |
| 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 |
| 22 | + |
| 23 | +/// UV space: |
| 24 | +/// 0.0, 0.0 is the top left |
| 25 | +/// 1.0, 1.0 is the bottom right |
| 26 | + |
| 27 | + |
| 28 | +// ----------------- |
| 29 | +// TO WORLD -------- |
| 30 | +// ----------------- |
| 31 | + |
| 32 | +/// Convert a view space position to world space |
| 33 | +fn position_view_to_world(view_pos: vec3<f32>) -> vec3<f32> { |
| 34 | + let world_pos = view_bindings::view.view * vec4(view_pos, 1.0); |
| 35 | + return world_pos.xyz; |
| 36 | +} |
| 37 | + |
| 38 | +/// Convert a clip space position to world space |
| 39 | +fn position_clip_to_world(clip_pos: vec4<f32>) -> vec3<f32> { |
| 40 | + let world_pos = view_bindings::view.inverse_view_proj * clip_pos; |
| 41 | + return world_pos.xyz; |
| 42 | +} |
| 43 | + |
| 44 | +/// Convert a ndc space position to world space |
| 45 | +fn position_ndc_to_world(ndc_pos: vec3<f32>) -> vec3<f32> { |
| 46 | + let world_pos = view_bindings::view.inverse_view_proj * vec4(ndc_pos, 1.0); |
| 47 | + return world_pos.xyz / world_pos.w; |
| 48 | +} |
| 49 | + |
| 50 | +/// Convert a view space direction to world space |
| 51 | +fn direction_view_to_world(view_dir: vec3<f32>) -> vec3<f32> { |
| 52 | + let world_dir = view_bindings::view.view * vec4(view_dir, 0.0); |
| 53 | + return world_dir.xyz; |
| 54 | +} |
| 55 | + |
| 56 | +/// Convert a clip space direction to world space |
| 57 | +fn direction_clip_to_world(clip_dir: vec4<f32>) -> vec3<f32> { |
| 58 | + let world_dir = view_bindings::view.inverse_view_proj * clip_dir; |
| 59 | + return world_dir.xyz; |
| 60 | +} |
| 61 | + |
| 62 | +// ----------------- |
| 63 | +// TO VIEW --------- |
| 64 | +// ----------------- |
| 65 | + |
| 66 | +/// Convert a world space position to view space |
| 67 | +fn position_world_to_view(world_pos: vec3<f32>) -> vec3<f32> { |
| 68 | + let view_pos = view_bindings::view.inverse_view * vec4(world_pos, 1.0); |
| 69 | + return view_pos.xyz; |
| 70 | +} |
| 71 | + |
| 72 | +/// Convert a clip space position to view space |
| 73 | +fn position_clip_to_view(clip_pos: vec4<f32>) -> vec3<f32> { |
| 74 | + let view_pos = view_bindings::view.inverse_projection * clip_pos; |
| 75 | + return view_pos.xyz; |
| 76 | +} |
| 77 | + |
| 78 | +/// Convert a ndc space position to view space |
| 79 | +fn position_ndc_to_view(ndc_pos: vec3<f32>) -> vec3<f32> { |
| 80 | + let view_pos = view_bindings::view.inverse_projection * vec4(ndc_pos, 1.0); |
| 81 | + return view_pos.xyz / view_pos.w; |
| 82 | +} |
| 83 | + |
| 84 | +/// Convert a world space direction to view space |
| 85 | +fn direction_world_to_view(world_dir: vec3<f32>) -> vec3<f32> { |
| 86 | + let view_dir = view_bindings::view.inverse_view * vec4(world_dir, 0.0); |
| 87 | + return view_dir.xyz; |
| 88 | +} |
| 89 | + |
| 90 | +/// Convert a clip space direction to view space |
| 91 | +fn direction_clip_to_view(clip_dir: vec4<f32>) -> vec3<f32> { |
| 92 | + let view_dir = view_bindings::view.inverse_projection * clip_dir; |
| 93 | + return view_dir.xyz; |
| 94 | +} |
| 95 | + |
| 96 | +// ----------------- |
| 97 | +// TO CLIP --------- |
| 98 | +// ----------------- |
| 99 | + |
| 100 | +/// Convert a world space position to clip space |
| 101 | +fn position_world_to_clip(world_pos: vec3<f32>) -> vec4<f32> { |
| 102 | + let clip_pos = view_bindings::view.view_proj * vec4(world_pos, 1.0); |
| 103 | + return clip_pos; |
| 104 | +} |
| 105 | + |
| 106 | +/// Convert a view space position to clip space |
| 107 | +fn position_view_to_clip(view_pos: vec3<f32>) -> vec4<f32> { |
| 108 | + let clip_pos = view_bindings::view.projection * vec4(view_pos, 1.0); |
| 109 | + return clip_pos; |
| 110 | +} |
| 111 | + |
| 112 | +/// Convert a world space direction to clip space |
| 113 | +fn direction_world_to_clip(world_dir: vec3<f32>) -> vec4<f32> { |
| 114 | + let clip_dir = view_bindings::view.view_proj * vec4(world_dir, 0.0); |
| 115 | + return clip_dir; |
| 116 | +} |
| 117 | + |
| 118 | +/// Convert a view space direction to clip space |
| 119 | +fn direction_view_to_clip(view_dir: vec3<f32>) -> vec4<f32> { |
| 120 | + let clip_dir = view_bindings::view.projection * vec4(view_dir, 0.0); |
| 121 | + return clip_dir; |
| 122 | +} |
| 123 | + |
| 124 | +// ----------------- |
| 125 | +// TO NDC ---------- |
| 126 | +// ----------------- |
| 127 | + |
| 128 | +/// Convert a world space position to ndc space |
| 129 | +fn position_world_to_ndc(world_pos: vec3<f32>) -> vec3<f32> { |
| 130 | + let ndc_pos = view_bindings::view.view_proj * vec4(world_pos, 1.0); |
| 131 | + return ndc_pos.xyz / ndc_pos.w; |
| 132 | +} |
| 133 | + |
| 134 | +/// Convert a view space position to ndc space |
| 135 | +fn position_view_to_ndc(view_pos: vec3<f32>) -> vec3<f32> { |
| 136 | + let ndc_pos = view_bindings::view.projection * vec4(view_pos, 1.0); |
| 137 | + return ndc_pos.xyz / ndc_pos.w; |
| 138 | +} |
| 139 | + |
| 140 | +// ----------------- |
| 141 | +// DEPTH ----------- |
| 142 | +// ----------------- |
| 143 | + |
| 144 | +/// Retrieve the perspective camera near clipping plane |
| 145 | +fn perspective_camera_near() -> f32 { |
| 146 | + return view_bindings::view.projection[3][2]; |
| 147 | +} |
| 148 | + |
| 149 | +/// Convert ndc depth to linear view z. |
| 150 | +/// Note: Depth values in front of the camera will be negative as -z is forward |
| 151 | +fn 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.projection[3][2] - ndc_depth) / view_bindings::view.projection[2][2]; |
| 156 | +#else |
| 157 | + let view_pos = view_bindings::view.inverse_projection * vec4(0.0, 0.0, ndc_depth, 1.0); |
| 158 | + return view_pos.z / view_pos.w; |
| 159 | +#endif |
| 160 | +} |
| 161 | + |
| 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 |
| 164 | +fn 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.projection[3][2] + view_z * view_bindings::view.projection[2][2]; |
| 169 | +#else |
| 170 | + let ndc_pos = view_bindings::view.projection * vec4(0.0, 0.0, view_z, 1.0); |
| 171 | + return ndc_pos.z / ndc_pos.w; |
| 172 | +#endif |
| 173 | +} |
| 174 | + |
| 175 | +// ----------------- |
| 176 | +// UV -------------- |
| 177 | +// ----------------- |
| 178 | + |
| 179 | +/// Convert ndc space xy coordinate [-1.0 .. 1.0] to uv [0.0 .. 1.0] |
| 180 | +fn ndc_to_uv(ndc: vec2<f32>) -> vec2<f32> { |
| 181 | + return ndc * vec2(0.5, -0.5) + vec2(0.5); |
| 182 | +} |
| 183 | + |
| 184 | +/// Convert uv [0.0 .. 1.0] coordinate to ndc space xy [-1.0 .. 1.0] |
| 185 | +fn uv_to_ndc(uv: vec2<f32>) -> vec2<f32> { |
| 186 | + return uv * vec2(2.0, -2.0) + vec2(-1.0, 1.0); |
| 187 | +} |
| 188 | + |
| 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)] |
| 191 | +fn frag_coord_to_uv(frag_coord: vec2<f32>) -> vec2<f32> { |
| 192 | + return (frag_coord - view_bindings::view.viewport.xy) / view_bindings::view.viewport.zw; |
| 193 | +} |
| 194 | + |
| 195 | +/// Convert frag coord to ndc |
| 196 | +fn 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); |
| 198 | +} |
0 commit comments