1//! World space:2//! +y is up3//!4//! View space:5//! -z is forward, +x is right, +y is up6//! 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 rotation8//! (0.0, 1.0, 0.0) is linear distance of 1.0 above the camera's view relative to the camera's rotation9//!10//! NDC (normalized device coordinate):11//! https://www.w3.org/TR/webgpu/#coordinate-systems12//! (-1.0, -1.0) in NDC is located at the bottom-left corner of NDC13//! (1.0, 1.0) in NDC is located at the top-right corner of NDC14//! Z is depth where:15//! 1.0 is near clipping plane16//! Perspective projection: 0.0 is inf far away17//! Orthographic projection: 0.0 is far clipping plane18//!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 left25//! 1.0, 1.0 is the bottom right27struct ColorGrading {28 balance: mat3x3<f32>,29 saturation: vec3<f32>,30 contrast: vec3<f32>,31 gamma: vec3<f32>,32 gain: vec3<f32>,33 lift: vec3<f32>,34 midtone_range: vec2<f32>,35 exposure: f32,36 hue: f32,37 post_saturation: f32,38}40struct View {41 clip_from_world: mat4x4<f32>,42 unjittered_clip_from_world: mat4x4<f32>,43 world_from_clip: mat4x4<f32>,44 world_from_view: mat4x4<f32>,45 view_from_world: mat4x4<f32>,46 /// Typically a column-major right-handed projection matrix, one of either:47 ///48 /// Perspective (infinite reverse z)49 /// ```50 /// f = 1 / tan(fov_y_radians / 2)51 ///52 /// ⎡ f / aspect 0 0 0 ⎤53 /// ⎢ 0 f 0 0 ⎥54 /// ⎢ 0 0 0 near ⎥55 /// ⎣ 0 0 -1 0 ⎦56 /// ```57 ///58 /// Orthographic59 /// ```60 /// w = right - left61 /// h = top - bottom62 /// d = far - near63 /// cw = -right - left64 /// ch = -top - bottom65 ///66 /// ⎡ 2 / w 0 0 cw / w ⎤67 /// ⎢ 0 2 / h 0 ch / h ⎥68 /// ⎢ 0 0 1 / d far / d ⎥69 /// ⎣ 0 0 0 1 ⎦70 /// ```71 ///72 /// `clip_from_view[3][3] == 1.0` is the standard way to check if a projection is orthographic73 ///74 /// Wgsl matrices are column major, so for example getting the near plane of a perspective projection is `clip_from_view[3][2]`75 ///76 /// Custom projections are also possible however.77 clip_from_view: mat4x4<f32>,78 view_from_clip: mat4x4<f32>,79 world_position: vec3<f32>,80 exposure: f32,81 /// viewport(x_origin, y_origin, width, height)82 viewport: vec4<f32>,83 main_pass_viewport: vec4<f32>,84 /// 6 world-space half spaces (normal: vec3, distance: f32) ordered left, right, top, bottom, near, far.85 /// The normal vectors point towards the interior of the frustum.86 /// A half space contains `p` if `normal.dot(p) + distance > 0.`87 frustum: array<vec4<f32>, 6>,88 /// The world-space position of the camera used to resolve visibility ranges.89 ///90 /// This is the position of the camera itself, unless this view isn't91 /// associated with a camera, in which case it's the position of the primary92 /// camera.93 lod_view_world_position: vec3<f32>,94 color_grading: ColorGrading,95 mip_bias: f32,96 frame_count: u32,97};99// -----------------100// TO WORLD --------101// -----------------103/// Convert a view space position to world space104fn position_view_to_world(view_pos: vec3<f32>, world_from_view: mat4x4<f32>) -> vec3<f32> {105 let world_pos = world_from_view * vec4(view_pos, 1.0);106 return world_pos.xyz;107}109/// Convert a clip space position to world space110fn position_clip_to_world(clip_pos: vec4<f32>, world_from_clip: mat4x4<f32>) -> vec3<f32> {111 let world_pos = world_from_clip * clip_pos;112 return world_pos.xyz;113}115/// Convert a ndc space position to world space116fn position_ndc_to_world(ndc_pos: vec3<f32>, world_from_clip: mat4x4<f32>) -> vec3<f32> {117 let world_pos = world_from_clip * vec4(ndc_pos, 1.0);118 return world_pos.xyz / world_pos.w;119}121/// Convert a view space direction to world space122fn direction_view_to_world(view_dir: vec3<f32>, world_from_view: mat4x4<f32>) -> vec3<f32> {123 let world_dir = world_from_view * vec4(view_dir, 0.0);124 return world_dir.xyz;125}127/// Convert a clip space direction to world space128fn direction_clip_to_world(clip_dir: vec4<f32>, world_from_clip: mat4x4<f32>) -> vec3<f32> {129 let world_dir = world_from_clip * clip_dir;130 return world_dir.xyz;131}133// -----------------134// TO VIEW ---------135// -----------------137/// Convert a world space position to view space138fn position_world_to_view(world_pos: vec3<f32>, view_from_world: mat4x4<f32>) -> vec3<f32> {139 let view_pos = view_from_world * vec4(world_pos, 1.0);140 return view_pos.xyz;141}143/// Convert a clip space position to view space144fn position_clip_to_view(clip_pos: vec4<f32>, view_from_clip: mat4x4<f32>) -> vec3<f32> {145 let view_pos = view_from_clip * clip_pos;146 return view_pos.xyz;147}149/// Convert a ndc space position to view space150fn position_ndc_to_view(ndc_pos: vec3<f32>, view_from_clip: mat4x4<f32>) -> vec3<f32> {151 let view_pos = view_from_clip * vec4(ndc_pos, 1.0);152 return view_pos.xyz / view_pos.w;153}155/// Convert a world space direction to view space156fn direction_world_to_view(world_dir: vec3<f32>, view_from_world: mat4x4<f32>) -> vec3<f32> {157 let view_dir = view_from_world * vec4(world_dir, 0.0);158 return view_dir.xyz;159}161/// Convert a clip space direction to view space162fn direction_clip_to_view(clip_dir: vec4<f32>, view_from_clip: mat4x4<f32>) -> vec3<f32> {163 let view_dir = view_from_clip * clip_dir;164 return view_dir.xyz;165}167// -----------------168// TO CLIP ---------169// -----------------171/// Convert a world space position to clip space172fn position_world_to_clip(world_pos: vec3<f32>, clip_from_world: mat4x4<f32>) -> vec4<f32> {173 let clip_pos = clip_from_world * vec4(world_pos, 1.0);174 return clip_pos;175}177/// Convert a view space position to clip space178fn position_view_to_clip(view_pos: vec3<f32>, clip_from_view: mat4x4<f32>) -> vec4<f32> {179 let clip_pos = clip_from_view * vec4(view_pos, 1.0);180 return clip_pos;181}183/// Convert a world space direction to clip space184fn direction_world_to_clip(world_dir: vec3<f32>, clip_from_world: mat4x4<f32>) -> vec4<f32> {185 let clip_dir = clip_from_world * vec4(world_dir, 0.0);186 return clip_dir;187}189/// Convert a view space direction to clip space190fn direction_view_to_clip(view_dir: vec3<f32>, clip_from_view: mat4x4<f32>) -> vec4<f32> {191 let clip_dir = clip_from_view * vec4(view_dir, 0.0);192 return clip_dir;193}195// -----------------196// TO NDC ----------197// -----------------199/// Convert a world space position to ndc space200fn position_world_to_ndc(world_pos: vec3<f32>, clip_from_world: mat4x4<f32>) -> vec3<f32> {201 let ndc_pos = clip_from_world * vec4(world_pos, 1.0);202 return ndc_pos.xyz / ndc_pos.w;203}205/// Convert a view space position to ndc space206fn position_view_to_ndc(view_pos: vec3<f32>, clip_from_view: mat4x4<f32>) -> vec3<f32> {207 let ndc_pos = clip_from_view * vec4(view_pos, 1.0);208 return ndc_pos.xyz / ndc_pos.w;209}211// -----------------212// DEPTH -----------213// -----------------215/// Retrieve the perspective camera near clipping plane216fn perspective_camera_near(clip_from_view: mat4x4<f32>) -> f32 {217 return clip_from_view[3][2];218}220/// Convert ndc depth to linear view z.221/// Note: Depth values in front of the camera will be negative as -z is forward222fn depth_ndc_to_view_z(ndc_depth: f32, clip_from_view: mat4x4<f32>, view_from_clip: mat4x4<f32>) -> f32 {223@if(VIEW_PROJECTION_PERSPECTIVE)224 return -perspective_camera_near(clip_from_view) / ndc_depth;225@elif(VIEW_PROJECTION_ORTHOGRAPHIC)226 return -(clip_from_view[3][2] - ndc_depth) / clip_from_view[2][2];227@else {228 let view_pos = view_from_clip * vec4(0.0, 0.0, ndc_depth, 1.0);229 return view_pos.z / view_pos.w;230}231}233/// Convert linear view z to ndc depth.234/// Note: View z input should be negative for values in front of the camera as -z is forward235fn view_z_to_depth_ndc(view_z: f32, clip_from_view: mat4x4<f32>) -> f32 {236@if(VIEW_PROJECTION_PERSPECTIVE)237 return -perspective_camera_near(clip_from_view) / view_z;238@elif(VIEW_PROJECTION_ORTHOGRAPHIC)239 return clip_from_view[3][2] + view_z * clip_from_view[2][2];240@else {241 let ndc_pos = clip_from_view * vec4(0.0, 0.0, view_z, 1.0);242 return ndc_pos.z / ndc_pos.w;243}244}246// -----------------247// UV --------------248// -----------------250/// Convert ndc space xy coordinate `[-1.0 .. 1.0]` to uv `[0.0 .. 1.0]`251fn ndc_to_uv(ndc: vec2<f32>) -> vec2<f32> {252 return ndc * vec2(0.5, -0.5) + vec2(0.5);253}255/// Convert uv `[0.0 .. 1.0]` coordinate to ndc space xy `[-1.0 .. 1.0]`256fn uv_to_ndc(uv: vec2<f32>) -> vec2<f32> {257 return uv * vec2(2.0, -2.0) + vec2(-1.0, 1.0);258}260/// returns the `(0.0, 0.0) .. (1.0, 1.0)` position within the viewport for the current render target261/// `[0 .. render target viewport size]` eg. `[(0.0, 0.0) .. (1280.0, 720.0)]` to `[(0.0, 0.0) .. (1.0, 1.0)]`262fn frag_coord_to_uv(frag_coord: vec2<f32>, viewport: vec4<f32>) -> vec2<f32> {263 return (frag_coord - viewport.xy) / viewport.zw;264}266/// Convert frag coord to ndc267fn frag_coord_to_ndc(frag_coord: vec4<f32>, viewport: vec4<f32>) -> vec3<f32> {268 return vec3(uv_to_ndc(frag_coord_to_uv(frag_coord.xy, viewport)), frag_coord.z);269}271/// Convert ndc space xy coordinate `[-1.0 .. 1.0]` to `[0 .. render target272/// viewport size]`273fn ndc_to_frag_coord(ndc: vec2<f32>, viewport: vec4<f32>) -> vec2<f32> {274 return ndc_to_uv(ndc) * viewport.zw;275}