Better light path + random colors

This commit is contained in:
Lauri Räsänen 2023-11-04 21:20:18 +02:00
parent cd0a1cbd13
commit 139a568a6d
2 changed files with 13 additions and 7 deletions

View file

@ -173,7 +173,7 @@ impl State {
.try_into() .try_into()
.expect("failed to create light depth texture views"); .expect("failed to create light depth texture views");
let light_uniform = LightUniform::new([100.0, 150.0, 0.0], [1.0, 1.0, 1.0, 200000.0]); let light_uniform = LightUniform::new([0.0, 0.0, 0.0], [1.0, 1.0, 1.0, 250000.0]);
// We'll want to update our lights position, so we use COPY_DST // We'll want to update our lights position, so we use COPY_DST
let light_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor { let light_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
@ -468,7 +468,7 @@ impl State {
.process_events(window_event, device_event) .process_events(window_event, device_event)
} }
pub fn update(&mut self, dt: Duration) { pub fn update(&mut self, dt: Duration, time: Duration) {
// Update camera // Update camera
self.camera.update(dt, &self.camera_controller); self.camera.update(dt, &self.camera_controller);
self.camera_controller.reset(false); self.camera_controller.reset(false);
@ -480,11 +480,15 @@ impl State {
); );
// Update the light // Update the light
let old_position: cgmath::Vector3<_> = self.light_uniform.position.into(); self.light_uniform.position[0] = f32::sin(time.as_secs_f32() * 0.5) * 500.0;
self.light_uniform.position = self.light_uniform.position[1] = 300.0 + f32::sin(time.as_secs_f32() * 0.3) * 150.0;
(cgmath::Quaternion::from_angle_y(cgmath::Deg(90.0 * dt.as_secs_f32())) * old_position) self.light_uniform.position[2] = f32::sin(time.as_secs_f32() * 0.8) * 100.0;
.into();
self.light_uniform.update_matrices(); self.light_uniform.update_matrices();
self.light_uniform.color[0] = f32::abs(f32::sin(time.as_secs_f32() * 1.0));
self.light_uniform.color[1] = f32::abs(f32::sin(time.as_secs_f32() * 0.6));
self.light_uniform.color[2] = f32::abs(f32::sin(time.as_secs_f32() * 0.4));
self.queue.write_buffer( self.queue.write_buffer(
&self.light_buffer, &self.light_buffer,
0, 0,

View file

@ -44,6 +44,7 @@ pub async fn run() {
let mut state = State::new(&window).await; let mut state = State::new(&window).await;
let mut last_render = instant::Instant::now(); let mut last_render = instant::Instant::now();
let start_time = instant::Instant::now();
let mut is_focused = true; let mut is_focused = true;
// Event loop // Event loop
@ -57,9 +58,10 @@ pub async fn run() {
if window_id == window.id() => { if window_id == window.id() => {
let now = instant::Instant::now(); let now = instant::Instant::now();
let dt = now - last_render; let dt = now - last_render;
let time = now - start_time;
last_render = now; last_render = now;
if is_focused { if is_focused {
state.update(dt); state.update(dt, time);
match state.render() { match state.render() {
Ok(_) => { Ok(_) => {
window.request_redraw(); window.request_redraw();