From 006ad7ce6d30d764411dcf8c2527f2c80e722491 Mon Sep 17 00:00:00 2001 From: metamuffin Date: Fri, 24 Jan 2025 21:18:55 +0100 Subject: minimal progress but diffuse lighting works again --- client/src/armature.rs | 7 +++++-- client/src/render/scene/draw.rs | 15 +++++++-------- client/src/shaders/fragment_pbr.wgsl | 10 +++++----- client/src/shaders/vertex_world.wgsl | 8 ++++---- client/src/shaders/vertex_world_skin.wgsl | 17 ++++++++++++----- 5 files changed, 33 insertions(+), 24 deletions(-) (limited to 'client/src') diff --git a/client/src/armature.rs b/client/src/armature.rs index abfc455..daf2725 100644 --- a/client/src/armature.rs +++ b/client/src/armature.rs @@ -19,6 +19,7 @@ use std::sync::Arc; use weareshared::resources::Armature; use wgpu::{Buffer, BufferDescriptor, BufferUsages, Device, Queue}; +const MAX_JOINTS: usize = 128; pub struct RArmature { pub joint_mat_uniform_buffer: Arc, joint_mat: Vec, @@ -30,7 +31,7 @@ impl RArmature { Self { joint_mat_uniform_buffer: Arc::new(device.create_buffer(&BufferDescriptor { label: Some("joint uniform"), - size: (armature.parent.as_ref().unwrap().len() * size_of::() * 16) as u64, + size: (MAX_JOINTS * size_of::() * 16) as u64, usage: BufferUsages::COPY_DST | BufferUsages::UNIFORM, mapped_at_creation: false, })), @@ -38,7 +39,9 @@ impl RArmature { joint_mat: vec![], } } - pub fn update(&mut self) {} + pub fn update(&mut self) { + self.joint_mat.fill(Mat4::IDENTITY); + } pub fn write_uniform(&self, queue: &Queue) { queue.write_buffer( &self.joint_mat_uniform_buffer, diff --git a/client/src/render/scene/draw.rs b/client/src/render/scene/draw.rs index bdbcb50..9efa561 100644 --- a/client/src/render/scene/draw.rs +++ b/client/src/render/scene/draw.rs @@ -63,8 +63,7 @@ impl ScenePipeline { }); for ob in scene.objects.values() { - let prefab_modelview = view - * Mat4::from_translation(ob.pos.into()) + let prefab_model = Mat4::from_translation(ob.pos.into()) * Mat4::from_mat3(Mat3::from_euler( EulerRot::YXZ, ob.rot.x, @@ -73,22 +72,22 @@ impl ScenePipeline { )); if let Some(prefab) = prefabs.try_get(ob.res.clone()) { for (affine, part) in &prefab.0 { - let modelview = prefab_modelview + let model = prefab_model * Mat4::from_translation(affine.translation.into()) * Mat4::from_mat3a(affine.matrix3); - let modelviewp = project * modelview; + let modelviewp = project * view * model; // let light = modelview * vec4(5., 5., 5., 1.); // let light = light.to_array().map(|v| v.to_le_bytes()); - let modelview = modelview.to_cols_array().map(|v| v.to_le_bytes()); - let modelviewp = modelviewp.to_cols_array().map(|v| v.to_le_bytes()); + let m = model.to_cols_array().map(|v| v.to_le_bytes()); + let mvp = modelviewp.to_cols_array().map(|v| v.to_le_bytes()); rpass.set_pipeline(&part.pipeline); rpass.set_bind_group(0, &*part.tex_albedo, &[]); rpass.set_bind_group(1, &*part.tex_normal, &[]); rpass.set_bind_group(2, &*part.material, &[]); - rpass.set_push_constants(ShaderStages::VERTEX, 0, modelviewp.as_flattened()); - rpass.set_push_constants(ShaderStages::VERTEX, 64, modelview.as_flattened()); + rpass.set_push_constants(ShaderStages::VERTEX, 0, mvp.as_flattened()); + rpass.set_push_constants(ShaderStages::VERTEX, 64, m.as_flattened()); // rpass.set_push_constants(ShaderStages::FRAGMENT, 128, light.as_flattened()); rpass.set_index_buffer(part.index.slice(..), IndexFormat::Uint32); rpass.set_vertex_buffer(0, part.va_position.slice(..)); diff --git a/client/src/shaders/fragment_pbr.wgsl b/client/src/shaders/fragment_pbr.wgsl index 60d7737..c8fb857 100644 --- a/client/src/shaders/fragment_pbr.wgsl +++ b/client/src/shaders/fragment_pbr.wgsl @@ -42,14 +42,14 @@ fn main(vo: VertexOut) -> @location(0) vec4 { let tangent_basis = mat3x3(vo.tangent, cross(vo.tangent, vo.normal), vo.normal); let normal = tangent_basis * (t_normal.rgb * 2. - 1.); - let light = vec3(0., 0., 0.); - let view = normalize(-vo.position); + let light = vec3(0.64, 0.64, 0.64); + // let view = normalize(-vo.position); let ambient = 0.1; - let diffuse = saturate(dot(light, normal)) * 0.7; - let specular = pow(saturate(dot(reflect(-light, normal), view)), 2.); + let diffuse = saturate(dot(light, normal)); + // let specular = pow(saturate(dot(reflect(-light, normal), view)), 2.); - let lighting = ambient + diffuse + specular; + let lighting = ambient + diffuse; let color = t_albedo.rgb * lighting; // let color = vec3(dot(normal, view) * 0.5 + 0.5) ; diff --git a/client/src/shaders/vertex_world.wgsl b/client/src/shaders/vertex_world.wgsl index e185289..4f342fd 100644 --- a/client/src/shaders/vertex_world.wgsl +++ b/client/src/shaders/vertex_world.wgsl @@ -29,7 +29,7 @@ struct VertexOut { struct PushConst { modelviewproject: mat4x4, - modelview: mat4x4, + model: mat4x4, } var pc: PushConst; @@ -39,10 +39,10 @@ fn main(vi: VertexIn) -> VertexOut { let clip = pc.modelviewproject * vec4(vi.position, 1.); let vo = VertexOut( clip, - normalize((pc.modelview * vec4(vi.normal, 0.)).xyz), - normalize((pc.modelview * vec4(vi.tangent, 0.)).xyz), + normalize((pc.model * vec4(vi.normal, 0.)).xyz), + normalize((pc.model * vec4(vi.tangent, 0.)).xyz), vi.texcoord, - (pc.modelview * vec4(vi.position, 1.)).xyz, + (pc.model * vec4(vi.position, 1.)).xyz, ); return vo; } diff --git a/client/src/shaders/vertex_world_skin.wgsl b/client/src/shaders/vertex_world_skin.wgsl index 633f06b..6e2b308 100644 --- a/client/src/shaders/vertex_world_skin.wgsl +++ b/client/src/shaders/vertex_world_skin.wgsl @@ -31,7 +31,7 @@ struct VertexOut { struct PushConst { modelviewproject: mat4x4, - modelview: mat4x4, + model: mat4x4, } @group(3) @binding(0) var joints: array, 128>; @@ -39,13 +39,20 @@ var pc: PushConst; @vertex fn main(vi: VertexIn) -> VertexOut { - let clip = pc.modelview * vec4(vi.position, 1.); + let pos_in = vec4(vi.position, 1.); + let j0 = vi.joint_weight.x * (joints[vi.joint_index.x] * pos_in); + let j1 = vi.joint_weight.y * (joints[vi.joint_index.y] * pos_in); + let j2 = vi.joint_weight.z * (joints[vi.joint_index.z] * pos_in); + let j3 = vi.joint_weight.w * (joints[vi.joint_index.w] * pos_in); + let position = j0 + j1 + j2 + j3; + + let clip = pc.modelviewproject * position; let vo = VertexOut( clip, - normalize((pc.modelview * vec4(vi.normal, 0.)).xyz), - normalize((pc.modelview * vec4(vi.tangent, 0.)).xyz), + normalize((pc.model * vec4(vi.normal, 0.)).xyz), + normalize((pc.model * vec4(vi.tangent, 0.)).xyz), vi.texcoord, - (pc.modelview * vec4(vi.position, 1.)).xyz, + (pc.model * vec4(vi.position, 1.)).xyz, ); return vo; } -- cgit v1.2.3-70-g09d2