-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
Add meshing for Cone
#11820
Merged
Merged
Add meshing for Cone
#11820
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
f33ab2d
Add meshing for `Cone`
Jondolf 978b128
Use variable instead of calling `.len()` several times
Jondolf 302fa5d
Fix vertex and index count
Jondolf 1665b2f
Fix normals and add more comments
Jondolf e9c367e
Fix slope and add test
Jondolf 8986195
Add a couple of comments to test
Jondolf 72fae50
Fix test name
Jondolf 5d0e9f8
Change block comments to normal comments
Jondolf 34b8617
Merge branch 'main' into cone-meshing
Jondolf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
use bevy_math::primitives::Cone; | ||
use wgpu::PrimitiveTopology; | ||
|
||
use crate::{ | ||
mesh::{Indices, Mesh, Meshable}, | ||
render_asset::RenderAssetUsages, | ||
}; | ||
|
||
/// A builder used for creating a [`Mesh`] with a [`Cone`] shape. | ||
#[derive(Clone, Copy, Debug)] | ||
pub struct ConeMeshBuilder { | ||
/// The [`Cone`] shape. | ||
pub cone: Cone, | ||
/// The number of vertices used for the base of the cone. | ||
/// | ||
/// The default is `32`. | ||
pub resolution: u32, | ||
} | ||
|
||
impl Default for ConeMeshBuilder { | ||
fn default() -> Self { | ||
Self { | ||
cone: Cone::default(), | ||
resolution: 32, | ||
} | ||
} | ||
} | ||
|
||
impl ConeMeshBuilder { | ||
/// Creates a new [`ConeMeshBuilder`] from a given radius, height, | ||
/// and number of vertices used for the base of the cone. | ||
#[inline] | ||
pub const fn new(radius: f32, height: f32, resolution: u32) -> Self { | ||
Self { | ||
cone: Cone { radius, height }, | ||
resolution, | ||
} | ||
} | ||
|
||
/// Sets the number of vertices used for the base of the cone. | ||
#[inline] | ||
pub const fn resolution(mut self, resolution: u32) -> Self { | ||
self.resolution = resolution; | ||
self | ||
} | ||
|
||
/// Builds a [`Mesh`] based on the configuration in `self`. | ||
pub fn build(&self) -> Mesh { | ||
let half_height = self.cone.height / 2.0; | ||
|
||
let num_vertices = self.resolution as usize * 2 + 1; | ||
let num_indices = self.resolution as usize * 3; | ||
Jondolf marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
let mut positions = Vec::with_capacity(num_vertices); | ||
let mut normals = Vec::with_capacity(num_vertices); | ||
let mut uvs = Vec::with_capacity(num_vertices); | ||
let mut indices = Vec::with_capacity(num_indices); | ||
|
||
// Tip | ||
positions.push([0.0, half_height, 0.0]); | ||
|
||
// The tip doesn't have a singular normal that works correctly. | ||
// We use an invalid normal here so that it becomes NaN in the fragment shader | ||
// and doesn't affect the overall shading. This might seem hacky, but it's one of | ||
// the only ways to get perfectly smooth cones without creases or other shading artefacts. | ||
// | ||
// Note that this requires that normals are not normalized in the vertex shader, | ||
// as that would make the entire triangle invalid and make the cone appear as black. | ||
normals.push([0.0, 0.0, 0.0]); | ||
|
||
uvs.push([0.5, 0.5]); | ||
|
||
// Lateral surface, i.e. the side of the cone | ||
let step_theta = std::f32::consts::TAU / self.resolution as f32; | ||
for segment in 0..=self.resolution { | ||
let theta = segment as f32 * step_theta; | ||
let (sin, cos) = theta.sin_cos(); | ||
|
||
positions.push([self.cone.radius * cos, -half_height, self.cone.radius * sin]); | ||
normals.push([cos, 0., sin]); | ||
Jondolf marked this conversation as resolved.
Show resolved
Hide resolved
|
||
uvs.push([0.5 + cos * 0.5, 0.5 + sin * 0.5]); | ||
} | ||
|
||
for j in 0..self.resolution { | ||
indices.extend_from_slice(&[0, j + 1, j]); | ||
} | ||
|
||
indices.extend(&[0, positions.len() as u32 - 1, positions.len() as u32 - 2]); | ||
|
||
// Base | ||
let index_offset = positions.len() as u32; | ||
alice-i-cecile marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
for i in 0..self.resolution { | ||
let theta = i as f32 * step_theta; | ||
let (sin, cos) = theta.sin_cos(); | ||
|
||
positions.push([cos * self.cone.radius, -half_height, sin * self.cone.radius]); | ||
normals.push([0.0, -1.0, 0.0]); | ||
uvs.push([0.5 * (cos + 1.0), 1.0 - 0.5 * (sin + 1.0)]); | ||
} | ||
|
||
for i in 1..(self.resolution - 1) { | ||
indices.extend_from_slice(&[index_offset, index_offset + i, index_offset + i + 1]); | ||
} | ||
|
||
Mesh::new( | ||
PrimitiveTopology::TriangleList, | ||
RenderAssetUsages::default(), | ||
) | ||
.with_inserted_indices(Indices::U32(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 Cone { | ||
type Output = ConeMeshBuilder; | ||
|
||
fn mesh(&self) -> Self::Output { | ||
ConeMeshBuilder { | ||
cone: *self, | ||
..Default::default() | ||
} | ||
} | ||
} | ||
|
||
impl From<Cone> for Mesh { | ||
fn from(cone: Cone) -> Self { | ||
cone.mesh().build() | ||
} | ||
} | ||
|
||
impl From<ConeMeshBuilder> for Mesh { | ||
fn from(cone: ConeMeshBuilder) -> Self { | ||
cone.build() | ||
} | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Diagrams for positions, normals, and UVs would make the math easier to follow.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For now I just added better comments explaining the math and logic. I can maybe try making some simple diagrams too if I can make clean ones, but personally I think text is probably enough here.