Added coloring
This commit is contained in:
parent
10fea1d695
commit
bdee0ceb3e
@ -24,12 +24,17 @@ pub async fn run() {
|
||||
struct RenderInstance {
|
||||
position: cgmath::Vector3<f32>,
|
||||
rotation: cgmath::Quaternion<f32>,
|
||||
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::<InstanceRaw>() as wgpu::BufferAddress,
|
||||
array_stride: std::mem::size_of::<InstanceRaw>() 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 },
|
||||
],
|
||||
}
|
||||
}
|
||||
@ -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::<Vec<_>>()
|
||||
};
|
||||
|
||||
@ -255,9 +267,14 @@ impl<'a> State<'a> {
|
||||
fn update(&mut self) {
|
||||
let updated_instances: Vec<RenderInstance> = {
|
||||
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()
|
||||
};
|
||||
|
||||
|
||||
@ -8,6 +8,7 @@ struct InstanceInput {
|
||||
@location(6) model_row1: vec4<f32>,
|
||||
@location(7) model_row2: vec4<f32>,
|
||||
@location(8) model_row3: vec4<f32>,
|
||||
@location(9) color: vec3<f32>,
|
||||
};
|
||||
|
||||
struct VSOutput {
|
||||
@ -25,7 +26,7 @@ fn vs_main(vertex: VertexInput, instance: InstanceInput) -> VSOutput {
|
||||
instance.model_row3
|
||||
);
|
||||
out.position = model * vec4<f32>(vertex.position, 1.0);
|
||||
out.color = vertex.color;
|
||||
out.color = instance.color;
|
||||
return out;
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user