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_render::view
3struct ColorGrading {
4 balance: mat3x3<f32>,
5 saturation: vec3<f32>,
6 contrast: vec3<f32>,
7 gamma: vec3<f32>,
8 gain: vec3<f32>,
9 lift: vec3<f32>,
10 midtone_range: vec2<f32>,
11 exposure: f32,
12 hue: f32,
13 post_saturation: f32,
16struct View {
17 clip_from_world: mat4x4<f32>,
18 unjittered_clip_from_world: mat4x4<f32>,
19 world_from_clip: mat4x4<f32>,
20 world_from_view: mat4x4<f32>,
21 view_from_world: mat4x4<f32>,
22 // Typically a column-major right-handed projection matrix, one of either:
23 //
24 // Perspective (infinite reverse z)
25 // ```
26 // f = 1 / tan(fov_y_radians / 2)
27 //
28 // ⎡ f / aspect 0 0 0 ⎤
29 // ⎢ 0 f 0 0 ⎥
30 // ⎢ 0 0 0 near ⎥
31 // ⎣ 0 0 -1 0 ⎦
32 // ```
33 //
34 // Orthographic
35 // ```
36 // w = right - left
37 // h = top - bottom
38 // d = far - near
39 // cw = -right - left
40 // ch = -top - bottom
41 //
42 // ⎡ 2 / w 0 0 cw / w ⎤
43 // ⎢ 0 2 / h 0 ch / h ⎥
44 // ⎢ 0 0 1 / d far / d ⎥
45 // ⎣ 0 0 0 1 ⎦
46 // ```
47 //
48 // `clip_from_view[3][3] == 1.0` is the standard way to check if a projection is orthographic
49 //
50 // Wgsl matrices are column major, so for example getting the near plane of a perspective projection is `clip_from_view[3][2]`
51 //
52 // Custom projections are also possible however.
53 clip_from_view: mat4x4<f32>,
54 view_from_clip: mat4x4<f32>,
55 world_position: vec3<f32>,
56 exposure: f32,
57 // viewport(x_origin, y_origin, width, height)
58 viewport: vec4<f32>,
59 main_pass_viewport: vec4<f32>,
60 // 6 world-space half spaces (normal: vec3, distance: f32) ordered left, right, top, bottom, near, far.
61 // The normal vectors point towards the interior of the frustum.
62 // A half space contains `p` if `normal.dot(p) + distance > 0.`
63 frustum: array<vec4<f32>, 6>,
64 // The world-space position of the camera used to resolve visibility ranges.
65 //
66 // This is the position of the camera itself, unless this view isn't
67 // associated with a camera, in which case it's the position of the primary
68 // camera.
69 lod_view_world_position: vec3<f32>,
70 color_grading: ColorGrading,
71 mip_bias: f32,
72 frame_count: u32,
73};
75/// World space:
76/// +y is up
78/// View space:
79/// -z is forward, +x is right, +y is up
80/// Forward is from the camera position into the scene.
81/// (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
82/// (0.0, 1.0, 0.0) is linear distance of 1.0 above the camera's view relative to the camera's rotation
84/// NDC (normalized device coordinate):
85/// https://www.w3.org/TR/webgpu/#coordinate-systems
86/// (-1.0, -1.0) in NDC is located at the bottom-left corner of NDC
87/// (1.0, 1.0) in NDC is located at the top-right corner of NDC
88/// Z is depth where:
89/// 1.0 is near clipping plane
90/// Perspective projection: 0.0 is inf far away
91/// Orthographic projection: 0.0 is far clipping plane
93/// Clip space:
94/// This is NDC before the perspective divide, still in homogenous coordinate space.
95/// Dividing a clip space point by its w component yields a point in NDC space.
97/// UV space:
98/// 0.0, 0.0 is the top left
99/// 1.0, 1.0 is the bottom right
102// -----------------
103// TO WORLD --------
104// -----------------
106/// Convert a view space position to world space
107fn position_view_to_world(view_pos: vec3<f32>, world_from_view: mat4x4<f32>) -> vec3<f32> {
108 let world_pos = world_from_view * vec4(view_pos, 1.0);
109 return world_pos.xyz;
112/// Convert a clip space position to world space
113fn position_clip_to_world(clip_pos: vec4<f32>, world_from_clip: mat4x4<f32>) -> vec3<f32> {
114 let world_pos = world_from_clip * clip_pos;
115 return world_pos.xyz;
118/// Convert a ndc space position to world space
119fn position_ndc_to_world(ndc_pos: vec3<f32>, world_from_clip: mat4x4<f32>) -> vec3<f32> {
120 let world_pos = world_from_clip * vec4(ndc_pos, 1.0);
121 return world_pos.xyz / world_pos.w;
124/// Convert a view space direction to world space
125fn direction_view_to_world(view_dir: vec3<f32>, world_from_view: mat4x4<f32>) -> vec3<f32> {
126 let world_dir = world_from_view * vec4(view_dir, 0.0);
127 return world_dir.xyz;
130/// Convert a clip space direction to world space
131fn direction_clip_to_world(clip_dir: vec4<f32>, world_from_clip: mat4x4<f32>) -> vec3<f32> {
132 let world_dir = world_from_clip * clip_dir;
133 return world_dir.xyz;
136// -----------------
137// TO VIEW ---------
138// -----------------
140/// Convert a world space position to view space
141fn position_world_to_view(world_pos: vec3<f32>, view_from_world: mat4x4<f32>) -> vec3<f32> {
142 let view_pos = view_from_world * vec4(world_pos, 1.0);
143 return view_pos.xyz;
146/// Convert a clip space position to view space
147fn position_clip_to_view(clip_pos: vec4<f32>, view_from_clip: mat4x4<f32>) -> vec3<f32> {
148 let view_pos = view_from_clip * clip_pos;
149 return view_pos.xyz;
152/// Convert a ndc space position to view space
153fn position_ndc_to_view(ndc_pos: vec3<f32>, view_from_clip: mat4x4<f32>) -> vec3<f32> {
154 let view_pos = view_from_clip * vec4(ndc_pos, 1.0);
155 return view_pos.xyz / view_pos.w;
158/// Convert a world space direction to view space
159fn direction_world_to_view(world_dir: vec3<f32>, view_from_world: mat4x4<f32>) -> vec3<f32> {
160 let view_dir = view_from_world * vec4(world_dir, 0.0);
161 return view_dir.xyz;
164/// Convert a clip space direction to view space
165fn direction_clip_to_view(clip_dir: vec4<f32>, view_from_clip: mat4x4<f32>) -> vec3<f32> {
166 let view_dir = view_from_clip * clip_dir;
167 return view_dir.xyz;
170// -----------------
171// TO CLIP ---------
172// -----------------
174/// Convert a world space position to clip space
175fn position_world_to_clip(world_pos: vec3<f32>, clip_from_world: mat4x4<f32>) -> vec4<f32> {
176 let clip_pos = clip_from_world * vec4(world_pos, 1.0);
177 return clip_pos;
180/// Convert a view space position to clip space
181fn position_view_to_clip(view_pos: vec3<f32>, clip_from_view: mat4x4<f32>) -> vec4<f32> {
182 let clip_pos = clip_from_view * vec4(view_pos, 1.0);
183 return clip_pos;
186/// Convert a world space direction to clip space
187fn direction_world_to_clip(world_dir: vec3<f32>, clip_from_world: mat4x4<f32>) -> vec4<f32> {
188 let clip_dir = clip_from_world * vec4(world_dir, 0.0);
189 return clip_dir;
192/// Convert a view space direction to clip space
193fn direction_view_to_clip(view_dir: vec3<f32>, clip_from_view: mat4x4<f32>) -> vec4<f32> {
194 let clip_dir = clip_from_view * vec4(view_dir, 0.0);
195 return clip_dir;
198// -----------------
199// TO NDC ----------
200// -----------------
202/// Convert a world space position to ndc space
203fn position_world_to_ndc(world_pos: vec3<f32>, clip_from_world: mat4x4<f32>) -> vec3<f32> {
204 let ndc_pos = clip_from_world * vec4(world_pos, 1.0);
205 return ndc_pos.xyz / ndc_pos.w;
208/// Convert a view space position to ndc space
209fn position_view_to_ndc(view_pos: vec3<f32>, clip_from_view: mat4x4<f32>) -> vec3<f32> {
210 let ndc_pos = clip_from_view * vec4(view_pos, 1.0);
211 return ndc_pos.xyz / ndc_pos.w;
214// -----------------
215// DEPTH -----------
216// -----------------
218/// Retrieve the perspective camera near clipping plane
219fn perspective_camera_near(clip_from_view: mat4x4<f32>) -> f32 {
220 return clip_from_view[3][2];
223/// Convert ndc depth to linear view z.
224/// Note: Depth values in front of the camera will be negative as -z is forward
225fn depth_ndc_to_view_z(ndc_depth: f32, clip_from_view: mat4x4<f32>, view_from_clip: mat4x4<f32>) -> f32 {
226#ifdef VIEW_PROJECTION_PERSPECTIVE
227 return -perspective_camera_near(clip_from_view) / ndc_depth;
228#else ifdef VIEW_PROJECTION_ORTHOGRAPHIC
229 return -(clip_from_view[3][2] - ndc_depth) / clip_from_view[2][2];
230#else
231 let view_pos = view_from_clip * vec4(0.0, 0.0, ndc_depth, 1.0);
232 return view_pos.z / view_pos.w;
233#endif
236/// Convert linear view z to ndc depth.
237/// Note: View z input should be negative for values in front of the camera as -z is forward
238fn view_z_to_depth_ndc(view_z: f32, clip_from_view: mat4x4<f32>) -> f32 {
239#ifdef VIEW_PROJECTION_PERSPECTIVE
240 return -perspective_camera_near(clip_from_view) / view_z;
241#else ifdef VIEW_PROJECTION_ORTHOGRAPHIC
242 return clip_from_view[3][2] + view_z * clip_from_view[2][2];
243#else
244 let ndc_pos = clip_from_view * vec4(0.0, 0.0, view_z, 1.0);
245 return ndc_pos.z / ndc_pos.w;
246#endif
249// -----------------
250// UV --------------
251// -----------------
253/// Convert ndc space xy coordinate [-1.0 .. 1.0] to uv [0.0 .. 1.0]
254fn ndc_to_uv(ndc: vec2<f32>) -> vec2<f32> {
255 return ndc * vec2(0.5, -0.5) + vec2(0.5);
258/// Convert uv [0.0 .. 1.0] coordinate to ndc space xy [-1.0 .. 1.0]
259fn uv_to_ndc(uv: vec2<f32>) -> vec2<f32> {
260 return uv * vec2(2.0, -2.0) + vec2(-1.0, 1.0);
263/// returns the (0.0, 0.0) .. (1.0, 1.0) position within the viewport for the current render target
264/// [0 .. render target viewport size] eg. [(0.0, 0.0) .. (1280.0, 720.0)] to [(0.0, 0.0) .. (1.0, 1.0)]
265fn frag_coord_to_uv(frag_coord: vec2<f32>, viewport: vec4<f32>) -> vec2<f32> {
266 return (frag_coord - viewport.xy) / viewport.zw;
269/// Convert frag coord to ndc
270fn frag_coord_to_ndc(frag_coord: vec4<f32>, viewport: vec4<f32>) -> vec3<f32> {
271 return vec3(uv_to_ndc(frag_coord_to_uv(frag_coord.xy, viewport)), frag_coord.z);
274/// Convert ndc space xy coordinate [-1.0 .. 1.0] to [0 .. render target
275/// viewport size]
276fn ndc_to_frag_coord(ndc: vec2<f32>, viewport: vec4<f32>) -> vec2<f32> {
277 return ndc_to_uv(ndc) * viewport.zw;
Contents