Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename Plane struct to HalfSpace #8744

Merged
merged 13 commits into from
Jun 12, 2023
22 changes: 13 additions & 9 deletions crates/bevy_pbr/src/light.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use bevy_render::{
color::Color,
extract_resource::ExtractResource,
prelude::Projection,
primitives::{Aabb, CascadesFrusta, CubemapFrusta, Frustum, Plane, Sphere},
primitives::{Aabb, CascadesFrusta, CubemapFrusta, Frustum, HalfSpace, Sphere},
render_resource::BufferBindingType,
renderer::RenderDevice,
view::{ComputedVisibility, RenderLayers, VisibleEntities},
Expand Down Expand Up @@ -1457,7 +1457,7 @@ pub(crate) fn assign_lights_to_clusters(
let view_x = clip_to_view(inverse_projection, Vec4::new(x_pos, 0.0, 1.0, 1.0)).x;
let normal = Vec3::X;
let d = view_x * normal.x;
x_planes.push(Plane::new(normal.extend(d)));
x_planes.push(HalfSpace::new(normal.extend(d)));
}

let y_slices = clusters.dimensions.y as f32;
Expand All @@ -1467,7 +1467,7 @@ pub(crate) fn assign_lights_to_clusters(
let view_y = clip_to_view(inverse_projection, Vec4::new(0.0, y_pos, 1.0, 1.0)).y;
let normal = Vec3::Y;
let d = view_y * normal.y;
y_planes.push(Plane::new(normal.extend(d)));
y_planes.push(HalfSpace::new(normal.extend(d)));
}
} else {
let x_slices = clusters.dimensions.x as f32;
Expand All @@ -1478,7 +1478,7 @@ pub(crate) fn assign_lights_to_clusters(
let nt = clip_to_view(inverse_projection, Vec4::new(x_pos, 1.0, 1.0, 1.0)).xyz();
let normal = nb.cross(nt);
let d = nb.dot(normal);
x_planes.push(Plane::new(normal.extend(d)));
x_planes.push(HalfSpace::new(normal.extend(d)));
}

let y_slices = clusters.dimensions.y as f32;
Expand All @@ -1489,7 +1489,7 @@ pub(crate) fn assign_lights_to_clusters(
let nr = clip_to_view(inverse_projection, Vec4::new(1.0, y_pos, 1.0, 1.0)).xyz();
let normal = nr.cross(nl);
let d = nr.dot(normal);
y_planes.push(Plane::new(normal.extend(d)));
y_planes.push(HalfSpace::new(normal.extend(d)));
}
}

Expand All @@ -1498,7 +1498,7 @@ pub(crate) fn assign_lights_to_clusters(
let view_z = z_slice_to_view_z(first_slice_depth, far_z, z_slices, z, is_orthographic);
let normal = -Vec3::Z;
let d = view_z * normal.z;
z_planes.push(Plane::new(normal.extend(d)));
z_planes.push(HalfSpace::new(normal.extend(d)));
}

let mut update_from_light_intersections = |visible_lights: &mut Vec<Entity>| {
Expand Down Expand Up @@ -1737,7 +1737,7 @@ pub(crate) fn assign_lights_to_clusters(
}

// NOTE: This exploits the fact that a x-plane normal has only x and z components
fn get_distance_x(plane: Plane, point: Vec3A, is_orthographic: bool) -> f32 {
fn get_distance_x(plane: HalfSpace, point: Vec3A, is_orthographic: bool) -> f32 {
if is_orthographic {
point.x - plane.d()
} else {
Expand All @@ -1750,7 +1750,7 @@ fn get_distance_x(plane: Plane, point: Vec3A, is_orthographic: bool) -> f32 {
}

// NOTE: This exploits the fact that a z-plane normal has only a z component
fn project_to_plane_z(z_light: Sphere, z_plane: Plane) -> Option<Sphere> {
fn project_to_plane_z(z_light: Sphere, z_plane: HalfSpace) -> Option<Sphere> {
// p = sphere center
// n = plane normal
// d = n.p if p is in the plane
Expand All @@ -1772,7 +1772,11 @@ fn project_to_plane_z(z_light: Sphere, z_plane: Plane) -> Option<Sphere> {
}

// NOTE: This exploits the fact that a y-plane normal has only y and z components
fn project_to_plane_y(y_light: Sphere, y_plane: Plane, is_orthographic: bool) -> Option<Sphere> {
fn project_to_plane_y(
y_light: Sphere,
y_plane: HalfSpace,
is_orthographic: bool,
) -> Option<Sphere> {
let distance_to_plane = if is_orthographic {
y_plane.d() - y_light.center.y
} else {
Expand Down
66 changes: 33 additions & 33 deletions crates/bevy_render/src/primitives/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,17 @@ impl Sphere {
}
}

/// A plane defined by a unit normal and distance from the origin along the normal
/// Any point `p` is in the plane if `n.p + d = 0`
/// For planes defining half-spaces such as for frusta, if `n.p + d > 0` then `p` is on
/// A half-space defined by a unit normal and distance from the origin along the normal
/// Any point `p` is in the half-space if `n.p + d = 0`
/// When defining half-spaces such as for frusta, if `n.p + d > 0` then `p` is on
alice-i-cecile marked this conversation as resolved.
Show resolved Hide resolved
/// the positive side (inside) of the plane.
#[derive(Clone, Copy, Debug, Default)]
pub struct Plane {
pub struct HalfSpace {
normal_d: Vec4,
}

impl Plane {
/// Constructs a `Plane` from a 4D vector whose first 3 components
impl HalfSpace {
/// Constructs a `HalfSpace` from a 4D vector whose first 3 components
/// are the normal and whose last component is the distance along the normal
alice-i-cecile marked this conversation as resolved.
Show resolved Hide resolved
/// from the origin.
/// This constructor ensures that the normal is normalized and the distance is
Expand All @@ -103,21 +103,21 @@ impl Plane {
}
}

/// `Plane` unit normal
/// `HalfSpace` unit normal
#[inline]
pub fn normal(&self) -> Vec3A {
Vec3A::from(self.normal_d)
}

/// Signed distance from the origin along the unit normal such that n.p + d = 0 for point p in
/// the `Plane`
/// the `HalfSpace`
#[inline]
pub fn d(&self) -> f32 {
self.normal_d.w
}

/// `Plane` unit normal and signed distance from the origin such that n.p + d = 0 for point p
/// in the `Plane`
/// `HalfSpace` unit normal and signed distance from the origin such that n.p + d = 0 for point p
/// in the `HalfSpace`
#[inline]
pub fn normal_d(&self) -> Vec4 {
self.normal_d
Expand All @@ -131,15 +131,15 @@ impl Plane {
#[reflect(Component)]
pub struct Frustum {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see the doc string for Frustrum mention planes. Could you reword it as well?

#[reflect(ignore)]
pub planes: [Plane; 6],
pub planes: [HalfSpace; 6],
}

impl Frustum {
/// Returns a frustum derived from `view_projection`.
#[inline]
pub fn from_view_projection(view_projection: &Mat4) -> Self {
let mut frustum = Frustum::from_view_projection_no_far(view_projection);
frustum.planes[5] = Plane::new(view_projection.row(2));
frustum.planes[5] = HalfSpace::new(view_projection.row(2));
frustum
}

Expand All @@ -154,7 +154,7 @@ impl Frustum {
) -> Self {
let mut frustum = Frustum::from_view_projection_no_far(view_projection);
let far_center = *view_translation - far * *view_backward;
frustum.planes[5] = Plane::new(view_backward.extend(-view_backward.dot(far_center)));
frustum.planes[5] = HalfSpace::new(view_backward.extend(-view_backward.dot(far_center)));
frustum
}

Expand All @@ -163,10 +163,10 @@ impl Frustum {
// Rendering by Lengyel.
fn from_view_projection_no_far(view_projection: &Mat4) -> Self {
let row3 = view_projection.row(3);
let mut planes = [Plane::default(); 6];
let mut planes = [HalfSpace::default(); 6];
for (i, plane) in planes.iter_mut().enumerate().take(5) {
let row = view_projection.row(i / 2);
*plane = Plane::new(if (i & 1) == 0 && i != 4 {
*plane = HalfSpace::new(if (i & 1) == 0 && i != 4 {
row3 + row
} else {
row3 - row
Expand Down Expand Up @@ -250,12 +250,12 @@ mod tests {
fn big_frustum() -> Frustum {
Frustum {
planes: [
Plane::new(Vec4::new(-0.9701, -0.2425, -0.0000, 7.7611)),
Plane::new(Vec4::new(-0.0000, 1.0000, -0.0000, 4.0000)),
Plane::new(Vec4::new(-0.0000, -0.2425, -0.9701, 2.9104)),
Plane::new(Vec4::new(-0.0000, -1.0000, -0.0000, 4.0000)),
Plane::new(Vec4::new(-0.0000, -0.2425, 0.9701, 2.9104)),
Plane::new(Vec4::new(0.9701, -0.2425, -0.0000, -1.9403)),
HalfSpace::new(Vec4::new(-0.9701, -0.2425, -0.0000, 7.7611)),
HalfSpace::new(Vec4::new(-0.0000, 1.0000, -0.0000, 4.0000)),
HalfSpace::new(Vec4::new(-0.0000, -0.2425, -0.9701, 2.9104)),
HalfSpace::new(Vec4::new(-0.0000, -1.0000, -0.0000, 4.0000)),
HalfSpace::new(Vec4::new(-0.0000, -0.2425, 0.9701, 2.9104)),
HalfSpace::new(Vec4::new(0.9701, -0.2425, -0.0000, -1.9403)),
],
}
}
Expand Down Expand Up @@ -286,12 +286,12 @@ mod tests {
fn frustum() -> Frustum {
Frustum {
planes: [
Plane::new(Vec4::new(-0.9701, -0.2425, -0.0000, 0.7276)),
Plane::new(Vec4::new(-0.0000, 1.0000, -0.0000, 1.0000)),
Plane::new(Vec4::new(-0.0000, -0.2425, -0.9701, 0.7276)),
Plane::new(Vec4::new(-0.0000, -1.0000, -0.0000, 1.0000)),
Plane::new(Vec4::new(-0.0000, -0.2425, 0.9701, 0.7276)),
Plane::new(Vec4::new(0.9701, -0.2425, -0.0000, 0.7276)),
HalfSpace::new(Vec4::new(-0.9701, -0.2425, -0.0000, 0.7276)),
HalfSpace::new(Vec4::new(-0.0000, 1.0000, -0.0000, 1.0000)),
HalfSpace::new(Vec4::new(-0.0000, -0.2425, -0.9701, 0.7276)),
HalfSpace::new(Vec4::new(-0.0000, -1.0000, -0.0000, 1.0000)),
HalfSpace::new(Vec4::new(-0.0000, -0.2425, 0.9701, 0.7276)),
HalfSpace::new(Vec4::new(0.9701, -0.2425, -0.0000, 0.7276)),
],
}
}
Expand Down Expand Up @@ -366,12 +366,12 @@ mod tests {
fn long_frustum() -> Frustum {
Frustum {
planes: [
Plane::new(Vec4::new(-0.9998, -0.0222, -0.0000, -1.9543)),
Plane::new(Vec4::new(-0.0000, 1.0000, -0.0000, 45.1249)),
Plane::new(Vec4::new(-0.0000, -0.0168, -0.9999, 2.2718)),
Plane::new(Vec4::new(-0.0000, -1.0000, -0.0000, 45.1249)),
Plane::new(Vec4::new(-0.0000, -0.0168, 0.9999, 2.2718)),
Plane::new(Vec4::new(0.9998, -0.0222, -0.0000, 7.9528)),
HalfSpace::new(Vec4::new(-0.9998, -0.0222, -0.0000, -1.9543)),
HalfSpace::new(Vec4::new(-0.0000, 1.0000, -0.0000, 45.1249)),
HalfSpace::new(Vec4::new(-0.0000, -0.0168, -0.9999, 2.2718)),
HalfSpace::new(Vec4::new(-0.0000, -1.0000, -0.0000, 45.1249)),
HalfSpace::new(Vec4::new(-0.0000, -0.0168, 0.9999, 2.2718)),
HalfSpace::new(Vec4::new(0.9998, -0.0222, -0.0000, 7.9528)),
],
}
}
Expand Down