|
2 | 2 |
|
3 | 3 | #import bevy_pbr::mesh_view_bindings as view_bindings |
4 | 4 | #import bevy_pbr::prepass_bindings |
5 | | -#import bevy_render::view |
6 | 5 |
|
7 | 6 | /// World space: |
8 | 7 | /// +y is up |
|
36 | 35 | // ----------------- |
37 | 36 |
|
38 | 37 | /// Convert a view space position to world space |
39 | | -/// DEPRECATED: use bevy_render::view::position_view_to_world instead |
40 | 38 | fn position_view_to_world(view_pos: vec3<f32>) -> vec3<f32> { |
41 | | - return view::position_view_to_world(view_pos, view_bindings::view.world_from_view); |
| 39 | + let world_pos = view_bindings::view.world_from_view * vec4(view_pos, 1.0); |
| 40 | + return world_pos.xyz; |
42 | 41 | } |
43 | 42 |
|
44 | 43 | /// Convert a clip space position to world space |
45 | | -/// DEPRECATED: use bevy_render::view::position_clip_to_world instead |
46 | 44 | fn position_clip_to_world(clip_pos: vec4<f32>) -> vec3<f32> { |
47 | | - return view::position_clip_to_world(clip_pos, view_bindings::view.world_from_clip); |
| 45 | + let world_pos = view_bindings::view.world_from_clip * clip_pos; |
| 46 | + return world_pos.xyz; |
48 | 47 | } |
49 | 48 |
|
50 | 49 | /// Convert a ndc space position to world space |
51 | | -/// DEPRECATED: use bevy_render::view::position_ndc_to_world instead |
52 | 50 | fn position_ndc_to_world(ndc_pos: vec3<f32>) -> vec3<f32> { |
53 | | - return view::position_ndc_to_world(ndc_pos, view_bindings::view.world_from_clip); |
| 51 | + let world_pos = view_bindings::view.world_from_clip * vec4(ndc_pos, 1.0); |
| 52 | + return world_pos.xyz / world_pos.w; |
54 | 53 | } |
55 | 54 |
|
56 | 55 | /// Convert a view space direction to world space |
57 | | -/// DEPRECATED: use bevy_render::view::direction_view_to_world instead |
58 | 56 | fn direction_view_to_world(view_dir: vec3<f32>) -> vec3<f32> { |
59 | | - return view::direction_view_to_world(view_dir, view_bindings::view.world_from_view); |
| 57 | + let world_dir = view_bindings::view.world_from_view * vec4(view_dir, 0.0); |
| 58 | + return world_dir.xyz; |
60 | 59 | } |
61 | 60 |
|
62 | 61 | /// Convert a clip space direction to world space |
63 | | -/// DEPRECATED: use bevy_render::view::direction_clip_to_world instead |
64 | 62 | fn direction_clip_to_world(clip_dir: vec4<f32>) -> vec3<f32> { |
65 | | - return view::direction_clip_to_world(clip_dir, view_bindings::view.world_from_clip); |
| 63 | + let world_dir = view_bindings::view.world_from_clip * clip_dir; |
| 64 | + return world_dir.xyz; |
66 | 65 | } |
67 | 66 |
|
68 | 67 | // ----------------- |
69 | 68 | // TO VIEW --------- |
70 | 69 | // ----------------- |
71 | 70 |
|
72 | 71 | /// Convert a world space position to view space |
73 | | -/// DEPRECATED: use bevy_render::view::position_world_to_view instead |
74 | 72 | fn position_world_to_view(world_pos: vec3<f32>) -> vec3<f32> { |
75 | | - return view::position_world_to_view(world_pos, view_bindings::view.view_from_world); |
| 73 | + let view_pos = view_bindings::view.view_from_world * vec4(world_pos, 1.0); |
| 74 | + return view_pos.xyz; |
76 | 75 | } |
77 | 76 |
|
78 | 77 | /// Convert a clip space position to view space |
79 | | -/// DEPRECATED: use bevy_render::view::position_clip_to_view instead |
80 | 78 | fn position_clip_to_view(clip_pos: vec4<f32>) -> vec3<f32> { |
81 | | - return view::position_clip_to_view(clip_pos, view_bindings::view.view_from_clip); |
| 79 | + let view_pos = view_bindings::view.view_from_clip * clip_pos; |
| 80 | + return view_pos.xyz; |
82 | 81 | } |
83 | 82 |
|
84 | 83 | /// Convert a ndc space position to view space |
85 | | -/// DEPRECATED: use bevy_render::view::position_ndc_to_view instead |
86 | 84 | fn position_ndc_to_view(ndc_pos: vec3<f32>) -> vec3<f32> { |
87 | | - return view::position_ndc_to_view(ndc_pos, view_bindings::view.view_from_clip); |
| 85 | + let view_pos = view_bindings::view.view_from_clip * vec4(ndc_pos, 1.0); |
| 86 | + return view_pos.xyz / view_pos.w; |
88 | 87 | } |
89 | 88 |
|
90 | 89 | /// Convert a world space direction to view space |
91 | | -/// DEPRECATED: use bevy_render::view::direction_world_to_view instead |
92 | 90 | fn direction_world_to_view(world_dir: vec3<f32>) -> vec3<f32> { |
93 | | - return view::direction_world_to_view(world_dir, view_bindings::view.view_from_world); |
| 91 | + let view_dir = view_bindings::view.view_from_world * vec4(world_dir, 0.0); |
| 92 | + return view_dir.xyz; |
94 | 93 | } |
95 | 94 |
|
96 | 95 | /// Convert a clip space direction to view space |
97 | | -/// DEPRECATED: use bevy_render::view::direction_clip_to_view instead |
98 | 96 | fn direction_clip_to_view(clip_dir: vec4<f32>) -> vec3<f32> { |
99 | | - return view::direction_clip_to_view(clip_dir, view_bindings::view.view_from_clip); |
| 97 | + let view_dir = view_bindings::view.view_from_clip * clip_dir; |
| 98 | + return view_dir.xyz; |
100 | 99 | } |
101 | 100 |
|
102 | 101 | // ----------------- |
103 | 102 | // TO PREV. VIEW --- |
104 | 103 | // ----------------- |
105 | 104 |
|
106 | | -/// DEPRECATED: use bevy_render::view::position_world_to_view instead |
107 | 105 | fn position_world_to_prev_view(world_pos: vec3<f32>) -> vec3<f32> { |
108 | | - return view::position_world_to_view(world_pos, prepass_bindings::previous_view_uniforms.view_from_world); |
| 106 | + let view_pos = prepass_bindings::previous_view_uniforms.view_from_world * |
| 107 | + vec4(world_pos, 1.0); |
| 108 | + return view_pos.xyz; |
109 | 109 | } |
110 | 110 |
|
111 | | -/// DEPRECATED: use bevy_render::view::position_world_to_ndc instead |
112 | 111 | fn position_world_to_prev_ndc(world_pos: vec3<f32>) -> vec3<f32> { |
113 | | - return view::position_world_to_ndc(world_pos, prepass_bindings::previous_view_uniforms.clip_from_world); |
| 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; |
114 | 115 | } |
115 | 116 |
|
116 | 117 | // ----------------- |
117 | 118 | // TO CLIP --------- |
118 | 119 | // ----------------- |
119 | 120 |
|
120 | 121 | /// Convert a world space position to clip space |
121 | | -/// DEPRECATED: use bevy_render::view::position_world_to_clip instead |
122 | 122 | fn position_world_to_clip(world_pos: vec3<f32>) -> vec4<f32> { |
123 | | - return view::position_world_to_clip(world_pos, view_bindings::view.clip_from_world); |
| 123 | + let clip_pos = view_bindings::view.clip_from_world * vec4(world_pos, 1.0); |
| 124 | + return clip_pos; |
124 | 125 | } |
125 | 126 |
|
126 | 127 | /// Convert a view space position to clip space |
127 | | -/// DEPRECATED: use bevy_render::view::position_view_to_clip instead |
128 | 128 | fn position_view_to_clip(view_pos: vec3<f32>) -> vec4<f32> { |
129 | | - return view::position_view_to_clip(view_pos, view_bindings::view.clip_from_view); |
| 129 | + let clip_pos = view_bindings::view.clip_from_view * vec4(view_pos, 1.0); |
| 130 | + return clip_pos; |
130 | 131 | } |
131 | 132 |
|
132 | 133 | /// Convert a world space direction to clip space |
133 | | -/// DEPRECATED: use bevy_render::view::direction_world_to_clip instead |
134 | 134 | fn direction_world_to_clip(world_dir: vec3<f32>) -> vec4<f32> { |
135 | | - return view::direction_world_to_clip(world_dir, view_bindings::view.clip_from_world); |
| 135 | + let clip_dir = view_bindings::view.clip_from_world * vec4(world_dir, 0.0); |
| 136 | + return clip_dir; |
136 | 137 | } |
137 | 138 |
|
138 | 139 | /// Convert a view space direction to clip space |
139 | | -/// DEPRECATED: use bevy_render::view::direction_view_to_clip instead |
140 | 140 | fn direction_view_to_clip(view_dir: vec3<f32>) -> vec4<f32> { |
141 | | - return view::direction_view_to_clip(view_dir, view_bindings::view.clip_from_view); |
| 141 | + let clip_dir = view_bindings::view.clip_from_view * vec4(view_dir, 0.0); |
| 142 | + return clip_dir; |
142 | 143 | } |
143 | 144 |
|
144 | 145 | // ----------------- |
145 | 146 | // TO NDC ---------- |
146 | 147 | // ----------------- |
147 | 148 |
|
148 | 149 | /// Convert a world space position to ndc space |
149 | | -/// DEPRECATED: use bevy_render::view::position_world_to_ndc instead |
150 | 150 | fn position_world_to_ndc(world_pos: vec3<f32>) -> vec3<f32> { |
151 | | - return view::position_world_to_ndc(world_pos, view_bindings::view.clip_from_world); |
| 151 | + let ndc_pos = view_bindings::view.clip_from_world * vec4(world_pos, 1.0); |
| 152 | + return ndc_pos.xyz / ndc_pos.w; |
152 | 153 | } |
153 | 154 |
|
154 | 155 | /// Convert a view space position to ndc space |
155 | | -/// DEPRECATED: use bevy_render::view::position_view_to_ndc instead |
156 | 156 | fn position_view_to_ndc(view_pos: vec3<f32>) -> vec3<f32> { |
157 | | - return view::position_view_to_ndc(view_pos, view_bindings::view.clip_from_view); |
| 157 | + let ndc_pos = view_bindings::view.clip_from_view * vec4(view_pos, 1.0); |
| 158 | + return ndc_pos.xyz / ndc_pos.w; |
158 | 159 | } |
159 | 160 |
|
160 | 161 | // ----------------- |
161 | 162 | // DEPTH ----------- |
162 | 163 | // ----------------- |
163 | 164 |
|
164 | 165 | /// Retrieve the perspective camera near clipping plane |
165 | | -/// DEPRECATED: use bevy_render::view::perspective_camera_near instead |
166 | 166 | fn perspective_camera_near() -> f32 { |
167 | | - return view::perspective_camera_near(view_bindings::view.clip_from_view); |
| 167 | + return view_bindings::view.clip_from_view[3][2]; |
168 | 168 | } |
169 | 169 |
|
170 | 170 | /// Convert ndc depth to linear view z. |
171 | 171 | /// Note: Depth values in front of the camera will be negative as -z is forward |
172 | | -/// DEPRECATED: use bevy_render::view::depth_ndc_to_view_z instead |
173 | 172 | fn depth_ndc_to_view_z(ndc_depth: f32) -> f32 { |
174 | | - return view::depth_ndc_to_view_z(ndc_depth, view_bindings::view.clip_from_view, view_bindings::view.view_from_clip); |
| 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 |
175 | 181 | } |
176 | 182 |
|
177 | 183 | /// Convert linear view z to ndc depth. |
178 | 184 | /// Note: View z input should be negative for values in front of the camera as -z is forward |
179 | | -/// DEPRECATED: use bevy_render::view::view_z_to_depth_ndc instead |
180 | 185 | fn view_z_to_depth_ndc(view_z: f32) -> f32 { |
181 | | - return view::view_z_to_depth_ndc(view_z, view_bindings::view.clip_from_view); |
| 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 |
182 | 194 | } |
183 | 195 |
|
184 | | -/// DEPRECATED: use bevy_render::view::prev_view_z_to_depth_ndc instead |
185 | 196 | fn prev_view_z_to_depth_ndc(view_z: f32) -> f32 { |
186 | | - return view::view_z_to_depth_ndc(view_z, prepass_bindings::previous_view_uniforms.clip_from_view); |
| 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 |
187 | 207 | } |
188 | 208 |
|
189 | 209 | // ----------------- |
190 | 210 | // UV -------------- |
191 | 211 | // ----------------- |
192 | 212 |
|
193 | 213 | /// Convert ndc space xy coordinate [-1.0 .. 1.0] to uv [0.0 .. 1.0] |
194 | | -/// DEPRECATED: use bevy_render::view::ndc_to_uv instead |
195 | 214 | fn ndc_to_uv(ndc: vec2<f32>) -> vec2<f32> { |
196 | | - return view::ndc_to_uv(ndc); |
| 215 | + return ndc * vec2(0.5, -0.5) + vec2(0.5); |
197 | 216 | } |
198 | 217 |
|
199 | 218 | /// Convert uv [0.0 .. 1.0] coordinate to ndc space xy [-1.0 .. 1.0] |
200 | | -/// DEPRECATED: use bevy_render::view::uv_to_ndc instead |
201 | 219 | fn uv_to_ndc(uv: vec2<f32>) -> vec2<f32> { |
202 | | - return view::uv_to_ndc(uv); |
| 220 | + return uv * vec2(2.0, -2.0) + vec2(-1.0, 1.0); |
203 | 221 | } |
204 | 222 |
|
205 | 223 | /// returns the (0.0, 0.0) .. (1.0, 1.0) position within the viewport for the current render target |
206 | 224 | /// [0 .. render target viewport size] eg. [(0.0, 0.0) .. (1280.0, 720.0)] to [(0.0, 0.0) .. (1.0, 1.0)] |
207 | | -/// DEPRECATED: use bevy_render::view::frag_coord_to_uv instead |
208 | 225 | fn frag_coord_to_uv(frag_coord: vec2<f32>) -> vec2<f32> { |
209 | | - return view::frag_coord_to_uv(frag_coord, view_bindings::view.viewport); |
| 226 | + return (frag_coord - view_bindings::view.viewport.xy) / view_bindings::view.viewport.zw; |
210 | 227 | } |
211 | 228 |
|
212 | 229 | /// Convert frag coord to ndc |
213 | | -/// DEPRECATED: use bevy_render::view::frag_coord_to_ndc instead |
214 | 230 | fn frag_coord_to_ndc(frag_coord: vec4<f32>) -> vec3<f32> { |
215 | | - return view::frag_coord_to_ndc(frag_coord, view_bindings::view.viewport); |
| 231 | + return vec3(uv_to_ndc(frag_coord_to_uv(frag_coord.xy)), frag_coord.z); |
216 | 232 | } |
217 | 233 |
|
218 | 234 | /// Convert ndc space xy coordinate [-1.0 .. 1.0] to [0 .. render target |
219 | 235 | /// viewport size] |
220 | | -/// DEPRECATED: use bevy_render::view::ndc_to_frag_coord instead |
221 | 236 | fn ndc_to_frag_coord(ndc: vec2<f32>) -> vec2<f32> { |
222 | | - return view::ndc_to_frag_coord(ndc, view_bindings::view.viewport); |
| 237 | + return ndc_to_uv(ndc) * view_bindings::view.viewport.zw; |
223 | 238 | } |
0 commit comments