Skip to content

Commit 0b32af6

Browse files
committed
Pass View and Proj Matrices Separetely
1 parent 7f0ee9a commit 0b32af6

8 files changed

Lines changed: 31 additions & 19 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "wgpu-bootstrap"
3-
version = "0.2.1"
3+
version = "0.2.2"
44
edition = "2021"
55

66
[dependencies]

examples/cube/shader.wgsl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
struct CameraUniform {
2-
view_proj: mat4x4<f32>,
2+
view: mat4x4<f32>,
3+
proj: mat4x4<f32>,
34
};
45
@group(0) @binding(0) var<uniform> camera: CameraUniform;
56

@@ -19,7 +20,7 @@ fn vs_main(
1920
) -> VertexOutput {
2021
var out: VertexOutput;
2122
out.color = model.color;
22-
out.clip_position = camera.view_proj * vec4<f32>(model.position, 1.0);
23+
out.clip_position = camera.proj * camera.view * vec4<f32>(model.position, 1.0);
2324
return out;
2425
}
2526

examples/gui/shader.wgsl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
struct CameraUniform {
2-
view_proj: mat4x4<f32>,
2+
view: mat4x4<f32>,
3+
proj: mat4x4<f32>,
34
};
45
@group(0) @binding(0) var<uniform> camera: CameraUniform;
56

@@ -19,7 +20,7 @@ fn vs_main(
1920
) -> VertexOutput {
2021
var out: VertexOutput;
2122
out.color = model.color;
22-
out.clip_position = camera.view_proj * vec4<f32>(model.position, 1.0);
23+
out.clip_position = camera.proj * camera.view * vec4<f32>(model.position, 1.0);
2324
return out;
2425
}
2526

examples/instances/shader.wgsl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
struct CameraUniform {
2-
view_proj: mat4x4<f32>,
2+
view: mat4x4<f32>,
3+
proj: mat4x4<f32>,
34
};
45
@group(0) @binding(0) var<uniform> camera: CameraUniform;
56

@@ -24,7 +25,7 @@ fn vs_main(
2425
) -> VertexOutput {
2526
var out: VertexOutput;
2627
out.color = model.color;
27-
out.clip_position = camera.view_proj * vec4<f32>(model.position + instance.pos, 1.0);
28+
out.clip_position = camera.proj * camera.view * vec4<f32>(model.position + instance.pos, 1.0);
2829
return out;
2930
}
3031

examples/shading/shader.wgsl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
struct CameraUniform {
2-
view_proj: mat4x4<f32>,
2+
view: mat4x4<f32>,
3+
proj: mat4x4<f32>,
34
};
45
@group(0) @binding(0) var<uniform> camera: CameraUniform;
56

@@ -24,7 +25,7 @@ fn vs_main(
2425
out.position = model.position;
2526
out.normal = model.normal;
2627
out.color = model.color;
27-
out.clip_position = camera.view_proj * vec4<f32>(model.position, 1.0);
28+
out.clip_position = camera.proj * camera.view * vec4<f32>(model.position, 1.0);
2829
return out;
2930
}
3031

examples/wireframe/shader.wgsl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
struct CameraUniform {
2-
view_proj: mat4x4<f32>,
2+
view: mat4x4<f32>,
3+
proj: mat4x4<f32>,
34
};
45
@group(0) @binding(0) var<uniform> camera: CameraUniform;
56

@@ -19,7 +20,7 @@ fn vs_main(
1920
) -> VertexOutput {
2021
var out: VertexOutput;
2122
out.color = model.color;
22-
out.clip_position = camera.view_proj * vec4<f32>(model.position, 1.0);
23+
out.clip_position = camera.proj * camera.view * vec4<f32>(model.position, 1.0);
2324
return out;
2425
}
2526

src/util/orbit_camera.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,31 @@ pub const OPENGL_TO_WGPU_MATRIX: cgmath::Matrix4<f32> = cgmath::Matrix4::new(
1919
pub struct CameraUniform {
2020
// We can't use cgmath with bytemuck directly so we'll have
2121
// to convert the Matrix4 into a 4x4 f32 array
22-
view_proj: [[f32; 4]; 4],
22+
view: [[f32; 4]; 4],
23+
proj: [[f32; 4]; 4],
2324
}
2425

2526
impl CameraUniform {
2627
pub fn new() -> Self {
2728
Self {
28-
view_proj: cgmath::Matrix4::identity().into(),
29+
view: cgmath::Matrix4::identity().into(),
30+
proj: cgmath::Matrix4::identity().into(),
2931
}
3032
}
3133

32-
pub fn update_view_proj(&mut self, matrix: cgmath::Matrix4<f32>) {
33-
self.view_proj = matrix.into();
34+
pub fn update_proj(&mut self, matrix: cgmath::Matrix4<f32>) {
35+
self.proj = matrix.into();
36+
}
37+
38+
pub fn update_view(&mut self, matrix: cgmath::Matrix4<f32>) {
39+
self.view = matrix.into();
3440
}
3541

3642
pub fn desc() -> wgpu::BindGroupLayoutDescriptor<'static> {
3743
wgpu::BindGroupLayoutDescriptor {
3844
entries: &[wgpu::BindGroupLayoutEntry {
3945
binding: 0,
40-
visibility: wgpu::ShaderStages::VERTEX,
46+
visibility: wgpu::ShaderStages::VERTEX_FRAGMENT,
4147
ty: wgpu::BindingType::Buffer {
4248
ty: wgpu::BufferBindingType::Uniform,
4349
has_dynamic_offset: false,
@@ -123,8 +129,9 @@ impl OrbitCamera {
123129
let pos = cgmath::Point3::from_vec(pos.to_vec() + self.target.to_vec());
124130

125131
let view = cgmath::Matrix4::look_at_rh(pos, self.target, self.up);
126-
let projection_matrix = OPENGL_TO_WGPU_MATRIX * proj * view;
127-
self.uniform.update_view_proj(projection_matrix);
132+
let projection_matrix = OPENGL_TO_WGPU_MATRIX * proj;
133+
self.uniform.update_proj(projection_matrix);
134+
self.uniform.update_view(view);
128135
context
129136
.queue()
130137
.write_buffer(&self.buffer, 0, bytemuck::cast_slice(&[self.uniform]));

0 commit comments

Comments
 (0)