fixed negative distance in voxel traversing

This commit is contained in:
Piotrek
2021-04-23 22:44:03 +02:00
parent 526ff05ed2
commit 829ae1ddb6
7 changed files with 37 additions and 27 deletions
+1
View File
@@ -102,6 +102,7 @@ impl BrickBuffer {
for z in 0..32 {
let value = if rng.gen::<f32>()> 0.99 { 1 } else { 0 };
bricks.set(0, x, y, z, value);
bricks.set(1, x, 0, z, 1);
}
}
}
+14 -13
View File
@@ -92,20 +92,21 @@ impl NodeBuffer {
// Data
let mut nodes = NodeData::new();
let mut rng = rand::thread_rng();
for x in 0..WORLD_SIZE {
for y in 0..WORLD_SIZE {
for z in 0..WORLD_SIZE {
let value = if rng.gen::<f32>()> 0.5 { 1 } else { 0 };
nodes.set(x, y, z, value);
}
}
}
// for i in 0..WORLD_SIZE {
// nodes.set(i, 0, 0, 1);
// nodes.set(0, i, 0, 1);
// nodes.set(0, 0, i, 1);
// let mut rng = rand::thread_rng();
// for x in 0..WORLD_SIZE {
// for y in 0..WORLD_SIZE {
// for z in 0..WORLD_SIZE {
// let value = if rng.gen::<f32>()> 0.5 { 1 } else { 0 };
// nodes.set(x, y, z, value);
// }
// }
// }
for i in 0..WORLD_SIZE {
nodes.set(i, 0, 0, 1);
nodes.set(0, i, 0, 1);
nodes.set(0, 0, i, 1);
}
nodes.set(0, 0, 0, 2);
// Done
Self { nodes, texture, size, bind_layout, bind_group }
+4 -2
View File
@@ -7,7 +7,8 @@ use wgpu::util::DeviceExt;
pub struct UniformValues {
view_inv: [[f32; 4]; 4],
proj_inv: [[f32; 4]; 4],
cam_pos: [f32; 3]
cam_pos: [f32; 3],
time: f32
}
impl UniformValues {
@@ -16,10 +17,11 @@ impl UniformValues {
Self::default()
}
pub fn update(&mut self, camera: &Camera) {
pub fn update(&mut self, camera: &Camera, time: f32) {
self.view_inv = camera.view_matrix().invert().unwrap().into();
self.proj_inv = camera.proj_matrix().invert().unwrap().into();
self.cam_pos = camera.position.into();
self.time = time;
}
}
+6 -3
View File
@@ -16,8 +16,9 @@ pub struct Renderer {
uniform_buffer: UniformBuffer,
node_buffer: NodeBuffer,
brick_buffer: BrickBuffer,
// Camera
// Other
pub camera: Camera,
start: std::time::Instant
}
impl Renderer {
@@ -117,9 +118,11 @@ impl Renderer {
// Pipeline
let pipeline = Self::create_pipeline(&device, &swapchain_desc, &[&uniform_buffer.bind_layout, &node_buffer.bind_layout, &brick_buffer.bind_layout]);
let start = std::time::Instant::now();
// Save values in app
println!("Initialized");
Self { surface, device, queue, swapchain_desc, swapchain, pipeline, raster_buffer, uniform_buffer, node_buffer, brick_buffer, camera }
Self { surface, device, queue, swapchain_desc, swapchain, pipeline, raster_buffer, uniform_buffer, node_buffer, brick_buffer, camera, start }
}
pub fn resize(&mut self, new_size: Option<PhysicalSize<u32>>) {
@@ -139,7 +142,7 @@ impl Renderer {
pub fn render(&mut self) -> Result<(), wgpu::SwapChainError> {
// Update uniform buffer
self.uniform_buffer.values.update(&self.camera);
self.uniform_buffer.values.update(&self.camera, self.start.elapsed().as_secs_f32());
self.queue.write_buffer(&self.uniform_buffer.buffer, 0, bytemuck::cast_slice(&[self.uniform_buffer.values]));
// Get next frame to render to
Binary file not shown.
+5 -3
View File
@@ -26,6 +26,8 @@ vec3 solve(vec3 hitPoint, vec3 view, vec3 normal)
*/
vec4 castBricks(vec3 origin, vec3 start, vec3 dir, vec3 incAxis, uint addr)
{
vec3 o = origin;
// Change scale
origin = start - origin;
origin += dir*0.00001;
@@ -49,7 +51,7 @@ vec4 castBricks(vec3 origin, vec3 start, vec3 dir, vec3 incAxis, uint addr)
// Hit point
vec3 mini = (pos-origin+0.5 - raySign*0.5) * rayInv;
float len = max(mini.x, max(mini.y, mini.z));
vec3 hitPoint = (origin + dir * len) / 32.0;
vec3 hitPoint = (origin + dir * len) / 32.0 + o;
// Normal vector (negate previous increment axis and mult by sign)
vec3 normal = -incAxis * raySign;
@@ -96,7 +98,7 @@ vec3 castNodes(vec3 origin, vec3 dir)
{
// Hit point
vec3 mini = (pos-origin+0.5 - raySign*0.5) * rayInv;
float len = max(mini.x, max(mini.y, mini.z));
float len = max(0.0, max(mini.x, max(mini.y, mini.z)));
vec3 hitPoint = origin + dir * len;
// Normal vector (negate previous increment axis and mult by sign)
//vec3 normal = -incAxis * raySign;
@@ -104,7 +106,7 @@ vec3 castNodes(vec3 origin, vec3 dir)
// Cast brick
vec4 result = castBricks(pos, hitPoint, dir, incAxis, node);
if(result.a > 0.0) return result.rgb; //todo: continue if not hit
if(result.a > 0.0) return result.rgb;
}
// Get new closest axis to increment
+7 -6
View File
@@ -7,9 +7,10 @@ layout(location=0) out vec4 out_color;
layout(set=0, binding=0) uniform Uniforms
{
mat4 view_matrix;
mat4 proj_matrix_inv;
vec3 cam_pos;
mat4 _viewMatrix;
mat4 _projMatrixInv;
vec3 _CamPos;
float _Time;
};
//layout(set = 1, binding = 0) uniform texture3D node_tex;
@@ -30,9 +31,9 @@ layout(set=2,binding=0) uniform usampler3D bricksTexture;
void main()
{
// Calculate ray direction
vec3 view_dir = (proj_matrix_inv * vec4(vert_uv, 0, 1)).xyz;
vec3 ray_dir = normalize(view_matrix * vec4(view_dir,0)).xyz;
vec3 view_dir = (_projMatrixInv * vec4(vert_uv, 0, 1)).xyz;
vec3 ray_dir = normalize(_viewMatrix * vec4(view_dir,0)).xyz;
// Raycast
out_color = vec4(castNodes(cam_pos, ray_dir), 1.0);
out_color = vec4(castNodes(_CamPos, ray_dir), 1.0);
}