From d9e8b96754e5ca154835c7e9c65f745135ff742a Mon Sep 17 00:00:00 2001 From: Florian RICHER Date: Tue, 14 Jun 2022 13:08:10 +0200 Subject: [PATCH] [INSTANCE] Update instances --- engine_core/src/state.rs | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/engine_core/src/state.rs b/engine_core/src/state.rs index e874d20..a7c53ca 100644 --- a/engine_core/src/state.rs +++ b/engine_core/src/state.rs @@ -35,6 +35,9 @@ const INSTANCE_DISPLACEMENT: cgmath::Vector3 = cgmath::Vector3::new( NUM_INSTANCES_PER_ROW as f32 * 0.5, ); +const FRAME_TIME: f32 = 1.0 / 60.0; +const ROTATION_SPEED: f32 = std::f32::consts::PI * FRAME_TIME * 0.5; + pub struct State { pub surface: wgpu::Surface, pub device: wgpu::Device, @@ -310,7 +313,7 @@ impl State { let instance_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor { label: Some("Instance Buffer"), contents: bytemuck::cast_slice(&instance_data), - usage: wgpu::BufferUsages::VERTEX, + usage: wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::COPY_DST, }); let render_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { @@ -418,6 +421,22 @@ impl State { 0, bytemuck::cast_slice(&[self.camera_uniform]), ); + + for instance in &mut self.instances { + let amount = cgmath::Quaternion::from_angle_y(cgmath::Rad(ROTATION_SPEED)); + let current = instance.rotation; + instance.rotation = amount * current; + } + let instance_data = self + .instances + .iter() + .map(super::instance::Instance::to_raw) + .collect::>(); + self.queue.write_buffer( + &self.instance_buffer, + 0, + bytemuck::cast_slice(&instance_data), + ); } pub fn render(&mut self) -> Result<(), wgpu::SurfaceError> {