From 32d493db1d897a61fd3e1170136fa3e812704837 Mon Sep 17 00:00:00 2001 From: metamuffin Date: Tue, 7 Jan 2025 00:00:55 +0100 Subject: monkey spin --- client/Cargo.toml | 1 + client/src/renderer.rs | 3 ++- client/src/scene_prepare.rs | 10 +++++++-- client/src/scene_render.rs | 52 ++++++++++++++++++++++++++++++++------------- client/src/shader.wgsl | 6 +++++- 5 files changed, 53 insertions(+), 19 deletions(-) (limited to 'client') diff --git a/client/Cargo.toml b/client/Cargo.toml index 3289cfd..4bcfb4c 100644 --- a/client/Cargo.toml +++ b/client/Cargo.toml @@ -15,3 +15,4 @@ wgpu = "23.0.1" winit = "0.30.8" weareshared = { path = "../shared" } rand = "0.9.0-beta.1" +glam = "0.29.2" diff --git a/client/src/renderer.rs b/client/src/renderer.rs index 34450b2..3561d87 100644 --- a/client/src/renderer.rs +++ b/client/src/renderer.rs @@ -43,7 +43,8 @@ impl<'a> Renderer<'a> { &DeviceDescriptor { required_features: Features::PUSH_CONSTANTS, required_limits: Limits { - max_push_constant_size: 48, + max_push_constant_size: 64, + max_vertex_buffers: 16, ..Limits::default() }, ..Default::default() diff --git a/client/src/scene_prepare.rs b/client/src/scene_prepare.rs index 47f168d..7b81c93 100644 --- a/client/src/scene_prepare.rs +++ b/client/src/scene_prepare.rs @@ -1,6 +1,6 @@ use crate::download::Downloader; use anyhow::{Context, Result}; -use log::{debug, info}; +use log::debug; use std::{ collections::{HashMap, HashSet}, sync::Arc, @@ -9,7 +9,6 @@ use weareshared::{ Affine3A, packets::{ReadWrite, Resource}, resources::{Attribute, Part, Prefab}, - store::sha256, }; use wgpu::{ Buffer, BufferUsages, Device, @@ -74,6 +73,13 @@ impl ScenePreparer { } for pres in &self.index_buffers_needed { if let Some(buf) = dls.try_get(*pres)? { + let buf = buf + .into_iter() + .array_chunks::<2>() + .map(u16::from_be_bytes) + .map(u16::to_le_bytes) + .flatten() + .collect::>(); let buffer = self.device.create_buffer_init(&BufferInitDescriptor { contents: &buf, label: None, 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::() 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(..)); diff --git a/client/src/shader.wgsl b/client/src/shader.wgsl index 68bd93d..d2b2d9f 100644 --- a/client/src/shader.wgsl +++ b/client/src/shader.wgsl @@ -15,10 +15,14 @@ struct VertexOut { @location(1) uv: vec2, } +var project: mat4x4; + @vertex fn vs_main(vi: VertexIn) -> VertexOut { + var clip = project * vec4(vi.x, vi.y, vi.z, -1.); + clip /= clip.w; let vo = VertexOut( - vec4(vi.x * 0.1, vi.y * 0.1, 0., 1.), + clip, vec3(vi.nx, vi.ny, vi.nz), vec2(vi.u, vi.v), ); -- cgit v1.2.3-70-g09d2