From 5b7ef55c55791ba85151400346321dffe9809411 Mon Sep 17 00:00:00 2001 From: lwansbrough Date: Sun, 3 Jan 2021 12:51:02 -0800 Subject: [PATCH 1/4] Update the camera uniform to include the view matrix --- .../bevy_render/src/render_graph/nodes/camera_node.rs | 11 +++++++---- .../bevy_render/src/render_graph/nodes/pass_node.rs | 2 +- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/crates/bevy_render/src/render_graph/nodes/camera_node.rs b/crates/bevy_render/src/render_graph/nodes/camera_node.rs index 5e677c75b3019..95b16f0522351 100644 --- a/crates/bevy_render/src/render_graph/nodes/camera_node.rs +++ b/crates/bevy_render/src/render_graph/nodes/camera_node.rs @@ -88,7 +88,7 @@ pub fn camera_node_system( render_resource_context.map_buffer(staging_buffer); staging_buffer } else { - let size = std::mem::size_of::<[[f32; 4]; 4]>(); + let size = std::mem::size_of::<[[[f32; 4]; 4]; 2]>(); let buffer = render_resource_context.create_buffer(BufferInfo { size, buffer_usage: BufferUsage::COPY_DST | BufferUsage::UNIFORM, @@ -114,9 +114,12 @@ pub fn camera_node_system( staging_buffer }; - let matrix_size = std::mem::size_of::<[[f32; 4]; 4]>(); - let camera_matrix: [f32; 16] = - (camera.projection_matrix * global_transform.compute_matrix().inverse()).to_cols_array(); + let matrix_size = std::mem::size_of::<[[[f32; 4]; 4]; 2]>(); + let view_matrix = global_transform.compute_matrix().inverse(); + let camera_matrix: [[f32; 16]; 2] = [ + (camera.projection_matrix * view_matrix).to_cols_array(), + view_matrix.to_cols_array() + ]; render_resource_context.write_mapped_buffer( staging_buffer, diff --git a/crates/bevy_render/src/render_graph/nodes/pass_node.rs b/crates/bevy_render/src/render_graph/nodes/pass_node.rs index 72f0dfb860be7..119b84a82c1ae 100644 --- a/crates/bevy_render/src/render_graph/nodes/pass_node.rs +++ b/crates/bevy_render/src/render_graph/nodes/pass_node.rs @@ -110,7 +110,7 @@ impl PassNode { index: 0, bind_type: BindType::Uniform { dynamic: false, - property: UniformProperty::Struct(vec![UniformProperty::Mat4]), + property: UniformProperty::Struct(vec![UniformProperty::Mat4, UniformProperty::Mat4]), }, shader_stage: BindingShaderStage::VERTEX | BindingShaderStage::FRAGMENT, }], From 21cf49cb70e4b9099282a01af69e8ef63ffeb3ec Mon Sep 17 00:00:00 2001 From: lwansbrough Date: Sun, 3 Jan 2021 13:19:05 -0800 Subject: [PATCH 2/4] Update shaders to include new uniform data --- assets/shaders/hot.vert | 1 + crates/bevy_pbr/src/render_graph/forward_pipeline/forward.frag | 1 + crates/bevy_pbr/src/render_graph/forward_pipeline/forward.vert | 1 + crates/bevy_render/src/shader/shader_reflect.rs | 1 + crates/bevy_sprite/src/render/sprite.vert | 1 + crates/bevy_sprite/src/render/sprite_sheet.vert | 1 + crates/bevy_ui/src/render/ui.vert | 1 + examples/shader/array_texture.rs | 1 + examples/shader/mesh_custom_attribute.rs | 1 + examples/shader/shader_custom_material.rs | 1 + examples/shader/shader_defs.rs | 1 + 11 files changed, 11 insertions(+) diff --git a/assets/shaders/hot.vert b/assets/shaders/hot.vert index 71a610e6e8463..11fa9321d3669 100644 --- a/assets/shaders/hot.vert +++ b/assets/shaders/hot.vert @@ -4,6 +4,7 @@ layout(location = 0) in vec3 Vertex_Position; layout(set = 0, binding = 0) uniform Camera { mat4 ViewProj; + mat4 View; }; layout(set = 1, binding = 0) uniform Transform { diff --git a/crates/bevy_pbr/src/render_graph/forward_pipeline/forward.frag b/crates/bevy_pbr/src/render_graph/forward_pipeline/forward.frag index c523b052fd44a..d882bd1741434 100644 --- a/crates/bevy_pbr/src/render_graph/forward_pipeline/forward.frag +++ b/crates/bevy_pbr/src/render_graph/forward_pipeline/forward.frag @@ -16,6 +16,7 @@ layout(location = 0) out vec4 o_Target; layout(set = 0, binding = 0) uniform Camera { mat4 ViewProj; + mat4 View; }; layout(set = 1, binding = 0) uniform Lights { diff --git a/crates/bevy_pbr/src/render_graph/forward_pipeline/forward.vert b/crates/bevy_pbr/src/render_graph/forward_pipeline/forward.vert index 028a86389c012..cb06377109313 100644 --- a/crates/bevy_pbr/src/render_graph/forward_pipeline/forward.vert +++ b/crates/bevy_pbr/src/render_graph/forward_pipeline/forward.vert @@ -10,6 +10,7 @@ layout(location = 2) out vec2 v_Uv; layout(set = 0, binding = 0) uniform Camera { mat4 ViewProj; + mat4 View; }; layout(set = 2, binding = 0) uniform Transform { diff --git a/crates/bevy_render/src/shader/shader_reflect.rs b/crates/bevy_render/src/shader/shader_reflect.rs index 13bfbeae38386..ad48ddd28b3fc 100644 --- a/crates/bevy_render/src/shader/shader_reflect.rs +++ b/crates/bevy_render/src/shader/shader_reflect.rs @@ -319,6 +319,7 @@ mod tests { layout(location = 0) out vec4 v_Position; layout(set = 0, binding = 0) uniform Camera { mat4 ViewProj; + mat4 View; }; layout(set = 1, binding = 0) uniform texture2D Texture; diff --git a/crates/bevy_sprite/src/render/sprite.vert b/crates/bevy_sprite/src/render/sprite.vert index fe1619d14b7fc..8556bb2875ea4 100644 --- a/crates/bevy_sprite/src/render/sprite.vert +++ b/crates/bevy_sprite/src/render/sprite.vert @@ -8,6 +8,7 @@ layout(location = 0) out vec2 v_Uv; layout(set = 0, binding = 0) uniform Camera { mat4 ViewProj; + mat4 View; }; layout(set = 2, binding = 0) uniform Transform { diff --git a/crates/bevy_sprite/src/render/sprite_sheet.vert b/crates/bevy_sprite/src/render/sprite_sheet.vert index e16012abef6fd..59c2334b9f7bb 100644 --- a/crates/bevy_sprite/src/render/sprite_sheet.vert +++ b/crates/bevy_sprite/src/render/sprite_sheet.vert @@ -9,6 +9,7 @@ layout(location = 1) out vec4 v_Color; layout(set = 0, binding = 0) uniform Camera { mat4 ViewProj; + mat4 View; }; // TODO: merge dimensions into "sprites" buffer when that is supported in the Uniforms derive abstraction diff --git a/crates/bevy_ui/src/render/ui.vert b/crates/bevy_ui/src/render/ui.vert index 2268af642b305..fe876b5c0c6ca 100644 --- a/crates/bevy_ui/src/render/ui.vert +++ b/crates/bevy_ui/src/render/ui.vert @@ -8,6 +8,7 @@ layout(location = 0) out vec2 v_Uv; layout(set = 0, binding = 0) uniform Camera { mat4 ViewProj; + mat4 View; }; layout(set = 1, binding = 0) uniform Transform { diff --git a/examples/shader/array_texture.rs b/examples/shader/array_texture.rs index 6e6308613ab5f..5905ca25d4cb6 100644 --- a/examples/shader/array_texture.rs +++ b/examples/shader/array_texture.rs @@ -34,6 +34,7 @@ layout(location = 0) out vec4 v_Position; layout(set = 0, binding = 0) uniform Camera { mat4 ViewProj; + mat4 View; }; layout(set = 1, binding = 0) uniform Transform { mat4 Model; diff --git a/examples/shader/mesh_custom_attribute.rs b/examples/shader/mesh_custom_attribute.rs index bd6f7562e5014..3ed530ebae986 100644 --- a/examples/shader/mesh_custom_attribute.rs +++ b/examples/shader/mesh_custom_attribute.rs @@ -31,6 +31,7 @@ layout(location = 0) out vec3 v_color; layout(set = 0, binding = 0) uniform Camera { mat4 ViewProj; + mat4 View; }; layout(set = 1, binding = 0) uniform Transform { mat4 Model; diff --git a/examples/shader/shader_custom_material.rs b/examples/shader/shader_custom_material.rs index 346a2c8338a80..6668aa5ef1420 100644 --- a/examples/shader/shader_custom_material.rs +++ b/examples/shader/shader_custom_material.rs @@ -30,6 +30,7 @@ const VERTEX_SHADER: &str = r#" layout(location = 0) in vec3 Vertex_Position; layout(set = 0, binding = 0) uniform Camera { mat4 ViewProj; + mat4 View; }; layout(set = 1, binding = 0) uniform Transform { mat4 Model; diff --git a/examples/shader/shader_defs.rs b/examples/shader/shader_defs.rs index e8b429c420875..451e410dd5798 100644 --- a/examples/shader/shader_defs.rs +++ b/examples/shader/shader_defs.rs @@ -38,6 +38,7 @@ const VERTEX_SHADER: &str = r#" layout(location = 0) in vec3 Vertex_Position; layout(set = 0, binding = 0) uniform Camera { mat4 ViewProj; + mat4 View; }; layout(set = 1, binding = 0) uniform Transform { mat4 Model; From 19eaf00dda01bba648d6beb559ae871617768407 Mon Sep 17 00:00:00 2001 From: lwansbrough Date: Sun, 3 Jan 2021 13:39:42 -0800 Subject: [PATCH 3/4] Clean up code --- crates/bevy_render/src/render_graph/nodes/pass_node.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/crates/bevy_render/src/render_graph/nodes/pass_node.rs b/crates/bevy_render/src/render_graph/nodes/pass_node.rs index 4458fc5820dc4..3d092a213412f 100644 --- a/crates/bevy_render/src/render_graph/nodes/pass_node.rs +++ b/crates/bevy_render/src/render_graph/nodes/pass_node.rs @@ -110,7 +110,10 @@ impl PassNode { index: 0, bind_type: BindType::Uniform { dynamic: false, - property: UniformProperty::Struct(vec![UniformProperty::Mat4, UniformProperty::Mat4]), + property: UniformProperty::Struct(vec![ + UniformProperty::Mat4, + UniformProperty::Mat4 + ]), }, shader_stage: BindingShaderStage::VERTEX | BindingShaderStage::FRAGMENT, }], From 09937beea5757729058e1be3b53f9a6ee06517e6 Mon Sep 17 00:00:00 2001 From: lwansbrough Date: Sun, 3 Jan 2021 13:52:12 -0800 Subject: [PATCH 4/4] Clean up --- crates/bevy_render/src/render_graph/nodes/pass_node.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/bevy_render/src/render_graph/nodes/pass_node.rs b/crates/bevy_render/src/render_graph/nodes/pass_node.rs index 3d092a213412f..3cea24d7268fc 100644 --- a/crates/bevy_render/src/render_graph/nodes/pass_node.rs +++ b/crates/bevy_render/src/render_graph/nodes/pass_node.rs @@ -111,8 +111,8 @@ impl PassNode { bind_type: BindType::Uniform { dynamic: false, property: UniformProperty::Struct(vec![ - UniformProperty::Mat4, - UniformProperty::Mat4 + UniformProperty::Mat4, // View Projection + UniformProperty::Mat4, // View ]), }, shader_stage: BindingShaderStage::VERTEX | BindingShaderStage::FRAGMENT,