diff --git a/Cargo.lock b/Cargo.lock index 26e84f9..c965981 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1661,7 +1661,7 @@ dependencies = [ [[package]] name = "wgpu-bootstrap" -version = "0.2.1" +version = "0.2.2" dependencies = [ "bytemuck", "cgmath", diff --git a/Cargo.toml b/Cargo.toml index cd40ab3..2ee2373 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "wgpu-bootstrap" -version = "0.2.1" +version = "0.2.2" edition = "2021" [dependencies] diff --git a/examples/cube/shader.wgsl b/examples/cube/shader.wgsl index 790ed71..b44a931 100644 --- a/examples/cube/shader.wgsl +++ b/examples/cube/shader.wgsl @@ -1,5 +1,6 @@ struct CameraUniform { - view_proj: mat4x4, + view: mat4x4, + proj: mat4x4, }; @group(0) @binding(0) var camera: CameraUniform; @@ -19,7 +20,7 @@ fn vs_main( ) -> VertexOutput { var out: VertexOutput; out.color = model.color; - out.clip_position = camera.view_proj * vec4(model.position, 1.0); + out.clip_position = camera.proj * camera.view * vec4(model.position, 1.0); return out; } diff --git a/examples/gui/shader.wgsl b/examples/gui/shader.wgsl index 790ed71..b44a931 100644 --- a/examples/gui/shader.wgsl +++ b/examples/gui/shader.wgsl @@ -1,5 +1,6 @@ struct CameraUniform { - view_proj: mat4x4, + view: mat4x4, + proj: mat4x4, }; @group(0) @binding(0) var camera: CameraUniform; @@ -19,7 +20,7 @@ fn vs_main( ) -> VertexOutput { var out: VertexOutput; out.color = model.color; - out.clip_position = camera.view_proj * vec4(model.position, 1.0); + out.clip_position = camera.proj * camera.view * vec4(model.position, 1.0); return out; } diff --git a/examples/instances/shader.wgsl b/examples/instances/shader.wgsl index ed0ba90..46a3d2e 100644 --- a/examples/instances/shader.wgsl +++ b/examples/instances/shader.wgsl @@ -1,5 +1,6 @@ struct CameraUniform { - view_proj: mat4x4, + view: mat4x4, + proj: mat4x4, }; @group(0) @binding(0) var camera: CameraUniform; @@ -24,7 +25,7 @@ fn vs_main( ) -> VertexOutput { var out: VertexOutput; out.color = model.color; - out.clip_position = camera.view_proj * vec4(model.position + instance.pos, 1.0); + out.clip_position = camera.proj * camera.view * vec4(model.position + instance.pos, 1.0); return out; } diff --git a/examples/shading/shader.wgsl b/examples/shading/shader.wgsl index 5d8f780..5caf9a8 100644 --- a/examples/shading/shader.wgsl +++ b/examples/shading/shader.wgsl @@ -1,5 +1,6 @@ struct CameraUniform { - view_proj: mat4x4, + view: mat4x4, + proj: mat4x4, }; @group(0) @binding(0) var camera: CameraUniform; @@ -24,7 +25,7 @@ fn vs_main( out.position = model.position; out.normal = model.normal; out.color = model.color; - out.clip_position = camera.view_proj * vec4(model.position, 1.0); + out.clip_position = camera.proj * camera.view * vec4(model.position, 1.0); return out; } diff --git a/examples/wireframe/shader.wgsl b/examples/wireframe/shader.wgsl index 790ed71..b44a931 100644 --- a/examples/wireframe/shader.wgsl +++ b/examples/wireframe/shader.wgsl @@ -1,5 +1,6 @@ struct CameraUniform { - view_proj: mat4x4, + view: mat4x4, + proj: mat4x4, }; @group(0) @binding(0) var camera: CameraUniform; @@ -19,7 +20,7 @@ fn vs_main( ) -> VertexOutput { var out: VertexOutput; out.color = model.color; - out.clip_position = camera.view_proj * vec4(model.position, 1.0); + out.clip_position = camera.proj * camera.view * vec4(model.position, 1.0); return out; } diff --git a/src/util/orbit_camera.rs b/src/util/orbit_camera.rs index dfda9ec..a93d609 100644 --- a/src/util/orbit_camera.rs +++ b/src/util/orbit_camera.rs @@ -19,25 +19,31 @@ pub const OPENGL_TO_WGPU_MATRIX: cgmath::Matrix4 = cgmath::Matrix4::new( pub struct CameraUniform { // We can't use cgmath with bytemuck directly so we'll have // to convert the Matrix4 into a 4x4 f32 array - view_proj: [[f32; 4]; 4], + view: [[f32; 4]; 4], + proj: [[f32; 4]; 4], } impl CameraUniform { pub fn new() -> Self { Self { - view_proj: cgmath::Matrix4::identity().into(), + view: cgmath::Matrix4::identity().into(), + proj: cgmath::Matrix4::identity().into(), } } - pub fn update_view_proj(&mut self, matrix: cgmath::Matrix4) { - self.view_proj = matrix.into(); + pub fn update_proj(&mut self, matrix: cgmath::Matrix4) { + self.proj = matrix.into(); + } + + pub fn update_view(&mut self, matrix: cgmath::Matrix4) { + self.view = matrix.into(); } pub fn desc() -> wgpu::BindGroupLayoutDescriptor<'static> { wgpu::BindGroupLayoutDescriptor { entries: &[wgpu::BindGroupLayoutEntry { binding: 0, - visibility: wgpu::ShaderStages::VERTEX, + visibility: wgpu::ShaderStages::VERTEX_FRAGMENT, ty: wgpu::BindingType::Buffer { ty: wgpu::BufferBindingType::Uniform, has_dynamic_offset: false, @@ -123,8 +129,9 @@ impl OrbitCamera { let pos = cgmath::Point3::from_vec(pos.to_vec() + self.target.to_vec()); let view = cgmath::Matrix4::look_at_rh(pos, self.target, self.up); - let projection_matrix = OPENGL_TO_WGPU_MATRIX * proj * view; - self.uniform.update_view_proj(projection_matrix); + let projection_matrix = OPENGL_TO_WGPU_MATRIX * proj; + self.uniform.update_proj(projection_matrix); + self.uniform.update_view(view); context .queue() .write_buffer(&self.buffer, 0, bytemuck::cast_slice(&[self.uniform]));