From ec22c4a6e4cd8ba00a90e83346ff33bf23e60a43 Mon Sep 17 00:00:00 2001 From: numzero Date: Mon, 19 Oct 2026 02:08:10 +0300 Subject: [PATCH] remove per-vertex color --- src/crown_gear.rs | 2 -- src/lib.rs | 5 +++-- src/render/mesh.rs | 9 ++------- src/render/mesh.wgsl | 12 +++++------- src/render/mesh/cpu.rs | 8 ++------ 5 files changed, 12 insertions(+), 24 deletions(-) diff --git a/src/crown_gear.rs b/src/crown_gear.rs index 66c6ba9..137ec1a 100644 --- a/src/crown_gear.rs +++ b/src/crown_gear.rs @@ -24,7 +24,6 @@ pub fn make_mesh_smooth( desc: &CrownGearDesc, visual: &CrownGearVisual, sides: usize, - color: Vec3, ) -> MeshIndexed { let teeth = 2.0 * desc.pitch_radius; let inner_scale = visual.inner_radius / desc.pitch_radius; @@ -50,7 +49,6 @@ pub fn make_mesh_smooth( let vertex = |x, z, n| Vertex { position: rot * vec3(x, 0.0, z), - color, normal: n, }; mesh.vertices.extend([ diff --git a/src/lib.rs b/src/lib.rs index 9a92840..2312f8f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -111,7 +111,7 @@ impl Core { 600, ) .to_mesh() - .to_standard_mesh(vec3(0.5, 0.5, 0.5)) + .to_standard_mesh() .into_indexed(); let bevel_gear_desc = bevel_gear::BevelGearDesc { @@ -128,7 +128,7 @@ impl Core { &bevel_gear::BevelGearShape::new_blank(&bevel_gear_desc, 60), ) .to_mesh() - .to_standard_mesh(vec3(0.5, 0.5, 0.5)) + .to_standard_mesh() .into_indexed(); let mesh_gray = render::mesh::GpuMeshIndexed::new(&device, &bevel_gear_mesh); @@ -168,6 +168,7 @@ impl Core { mvp: proj * view, light: vec3(0.7, 0.5, 1.0).normalize(), _zero: 0., + color: vec4(0.5, 0.5, 0.5, 1.0), }, ); self.queue.submit([]); // flush buffer updates diff --git a/src/render/mesh.rs b/src/render/mesh.rs index 06e3cdd..28446a8 100644 --- a/src/render/mesh.rs +++ b/src/render/mesh.rs @@ -1,7 +1,7 @@ use std::mem::offset_of; use bytemuck::{Pod, Zeroable, bytes_of, cast_slice}; -use glam::{Mat4, Vec3}; +use glam::{Mat4, Vec3, Vec4}; use wgpu::util::DeviceExt; pub use cpu::*; @@ -14,7 +14,6 @@ static SHADER: &str = include_str!("mesh.wgsl"); #[repr(C)] pub struct Vertex { pub position: Vec3, - pub color: Vec3, pub normal: Vec3, } @@ -24,6 +23,7 @@ pub struct Params { pub mvp: Mat4, pub light: Vec3, pub _zero: f32, + pub color: Vec4, } pub struct GpuMesh { @@ -121,11 +121,6 @@ impl Pipeline { }, wgpu::VertexAttribute { shader_location: 1, - offset: offset_of!(Vertex, color) as u64, - format: wgpu::VertexFormat::Float32x3, - }, - wgpu::VertexAttribute { - shader_location: 2, offset: offset_of!(Vertex, normal) as u64, format: wgpu::VertexFormat::Float32x3, }, diff --git a/src/render/mesh.wgsl b/src/render/mesh.wgsl index 14b3acc..35ea427 100644 --- a/src/render/mesh.wgsl +++ b/src/render/mesh.wgsl @@ -2,18 +2,17 @@ struct Params { mvp: mat4x4f, light: vec3f, _zero: f32, + color: vec4f, } struct Vertex { @location(0) position: vec3f, - @location(1) color: vec3f, - @location(2) normal: vec3f, + @location(1) normal: vec3f, } struct Varying { @builtin(position) screen: vec4f, - @location(0) color: vec4f, - @location(1) normal: vec3f, + @location(0) normal: vec3f, } @group(0) @binding(0) var params: Params; @@ -21,9 +20,8 @@ struct Varying { @vertex fn on_vertex(in: Vertex) -> Varying { let position = params.mvp * vec4f(in.position, 1.0); - let color = vec4f(in.color, 1.0); let normal = in.normal; - return Varying(position, color, normal); + return Varying(position, normal); } @fragment @@ -31,6 +29,6 @@ fn on_fragment(in: Varying) -> @location(0) vec4f { let normal = normalize(in.normal); let light = dot(params.light, normal); let u = 0.5 + 0.5 * light; - let color = vec4f(u * in.color.xyz, in.color.w); + let color = vec4f(u * params.color.xyz, params.color.w); return color; } diff --git a/src/render/mesh/cpu.rs b/src/render/mesh/cpu.rs index 4d369b6..6ad30c3 100644 --- a/src/render/mesh/cpu.rs +++ b/src/render/mesh/cpu.rs @@ -46,7 +46,7 @@ impl Mesh { } impl Mesh { - pub fn to_standard_mesh(&self, color: Vec3) -> CpuMesh { + pub fn to_standard_mesh(&self) -> CpuMesh { CpuMesh { faces: self .faces @@ -54,11 +54,7 @@ impl Mesh { .map(|face| { let [a, b, c] = face; let normal = (b - a).cross(c - a).normalize(); - face.map(|position| Vertex { - position, - color, - normal, - }) + face.map(|position| Vertex { position, normal }) }) .collect(), }