basic fully working octree renderer
This commit is contained in:
498
Cargo.lock
generated
498
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@@ -10,14 +10,13 @@ rustflags = ["-Z", "threads=14"]
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
bytemuck = {version="1.14.0", features=["derive"]}
|
bytemuck = {version="1.14.0", features=["derive"]}
|
||||||
nalgebra = {version="0.32.5", features=["bytemuck"]}
|
nalgebra = {version="0.33.0", features=["bytemuck"]}
|
||||||
ndarray = "0.15.6"
|
ndarray = "0.15.6"
|
||||||
pollster = "0.3"
|
pollster = "0.3"
|
||||||
rand = "0.8.5"
|
rand = "0.8.5"
|
||||||
simba = "0.8.1"
|
simba = "0.8.1"
|
||||||
simdnoise = "3.1.6"
|
simdnoise = "3.1.6"
|
||||||
smaa = "0.14.0"
|
wgpu = "22.0.0"
|
||||||
wgpu = "0.20.1"
|
|
||||||
bevy_ecs = "0.13.2"
|
bevy_ecs = "0.13.2"
|
||||||
bevy_derive = "0.13.2"
|
bevy_derive = "0.13.2"
|
||||||
winit = {version="0.30.1", features=["serde"]}
|
winit = {version="0.30.1", features=["serde"]}
|
||||||
|
|||||||
@@ -55,7 +55,7 @@ impl Client<'_> {
|
|||||||
// camera orientation
|
// camera orientation
|
||||||
let old_camera = state.camera;
|
let old_camera = state.camera;
|
||||||
if self.grabbed_cursor {
|
if self.grabbed_cursor {
|
||||||
let delta = input.mouse_delta * 0.003;
|
let delta = input.mouse_delta * 0.003 / state.camera.scale;
|
||||||
if delta.x != 0.0 {
|
if delta.x != 0.0 {
|
||||||
state.camera.orientation = Rotation3::from_axis_angle(&state.camera.up(), delta.x)
|
state.camera.orientation = Rotation3::from_axis_angle(&state.camera.up(), delta.x)
|
||||||
* state.camera.orientation;
|
* state.camera.orientation;
|
||||||
|
|||||||
@@ -83,6 +83,12 @@ impl Input {
|
|||||||
self.mouse_just_released.insert(button);
|
self.mouse_just_released.insert(button);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
WindowEvent::MouseWheel { device_id, delta, phase } => {
|
||||||
|
self.scroll_delta = match delta {
|
||||||
|
MouseScrollDelta::LineDelta(_, v) => v,
|
||||||
|
MouseScrollDelta::PixelDelta(v) => (v.y / 2.0) as f32,
|
||||||
|
};
|
||||||
|
}
|
||||||
_ => (),
|
_ => (),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ pub use command::*;
|
|||||||
use super::camera::Camera;
|
use super::camera::Camera;
|
||||||
use crate::client::rsc::CLEAR_COLOR;
|
use crate::client::rsc::CLEAR_COLOR;
|
||||||
use nalgebra::Vector2;
|
use nalgebra::Vector2;
|
||||||
use smaa::{SmaaMode, SmaaTarget};
|
|
||||||
use util::Texture;
|
use util::Texture;
|
||||||
use voxel::VoxelPipeline;
|
use voxel::VoxelPipeline;
|
||||||
use winit::dpi::PhysicalSize;
|
use winit::dpi::PhysicalSize;
|
||||||
@@ -20,7 +19,6 @@ pub struct Renderer<'a> {
|
|||||||
config: wgpu::SurfaceConfiguration,
|
config: wgpu::SurfaceConfiguration,
|
||||||
staging_belt: wgpu::util::StagingBelt,
|
staging_belt: wgpu::util::StagingBelt,
|
||||||
voxel_pipeline: VoxelPipeline,
|
voxel_pipeline: VoxelPipeline,
|
||||||
smaa_target: SmaaTarget,
|
|
||||||
camera: Camera,
|
camera: Camera,
|
||||||
depth_texture: Texture,
|
depth_texture: Texture,
|
||||||
}
|
}
|
||||||
@@ -43,6 +41,7 @@ impl<'a> Renderer<'a> {
|
|||||||
label: None,
|
label: None,
|
||||||
required_features: wgpu::Features::empty(),
|
required_features: wgpu::Features::empty(),
|
||||||
required_limits: wgpu::Limits::default(),
|
required_limits: wgpu::Limits::default(),
|
||||||
|
memory_hints: wgpu::MemoryHints::default(),
|
||||||
},
|
},
|
||||||
None, // Trace path
|
None, // Trace path
|
||||||
))
|
))
|
||||||
@@ -79,15 +78,6 @@ impl<'a> Renderer<'a> {
|
|||||||
// doesn't affect performance much and depends on "normal" zoom
|
// doesn't affect performance much and depends on "normal" zoom
|
||||||
let staging_belt = wgpu::util::StagingBelt::new(4096 * 4);
|
let staging_belt = wgpu::util::StagingBelt::new(4096 * 4);
|
||||||
|
|
||||||
let smaa_target = SmaaTarget::new(
|
|
||||||
&device,
|
|
||||||
&queue,
|
|
||||||
size.width,
|
|
||||||
size.height,
|
|
||||||
surface_format,
|
|
||||||
SmaaMode::Smaa1X,
|
|
||||||
);
|
|
||||||
|
|
||||||
let depth_texture = Texture::create_depth_texture(&device, &config, "depth_texture");
|
let depth_texture = Texture::create_depth_texture(&device, &config, "depth_texture");
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
@@ -100,7 +90,6 @@ impl<'a> Renderer<'a> {
|
|||||||
device,
|
device,
|
||||||
config,
|
config,
|
||||||
queue,
|
queue,
|
||||||
smaa_target,
|
|
||||||
depth_texture,
|
depth_texture,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -117,34 +106,29 @@ impl<'a> Renderer<'a> {
|
|||||||
let view = output
|
let view = output
|
||||||
.texture
|
.texture
|
||||||
.create_view(&wgpu::TextureViewDescriptor::default());
|
.create_view(&wgpu::TextureViewDescriptor::default());
|
||||||
let smaa_frame = self
|
let mut render_pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
|
||||||
.smaa_target
|
label: Some("Render Pass"),
|
||||||
.start_frame(&self.device, &self.queue, &view);
|
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
|
||||||
{
|
view: &view,
|
||||||
let render_pass = &mut encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
|
resolve_target: None,
|
||||||
label: Some("Render Pass"),
|
ops: wgpu::Operations {
|
||||||
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
|
load: wgpu::LoadOp::Clear(CLEAR_COLOR),
|
||||||
view: &smaa_frame,
|
store: wgpu::StoreOp::Store,
|
||||||
resolve_target: None,
|
},
|
||||||
ops: wgpu::Operations {
|
})],
|
||||||
load: wgpu::LoadOp::Clear(CLEAR_COLOR),
|
depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment {
|
||||||
store: wgpu::StoreOp::Store,
|
view: &self.depth_texture.view,
|
||||||
},
|
depth_ops: Some(wgpu::Operations {
|
||||||
})],
|
load: wgpu::LoadOp::Clear(1.0),
|
||||||
depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment {
|
store: wgpu::StoreOp::Store,
|
||||||
view: &self.depth_texture.view,
|
|
||||||
depth_ops: Some(wgpu::Operations {
|
|
||||||
load: wgpu::LoadOp::Clear(1.0),
|
|
||||||
store: wgpu::StoreOp::Store,
|
|
||||||
}),
|
|
||||||
stencil_ops: None,
|
|
||||||
}),
|
}),
|
||||||
timestamp_writes: None,
|
stencil_ops: None,
|
||||||
occlusion_query_set: None,
|
}),
|
||||||
});
|
timestamp_writes: None,
|
||||||
self.voxel_pipeline.draw(render_pass);
|
occlusion_query_set: None,
|
||||||
}
|
});
|
||||||
smaa_frame.resolve();
|
self.voxel_pipeline.draw(&mut render_pass);
|
||||||
|
drop(render_pass);
|
||||||
|
|
||||||
self.staging_belt.finish();
|
self.staging_belt.finish();
|
||||||
self.queue.submit(std::iter::once(encoder.finish()));
|
self.queue.submit(std::iter::once(encoder.finish()));
|
||||||
@@ -157,8 +141,6 @@ impl<'a> Renderer<'a> {
|
|||||||
self.config.width = size.width;
|
self.config.width = size.width;
|
||||||
self.config.height = size.height;
|
self.config.height = size.height;
|
||||||
self.surface.configure(&self.device, &self.config);
|
self.surface.configure(&self.device, &self.config);
|
||||||
self.smaa_target
|
|
||||||
.resize(&self.device, size.width, size.height);
|
|
||||||
|
|
||||||
self.depth_texture =
|
self.depth_texture =
|
||||||
Texture::create_depth_texture(&self.device, &self.config, "depth_texture");
|
Texture::create_depth_texture(&self.device, &self.config, "depth_texture");
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ mod light;
|
|||||||
mod view;
|
mod view;
|
||||||
|
|
||||||
pub use color::*;
|
pub use color::*;
|
||||||
|
use wgpu::include_wgsl;
|
||||||
|
|
||||||
use super::super::UpdateGridTransform;
|
use super::super::UpdateGridTransform;
|
||||||
use crate::{client::{
|
use crate::{client::{
|
||||||
@@ -35,10 +36,7 @@ pub struct VoxelPipeline {
|
|||||||
impl VoxelPipeline {
|
impl VoxelPipeline {
|
||||||
pub fn new(device: &wgpu::Device, config: &wgpu::SurfaceConfiguration) -> Self {
|
pub fn new(device: &wgpu::Device, config: &wgpu::SurfaceConfiguration) -> Self {
|
||||||
// shaders
|
// shaders
|
||||||
let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
|
let shader = device.create_shader_module(include_wgsl!("shader.wgsl"));
|
||||||
label: Some("Tile Shader"),
|
|
||||||
source: wgpu::ShaderSource::Wgsl(include_str!("shader.wgsl").into()),
|
|
||||||
});
|
|
||||||
|
|
||||||
let view = Uniform::init(device, "view", 0);
|
let view = Uniform::init(device, "view", 0);
|
||||||
let voxels = Storage::init(device, "voxels", 1);
|
let voxels = Storage::init(device, "voxels", 1);
|
||||||
@@ -123,6 +121,7 @@ impl VoxelPipeline {
|
|||||||
alpha_to_coverage_enabled: true,
|
alpha_to_coverage_enabled: true,
|
||||||
},
|
},
|
||||||
multiview: None,
|
multiview: None,
|
||||||
|
cache: None,
|
||||||
});
|
});
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
|
|||||||
@@ -57,7 +57,7 @@ fn fs_main(
|
|||||||
let aspect = win_dim.y / win_dim.x;
|
let aspect = win_dim.y / win_dim.x;
|
||||||
let pixel_pos = vec3<f32>(
|
let pixel_pos = vec3<f32>(
|
||||||
(in.clip_position.xy / win_dim - vec2<f32>(0.5)) * vec2<f32>(2.0, -2.0 * aspect),
|
(in.clip_position.xy / win_dim - vec2<f32>(0.5)) * vec2<f32>(2.0, -2.0 * aspect),
|
||||||
1.0
|
view.zoom
|
||||||
);
|
);
|
||||||
|
|
||||||
// move to position in world
|
// move to position in world
|
||||||
@@ -127,6 +127,7 @@ fn trace_full(pos_view: vec4<f32>, dir_view: vec4<f32>) -> vec4<f32> {
|
|||||||
t = select(select(t_i.z, t_i.y, hit.y), t_i.x, hit.x);
|
t = select(select(t_i.z, t_i.y, hit.y), t_i.x, hit.x);
|
||||||
next_normal = select(select(normals[2], normals[1], hit.y), normals[0], hit.x);
|
next_normal = select(select(normals[2], normals[1], hit.y), normals[0], hit.x);
|
||||||
}
|
}
|
||||||
|
// voxel position relative to low_corner
|
||||||
var vox_pos = clamp(vec3<i32>(pos), vec3<i32>(0), dim_i - vec3<i32>(1));
|
var vox_pos = clamp(vec3<i32>(pos), vec3<i32>(0), dim_i - vec3<i32>(1));
|
||||||
|
|
||||||
|
|
||||||
@@ -144,11 +145,10 @@ fn trace_full(pos_view: vec4<f32>, dir_view: vec4<f32>) -> vec4<f32> {
|
|||||||
var data_start = 1u;
|
var data_start = 1u;
|
||||||
var i = 0u;
|
var i = 0u;
|
||||||
var axis = 0;
|
var axis = 0;
|
||||||
var hits = 0;
|
|
||||||
for (var safety = 0; safety < 1000; safety += 1) {
|
for (var safety = 0; safety < 1000; safety += 1) {
|
||||||
let node = voxels[group.offset + i];
|
let node = voxels[group.offset + i];
|
||||||
if node >= LEAF_BIT {
|
if node >= LEAF_BIT {
|
||||||
hits += 1;
|
// leaf
|
||||||
let vcolor = get_color(node & LEAF_MASK);
|
let vcolor = get_color(node & LEAF_MASK);
|
||||||
if vcolor.a > 0.0 {
|
if vcolor.a > 0.0 {
|
||||||
let diffuse = max(dot(global_lights[0].dir, next_normal) + 0.1, 0.0);
|
let diffuse = max(dot(global_lights[0].dir, next_normal) + 0.1, 0.0);
|
||||||
@@ -156,6 +156,7 @@ fn trace_full(pos_view: vec4<f32>, dir_view: vec4<f32>) -> vec4<f32> {
|
|||||||
let lighting = max(diffuse, ambient);
|
let lighting = max(diffuse, ambient);
|
||||||
let new_color = min(vcolor.xyz * lighting, vec3<f32>(1.0));
|
let new_color = min(vcolor.xyz * lighting, vec3<f32>(1.0));
|
||||||
color += vec4<f32>(new_color.xyz * vcolor.a, vcolor.a) * (1.0 - color.a);
|
color += vec4<f32>(new_color.xyz * vcolor.a, vcolor.a) * (1.0 - color.a);
|
||||||
|
color = vec4<f32>(f32(safety) / 1000.0, 0.0, 0.0, 1.0);
|
||||||
if color.a > .999 {
|
if color.a > .999 {
|
||||||
return color;
|
return color;
|
||||||
}
|
}
|
||||||
@@ -171,14 +172,10 @@ fn trace_full(pos_view: vec4<f32>, dir_view: vec4<f32>) -> vec4<f32> {
|
|||||||
pos = pos_start + t * dir;
|
pos = pos_start + t * dir;
|
||||||
let old = vox_pos[axis];
|
let old = vox_pos[axis];
|
||||||
vox_pos = vec3<i32>(pos) - low_corner;
|
vox_pos = vec3<i32>(pos) - low_corner;
|
||||||
vox_pos[axis] += select(0, dir_i[axis], vox_pos[axis] == 0 || vox_pos[axis] == side_len - 1);
|
vox_pos = clamp(vox_pos, vec3<i32>(0), side_len - vec3<i32>(1));
|
||||||
// if hits == 1 {
|
vox_pos[axis] += dir_i[axis];
|
||||||
// // var axis_c = vec3<f32>(0.0);
|
|
||||||
// // axis_c[axis] = 1.0;
|
|
||||||
// // return vec4<f32>(axis_c, 1.0);
|
|
||||||
// return vec4<f32>(vec3<f32>(vox_pos), 1.0);
|
|
||||||
// }
|
|
||||||
} else if inside3i(vox_pos, vec3<i32>(0), vec3<i32>(side_len - 1)) {
|
} else if inside3i(vox_pos, vec3<i32>(0), vec3<i32>(side_len - 1)) {
|
||||||
|
// node
|
||||||
let node_pos = data_start + node;
|
let node_pos = data_start + node;
|
||||||
side_len /= 2;
|
side_len /= 2;
|
||||||
let vcorner = vox_pos / side_len;
|
let vcorner = vox_pos / side_len;
|
||||||
@@ -194,7 +191,8 @@ fn trace_full(pos_view: vec4<f32>, dir_view: vec4<f32>) -> vec4<f32> {
|
|||||||
|
|
||||||
// idrk what to put here tbh but this prolly works; don't zoom out if max
|
// idrk what to put here tbh but this prolly works; don't zoom out if max
|
||||||
if side_len == 256 {
|
if side_len == 256 {
|
||||||
return color;
|
let a = f32(safety) / 1000.0;
|
||||||
|
return vec4<f32>(0.0, 0.0, a, 1.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
// get parent info and reset "pointers" to parent
|
// get parent info and reset "pointers" to parent
|
||||||
|
|||||||
282
src/client/render/voxel/ray_oct/shader_old.wgsl
Normal file
282
src/client/render/voxel/ray_oct/shader_old.wgsl
Normal file
@@ -0,0 +1,282 @@
|
|||||||
|
// Vertex shader
|
||||||
|
|
||||||
|
struct GlobalLight {
|
||||||
|
dir: vec3<f32>,
|
||||||
|
};
|
||||||
|
|
||||||
|
struct VertexOutput {
|
||||||
|
@builtin(position) clip_position: vec4<f32>,
|
||||||
|
};
|
||||||
|
|
||||||
|
struct View {
|
||||||
|
transform: mat4x4<f32>,
|
||||||
|
width: u32,
|
||||||
|
height: u32,
|
||||||
|
zoom: f32,
|
||||||
|
};
|
||||||
|
|
||||||
|
struct VoxelGroup {
|
||||||
|
transform: mat4x4<f32>,
|
||||||
|
transform_inv: mat4x4<f32>,
|
||||||
|
dimensions: vec3<u32>,
|
||||||
|
offset: u32,
|
||||||
|
};
|
||||||
|
|
||||||
|
@group(0) @binding(0)
|
||||||
|
var<uniform> view: View;
|
||||||
|
@group(0) @binding(1)
|
||||||
|
var<storage, read> voxels: array<u32>;
|
||||||
|
@group(0) @binding(2)
|
||||||
|
var<storage, read> voxel_groups: array<VoxelGroup>;
|
||||||
|
@group(0) @binding(3)
|
||||||
|
var<storage, read> global_lights: array<GlobalLight>;
|
||||||
|
|
||||||
|
@vertex
|
||||||
|
fn vs_main(
|
||||||
|
@builtin(vertex_index) vi: u32,
|
||||||
|
@builtin(instance_index) ii: u32,
|
||||||
|
) -> VertexOutput {
|
||||||
|
var out: VertexOutput;
|
||||||
|
|
||||||
|
var pos = vec2<f32>(
|
||||||
|
f32(vi % 2u) * 2.0 - 1.0,
|
||||||
|
f32(vi / 2u) * 2.0 - 1.0,
|
||||||
|
) ;
|
||||||
|
out.clip_position = vec4<f32>(pos.x, pos.y, 0.0, 1.0);
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fragment shader
|
||||||
|
|
||||||
|
@fragment
|
||||||
|
fn fs_main(
|
||||||
|
in: VertexOutput,
|
||||||
|
) -> @location(0) vec4<f32> {
|
||||||
|
// get position of the pixel; eye at origin, pixel on plane z = 1
|
||||||
|
let win_dim = vec2<f32>(f32(view.width), f32(view.height));
|
||||||
|
let aspect = win_dim.y / win_dim.x;
|
||||||
|
let pixel_pos = vec3<f32>(
|
||||||
|
(in.clip_position.xy / win_dim - vec2<f32>(0.5)) * vec2<f32>(2.0, -2.0 * aspect),
|
||||||
|
view.zoom
|
||||||
|
);
|
||||||
|
|
||||||
|
// move to position in world
|
||||||
|
let pos = view.transform * vec4<f32>(pixel_pos, 1.0);
|
||||||
|
let dir = view.transform * vec4<f32>(normalize(pixel_pos), 0.0);
|
||||||
|
|
||||||
|
var color = trace_full(pos, dir);
|
||||||
|
let light_mult = clamp((-dot(dir.xyz, global_lights[0].dir) - 0.99) * 200.0, 0.0, 1.0);
|
||||||
|
let sky_color = light_mult * vec3<f32>(1.0, 1.0, 1.0);
|
||||||
|
color += vec4<f32>(sky_color * (1.0 - color.a), 1.0 - color.a);
|
||||||
|
color.a = 1.0;
|
||||||
|
return color;
|
||||||
|
}
|
||||||
|
|
||||||
|
const ZERO3F = vec3<f32>(0.0);
|
||||||
|
const ZERO2F = vec2<f32>(0.0);
|
||||||
|
const DEPTH = 16u;
|
||||||
|
const FULL_ALPHA = 0.9999;
|
||||||
|
|
||||||
|
fn trace_full(pos_view: vec4<f32>, dir_view: vec4<f32>) -> vec4<f32> {
|
||||||
|
let gi = 0;
|
||||||
|
let group = voxel_groups[gi];
|
||||||
|
if group.dimensions.x == 0 {
|
||||||
|
return vec4<f32>(0.0);
|
||||||
|
}
|
||||||
|
let dim_f = vec3<f32>(group.dimensions);
|
||||||
|
let dim_i = vec3<i32>(group.dimensions);
|
||||||
|
|
||||||
|
// transform so that group is at 0,0
|
||||||
|
let pos_start = (group.transform_inv * pos_view).xyz;
|
||||||
|
let dir = (group.transform_inv * dir_view).xyz;
|
||||||
|
|
||||||
|
let dir_if = sign(dir);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// calculate normals
|
||||||
|
var normals = mat3x3<f32>(
|
||||||
|
(group.transform * vec4<f32>(dir_if.x, 0.0, 0.0, 0.0)).xyz,
|
||||||
|
(group.transform * vec4<f32>(0.0, dir_if.y, 0.0, 0.0)).xyz,
|
||||||
|
(group.transform * vec4<f32>(0.0, 0.0, dir_if.z, 0.0)).xyz,
|
||||||
|
);
|
||||||
|
var next_normal = vec3<f32>(0.0, 0.0, 0.0);
|
||||||
|
|
||||||
|
// find where ray intersects with group
|
||||||
|
let plane_point = (vec3<f32>(1.0) - dir_if) / 2.0 * dim_f;
|
||||||
|
var pos = pos_start;
|
||||||
|
var t = 0.0;
|
||||||
|
if outside3f(pos, ZERO3F, dim_f) {
|
||||||
|
// time of intersection; x = td + p, solve for t
|
||||||
|
let t_i = (plane_point - pos) / dir;
|
||||||
|
// points of intersection
|
||||||
|
let px = pos + t_i.x * dir;
|
||||||
|
let py = pos + t_i.y * dir;
|
||||||
|
let pz = pos + t_i.z * dir;
|
||||||
|
|
||||||
|
// check if point is in bounds
|
||||||
|
let hit = vec3<bool>(
|
||||||
|
inside2f(px.yz, ZERO2F, dim_f.yz),
|
||||||
|
inside2f(py.xz, ZERO2F, dim_f.xz),
|
||||||
|
inside2f(pz.xy, ZERO2F, dim_f.xy),
|
||||||
|
) && (t_i > ZERO3F);
|
||||||
|
if !any(hit) {
|
||||||
|
return vec4<f32>(0.0);
|
||||||
|
}
|
||||||
|
pos = select(select(pz, py, hit.y), px, hit.x);
|
||||||
|
t = select(select(t_i.z, t_i.y, hit.y), t_i.x, hit.x);
|
||||||
|
next_normal = select(select(normals[2], normals[1], hit.y), normals[0], hit.x);
|
||||||
|
}
|
||||||
|
// voxel position relative to low_corner
|
||||||
|
var vox_pos = clamp(vec3<i32>(pos), vec3<i32>(0), dim_i - vec3<i32>(1));
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
let dir_i = vec3<i32>(dir_if);
|
||||||
|
let dir_u = ((dir_i + vec3<i32>(1)) / 2);
|
||||||
|
let dir_bits = u32(dir_u.x * 4 + dir_u.y * 2 + dir_u.z);
|
||||||
|
// time to move 1 unit using dir
|
||||||
|
let inc_t = abs(1.0 / dir);
|
||||||
|
var side_len = 256;
|
||||||
|
// "unsigned" minimum cube coords of current tree
|
||||||
|
var low_corner = vec3<i32>(0);
|
||||||
|
// time of next 1 unit plane hit in each direction
|
||||||
|
var color = vec4<f32>(0.0);
|
||||||
|
var data_start = 1u;
|
||||||
|
var i = 0u;
|
||||||
|
var axis = 0;
|
||||||
|
var hits = 0;
|
||||||
|
for (var safety = 0; safety < 1000; safety += 1) {
|
||||||
|
let node = voxels[group.offset + i];
|
||||||
|
if node >= LEAF_BIT {
|
||||||
|
// leaf
|
||||||
|
hits += 1;
|
||||||
|
let vcolor = get_color(node & LEAF_MASK);
|
||||||
|
if vcolor.a > 0.0 {
|
||||||
|
let diffuse = max(dot(global_lights[0].dir, next_normal) + 0.1, 0.0);
|
||||||
|
let ambient = 0.2;
|
||||||
|
let lighting = max(diffuse, ambient);
|
||||||
|
let new_color = min(vcolor.xyz * lighting, vec3<f32>(1.0));
|
||||||
|
color += vec4<f32>(new_color.xyz * vcolor.a, vcolor.a) * (1.0 - color.a);
|
||||||
|
if color.a > .999 {
|
||||||
|
return color;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// move to next face of cube
|
||||||
|
let half_len = f32(side_len) / 2.0;
|
||||||
|
let corner = vec3<f32>(low_corner) + vec3<f32>(half_len) + dir_if * half_len;
|
||||||
|
let next_t = inc_t * abs(corner - pos_start);
|
||||||
|
axis = select(select(2, 1, next_t.y < next_t.z), 0, next_t.x < next_t.y && next_t.x < next_t.z);
|
||||||
|
t = next_t[axis];
|
||||||
|
next_normal = normals[axis];
|
||||||
|
pos = pos_start + t * dir;
|
||||||
|
let old = vox_pos[axis];
|
||||||
|
vox_pos = vec3<i32>(pos) - low_corner;
|
||||||
|
vox_pos = clamp(vox_pos, vec3<i32>(0), side_len - vec3<i32>(1));
|
||||||
|
vox_pos[axis] += dir_i[axis];
|
||||||
|
} else if inside3i(vox_pos, vec3<i32>(0), vec3<i32>(side_len - 1)) {
|
||||||
|
// node
|
||||||
|
let node_pos = data_start + node;
|
||||||
|
side_len /= 2;
|
||||||
|
let vcorner = vox_pos / side_len;
|
||||||
|
vox_pos -= vcorner * side_len;
|
||||||
|
let j = u32(vcorner.x * 4 + vcorner.y * 2 + vcorner.z);
|
||||||
|
i = node_pos + j;
|
||||||
|
data_start = node_pos + 9;
|
||||||
|
|
||||||
|
low_corner += vec3<i32>(dir_to_vec(j)) * i32(side_len);
|
||||||
|
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// idrk what to put here tbh but this prolly works; don't zoom out if max
|
||||||
|
if side_len == 256 {
|
||||||
|
return color;
|
||||||
|
}
|
||||||
|
|
||||||
|
// get parent info and reset "pointers" to parent
|
||||||
|
let parent_info_i = data_start - 1;
|
||||||
|
let parent_info = voxels[group.offset + parent_info_i];
|
||||||
|
let parent_root = parent_info_i - (parent_info >> 3);
|
||||||
|
let parent_loc = parent_info & 7;
|
||||||
|
let loc = 8 - (data_start - 1 - i);
|
||||||
|
// let test = (parent_root + 9 + voxels[group.offset + parent_root + parent_loc] + loc) == i;
|
||||||
|
i = parent_root + parent_loc;
|
||||||
|
data_start = parent_root + 9;
|
||||||
|
|
||||||
|
// adjust corner back to parent
|
||||||
|
let low_corner_adj = vec3<i32>(dir_to_vec(loc)) * i32(side_len);
|
||||||
|
low_corner -= low_corner_adj;
|
||||||
|
|
||||||
|
// update vox pos to be relative to parent
|
||||||
|
vox_pos += low_corner_adj;
|
||||||
|
|
||||||
|
side_len *= 2;
|
||||||
|
// return vec4<f32>(vec3<f32>(dir_to_vec(parent_loc)) * f32(loc) / 8.0, 1.0);
|
||||||
|
// return vec4<f32>(vec3<f32>(f32(test)), 1.0);
|
||||||
|
}
|
||||||
|
return vec4<f32>(1.0, 0.0, 1.0, 1.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
const LEAF_BIT = 1u << 31u;
|
||||||
|
const LEAF_MASK = ~LEAF_BIT;
|
||||||
|
|
||||||
|
// there's no way this is efficient, mod is faster for all I know
|
||||||
|
fn dir_to_vec(bits: u32) -> vec3<u32> {
|
||||||
|
return vec3<u32>(extractBits(bits, 2u, 1u), extractBits(bits, 1u, 1u), extractBits(bits, 0u, 1u));
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_voxel(offset: u32, pos_: vec3<u32>) -> u32 {
|
||||||
|
var data_start = 1u;
|
||||||
|
var i = 0u;
|
||||||
|
var pos = pos_;
|
||||||
|
var side_len: u32 = 256;
|
||||||
|
var safety = 0;
|
||||||
|
while voxels[offset + i] < LEAF_BIT {
|
||||||
|
let node_pos = data_start + voxels[offset + i];
|
||||||
|
side_len /= 2u;
|
||||||
|
let corner = pos / side_len;
|
||||||
|
pos -= corner * side_len;
|
||||||
|
let j = corner.x * 4 + corner.y * 2 + corner.z;
|
||||||
|
i = node_pos + j;
|
||||||
|
data_start = node_pos + 8;
|
||||||
|
if safety == 10 {
|
||||||
|
return 10u;
|
||||||
|
}
|
||||||
|
safety += 1;
|
||||||
|
}
|
||||||
|
return voxels[offset + i] & LEAF_MASK;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_color(id: u32) -> vec4<f32> {
|
||||||
|
switch id {
|
||||||
|
case 0u: {
|
||||||
|
return vec4<f32>(0.0);
|
||||||
|
}
|
||||||
|
case 1u: {
|
||||||
|
return vec4<f32>(0.5, 0.5, 0.5, 1.0);
|
||||||
|
}
|
||||||
|
case 2u: {
|
||||||
|
return vec4<f32>(0.5, 1.0, 0.5, 1.0);
|
||||||
|
}
|
||||||
|
case 3u: {
|
||||||
|
return vec4<f32>(0.5, 0.5, 1.0, 0.5);
|
||||||
|
}
|
||||||
|
default: {
|
||||||
|
return vec4<f32>(1.0, 0.0, 0.0, 1.0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn outside3f(v: vec3<f32>, low: vec3<f32>, high: vec3<f32>) -> bool {
|
||||||
|
return any(v < low) || any(v > high);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn inside2f(v: vec2<f32>, low: vec2<f32>, high: vec2<f32>) -> bool {
|
||||||
|
return all(v >= low) && all(v <= high);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn inside3i(v: vec3<i32>, low: vec3<i32>, high: vec3<i32>) -> bool {
|
||||||
|
return all(v >= low) && all(v <= high);
|
||||||
|
}
|
||||||
@@ -155,6 +155,8 @@ impl OctTree {
|
|||||||
pub fn raw(&self) -> &[OctNode] {
|
pub fn raw(&self) -> &[OctNode] {
|
||||||
&self.data
|
&self.data
|
||||||
}
|
}
|
||||||
|
pub fn mesh(&self) {
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct OctTreeIter<'a> {
|
pub struct OctTreeIter<'a> {
|
||||||
|
|||||||
Reference in New Issue
Block a user