Skip to content
This repository has been archived by the owner on Dec 4, 2024. It is now read-only.

Commit

Permalink
more corrections
Browse files Browse the repository at this point in the history
  • Loading branch information
manglemix committed Apr 22, 2024
1 parent 9650a28 commit 4407fa8
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 14 deletions.
15 changes: 9 additions & 6 deletions obstacles/src/sources/depth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ pub fn new_depth_map<N: Float, D: Send + 'static>(
struct Cylinder<N: Float> {
height: N,
radius: N,
matrix: [[N; 4]; 4],
inv_matrix: [[N; 4]; 4],
}
unsafe impl<N: Float + bytemuck::Pod + bytemuck::NoUninit> bytemuck::Pod for Cylinder<N> {}
Expand Down Expand Up @@ -180,11 +181,12 @@ impl<D: Deref<Target = [f32]> + Send + 'static> AsyncNode for DepthMap<f32, D> {
height,
isometry,
} => {
let inv_matrix: Matrix4<f32> =
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,
});
}
Expand All @@ -198,7 +200,7 @@ impl<D: Deref<Target = [f32]> + Send + 'static> AsyncNode for DepthMap<f32, D> {
&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
Expand All @@ -217,11 +219,12 @@ impl<D: Deref<Target = [f32]> + Send + 'static> AsyncNode for DepthMap<f32, D> {
height,
isometry,
} => {
let inv_matrix: Matrix4<f32> =
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,
});
}
Expand All @@ -236,7 +239,7 @@ impl<D: Deref<Target = [f32]> + Send + 'static> AsyncNode for DepthMap<f32, D> {
)
.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 {
Expand Down
33 changes: 25 additions & 8 deletions obstacles/src/sources/depthf32.wgsl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
struct Cylinder {
height: f32,
radius: f32,
matrix: mat4x4<f32>,
inv_matrix: mat4x4<f32>,
}

Expand All @@ -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<f32>(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;
}

0 comments on commit 4407fa8

Please sign in to comment.