-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
309745c
commit a2db276
Showing
37 changed files
with
235 additions
and
201 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,12 @@ | ||
mod capsule; | ||
mod cuboid; | ||
mod cylinder; | ||
mod plane; | ||
mod rect; | ||
mod sphere; | ||
mod torus; | ||
|
||
pub use capsule::*; | ||
pub use cylinder::*; | ||
pub use plane::*; | ||
pub use rect::*; | ||
pub use sphere::*; | ||
pub use torus::*; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
use bevy_math::{primitives::Rect3d, Dir3, Quat, Vec2, Vec3}; | ||
use wgpu::PrimitiveTopology; | ||
|
||
use crate::{ | ||
mesh::{Indices, Mesh, Meshable}, | ||
render_asset::RenderAssetUsages, | ||
}; | ||
|
||
/// A builder used for creating a [`Mesh`] with a [`Rect3d`] shape. | ||
#[derive(Clone, Copy, Debug, Default)] | ||
pub struct RectMeshBuilder { | ||
/// The [`Rect3d`] shape. | ||
pub rect: Rect3d, | ||
} | ||
|
||
impl RectMeshBuilder { | ||
/// Creates a new [`RectMeshBuilder`] from a given normal and size. | ||
#[inline] | ||
pub fn new(normal: Dir3, size: Vec2) -> Self { | ||
Self { | ||
rect: Rect3d { | ||
normal, | ||
half_size: size / 2.0, | ||
}, | ||
} | ||
} | ||
|
||
/// Creates a new [`RectMeshBuilder`] from the given size, with the normal pointing upwards. | ||
#[inline] | ||
pub fn from_size(size: Vec2) -> Self { | ||
Self { | ||
rect: Rect3d { | ||
half_size: size / 2.0, | ||
..Default::default() | ||
}, | ||
} | ||
} | ||
|
||
/// Creates a new [`RectMeshBuilder`] from the given length, with the normal pointing upwards, | ||
/// and the resulting [`RectMeshBuilder`] being a square. | ||
#[inline] | ||
pub fn from_length(length: f32) -> Self { | ||
Self { | ||
rect: Rect3d { | ||
half_size: Vec2::splat(length) / 2.0, | ||
..Default::default() | ||
}, | ||
} | ||
} | ||
|
||
/// Sets the normal of the rectangle, aka the direction the rectangle is facing. | ||
#[inline] | ||
#[doc(alias = "facing")] | ||
pub fn normal(mut self, normal: Dir3) -> Self { | ||
self.rect = Rect3d { | ||
normal, | ||
..self.rect | ||
}; | ||
self | ||
} | ||
|
||
/// Sets the size of the rectangle mesh. | ||
#[inline] | ||
pub fn size(mut self, width: f32, height: f32) -> Self { | ||
self.rect.half_size = Vec2::new(width, height) / 2.0; | ||
self | ||
} | ||
|
||
/// Builds a [`Mesh`] based on the configuration in `self`. | ||
pub fn build(&self) -> Mesh { | ||
let rotation = Quat::from_rotation_arc(Vec3::Y, *self.rect.normal); | ||
let positions = vec![ | ||
rotation * Vec3::new(self.rect.half_size.x, 0.0, -self.rect.half_size.y), | ||
rotation * Vec3::new(-self.rect.half_size.x, 0.0, -self.rect.half_size.y), | ||
rotation * Vec3::new(-self.rect.half_size.x, 0.0, self.rect.half_size.y), | ||
rotation * Vec3::new(self.rect.half_size.x, 0.0, self.rect.half_size.y), | ||
]; | ||
|
||
let normals = vec![self.rect.normal.to_array(); 4]; | ||
let uvs = vec![[1.0, 0.0], [0.0, 0.0], [0.0, 1.0], [1.0, 1.0]]; | ||
let indices = Indices::U32(vec![0, 1, 2, 0, 2, 3]); | ||
|
||
Mesh::new( | ||
PrimitiveTopology::TriangleList, | ||
RenderAssetUsages::default(), | ||
) | ||
.with_inserted_indices(indices) | ||
.with_inserted_attribute(Mesh::ATTRIBUTE_POSITION, positions) | ||
.with_inserted_attribute(Mesh::ATTRIBUTE_NORMAL, normals) | ||
.with_inserted_attribute(Mesh::ATTRIBUTE_UV_0, uvs) | ||
} | ||
} | ||
|
||
impl Meshable for Rect3d { | ||
type Output = RectMeshBuilder; | ||
|
||
fn mesh(&self) -> Self::Output { | ||
RectMeshBuilder { rect: *self } | ||
} | ||
} | ||
|
||
impl From<Rect3d> for Mesh { | ||
fn from(rect: Rect3d) -> Self { | ||
rect.mesh().build() | ||
} | ||
} | ||
|
||
impl From<RectMeshBuilder> for Mesh { | ||
fn from(rect: RectMeshBuilder) -> Self { | ||
rect.build() | ||
} | ||
} |
Oops, something went wrong.