Added coloring

This commit is contained in:
Verox001 2025-05-04 12:51:41 +02:00
parent 10fea1d695
commit bdee0ceb3e
2 changed files with 29 additions and 11 deletions

View File

@ -24,12 +24,17 @@ pub async fn run() {
struct RenderInstance { struct RenderInstance {
position: cgmath::Vector3<f32>, position: cgmath::Vector3<f32>,
rotation: cgmath::Quaternion<f32>, rotation: cgmath::Quaternion<f32>,
color: [f32; 3],
} }
impl RenderInstance { impl RenderInstance {
fn to_raw(&self) -> InstanceRaw { fn to_raw(&self) -> InstanceRaw {
let model = cgmath::Matrix4::from_translation(self.position) * cgmath::Matrix4::from(self.rotation); let model = cgmath::Matrix4::from_translation(self.position)
InstanceRaw { model: model.into() } * cgmath::Matrix4::from(self.rotation);
InstanceRaw {
model: model.into(),
color: self.color
}
} }
} }
@ -37,18 +42,20 @@ impl RenderInstance {
#[derive(Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)] #[derive(Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
struct InstanceRaw { struct InstanceRaw {
model: [[f32; 4]; 4], model: [[f32; 4]; 4],
color: [f32; 3],
} }
impl InstanceRaw { impl InstanceRaw {
fn desc() -> wgpu::VertexBufferLayout<'static> { fn desc() -> wgpu::VertexBufferLayout<'static> {
wgpu::VertexBufferLayout { wgpu::VertexBufferLayout {
array_stride: size_of::<InstanceRaw>() as wgpu::BufferAddress, array_stride: std::mem::size_of::<InstanceRaw>() as wgpu::BufferAddress,
step_mode: wgpu::VertexStepMode::Instance, step_mode: wgpu::VertexStepMode::Instance,
attributes: &[ attributes: &[
wgpu::VertexAttribute { offset: 0, shader_location: 5, format: wgpu::VertexFormat::Float32x4 }, wgpu::VertexAttribute { offset: 0, shader_location: 5, format: wgpu::VertexFormat::Float32x4 },
wgpu::VertexAttribute { offset: size_of::<[f32; 4]>() as wgpu::BufferAddress, shader_location: 6, format: wgpu::VertexFormat::Float32x4 }, wgpu::VertexAttribute { offset: 16, shader_location: 6, format: wgpu::VertexFormat::Float32x4 },
wgpu::VertexAttribute { offset: size_of::<[f32; 8]>() as wgpu::BufferAddress, shader_location: 7, format: wgpu::VertexFormat::Float32x4 }, wgpu::VertexAttribute { offset: 32, shader_location: 7, format: wgpu::VertexFormat::Float32x4 },
wgpu::VertexAttribute { offset: size_of::<[f32; 12]>() as wgpu::BufferAddress, shader_location: 8, format: wgpu::VertexFormat::Float32x4 }, wgpu::VertexAttribute { offset: 48, shader_location: 8, format: wgpu::VertexFormat::Float32x4 },
wgpu::VertexAttribute { offset: 64, shader_location: 9, format: wgpu::VertexFormat::Float32x3 },
], ],
} }
} }
@ -209,13 +216,18 @@ impl<'a> State<'a> {
let instances = { let instances = {
let sim = simulator.read().unwrap(); let sim = simulator.read().unwrap();
sim.bodies.iter().map(|b| RenderInstance { sim.bodies.iter().enumerate().map(|(i, b)| RenderInstance {
position: cgmath::Vector3::new( position: cgmath::Vector3::new(
(b.position[0] / 1.496e11) as f32, (b.position[0] / 1.496e11) as f32,
(b.position[1] / 1.496e11) as f32, (b.position[1] / 1.496e11) as f32,
0.0, 0.0,
), ),
rotation: cgmath::Quaternion::from_angle_z(cgmath::Deg(0.0)), rotation: cgmath::Quaternion::from_angle_z(cgmath::Deg(0.0)),
color: match i {
0 => [1.0, 1.0, 0.0],
1 => [0.0, 0.0, 1.0],
_ => [0.5, 0.5, 0.5],
},
}).collect::<Vec<_>>() }).collect::<Vec<_>>()
}; };
@ -255,9 +267,14 @@ impl<'a> State<'a> {
fn update(&mut self) { fn update(&mut self) {
let updated_instances: Vec<RenderInstance> = { let updated_instances: Vec<RenderInstance> = {
let sim = self.simulator.read().unwrap(); let sim = self.simulator.read().unwrap();
sim.bodies.iter().map(|b| RenderInstance { sim.bodies.iter().enumerate().map(|(i, b)| RenderInstance {
position: cgmath::Vector3::new((b.position[0] / 1.496e11) as f32, (b.position[1] / 1.496e11) as f32, 0.0), position: cgmath::Vector3::new((b.position[0] / 1.496e11) as f32, (b.position[1] / 1.496e11) as f32, 0.0),
rotation: cgmath::Quaternion::from_angle_z(cgmath::Deg(0.0)), rotation: cgmath::Quaternion::from_angle_z(cgmath::Deg(0.0)),
color: match i {
0 => [1.0, 1.0, 0.0],
1 => [0.0, 0.0, 1.0],
_ => [0.5, 0.5, 0.5],
},
}).collect() }).collect()
}; };

View File

@ -8,6 +8,7 @@ struct InstanceInput {
@location(6) model_row1: vec4<f32>, @location(6) model_row1: vec4<f32>,
@location(7) model_row2: vec4<f32>, @location(7) model_row2: vec4<f32>,
@location(8) model_row3: vec4<f32>, @location(8) model_row3: vec4<f32>,
@location(9) color: vec3<f32>,
}; };
struct VSOutput { struct VSOutput {
@ -25,7 +26,7 @@ fn vs_main(vertex: VertexInput, instance: InstanceInput) -> VSOutput {
instance.model_row3 instance.model_row3
); );
out.position = model * vec4<f32>(vertex.position, 1.0); out.position = model * vec4<f32>(vertex.position, 1.0);
out.color = vertex.color; out.color = instance.color;
return out; return out;
} }