diff --git a/obstacles/src/sources/depth.rs b/obstacles/src/sources/depth.rs index 2fe4ad5..4909cec 100644 --- a/obstacles/src/sources/depth.rs +++ b/obstacles/src/sources/depth.rs @@ -114,6 +114,7 @@ pub fn new_depth_map( struct Cylinder { height: N, radius: N, + matrix: [[N; 4]; 4], inv_matrix: [[N; 4]; 4], } unsafe impl bytemuck::Pod for Cylinder {} @@ -180,11 +181,12 @@ impl + Send + 'static> AsyncNode for DepthMap { height, isometry, } => { - let inv_matrix: Matrix4 = - isometry.to_homogeneous().try_inverse().unwrap(); + let matrix = isometry.to_homogeneous(); + let inv_matrix = matrix.try_inverse().unwrap(); cylinder_buf.push(Cylinder { radius, height, + matrix: matrix.data.0, inv_matrix: inv_matrix.data.0, }); } @@ -198,7 +200,7 @@ impl + Send + 'static> AsyncNode for DepthMap { &transform, ) .await; - let _ = sender.send(HeightOnly::from_iter(heights.into_iter().copied().map( + let _ = sender.send(HeightOnly::from_iter(heights.into_iter().copied().filter(|n| *n != f32::MAX).map( |n| { if n == f32::MIN { None @@ -217,11 +219,12 @@ impl + Send + 'static> AsyncNode for DepthMap { height, isometry, } => { - let inv_matrix: Matrix4 = - isometry.to_homogeneous().try_inverse().unwrap(); + let matrix = isometry.to_homogeneous(); + let inv_matrix = matrix.try_inverse().unwrap(); cylinder_buf.push(Cylinder { radius, height, + matrix: matrix.data.0, inv_matrix: inv_matrix.data.0, }); } @@ -236,7 +239,7 @@ impl + Send + 'static> AsyncNode for DepthMap { ) .await; let _ = sender.send(HeightAndVariance::from_iter( - heights.into_iter().copied().map(|n| { + heights.into_iter().copied().filter(|n| *n != f32::MAX).map(|n| { if n == f32::MIN { None } else { diff --git a/obstacles/src/sources/depthf32.wgsl b/obstacles/src/sources/depthf32.wgsl index 0d739e9..071c7f7 100644 --- a/obstacles/src/sources/depthf32.wgsl +++ b/obstacles/src/sources/depthf32.wgsl @@ -1,6 +1,7 @@ struct Cylinder { height: f32, radius: f32, + matrix: mat4x4, inv_matrix: mat4x4, } @@ -16,18 +17,34 @@ struct Cylinder { fn main( @builtin(local_invocation_index) local_invocation_index : u32, ) { - let point = transform * rays[local_invocation_index] * depths[local_invocation_index]; + let depth = depths[local_invocation_index]; + if depth == 0.0 { + returned[local_invocation_index] = f32.max_positive; + return; + } + let ray = transform * rays[local_invocation_index]; + let point = ray * depth; + var past_all_shapes = true; for (var i = 0; i < cylinder_count; i++) { let cylinder = cylinders[i]; - let local_point = cylinders.inv_matrix * point; + let local_point = cylinder.inv_matrix * point; let half_height = cylinder.height / 2.0; - if (local_point.y < -half_height || local_point.y > half_height) { - continue; - } - if (length(local_point.xz) > cylinder.radius) { + if (local_point.y < -half_height || local_point.y > half_height || length(local_point.xz) > cylinder.radius) { + if past_all_shapes { + let shape_origin = cylinder.matrix * vec3(0.0, 0.0, 0.0); + let distance = length(shape_origin); + if distance < depth { + past_all_shapes = false; + } + } continue; } - returned[index] = point.y; + returned[local_invocation_index] = point.y; + return; + } + if past_all_shapes { + returned[local_invocation_index] = f32.max_positive; + } else { + returned[local_invocation_index] = f32.min_positive; } - returned[index] = f32.min_positive; } \ No newline at end of file