diff --git a/simulator/src/main.rs b/simulator/src/main.rs index daaec28..21fb16a 100644 --- a/simulator/src/main.rs +++ b/simulator/src/main.rs @@ -24,12 +24,17 @@ pub async fn run() { struct RenderInstance { position: cgmath::Vector3, rotation: cgmath::Quaternion, + color: [f32; 3], } impl RenderInstance { fn to_raw(&self) -> InstanceRaw { - let model = cgmath::Matrix4::from_translation(self.position) * cgmath::Matrix4::from(self.rotation); - InstanceRaw { model: model.into() } + let model = cgmath::Matrix4::from_translation(self.position) + * 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)] struct InstanceRaw { model: [[f32; 4]; 4], + color: [f32; 3], } impl InstanceRaw { fn desc() -> wgpu::VertexBufferLayout<'static> { wgpu::VertexBufferLayout { - array_stride: size_of::() as wgpu::BufferAddress, + array_stride: std::mem::size_of::() as wgpu::BufferAddress, step_mode: wgpu::VertexStepMode::Instance, attributes: &[ 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: size_of::<[f32; 8]>() as wgpu::BufferAddress, 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: 16, shader_location: 6, format: wgpu::VertexFormat::Float32x4 }, + wgpu::VertexAttribute { offset: 32, shader_location: 7, 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 }, ], } } @@ -193,7 +200,7 @@ impl<'a> State<'a> { velocity: [0.0, 29780.0], mass: 5.972e24, }); - + let simulator = Arc::new(RwLock::new(sim)); let sim_clone = simulator.clone(); @@ -209,13 +216,18 @@ impl<'a> State<'a> { let instances = { let sim = 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, ), 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::>() }; @@ -255,9 +267,14 @@ impl<'a> State<'a> { fn update(&mut self) { let updated_instances: Vec = { 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), 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() }; diff --git a/simulator/src/shader.wgsl b/simulator/src/shader.wgsl index e97d14b..5c2fb18 100644 --- a/simulator/src/shader.wgsl +++ b/simulator/src/shader.wgsl @@ -8,6 +8,7 @@ struct InstanceInput { @location(6) model_row1: vec4, @location(7) model_row2: vec4, @location(8) model_row3: vec4, + @location(9) color: vec3, }; struct VSOutput { @@ -25,11 +26,11 @@ fn vs_main(vertex: VertexInput, instance: InstanceInput) -> VSOutput { instance.model_row3 ); out.position = model * vec4(vertex.position, 1.0); - out.color = vertex.color; + out.color = instance.color; return out; } @fragment fn fs_main(input: VSOutput) -> @location(0) vec4 { return vec4(input.color, 1.0); -} \ No newline at end of file +}