From cb422ff5dd5506761f8af8cb6e5a514f07711838 Mon Sep 17 00:00:00 2001 From: shadow cat Date: Wed, 18 Sep 2024 21:17:52 -0400 Subject: [PATCH] specular lighting + reset zoom keybind --- src/client/handle_input.rs | 12 ++++++++++-- .../render/voxel/ray_oct/shader/compute.wgsl | 19 ++++++++++++++----- src/common/component/chunk/mod.rs | 2 +- 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/src/client/handle_input.rs b/src/client/handle_input.rs index 4dd91d0..7f5502b 100644 --- a/src/client/handle_input.rs +++ b/src/client/handle_input.rs @@ -2,9 +2,12 @@ use std::time::Duration; use nalgebra::Rotation3; use ndarray::Array3; -use winit::{keyboard::KeyCode as Key, window::CursorGrabMode}; +use winit::{event::MouseButton, keyboard::KeyCode as Key, window::CursorGrabMode}; -use crate::common::{component::{chunk, VoxelGrid, VoxelGridBundle}, ServerMessage}; +use crate::common::{ + component::{chunk, VoxelGrid, VoxelGridBundle}, + ServerMessage, +}; use super::{render::voxel::VoxelColor, Client}; @@ -73,6 +76,11 @@ impl Client<'_> { } } + if input.mouse_pressed(MouseButton::Middle) && input.pressed(Key::ControlLeft) { + state.camera_scroll = 0.0; + state.camera.scale = 0.2f32.exp(); + } + // camera position let move_dist = dt * state.speed.exp() * (chunk::SIDE_LENGTH / 16) as f32; if input.pressed(Key::KeyW) { diff --git a/src/client/render/voxel/ray_oct/shader/compute.wgsl b/src/client/render/voxel/ray_oct/shader/compute.wgsl index 7547b5a..74e9009 100644 --- a/src/client/render/voxel/ray_oct/shader/compute.wgsl +++ b/src/client/render/voxel/ray_oct/shader/compute.wgsl @@ -62,6 +62,8 @@ const MAX_ITERS = 2000; // NOTE: CANNOT GO HIGHER THAN 23 due to how floating point // numbers are stored and the bit manipulation used const MAX_SCALE: u32 = 13; +const AMBIENT: f32 = 0.2; +const SPECULAR: f32 = 0.5; fn trace_full(pos_view: vec4, dir_view: vec4) -> vec4 { let gi = 0; @@ -169,11 +171,18 @@ fn trace_full(pos_view: vec4, dir_view: vec4) -> vec4 { // let pos = t / t_mult; // if true {return vec4(pos, 1.0);} let vcolor = get_color(node & LEAF_MASK, pos); - let diffuse = max(dot(global_lights[0].dir, normals[axis]) + 0.1, 0.0); - let ambient = 0.2; - let lighting = max(diffuse, ambient); - let new_color = min(vcolor.xyz * lighting, vec3(1.0)); - color += vec4(new_color.xyz * vcolor.a, vcolor.a) * (1.0 - color.a); + let normal = normals[axis]; + let light_color = vec3(1.0); + let light_dir = global_lights[0].dir; + + let diffuse = max(dot(light_dir, normal), 0.0) * light_color; + let ambient = AMBIENT * light_color; + let spec_val = pow(max(dot(dir_view.xyz, reflect(-light_dir, normal)), 0.0), 32.0) * SPECULAR; + let specular = spec_val * light_color; + let new_color = (ambient + diffuse + specular) * vcolor.xyz; + let new_a = min(vcolor.a + spec_val, 1.0); + + color += vec4(new_color.xyz * new_a, new_a) * (1.0 - color.a); if color.a > FULL_ALPHA { break; } } prev = node; diff --git a/src/common/component/chunk/mod.rs b/src/common/component/chunk/mod.rs index 6946910..2db49b8 100644 --- a/src/common/component/chunk/mod.rs +++ b/src/common/component/chunk/mod.rs @@ -8,7 +8,7 @@ use bevy_derive::{Deref, DerefMut}; use bevy_ecs::{bundle::Bundle, component::Component, entity::Entity, system::Resource}; use nalgebra::Vector3; -pub const SCALE: u32 = 13; +pub const SCALE: u32 = 10; pub const SIDE_LENGTH: usize = 2usize.pow(SCALE); pub const SHAPE: (usize, usize, usize) = (SIDE_LENGTH, SIDE_LENGTH, SIDE_LENGTH); pub const DIMENSIONS: Vector3 = Vector3::new(SIDE_LENGTH, SIDE_LENGTH, SIDE_LENGTH);