From e937d2d01d662f5072b5a00530b565aa3cedf270 Mon Sep 17 00:00:00 2001 From: DGriffin91 Date: Fri, 8 Sep 2023 12:15:14 -0700 Subject: [PATCH 01/13] initial setup --- crates/bevy_pbr/src/lib.rs | 8 + .../src/render/view_transformations.wgsl | 170 ++++++++++++++++++ 2 files changed, 178 insertions(+) create mode 100644 crates/bevy_pbr/src/render/view_transformations.wgsl diff --git a/crates/bevy_pbr/src/lib.rs b/crates/bevy_pbr/src/lib.rs index 228de223caa98..a6900bfc3f69d 100644 --- a/crates/bevy_pbr/src/lib.rs +++ b/crates/bevy_pbr/src/lib.rs @@ -75,6 +75,8 @@ pub const PBR_FUNCTIONS_HANDLE: Handle = Handle::weak_from_u128(16550102 pub const PBR_AMBIENT_HANDLE: Handle = Handle::weak_from_u128(2441520459096337034); pub const PARALLAX_MAPPING_SHADER_HANDLE: Handle = Handle::weak_from_u128(17035894873630133905); +pub const VIEW_TRANSFORMATIONS_SHADER_HANDLE: Handle = + Handle::weak_from_u128(2098345702398750291); /// Sets up the entire PBR infrastructure of bevy. pub struct PbrPlugin { @@ -149,6 +151,12 @@ impl Plugin for PbrPlugin { "render/parallax_mapping.wgsl", Shader::from_wgsl ); + load_internal_asset!( + app, + VIEW_TRANSFORMATIONS_SHADER_HANDLE, + "render/view_transformations.wgsl", + Shader::from_wgsl + ); app.register_asset_reflect::() .register_type::() diff --git a/crates/bevy_pbr/src/render/view_transformations.wgsl b/crates/bevy_pbr/src/render/view_transformations.wgsl new file mode 100644 index 0000000000000..a3f3af82f0a1c --- /dev/null +++ b/crates/bevy_pbr/src/render/view_transformations.wgsl @@ -0,0 +1,170 @@ +#define_import_path bevy_pbr::view_transformations +#import bevy_pbr::mesh_view_bindings as view_bindings + +/// World space: +/// +y is up + +/// View space: +/// -z is forward, +x is right, +y is up +/// (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 +/// (0.0, 1.0, 0.0) is linear distance of 1.0 above the camera's view relative to the camera's rotation + +/// NDC (normalized device coordinate): +/// https://www.w3.org/TR/webgpu/#coordinate-systems +/// (-1.0, -1.0) in NDC is located at the bottom-left corner of NDC +/// (1.0, 1.0) in NDC is located at the top-right corner of NDC +/// Z is depth where 1.0 is near clipping plane, and 0.0 is inf far away + +/// UV space: +/// 0.0, 0.0 is the top left +/// 1.0, 1.0 is the bottom right + + +// ----------------- +// TO WORLD -------- +// ----------------- + +/// Convert a view space position to world space +fn position_view_to_world(view_pos: vec3) -> vec3 { + let world_pos = view_bindings::view.view * vec4(view_pos, 1.0); + return world_pos.xyz; +} + +/// Convert a clip space position to world space +fn position_clip_to_world(clip_pos: vec4) -> vec3 { + let world_pos = view_bindings::view.inverse_view_proj * clip_pos; + return world_pos.xyz; +} + +/// Convert a ndc space position to world space +fn position_ndc_to_world(ndc_pos: vec3) -> vec3 { + let world_pos = view_bindings::view.inverse_view_proj * vec4(ndc_pos, 1.0); + return world_pos.xyz / world_pos.w; +} + +/// Convert a view space direction to world space +fn direction_view_to_world(view_dir: vec3) -> vec3 { + let world_dir = view_bindings::view.view * vec4(view_dir, 0.0); + return world_dir.xyz; +} + +/// Convert a clip space direction to world space +fn direction_clip_to_world(clip_dir: vec4) -> vec3 { + let world_dir = view_bindings::view.inverse_view_proj * clip_dir; + return world_dir.xyz; +} + +// ----------------- +// TO VIEW --------- +// ----------------- + +/// Convert a world space position to view space +fn position_world_to_view(world_pos: vec3) -> vec3 { + let view_pos = view_bindings::view.inverse_view * vec4(world_pos, 1.0); + return view_pos.xyz; +} + +/// Convert a clip space position to view space +fn position_clip_to_view(clip_pos: vec4) -> vec3 { + let view_pos = view_bindings::view.inverse_projection * clip_pos; + return view_pos.xyz / view_pos.w; +} + +/// Convert a ndc space position to view space +fn position_ndc_to_view(ndc_pos: vec3) -> vec3 { + let view_pos = view_bindings::view.inverse_projection * vec4(ndc_pos, 1.0); + return view_pos.xyz / view_pos.w; +} + +/// Convert a world space direction to view space +fn direction_world_to_view(world_dir: vec3) -> vec3 { + let view_dir = view_bindings::view.inverse_view * vec4(world_dir, 0.0); + return view_dir.xyz; +} + +/// Convert a clip space direction to view space +fn direction_clip_to_view(clip_dir: vec4) -> vec3 { + let view_dir = view_bindings::view.inverse_projection * clip_dir; + return view_dir.xyz; +} + +// ----------------- +// TO CLIP --------- +// ----------------- + +/// Convert a world space position to clip space +fn position_world_to_clip(world_pos: vec3) -> vec4 { + let clip_pos = view_bindings::view.view_proj * vec4(world_pos, 1.0); + return clip_pos; +} + +/// Convert a view space position to clip space +fn position_view_to_clip(view_pos: vec3) -> vec4 { + let clip_pos = view_bindings::view.projection * vec4(view_pos, 1.0); + return clip_pos; +} + +/// Convert a world space direction to clip space +fn direction_world_to_clip(world_dir: vec3) -> vec4 { + let clip_dir = view_bindings::view.view_proj * vec4(world_dir, 0.0); + return clip_dir; +} + +/// Convert a view space direction to clip space +fn direction_view_to_clip(view_dir: vec3) -> vec4 { + let clip_dir = view_bindings::view.projection * vec4(view_dir, 0.0); + return clip_dir; +} + +// ----------------- +// TO NDC ---------- +// ----------------- + +/// Convert a world space position to ndc space +fn position_world_to_ndc(world_pos: vec3) -> vec3 { + let ndc_pos = view_bindings::view.view_proj * vec4(world_pos, 1.0); + return ndc_pos.xyz / ndc_pos.w; +} + +/// Convert a view space position to ndc space +fn position_view_to_ndc(view_pos: vec3) -> vec3 { + let ndc_pos = view_bindings::view.projection * vec4(view_pos, 1.0); + return ndc_pos.xyz / ndc_pos.w; +} + + +/// Retrieve the camera near clipping plane +fn camera_near() -> f32 { + return view_bindings::view.projection[3][2]; +} + +/// Convert ndc space depth to linear world space +fn depth_ndc_to_linear(ndc_depth: f32) -> f32 { + return camera_near() / ndc_depth; +} + +/// Convert linear space depth to ndc world space +fn depth_linear_to_ndc(linear_depth: f32) -> f32 { + return camera_near() / linear_depth; +} + +/// Convert ndc space xy coordinate [-1.0 .. 1.0] to uv [0.0 .. 1.0] +fn ndc_to_uv(ndc: vec2) -> vec2 { + return ndc * vec2(0.5, -0.5) + vec2(0.5); +} + +/// Convert uv [0.0 .. 1.0] coordinate to ndc space xy [-1.0 .. 1.0] +fn uv_to_ndc(uv: vec2) -> vec2 { + return (uv - vec2(0.5)) * vec2(2.0, -2.0); +} + +/// returns the (0.0, 0.0) .. (1.0, 1.0) position within the viewport for the current render target +/// [0 .. render target viewport size] eg. [(0.0, 0.0) .. (1280.0, 720.0)] to [(0.0, 0.0) .. (1.0, 1.0)] +fn frag_coord_to_uv(frag_coord: vec2) -> vec2 { + return (frag_coord - view_bindings::view.viewport.xy) / view_bindings::view.viewport.zw; +} + +/// Convert frag coord to ndc +fn frag_coord_to_ndc(frag_coord: vec4) -> vec3 { + return vec3(uv_to_ndc(frag_coord_to_uv(frag_coord.xy)), frag_coord.z); +} \ No newline at end of file From 71cedc5911085fe272fa58a83aa6f5787612e83c Mon Sep 17 00:00:00 2001 From: DGriffin91 Date: Fri, 8 Sep 2023 17:16:00 -0700 Subject: [PATCH 02/13] nits --- crates/bevy_pbr/src/render/view_transformations.wgsl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/crates/bevy_pbr/src/render/view_transformations.wgsl b/crates/bevy_pbr/src/render/view_transformations.wgsl index a3f3af82f0a1c..0e4f71bcdf103 100644 --- a/crates/bevy_pbr/src/render/view_transformations.wgsl +++ b/crates/bevy_pbr/src/render/view_transformations.wgsl @@ -1,4 +1,5 @@ #define_import_path bevy_pbr::view_transformations + #import bevy_pbr::mesh_view_bindings as view_bindings /// World space: @@ -6,6 +7,7 @@ /// View space: /// -z is forward, +x is right, +y is up +/// Forward is from the camera position into the scene. /// (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 /// (0.0, 1.0, 0.0) is linear distance of 1.0 above the camera's view relative to the camera's rotation From e3c4c7ef28b3d3ac9ddc501dc7270e4f7be996a1 Mon Sep 17 00:00:00 2001 From: DGriffin91 Date: Sun, 10 Sep 2023 00:21:41 -0700 Subject: [PATCH 03/13] Update view_transformations.wgsl --- .../src/render/view_transformations.wgsl | 37 +++++++++++++++++-- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/crates/bevy_pbr/src/render/view_transformations.wgsl b/crates/bevy_pbr/src/render/view_transformations.wgsl index 0e4f71bcdf103..533863f24a18c 100644 --- a/crates/bevy_pbr/src/render/view_transformations.wgsl +++ b/crates/bevy_pbr/src/render/view_transformations.wgsl @@ -134,22 +134,51 @@ fn position_view_to_ndc(view_pos: vec3) -> vec3 { return ndc_pos.xyz / ndc_pos.w; } +// ----------------- +// DEPTH ----------- +// ----------------- /// Retrieve the camera near clipping plane fn camera_near() -> f32 { return view_bindings::view.projection[3][2]; } -/// Convert ndc space depth to linear world space -fn depth_ndc_to_linear(ndc_depth: f32) -> f32 { +/// Convert ndc space depth to linear view space. Only for use with perspective projections. +fn depth_ndc_to_linear_perspective(ndc_depth: f32) -> f32 { return camera_near() / ndc_depth; } -/// Convert linear space depth to ndc world space -fn depth_linear_to_ndc(linear_depth: f32) -> f32 { +/// Convert linear view space depth to ndc space. Only for use with perspective projections. +fn depth_linear_to_ndc_perspective(linear_depth: f32) -> f32 { return camera_near() / linear_depth; } +/// Convert ndc space depth to linear view space. Only for use with orthographic projections. +fn depth_ndc_to_linear_orthographic(ndc_depth: f32) -> f32 { + return (view_bindings::view.projection[3][2] - ndc_depth) / view_bindings::view.projection[2][2]; +} + +/// Convert linear view space depth to ndc space. Only for use with orthographic projections. +fn depth_linear_to_ndc_orthographic(linear_depth: f32) -> f32 { + return view_bindings::view.projection[3][2] - linear_depth * view_bindings::view.projection[2][2]; +} + +/// Convert ndc space depth to linear view space. +fn depth_ndc_to_linear(ndc_depth: f32) -> f32 { + let view_pos = view_bindings::view.inverse_projection * vec4(0.0, 0.0, ndc_depth, 1.0); + return -view_pos.z / view_pos.w; +} + +/// Convert linear view space depth to ndc space. +fn depth_linear_to_ndc(view_depth: f32) -> f32 { + let ndc_pos = view_bindings::view.projection * vec4(0.0, 0.0, view_depth, 1.0); + return -ndc_pos.z / ndc_pos.w; +} + +// ----------------- +// UV -------------- +// ----------------- + /// Convert ndc space xy coordinate [-1.0 .. 1.0] to uv [0.0 .. 1.0] fn ndc_to_uv(ndc: vec2) -> vec2 { return ndc * vec2(0.5, -0.5) + vec2(0.5); From e86324d87cfc147350ed80f3bce0d6654a5f3508 Mon Sep 17 00:00:00 2001 From: DGriffin91 Date: Sun, 10 Sep 2023 14:16:42 -0700 Subject: [PATCH 04/13] optimized view z functions --- crates/bevy_pbr/src/material.rs | 10 ++++ crates/bevy_pbr/src/render/mesh.rs | 17 ++++++ .../src/render/view_transformations.wgsl | 58 +++++++++---------- crates/bevy_render/src/camera/camera.rs | 8 +++ 4 files changed, 62 insertions(+), 31 deletions(-) diff --git a/crates/bevy_pbr/src/material.rs b/crates/bevy_pbr/src/material.rs index 8f6c59ce0f7c3..f0ee648a859bf 100644 --- a/crates/bevy_pbr/src/material.rs +++ b/crates/bevy_pbr/src/material.rs @@ -20,6 +20,7 @@ use bevy_ecs::{ }, }; use bevy_render::{ + camera::Projection, extract_component::ExtractComponentPlugin, mesh::{Mesh, MeshVertexBufferLayout}, prelude::Image, @@ -390,6 +391,7 @@ pub fn queue_material_meshes( Option<&ScreenSpaceAmbientOcclusionSettings>, Option<&NormalPrepass>, Option<&TemporalAntiAliasSettings>, + Option<&Projection>, &mut RenderPhase, &mut RenderPhase, &mut RenderPhase, @@ -406,6 +408,7 @@ pub fn queue_material_meshes( ssao, normal_prepass, taa_settings, + projection, mut opaque_phase, mut alpha_mask_phase, mut transparent_phase, @@ -434,6 +437,13 @@ pub fn queue_material_meshes( view_key |= MeshPipelineKey::ENVIRONMENT_MAP; } + if let Some(projection) = projection { + view_key |= match projection { + Projection::Perspective(_) => MeshPipelineKey::VIEW_PROJECTION_PERSPECTIVE, + Projection::Orthographic(_) => MeshPipelineKey::VIEW_PROJECTION_ORTHOGRAPHIC, + }; + } + if !view.hdr { if let Some(tonemapping) = tonemapping { view_key |= MeshPipelineKey::TONEMAP_IN_SHADER; diff --git a/crates/bevy_pbr/src/render/mesh.rs b/crates/bevy_pbr/src/render/mesh.rs index a8127d0ce59ea..cb37bc46a851f 100644 --- a/crates/bevy_pbr/src/render/mesh.rs +++ b/crates/bevy_pbr/src/render/mesh.rs @@ -751,6 +751,11 @@ bitflags::bitflags! { const TONEMAP_METHOD_SOMEWHAT_BORING_DISPLAY_TRANSFORM = 5 << Self::TONEMAP_METHOD_SHIFT_BITS; const TONEMAP_METHOD_TONY_MC_MAPFACE = 6 << Self::TONEMAP_METHOD_SHIFT_BITS; const TONEMAP_METHOD_BLENDER_FILMIC = 7 << Self::TONEMAP_METHOD_SHIFT_BITS; + const VIEW_PROJECTION_RESERVED_BITS = Self::VIEW_PROJECTION_MASK_BITS << Self::VIEW_PROJECTION_SHIFT_BITS; + const VIEW_PROJECTION_NONSTANDARD = 0 << Self::VIEW_PROJECTION_SHIFT_BITS; + const VIEW_PROJECTION_PERSPECTIVE = 1 << Self::VIEW_PROJECTION_SHIFT_BITS; + const VIEW_PROJECTION_ORTHOGRAPHIC = 2 << Self::VIEW_PROJECTION_SHIFT_BITS; + const VIEW_PROJECTION_RESERVED = 3 << Self::VIEW_PROJECTION_SHIFT_BITS; } } @@ -766,6 +771,9 @@ impl MeshPipelineKey { const TONEMAP_METHOD_MASK_BITS: u32 = 0b111; const TONEMAP_METHOD_SHIFT_BITS: u32 = Self::BLEND_SHIFT_BITS - Self::TONEMAP_METHOD_MASK_BITS.count_ones(); + const VIEW_PROJECTION_MASK_BITS: u32 = 0b11; + const VIEW_PROJECTION_SHIFT_BITS: u32 = + Self::TONEMAP_METHOD_SHIFT_BITS - Self::VIEW_PROJECTION_MASK_BITS.count_ones(); pub fn from_msaa_samples(msaa_samples: u32) -> Self { let msaa_bits = @@ -948,6 +956,15 @@ impl SpecializedMeshPipeline for MeshPipeline { shader_defs.push("LOAD_PREPASS_NORMALS".into()); } + let view_projection = key.intersection(MeshPipelineKey::VIEW_PROJECTION_RESERVED_BITS); + if view_projection == MeshPipelineKey::VIEW_PROJECTION_NONSTANDARD { + shader_defs.push("VIEW_PROJECTION_NONSTANDARD".into()); + } else if view_projection == MeshPipelineKey::VIEW_PROJECTION_PERSPECTIVE { + shader_defs.push("VIEW_PROJECTION_PERSPECTIVE".into()); + } else if view_projection == MeshPipelineKey::VIEW_PROJECTION_ORTHOGRAPHIC { + shader_defs.push("VIEW_PROJECTION_ORTHOGRAPHIC".into()); + } + if key.contains(MeshPipelineKey::TONEMAP_IN_SHADER) { shader_defs.push("TONEMAP_IN_SHADER".into()); diff --git a/crates/bevy_pbr/src/render/view_transformations.wgsl b/crates/bevy_pbr/src/render/view_transformations.wgsl index 533863f24a18c..9e2756c82fbb8 100644 --- a/crates/bevy_pbr/src/render/view_transformations.wgsl +++ b/crates/bevy_pbr/src/render/view_transformations.wgsl @@ -138,41 +138,37 @@ fn position_view_to_ndc(view_pos: vec3) -> vec3 { // DEPTH ----------- // ----------------- -/// Retrieve the camera near clipping plane -fn camera_near() -> f32 { +/// Retrieve the perspective camera near clipping plane +fn perspective_camera_near() -> f32 { return view_bindings::view.projection[3][2]; } -/// Convert ndc space depth to linear view space. Only for use with perspective projections. -fn depth_ndc_to_linear_perspective(ndc_depth: f32) -> f32 { - return camera_near() / ndc_depth; -} - -/// Convert linear view space depth to ndc space. Only for use with perspective projections. -fn depth_linear_to_ndc_perspective(linear_depth: f32) -> f32 { - return camera_near() / linear_depth; -} - -/// Convert ndc space depth to linear view space. Only for use with orthographic projections. -fn depth_ndc_to_linear_orthographic(ndc_depth: f32) -> f32 { - return (view_bindings::view.projection[3][2] - ndc_depth) / view_bindings::view.projection[2][2]; -} - -/// Convert linear view space depth to ndc space. Only for use with orthographic projections. -fn depth_linear_to_ndc_orthographic(linear_depth: f32) -> f32 { - return view_bindings::view.projection[3][2] - linear_depth * view_bindings::view.projection[2][2]; -} - -/// Convert ndc space depth to linear view space. -fn depth_ndc_to_linear(ndc_depth: f32) -> f32 { +/// Convert ndc depth to linear view z. +fn depth_ndc_to_view_z(ndc_depth: f32) -> f32 { +#ifdef VIEW_PROJECTION_PERSPECTIVE + return -perspective_camera_near() / ndc_depth; +#else +#ifdef VIEW_PROJECTION_ORTHOGRAPHIC + return -(view_bindings::view.projection[3][2] - ndc_depth) / view_bindings::view.projection[2][2]; +#else let view_pos = view_bindings::view.inverse_projection * vec4(0.0, 0.0, ndc_depth, 1.0); - return -view_pos.z / view_pos.w; -} - -/// Convert linear view space depth to ndc space. -fn depth_linear_to_ndc(view_depth: f32) -> f32 { - let ndc_pos = view_bindings::view.projection * vec4(0.0, 0.0, view_depth, 1.0); - return -ndc_pos.z / ndc_pos.w; + return view_pos.z / view_pos.w; +#endif // VIEW_PROJECTION_ORTHOGRAPHIC +#endif // VIEW_PROJECTION_PERSPECTIVE +} + +/// Convert linear view z to ndc depth. +fn view_z_to_depth_ndc(view_z: f32) -> f32 { +#ifdef VIEW_PROJECTION_PERSPECTIVE + return -perspective_camera_near() / view_z; +#else +#ifdef VIEW_PROJECTION_ORTHOGRAPHIC + return view_bindings::view.projection[3][2] + view_z * view_bindings::view.projection[2][2]; +#else + let ndc_pos = view_bindings::view.projection * vec4(0.0, 0.0, view_z, 1.0); + return ndc_pos.z / ndc_pos.w; +#endif // VIEW_PROJECTION_ORTHOGRAPHIC +#endif // VIEW_PROJECTION_PERSPECTIVE } // ----------------- diff --git a/crates/bevy_render/src/camera/camera.rs b/crates/bevy_render/src/camera/camera.rs index 4403eb76308f9..a1d36a8c62610 100644 --- a/crates/bevy_render/src/camera/camera.rs +++ b/crates/bevy_render/src/camera/camera.rs @@ -29,6 +29,8 @@ use bevy_window::{ use std::{borrow::Cow, ops::Range}; use wgpu::{BlendState, Extent3d, LoadOp, TextureFormat}; +use super::Projection; + /// Render viewport configuration for the [`Camera`] component. /// /// The viewport defines the area on the render target to which the camera renders its image. @@ -644,6 +646,7 @@ pub fn extract_cameras( Option<&ColorGrading>, Option<&TemporalJitter>, Option<&RenderLayers>, + Option<&Projection>, )>, >, primary_window: Extract>>, @@ -658,6 +661,7 @@ pub fn extract_cameras( color_grading, temporal_jitter, render_layers, + perspective, ) in query.iter() { let color_grading = *color_grading.unwrap_or(&ColorGrading::default()); @@ -720,6 +724,10 @@ pub fn extract_cameras( if let Some(render_layers) = render_layers { commands.insert(*render_layers); } + + if let Some(perspective) = perspective { + commands.insert(perspective.clone()); + } } } } From a9e50dd80178bc50b644f557511111ba2c265a56 Mon Sep 17 00:00:00 2001 From: DGriffin91 Date: Sun, 10 Sep 2023 14:31:33 -0700 Subject: [PATCH 05/13] view z notes --- crates/bevy_pbr/src/render/view_transformations.wgsl | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/crates/bevy_pbr/src/render/view_transformations.wgsl b/crates/bevy_pbr/src/render/view_transformations.wgsl index 9e2756c82fbb8..181916d8fac30 100644 --- a/crates/bevy_pbr/src/render/view_transformations.wgsl +++ b/crates/bevy_pbr/src/render/view_transformations.wgsl @@ -143,7 +143,8 @@ fn perspective_camera_near() -> f32 { return view_bindings::view.projection[3][2]; } -/// Convert ndc depth to linear view z. +/// Convert ndc depth to linear view z. +/// Note: Depth values in front of the camera will be negative as -z is forward fn depth_ndc_to_view_z(ndc_depth: f32) -> f32 { #ifdef VIEW_PROJECTION_PERSPECTIVE return -perspective_camera_near() / ndc_depth; @@ -158,6 +159,7 @@ fn depth_ndc_to_view_z(ndc_depth: f32) -> f32 { } /// Convert linear view z to ndc depth. +/// Note: View z input should be negative for values in front of the camera as -z is forward fn view_z_to_depth_ndc(view_z: f32) -> f32 { #ifdef VIEW_PROJECTION_PERSPECTIVE return -perspective_camera_near() / view_z; From 28410bf66bf3f610f138fdcb445bd2532d586a82 Mon Sep 17 00:00:00 2001 From: DGriffin91 Date: Fri, 13 Oct 2023 11:44:40 -0700 Subject: [PATCH 06/13] include orthographic in NDC docs --- crates/bevy_pbr/src/render/view_transformations.wgsl | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/crates/bevy_pbr/src/render/view_transformations.wgsl b/crates/bevy_pbr/src/render/view_transformations.wgsl index 181916d8fac30..47667a0b46685 100644 --- a/crates/bevy_pbr/src/render/view_transformations.wgsl +++ b/crates/bevy_pbr/src/render/view_transformations.wgsl @@ -15,7 +15,10 @@ /// https://www.w3.org/TR/webgpu/#coordinate-systems /// (-1.0, -1.0) in NDC is located at the bottom-left corner of NDC /// (1.0, 1.0) in NDC is located at the top-right corner of NDC -/// Z is depth where 1.0 is near clipping plane, and 0.0 is inf far away +/// Z is depth where: +/// 1.0 is near clipping plane +/// Perspective projection: 0.0 is inf far away +/// Orthographic projection: 0.0 is far clipping plane /// UV space: /// 0.0, 0.0 is the top left From a53656994dfd755b03218cb61b12c8311f063f18 Mon Sep 17 00:00:00 2001 From: DGriffin91 Date: Fri, 13 Oct 2023 11:53:50 -0700 Subject: [PATCH 07/13] replace mesh_position_world_to_clip --- crates/bevy_pbr/src/render/mesh.wgsl | 9 +++++---- crates/bevy_pbr/src/render/mesh_functions.wgsl | 17 +++++++---------- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/crates/bevy_pbr/src/render/mesh.wgsl b/crates/bevy_pbr/src/render/mesh.wgsl index 84ad95dd361db..ec5bfebc1b126 100644 --- a/crates/bevy_pbr/src/render/mesh.wgsl +++ b/crates/bevy_pbr/src/render/mesh.wgsl @@ -1,9 +1,10 @@ #import bevy_pbr::mesh_functions as mesh_functions #import bevy_pbr::skinning #import bevy_pbr::morph -#import bevy_pbr::mesh_bindings mesh -#import bevy_pbr::mesh_vertex_output MeshVertexOutput -#import bevy_render::instance_index get_instance_index +#import bevy_pbr::mesh_bindings mesh +#import bevy_pbr::mesh_vertex_output MeshVertexOutput +#import bevy_render::instance_index get_instance_index +#import bevy_pbr::view_transformations position_world_to_clip struct Vertex { @builtin(instance_index) instance_index: u32, @@ -86,7 +87,7 @@ fn vertex(vertex_no_morph: Vertex) -> MeshVertexOutput { #ifdef VERTEX_POSITIONS out.world_position = mesh_functions::mesh_position_local_to_world(model, vec4(vertex.position, 1.0)); - out.position = mesh_functions::mesh_position_world_to_clip(out.world_position); + out.position = position_world_to_clip(out.world_position.xyz); #endif #ifdef VERTEX_UVS diff --git a/crates/bevy_pbr/src/render/mesh_functions.wgsl b/crates/bevy_pbr/src/render/mesh_functions.wgsl index a0c689d3299da..4f926b8ac2b37 100644 --- a/crates/bevy_pbr/src/render/mesh_functions.wgsl +++ b/crates/bevy_pbr/src/render/mesh_functions.wgsl @@ -1,10 +1,11 @@ #define_import_path bevy_pbr::mesh_functions -#import bevy_pbr::mesh_view_bindings view -#import bevy_pbr::mesh_bindings mesh -#import bevy_pbr::mesh_types MESH_FLAGS_SIGN_DETERMINANT_MODEL_3X3_BIT -#import bevy_render::instance_index get_instance_index -#import bevy_render::maths affine_to_square, mat2x4_f32_to_mat3x3_unpack +#import bevy_pbr::mesh_view_bindings view +#import bevy_pbr::mesh_bindings mesh +#import bevy_pbr::mesh_types MESH_FLAGS_SIGN_DETERMINANT_MODEL_3X3_BIT +#import bevy_render::instance_index get_instance_index +#import bevy_render::maths affine_to_square, mat2x4_f32_to_mat3x3_unpack +#import bevy_pbr::view_transformations position_world_to_clip fn get_model_matrix(instance_index: u32) -> mat4x4 { return affine_to_square(mesh[get_instance_index(instance_index)].model); @@ -18,16 +19,12 @@ fn mesh_position_local_to_world(model: mat4x4, vertex_position: vec4) return model * vertex_position; } -fn mesh_position_world_to_clip(world_position: vec4) -> vec4 { - return view.view_proj * world_position; -} - // NOTE: The intermediate world_position assignment is important // for precision purposes when using the 'equals' depth comparison // function. fn mesh_position_local_to_clip(model: mat4x4, vertex_position: vec4) -> vec4 { let world_position = mesh_position_local_to_world(model, vertex_position); - return mesh_position_world_to_clip(world_position); + return position_world_to_clip(world_position.xyz); } fn mesh_normal_local_to_world(vertex_normal: vec3, instance_index: u32) -> vec3 { From 45eb9f9440a719e854fcc5ea98ee0c5f6a5f3cf9 Mon Sep 17 00:00:00 2001 From: DGriffin91 Date: Fri, 13 Oct 2023 12:09:18 -0700 Subject: [PATCH 08/13] use #else ifdef --- crates/bevy_pbr/src/render/view_transformations.wgsl | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/crates/bevy_pbr/src/render/view_transformations.wgsl b/crates/bevy_pbr/src/render/view_transformations.wgsl index 47667a0b46685..7f407d4319c16 100644 --- a/crates/bevy_pbr/src/render/view_transformations.wgsl +++ b/crates/bevy_pbr/src/render/view_transformations.wgsl @@ -151,14 +151,12 @@ fn perspective_camera_near() -> f32 { fn depth_ndc_to_view_z(ndc_depth: f32) -> f32 { #ifdef VIEW_PROJECTION_PERSPECTIVE return -perspective_camera_near() / ndc_depth; -#else -#ifdef VIEW_PROJECTION_ORTHOGRAPHIC +#else ifdef VIEW_PROJECTION_ORTHOGRAPHIC return -(view_bindings::view.projection[3][2] - ndc_depth) / view_bindings::view.projection[2][2]; #else let view_pos = view_bindings::view.inverse_projection * vec4(0.0, 0.0, ndc_depth, 1.0); return view_pos.z / view_pos.w; -#endif // VIEW_PROJECTION_ORTHOGRAPHIC -#endif // VIEW_PROJECTION_PERSPECTIVE +#endif } /// Convert linear view z to ndc depth. @@ -166,14 +164,12 @@ fn depth_ndc_to_view_z(ndc_depth: f32) -> f32 { fn view_z_to_depth_ndc(view_z: f32) -> f32 { #ifdef VIEW_PROJECTION_PERSPECTIVE return -perspective_camera_near() / view_z; -#else -#ifdef VIEW_PROJECTION_ORTHOGRAPHIC +#else ifdef VIEW_PROJECTION_ORTHOGRAPHIC return view_bindings::view.projection[3][2] + view_z * view_bindings::view.projection[2][2]; #else let ndc_pos = view_bindings::view.projection * vec4(0.0, 0.0, view_z, 1.0); return ndc_pos.z / ndc_pos.w; -#endif // VIEW_PROJECTION_ORTHOGRAPHIC -#endif // VIEW_PROJECTION_PERSPECTIVE +#endif } // ----------------- From a7c549c7617a814d028cdc5a4d9a600de05fa90f Mon Sep 17 00:00:00 2001 From: DGriffin91 Date: Fri, 13 Oct 2023 12:11:34 -0700 Subject: [PATCH 09/13] FMA --- crates/bevy_pbr/src/render/view_transformations.wgsl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_pbr/src/render/view_transformations.wgsl b/crates/bevy_pbr/src/render/view_transformations.wgsl index 7f407d4319c16..88dc075b94ba6 100644 --- a/crates/bevy_pbr/src/render/view_transformations.wgsl +++ b/crates/bevy_pbr/src/render/view_transformations.wgsl @@ -183,7 +183,7 @@ fn ndc_to_uv(ndc: vec2) -> vec2 { /// Convert uv [0.0 .. 1.0] coordinate to ndc space xy [-1.0 .. 1.0] fn uv_to_ndc(uv: vec2) -> vec2 { - return (uv - vec2(0.5)) * vec2(2.0, -2.0); + return uv * vec2(2.0, -2.0) + vec2(-1.0, 1.0); } /// returns the (0.0, 0.0) .. (1.0, 1.0) position within the viewport for the current render target From da8891ea184411be5a42a5667a316e336ac0a190 Mon Sep 17 00:00:00 2001 From: DGriffin91 Date: Fri, 13 Oct 2023 12:15:39 -0700 Subject: [PATCH 10/13] use in deferred --- .../src/deferred/pbr_deferred_functions.wgsl | 32 +------------------ .../src/render/view_transformations.wgsl | 2 +- 2 files changed, 2 insertions(+), 32 deletions(-) diff --git a/crates/bevy_pbr/src/deferred/pbr_deferred_functions.wgsl b/crates/bevy_pbr/src/deferred/pbr_deferred_functions.wgsl index c98d5c19a7f7b..580f93c9783eb 100644 --- a/crates/bevy_pbr/src/deferred/pbr_deferred_functions.wgsl +++ b/crates/bevy_pbr/src/deferred/pbr_deferred_functions.wgsl @@ -6,37 +6,7 @@ #import bevy_pbr::mesh_view_bindings as view_bindings #import bevy_pbr::mesh_view_bindings view #import bevy_pbr::utils octahedral_encode, octahedral_decode - -// --------------------------- -// from https://github.com/DGriffin91/bevy_coordinate_systems/blob/main/src/transformations.wgsl -// --------------------------- - -/// Convert a ndc space position to world space -fn position_ndc_to_world(ndc_pos: vec3) -> vec3 { - let world_pos = view.inverse_view_proj * vec4(ndc_pos, 1.0); - return world_pos.xyz / world_pos.w; -} - -/// Convert ndc space xy coordinate [-1.0 .. 1.0] to uv [0.0 .. 1.0] -fn ndc_to_uv(ndc: vec2) -> vec2 { - return ndc * vec2(0.5, -0.5) + vec2(0.5); -} - -/// Convert uv [0.0 .. 1.0] coordinate to ndc space xy [-1.0 .. 1.0] -fn uv_to_ndc(uv: vec2) -> vec2 { - return uv * vec2(2.0, -2.0) + vec2(-1.0, 1.0); -} - -/// Returns the (0.0, 0.0) .. (1.0, 1.0) position within the viewport for the current render target. -/// [0 .. render target viewport size] eg. [(0.0, 0.0) .. (1280.0, 720.0)] to [(0.0, 0.0) .. (1.0, 1.0)] -fn frag_coord_to_uv(frag_coord: vec2) -> vec2 { - return (frag_coord - view.viewport.xy) / view.viewport.zw; -} - -/// Convert frag coord to ndc. -fn frag_coord_to_ndc(frag_coord: vec4) -> vec3 { - return vec3(uv_to_ndc(frag_coord_to_uv(frag_coord.xy)), frag_coord.z); -} +#import bevy_pbr::view_transformations position_ndc_to_world, frag_coord_to_ndc // Creates the deferred gbuffer from a PbrInput. fn deferred_gbuffer_from_pbr_input(in: PbrInput) -> vec4 { diff --git a/crates/bevy_pbr/src/render/view_transformations.wgsl b/crates/bevy_pbr/src/render/view_transformations.wgsl index 88dc075b94ba6..95a6cf1e777d5 100644 --- a/crates/bevy_pbr/src/render/view_transformations.wgsl +++ b/crates/bevy_pbr/src/render/view_transformations.wgsl @@ -195,4 +195,4 @@ fn frag_coord_to_uv(frag_coord: vec2) -> vec2 { /// Convert frag coord to ndc fn frag_coord_to_ndc(frag_coord: vec4) -> vec3 { return vec3(uv_to_ndc(frag_coord_to_uv(frag_coord.xy)), frag_coord.z); -} \ No newline at end of file +} From 9360f33fcbc97bd7b78099f62c1bdd56209a74c6 Mon Sep 17 00:00:00 2001 From: DGriffin91 Date: Sat, 14 Oct 2023 11:38:46 -0700 Subject: [PATCH 11/13] add missing perspective divide --- crates/bevy_pbr/src/render/view_transformations.wgsl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_pbr/src/render/view_transformations.wgsl b/crates/bevy_pbr/src/render/view_transformations.wgsl index 95a6cf1e777d5..6521bd99decea 100644 --- a/crates/bevy_pbr/src/render/view_transformations.wgsl +++ b/crates/bevy_pbr/src/render/view_transformations.wgsl @@ -38,7 +38,7 @@ fn position_view_to_world(view_pos: vec3) -> vec3 { /// Convert a clip space position to world space fn position_clip_to_world(clip_pos: vec4) -> vec3 { let world_pos = view_bindings::view.inverse_view_proj * clip_pos; - return world_pos.xyz; + return world_pos.xyz / world_pos.w; } /// Convert a ndc space position to world space From d7c2aaa96cbf87ec09ab0612fadab099e6f450b8 Mon Sep 17 00:00:00 2001 From: DGriffin91 Date: Sat, 14 Oct 2023 11:49:08 -0700 Subject: [PATCH 12/13] rename to projection --- crates/bevy_render/src/camera/camera.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/bevy_render/src/camera/camera.rs b/crates/bevy_render/src/camera/camera.rs index 17c6c4a93461c..5892b9a0818eb 100644 --- a/crates/bevy_render/src/camera/camera.rs +++ b/crates/bevy_render/src/camera/camera.rs @@ -661,7 +661,7 @@ pub fn extract_cameras( color_grading, temporal_jitter, render_layers, - perspective, + projection, ) in query.iter() { let color_grading = *color_grading.unwrap_or(&ColorGrading::default()); @@ -725,7 +725,7 @@ pub fn extract_cameras( commands.insert(*render_layers); } - if let Some(perspective) = perspective { + if let Some(perspective) = projection { commands.insert(perspective.clone()); } } From cfbc9ad53c1cb4f29edacda26948274c187a549d Mon Sep 17 00:00:00 2001 From: DGriffin91 Date: Mon, 16 Oct 2023 23:37:25 -0700 Subject: [PATCH 13/13] remove unnecessary perspective divides --- crates/bevy_pbr/src/render/view_transformations.wgsl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/bevy_pbr/src/render/view_transformations.wgsl b/crates/bevy_pbr/src/render/view_transformations.wgsl index 6521bd99decea..89a004b4af4be 100644 --- a/crates/bevy_pbr/src/render/view_transformations.wgsl +++ b/crates/bevy_pbr/src/render/view_transformations.wgsl @@ -38,7 +38,7 @@ fn position_view_to_world(view_pos: vec3) -> vec3 { /// Convert a clip space position to world space fn position_clip_to_world(clip_pos: vec4) -> vec3 { let world_pos = view_bindings::view.inverse_view_proj * clip_pos; - return world_pos.xyz / world_pos.w; + return world_pos.xyz; } /// Convert a ndc space position to world space @@ -72,7 +72,7 @@ fn position_world_to_view(world_pos: vec3) -> vec3 { /// Convert a clip space position to view space fn position_clip_to_view(clip_pos: vec4) -> vec3 { let view_pos = view_bindings::view.inverse_projection * clip_pos; - return view_pos.xyz / view_pos.w; + return view_pos.xyz; } /// Convert a ndc space position to view space