clean up shader

This commit is contained in:
2024-06-05 17:35:59 -04:00
parent 44a37114e3
commit 02f5a94fe5
4 changed files with 65 additions and 115 deletions

View File

@@ -1,11 +1,7 @@
use std::{f32::consts::PI, time::Duration}; use std::time::Duration;
use nalgebra::Rotation3; use nalgebra::Rotation3;
use winit::{ use winit::{dpi::PhysicalPosition, keyboard::KeyCode as Key, window::CursorGrabMode};
dpi::{LogicalPosition, PhysicalPosition},
keyboard::KeyCode as Key,
window::CursorGrabMode,
};
use super::Client; use super::Client;
@@ -27,11 +23,13 @@ impl Client<'_> {
self.keep_cursor = true; self.keep_cursor = true;
window.set_cursor_grab(CursorGrabMode::Confined) window.set_cursor_grab(CursorGrabMode::Confined)
}) })
.expect("wah"); .expect("cursor lock");
} else { } else {
self.keep_cursor = false; self.keep_cursor = false;
window.set_cursor_visible(true); window.set_cursor_visible(true);
window.set_cursor_grab(CursorGrabMode::None).expect("wah"); window
.set_cursor_grab(CursorGrabMode::None)
.expect("cursor unlock");
}; };
} }
return; return;
@@ -42,21 +40,18 @@ impl Client<'_> {
let size = window.inner_size(); let size = window.inner_size();
window window
.set_cursor_position(PhysicalPosition::new(size.width / 2, size.height / 2)) .set_cursor_position(PhysicalPosition::new(size.width / 2, size.height / 2))
.expect("wah"); .expect("cursor move");
} }
} }
let delta = input.mouse_delta; let delta = input.mouse_delta * 0.003;
if delta.x != 0.0 { if delta.x != 0.0 {
state.camera.orientation = Rotation3::from_axis_angle( state.camera.orientation = Rotation3::from_axis_angle(&state.camera.up(), delta.x)
&state.camera.up(), * state.camera.orientation;
(delta.x * 0.003).clamp(-PI, PI),
) * state.camera.orientation;
} }
if delta.y != 0.0 { if delta.y != 0.0 {
state.camera.orientation = Rotation3::from_axis_angle( state.camera.orientation =
&state.camera.right(), Rotation3::from_axis_angle(&state.camera.right(), delta.y)
(delta.y * 0.003).clamp(-PI, PI), * state.camera.orientation;
) * state.camera.orientation;
} }
} }
let rot_dist = 1.0 * dt; let rot_dist = 1.0 * dt;
@@ -95,13 +90,5 @@ impl Client<'_> {
if input.pressed(Key::ShiftLeft) { if input.pressed(Key::ShiftLeft) {
state.camera.pos += *state.camera.down() * move_dist; state.camera.pos += *state.camera.down() * move_dist;
} }
if input.pressed(Key::KeyZ) {
state.camera_scroll += dt * 10.0;
state.camera.scale = (state.camera_scroll * 0.1).exp();
}
if input.pressed(Key::KeyX) {
state.camera_scroll -= dt * 10.0;
state.camera.scale = (state.camera_scroll * 0.1).exp();
}
} }
} }

View File

@@ -5,6 +5,7 @@ use nalgebra::{Transform3, Vector3};
pub struct VoxelGroup { pub struct VoxelGroup {
pub transform: Transform3<f32>, pub transform: Transform3<f32>,
pub dimensions: Vector3<u32>, pub dimensions: Vector3<u32>,
pub offset: u32,
} }
unsafe impl bytemuck::Pod for VoxelGroup {} unsafe impl bytemuck::Pod for VoxelGroup {}

View File

@@ -155,6 +155,7 @@ impl VoxelPipeline {
let group = VoxelGroup { let group = VoxelGroup {
transform: Transform3::identity() * thing.inverse(), transform: Transform3::identity() * thing.inverse(),
dimensions: Vector3::new(lx as u32, ly as u32, lz as u32), dimensions: Vector3::new(lx as u32, ly as u32, lz as u32),
offset: 0,
}; };
self.voxel_groups.update( self.voxel_groups.update(
device, device,

View File

@@ -16,6 +16,7 @@ struct View {
struct VoxelGroup { struct VoxelGroup {
transform: mat4x4<f32>, transform: mat4x4<f32>,
dimensions: vec3<u32>, dimensions: vec3<u32>,
offset: u32,
}; };
@group(0) @binding(0) @group(0) @binding(0)
@@ -43,106 +44,82 @@ fn vs_main(
// Fragment shader // Fragment shader
const n0 = vec3<f32>(1.0, 0.0, 0.0); const X_AXIS = vec3<f32>(1.0, 0.0, 0.0);
const n1 = -n0; const Y_AXIS = vec3<f32>(0.0, 1.0, 0.0);
const n2 = vec3<f32>(0.0, 1.0, 0.0); const Z_AXIS = vec3<f32>(0.0, 0.0, 1.0);
const n3 = -n2; const AXIS = mat3x3<f32>(X_AXIS, Y_AXIS, Z_AXIS);
const n4 = vec3<f32>(0.0, 0.0, 1.0);
const n5 = -n4;
const ORIGIN = vec3<f32>(0.0, 0.0, 0.0); const ZERO3F = vec3<f32>(0.0);
const ORIGIN2 = vec2<f32>(0.0, 0.0); const ZERO2F = vec2<f32>(0.0);
const NO_COLOR = vec4<f32>(0.0, 0.0, 0.0, 0.0); const NO_COLOR = vec4<f32>(0.0);
@fragment @fragment
fn fs_main( fn fs_main(
in: VertexOutput, in: VertexOutput,
) -> @location(0) vec4<f32> { ) -> @location(0) vec4<f32> {
let aspect = f32(view.height) / f32(view.width); // get position of the pixel; eye at origin, pixel on plane z = 1
var pos = vec3<f32>(in.clip_position.x / f32(view.width), 1.0 - in.clip_position.y / f32(view.height), 1.0); let win_dim = vec2<f32>(f32(view.width), f32(view.height));
pos.x -= 0.5; let aspect = win_dim.y / win_dim.x;
pos.y -= 0.5; let pixel_pos = vec3<f32>(
pos.x *= 2.0; (in.clip_position.xy / win_dim - vec2<f32>(0.5)) * vec2<f32>(2.0, -2.0 * aspect),
pos.y *= 2.0; 1.0
pos.y *= aspect; );
let group = voxel_groups[0]; let group = voxel_groups[0];
let dim_f = vec3<f32>(group.dimensions); let dim_f = vec3<f32>(group.dimensions);
let dim_i = vec3<i32>(group.dimensions);
// this should definitely be done per pixel trust me guys // transform position so that group is at 0,0 & find direction
let transform = group.transform * view.transform; let transform = group.transform * view.transform;
pos = (transform * vec4<f32>(pos, 1.0)).xyz; let dir = (transform * vec4<f32>(normalize(pixel_pos), 0.0)).xyz;
let origin = (transform * vec4<f32>(ORIGIN, 1.0)).xyz; var pos = (transform * vec4<f32>(pixel_pos, 1.0)).xyz;
let dir = normalize(pos - origin);
var p = ORIGIN; // find where ray intersects with group
let plane_point = (vec3<f32>(1.0) - sign(dir)) / 2.0 * dim_f;
if outside3f(pos, ZERO3F, dim_f) {
// x = td + p, solve for t
let t = (plane_point - pos) / dir;
// points of intersection
let px = pos + t.x * dir;
let py = pos + t.y * dir;
let pz = pos + t.z * dir;
var na: vec3<f32>; // check if point is in bounds
var nb: vec3<f32>; let hit = vec3<bool>(
var nc: vec3<f32>; inside2f(px.yz, ZERO2F, dim_f.yz),
if dot(dir, n0) < 0.0 { inside2f(py.xz, ZERO2F, dim_f.xz),
na = n0; inside2f(pz.xy, ZERO2F, dim_f.xy),
p.x = 1.0; ) && (t > ZERO3F);
} else { if !any(hit) {
na = n1;
}
if dot(dir, n2) < 0.0 {
nb = n2;
p.y = 1.0;
} else {
nb = n3;
}
if dot(dir, n4) < 0.0 {
nc = n4;
p.z = 1.0;
} else {
nc = n5;
}
p *= dim_f;
var vox_pos: vec3<i32>;
var offset = ORIGIN;
let dir_if = sign(dir) * ceil(abs(dir));
if outside(pos, ORIGIN, dim_f) {
let ta = intersect(pos, dir, p, na);
let tb = intersect(pos, dir, p, nb);
let tc = intersect(pos, dir, p, nc);
let pa = pos + ta * dir;
let pb = pos + tb * dir;
let pc = pos + tc * dir;
if inside2(pa.yz, ORIGIN2, dim_f.yz) && ta > 0.0 {
pos = pa;
} else if inside2(pb.xz, ORIGIN2, dim_f.xz) && tb > 0.0 {
pos = pb;
} else if inside2(pc.xy, ORIGIN2, dim_f.xy) && tc > 0.0 {
pos = pc;
} else {
return NO_COLOR; return NO_COLOR;
} }
pos = select(select(pz, py, hit.y), px, hit.x);
} }
vox_pos = vec3<i32>(pos); var vox_pos = clamp(vec3<i32>(pos), vec3<i32>(0), dim_i - vec3<i32>(1));
let dim_i = vec3<i32>(group.dimensions);
vox_pos = clamp(vox_pos, vec3<i32>(0, 0, 0), dim_i - vec3<i32>(1, 1, 1));
let dir_if = sign(dir) * ceil(abs(dir));
let dir_i = vec3<i32>(dir_if); let dir_i = vec3<i32>(dir_if);
// time to move 1 unit using dir
let inc_t = abs(1.0 / dir); let inc_t = abs(1.0 / dir);
let corner = vec3<f32>(vox_pos) + vec3<f32>(0.5, 0.5, 0.5) + dir_if / 2.0; let corner = vec3<f32>(vox_pos) + vec3<f32>(0.5, 0.5, 0.5) + dir_if / 2.0;
// time of next plane hit for each direction
var next_t = inc_t * abs(pos - corner); var next_t = inc_t * abs(pos - corner);
var color = NO_COLOR; var color = NO_COLOR;
var t = 0; var safety = 0;
loop { loop {
let i = u32(vox_pos.x + vox_pos.y * dim_i.x + vox_pos.z * dim_i.x * dim_i.y); let i = u32(vox_pos.x + vox_pos.y * dim_i.x + vox_pos.z * dim_i.x * dim_i.y) + group.offset;
let vcolor = unpack4x8unorm(voxels[i]); let vcolor = unpack4x8unorm(voxels[i]);
color += vec4<f32>(vcolor.xyz * vcolor.a * (1.0 - color.a), (1.0 - color.a) * vcolor.a); color += vec4<f32>(vcolor.xyz * vcolor.a * (1.0 - color.a), (1.0 - color.a) * vcolor.a);
if color.a >= 1.0 { if color.a >= 1.0 {
return color; return color;
} }
// select next voxel to move to next based on least time
if next_t.x < next_t.y && next_t.x < next_t.z { if next_t.x < next_t.y && next_t.x < next_t.z {
vox_pos.x += dir_i.x; vox_pos.x += dir_i.x;
next_t.x += inc_t.x; next_t.x += inc_t.x;
@@ -163,34 +140,18 @@ fn fs_main(
} }
} }
t += 1; safety += 1;
if t > 1000 { if safety > 1000 {
break; break;
} }
} }
return vec4<f32>(1.0, 0.0, 0.0, 1.0); return vec4<f32>(1.0, 0.0, 0.0, 1.0);
} }
fn intersect(lp: vec3<f32>, ld: vec3<f32>, pp: vec3<f32>, pn: vec3<f32>) -> f32 { fn outside3f(v: vec3<f32>, low: vec3<f32>, high: vec3<f32>) -> bool {
let v = pn * (lp - pp); return any(v < low) || any(v > high);
let a = v.x + v.y + v.z;
let u = pn * ld;
let b = u.x + u.y + u.z;
return -a / b;
} }
fn outside(v: vec3<f32>, low: vec3<f32>, high: vec3<f32>) -> bool { fn inside2f(v: vec2<f32>, low: vec2<f32>, high: vec2<f32>) -> bool {
return v.x < low.x || v.y < low.y || v.z < low.z || v.x > high.x || v.y > high.y || v.z > high.z; return all(v >= low) && all(v <= high);
}
fn inside(v: vec3<f32>, low: vec3<f32>, high: vec3<f32>) -> bool {
return !outside(v, low, high);
}
fn outside2(v: vec2<f32>, low: vec2<f32>, high: vec2<f32>) -> bool {
return v.x < low.x || v.y < low.y || v.x > high.x || v.y > high.y;
}
fn inside2(v: vec2<f32>, low: vec2<f32>, high: vec2<f32>) -> bool {
return !outside2(v, low, high);
} }