diff options
Diffstat (limited to 'client/src/scene_render.rs')
-rw-r--r-- | client/src/scene_render.rs | 52 |
1 files changed, 37 insertions, 15 deletions
diff --git a/client/src/scene_render.rs b/client/src/scene_render.rs index b2cd915..b4965e1 100644 --- a/client/src/scene_render.rs +++ b/client/src/scene_render.rs @@ -1,3 +1,4 @@ +use glam::{EulerRot, Mat3, Mat4, Vec3, vec3}; use std::collections::{HashMap, HashSet}; use weareshared::{packets::Resource, tree::SceneTree}; use wgpu::{ @@ -17,6 +18,20 @@ pub struct ScenePipeline { bind_group: BindGroup, } +macro_rules! v_attr { + ($($n:literal),*) => { + [$(VertexBufferLayout { + step_mode: VertexStepMode::Vertex, + array_stride: 4, + attributes: &[VertexAttribute { + format: VertexFormat::Float32, + offset: 0, + shader_location: $n, + }], + }),*] + }; +} + impl ScenePipeline { pub fn new(device: &Device, format: TextureFormat) -> Self { let module = device.create_shader_module(include_wgsl!("shader.wgsl")); @@ -34,7 +49,7 @@ impl ScenePipeline { label: None, bind_group_layouts: &[&bind_group_layout], push_constant_ranges: &[PushConstantRange { - range: 0..(12 * 4), + range: 0..(4 * 4 * size_of::<f32>() as u32), stages: ShaderStages::VERTEX, }], }); @@ -54,15 +69,7 @@ impl ScenePipeline { vertex: VertexState { module: &module, entry_point: Some("vs_main"), - buffers: &[VertexBufferLayout { - step_mode: VertexStepMode::Vertex, - array_stride: 4, - attributes: &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9].map(|i| VertexAttribute { - format: VertexFormat::Float32, - offset: 0, - shader_location: i, - }), - }], + buffers: &v_attr!(0, 1, 2, 3, 4, 5, 6, 7), compilation_options: PipelineCompilationOptions::default(), }, primitive: PrimitiveState { @@ -98,9 +105,9 @@ impl ScenePipeline { ops: Operations { store: StoreOp::Store, load: LoadOp::Clear(Color { - r: 0.1, - g: 0.1, - b: 0.1, + r: 0.01, + g: 0.01, + b: 0.01, a: 1., }), }, @@ -108,13 +115,28 @@ impl ScenePipeline { ..Default::default() }); + let camera = Mat4::perspective_infinite_reverse_rh(1., 1., 0.1) + * Mat4::look_at_rh(vec3(-5., 0., 0.), vec3(0., 0., 0.), Vec3::Y); + for ob in scene.objects.values() { + let prefab_projection = camera + * Mat4::from_mat3(Mat3::from_euler( + EulerRot::YXZ, + ob.rot.x, + ob.rot.y, + ob.rot.z, + )) + * Mat4::from_translation(ob.pos.into()); if let Some(prefab) = prefabs.get(&ob.res) { for (affine, part) in &prefab.0 { - let affine = affine.to_cols_array().map(|v| v.to_le_bytes()); + let part_projection = prefab_projection + * Mat4::from_mat3a(affine.matrix3) + * Mat4::from_translation(affine.translation.into()); + let projection = part_projection.to_cols_array().map(|v| v.to_le_bytes()); + rpass.set_bind_group(0, &self.bind_group, &[]); rpass.set_pipeline(&self.pipeline); - rpass.set_push_constants(ShaderStages::VERTEX, 0, affine.as_flattened()); + rpass.set_push_constants(ShaderStages::VERTEX, 0, projection.as_flattened()); rpass.set_index_buffer(part.index.slice(..), IndexFormat::Uint16); rpass.set_vertex_buffer(0, part.position[0].slice(..)); rpass.set_vertex_buffer(1, part.position[1].slice(..)); |