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

Add cone, make cylinder special case of a cone #10298

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions assets/docs/Cone.drawio
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<mxfile host="app.diagrams.net" modified="2023-11-04T10:06:46.179Z" agent="Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/119.0" etag="GIrsQCNnqk9fznUI_tQd" version="22.0.8" type="device">
<diagram name="Page-1" id="KndnemwuQtrnsTKoXNmA">
<mxGraphModel dx="1648" dy="1542" grid="0" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="850" pageHeight="1100" math="0" shadow="0">
<root>
<mxCell id="0" />
<mxCell id="1" parent="0" />
<mxCell id="tZ0rMIF4X-gDuEOUvdpK-2" value="" style="verticalLabelPosition=bottom;verticalAlign=top;html=1;shape=mxgraph.basic.cone;strokeWidth=2;" vertex="1" parent="1">
<mxGeometry x="320" y="440" width="120" height="120" as="geometry" />
</mxCell>
<mxCell id="tZ0rMIF4X-gDuEOUvdpK-3" value="" style="shape=curlyBracket;whiteSpace=wrap;html=1;rounded=1;flipH=1;labelPosition=right;verticalLabelPosition=middle;align=left;verticalAlign=middle;opacity=20;" vertex="1" parent="1">
<mxGeometry x="440" y="440" width="20" height="120" as="geometry" />
</mxCell>
<mxCell id="tZ0rMIF4X-gDuEOUvdpK-8" value="" style="endArrow=classic;html=1;rounded=0;fillColor=#f8cecc;strokeColor=#b85450;opacity=40;" edge="1" parent="1">
<mxGeometry width="50" height="50" relative="1" as="geometry">
<mxPoint x="240" y="500" as="sourcePoint" />
<mxPoint x="560" y="500" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="tZ0rMIF4X-gDuEOUvdpK-4" value="height" style="text;html=1;align=center;verticalAlign=middle;resizable=0;points=[];autosize=1;strokeColor=none;fillColor=none;fontSize=10;fontFamily=Lucida Console;textOpacity=30;" vertex="1" parent="1">
<mxGeometry x="450" y="450" width="50" height="30" as="geometry" />
</mxCell>
<mxCell id="tZ0rMIF4X-gDuEOUvdpK-6" value="" style="shape=curlyBracket;whiteSpace=wrap;html=1;rounded=1;flipH=1;labelPosition=right;verticalLabelPosition=middle;align=left;verticalAlign=middle;opacity=20;rotation=90;" vertex="1" parent="1">
<mxGeometry x="372.5" y="515" width="20" height="120" as="geometry" />
</mxCell>
<mxCell id="tZ0rMIF4X-gDuEOUvdpK-7" value="bottom radius" style="text;html=1;align=center;verticalAlign=middle;resizable=0;points=[];autosize=1;strokeColor=none;fillColor=none;fontSize=10;fontFamily=Lucida Console;textOpacity=30;" vertex="1" parent="1">
<mxGeometry x="372.5" y="570" width="90" height="30" as="geometry" />
</mxCell>
<mxCell id="tZ0rMIF4X-gDuEOUvdpK-10" value="" style="endArrow=classic;html=1;rounded=0;fillColor=#d5e8d4;strokeColor=#82b366;opacity=40;" edge="1" parent="1">
<mxGeometry width="50" height="50" relative="1" as="geometry">
<mxPoint x="379.83" y="620" as="sourcePoint" />
<mxPoint x="379.83" y="380" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="tZ0rMIF4X-gDuEOUvdpK-11" value="x" style="text;html=1;align=center;verticalAlign=middle;resizable=0;points=[];autosize=1;strokeColor=none;fillColor=none;fontColor=#ed333b;textOpacity=50;" vertex="1" parent="1">
<mxGeometry x="540" y="500" width="30" height="30" as="geometry" />
</mxCell>
<mxCell id="tZ0rMIF4X-gDuEOUvdpK-13" value="y" style="text;html=1;align=center;verticalAlign=middle;resizable=0;points=[];autosize=1;strokeColor=none;fillColor=none;fontColor=#25A23E;textOpacity=40;" vertex="1" parent="1">
<mxGeometry x="380" y="380" width="30" height="30" as="geometry" />
</mxCell>
<mxCell id="tZ0rMIF4X-gDuEOUvdpK-15" value="&amp;nbsp;" style="text;whiteSpace=wrap;html=1;" vertex="1" parent="1">
<mxGeometry x="-320" y="-80" width="40" height="40" as="geometry" />
</mxCell>
<mxCell id="tZ0rMIF4X-gDuEOUvdpK-18" value="" style="shape=waypoint;sketch=0;fillStyle=solid;size=6;pointerEvents=1;points=[];fillColor=none;resizable=0;rotatable=0;perimeter=centerPerimeter;snapToPoint=1;" vertex="1" parent="1">
<mxGeometry x="392.5" y="550" width="20" height="20" as="geometry" />
</mxCell>
<mxCell id="tZ0rMIF4X-gDuEOUvdpK-19" value="" style="shape=waypoint;sketch=0;fillStyle=solid;size=6;pointerEvents=1;points=[];fillColor=none;resizable=0;rotatable=0;perimeter=centerPerimeter;snapToPoint=1;" vertex="1" parent="1">
<mxGeometry x="360" y="550" width="20" height="20" as="geometry" />
</mxCell>
<mxCell id="tZ0rMIF4X-gDuEOUvdpK-20" value="" style="shape=waypoint;fillStyle=solid;size=6;pointerEvents=1;points=[];fillColor=none;resizable=0;rotatable=0;perimeter=centerPerimeter;snapToPoint=0;" vertex="1" parent="1">
<mxGeometry x="330" y="547" width="20" height="20" as="geometry" />
</mxCell>
<mxCell id="tZ0rMIF4X-gDuEOUvdpK-21" value="" style="shape=waypoint;fillStyle=solid;size=6;pointerEvents=1;points=[];fillColor=none;resizable=0;rotatable=0;perimeter=centerPerimeter;snapToPoint=0;" vertex="1" parent="1">
<mxGeometry x="420" y="545" width="20" height="20" as="geometry" />
</mxCell>
<mxCell id="tZ0rMIF4X-gDuEOUvdpK-22" value="" style="shape=waypoint;fillStyle=solid;size=6;pointerEvents=1;points=[];fillColor=none;resizable=0;rotatable=0;perimeter=centerPerimeter;snapToPoint=0;" vertex="1" parent="1">
<mxGeometry x="407.5" y="530" width="20" height="20" as="geometry" />
</mxCell>
<mxCell id="tZ0rMIF4X-gDuEOUvdpK-23" value="" style="shape=waypoint;fillStyle=solid;size=6;pointerEvents=1;points=[];fillColor=none;resizable=0;rotatable=0;perimeter=centerPerimeter;snapToPoint=0;" vertex="1" parent="1">
<mxGeometry x="370" y="527" width="20" height="20" as="geometry" />
</mxCell>
<mxCell id="tZ0rMIF4X-gDuEOUvdpK-24" value="" style="shape=waypoint;fillStyle=solid;size=6;pointerEvents=1;points=[];fillColor=none;resizable=0;rotatable=0;perimeter=centerPerimeter;snapToPoint=0;" vertex="1" parent="1">
<mxGeometry x="340" y="530" width="20" height="20" as="geometry" />
</mxCell>
<mxCell id="tZ0rMIF4X-gDuEOUvdpK-25" value="" style="shape=waypoint;fillStyle=solid;size=6;pointerEvents=1;points=[];fillColor=none;resizable=0;rotatable=0;perimeter=centerPerimeter;snapToPoint=0;" vertex="1" parent="1">
<mxGeometry x="310" y="537" width="20" height="20" as="geometry" />
</mxCell>
<mxCell id="tZ0rMIF4X-gDuEOUvdpK-27" value="" style="endArrow=none;html=1;strokeColor=#000000;curved=1;endFill=0;opacity=80;fillColor=#e1d5e7;dashed=1;" edge="1" parent="1">
<mxGeometry width="50" height="50" relative="1" as="geometry">
<mxPoint x="420" y="510" as="sourcePoint" />
<mxPoint x="340" y="510" as="targetPoint" />
<Array as="points">
<mxPoint x="380" y="520" />
</Array>
</mxGeometry>
</mxCell>
<mxCell id="tZ0rMIF4X-gDuEOUvdpK-28" value="" style="endArrow=none;html=1;strokeColor=#000000;curved=1;endFill=0;opacity=80;fillColor=#e1d5e7;exitX=0.664;exitY=0.304;exitDx=0;exitDy=0;exitPerimeter=0;entryX=0.328;entryY=0.313;entryDx=0;entryDy=0;entryPerimeter=0;dashed=1;" edge="1" parent="1" source="tZ0rMIF4X-gDuEOUvdpK-2" target="tZ0rMIF4X-gDuEOUvdpK-2">
<mxGeometry width="50" height="50" relative="1" as="geometry">
<mxPoint x="420" y="480" as="sourcePoint" />
<mxPoint x="340" y="480" as="targetPoint" />
<Array as="points">
<mxPoint x="380" y="480" />
</Array>
</mxGeometry>
</mxCell>
</root>
</mxGraphModel>
</diagram>
</mxfile>
Binary file added assets/docs/Cone.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
173 changes: 173 additions & 0 deletions crates/bevy_render/src/mesh/shape/cone.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
use crate::mesh::{Indices, Mesh};
use bevy_math::Vec3;
use wgpu::PrimitiveTopology;

/// A cone which stands on the XZ plane.
///
/// If either radius is 0., the tip will be sharp. Otherwise, the cone will appear truncated, with flat surfaces on either end.
///
/// # Diagram
///
/// See an illustrative diagram [here](https://github.com/bevyengine/bevy/blob/main/assets/docs/Cone.png).
/// The diagram illustrates the following properties:
///
/// * The height of the cone is evenly split by the XZ plane, so half height above, half below.
/// * A resolution of 8 means each horizontal (XZ) face has 8 vertices.
/// * A top radius of 0.0 means a sharp tip.
/// * 3 segments (divided by the dotted lines) means there are 3 segments of quads enveloping the cone.
///
/// Note that the sharp tip still contains the same amount of vertices as the bottom.
#[derive(Clone, Copy, Debug)]
pub struct Cone {
/// Radius in the XZ plane at the upper face.
///
/// Can be zero for a sharp tip, but both top and bottom should not be zero at the same time.
pub top_radius: f32,

/// Radius in the XZ plane at the bottom face.
///
/// Can be zero for a sharp tip, but both top and bottom should not be zero at the same time.
pub bottom_radius: f32,

/// Height of the cone in the Y axis.
///
/// Should be greater than zero.
pub height: f32,

/// The number of vertices around each horizontal slice of the cone. If you are looking at the cone from
/// above, this is the number of points you will see on the circle.
/// A higher number will make it appear more circular.
///
/// Should be above two.
pub resolution: u32,

/// The number of segments between the two ends. Setting this to 1 will have triangles spanning the full
/// height of the cone. Setting it to 2 will have two sets of triangles with a horizontal slice in the middle of
/// the cone. Greater numbers increase triangles/slices in the same way.
///
/// Should be greater than zero.
pub segments: u32,
}

impl Default for Cone {
fn default() -> Self {
Self {
top_radius: 0.0,
bottom_radius: 0.5,
height: 1.0,
resolution: 64,
segments: 4,
}
}
}

impl From<Cone> for Mesh {
fn from(c: Cone) -> Self {
debug_assert!(c.top_radius >= 0.0);
debug_assert!(c.bottom_radius >= 0.0);
debug_assert!(!(c.bottom_radius == 0.0 && c.top_radius == 0.0));
debug_assert!(c.height > 0.0);
debug_assert!(c.resolution > 2);
debug_assert!(c.segments > 0);

let num_rings = c.segments + 1;
let num_vertices = c.resolution * 2 + num_rings * (c.resolution + 1);
let num_faces = c.resolution * (num_rings - 2);
let num_indices = (2 * num_faces + 2 * (c.resolution - 1) * 2) * 3;

let mut positions = Vec::with_capacity(num_vertices as usize);
let mut normals = Vec::with_capacity(num_vertices as usize);
let mut uvs = Vec::with_capacity(num_vertices as usize);
let mut indices = Vec::with_capacity(num_indices as usize);

let step_theta = std::f32::consts::TAU / c.resolution as f32;
let step_y = c.height / c.segments as f32;
let step_radius = (c.top_radius - c.bottom_radius) / c.segments as f32;

// rings

for ring in 0..num_rings {
let y = -c.height / 2.0 + ring as f32 * step_y;
let radius = c.bottom_radius + ring as f32 * step_radius;

for segment in 0..=c.resolution {
let theta = segment as f32 * step_theta;
let (sin, cos) = theta.sin_cos();

positions.push([radius * cos, y, radius * sin]);
normals.push(
Vec3::new(cos, (c.bottom_radius - c.top_radius) / c.height, sin)
.normalize()
.to_array(),
);
uvs.push([
segment as f32 / c.resolution as f32,
ring as f32 / c.segments as f32,
]);
}
}

// barrel skin

for i in 0..c.segments {
let ring = i * (c.resolution + 1);
let next_ring = (i + 1) * (c.resolution + 1);

for j in 0..c.resolution {
indices.extend_from_slice(&[
ring + j,
next_ring + j,
ring + j + 1,
next_ring + j,
next_ring + j + 1,
ring + j + 1,
]);
}
}

// caps

let mut build_cap = |top: bool| {
torsteingrindvik marked this conversation as resolved.
Show resolved Hide resolved
let offset = positions.len() as u32;
let (y, normal_y, winding) = if top {
(c.height / 2., 1., (1, 0))
} else {
(c.height / -2., -1., (0, 1))
};

let radius = if top { c.top_radius } else { c.bottom_radius };

if radius == 0.0 {
return;
}

for i in 0..c.resolution {
let theta = i as f32 * step_theta;
let (sin, cos) = theta.sin_cos();

positions.push([cos * radius, y, sin * radius]);
normals.push([0.0, normal_y, 0.0]);
uvs.push([0.5 * (cos + 1.0), 1.0 - 0.5 * (sin + 1.0)]);
}

for i in 1..(c.resolution - 1) {
indices.extend_from_slice(&[
offset,
offset + i + winding.0,
offset + i + winding.1,
]);
}
};

// top

build_cap(true);
build_cap(false);

Mesh::new(PrimitiveTopology::TriangleList)
.with_indices(Some(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)
}
}
104 changes: 11 additions & 93 deletions crates/bevy_render/src/mesh/shape/cylinder.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use crate::mesh::{Indices, Mesh};
use wgpu::PrimitiveTopology;
use crate::mesh::Mesh;

use super::Cone;

/// A cylinder which stands on the XZ plane
#[derive(Clone, Copy, Debug)]
pub struct Cylinder {
/// Radius in the XZ plane.
pub radius: f32,

/// Height of the cylinder in the Y axis.
pub height: f32,
/// The number of vertices around each horizontal slice of the cylinder. If you are looking at the cylinder from
Expand All @@ -31,97 +33,13 @@ impl Default for Cylinder {

impl From<Cylinder> for Mesh {
fn from(c: Cylinder) -> Self {
debug_assert!(c.radius > 0.0);
debug_assert!(c.height > 0.0);
debug_assert!(c.resolution > 2);
debug_assert!(c.segments > 0);

let num_rings = c.segments + 1;
let num_vertices = c.resolution * 2 + num_rings * (c.resolution + 1);
let num_faces = c.resolution * (num_rings - 2);
let num_indices = (2 * num_faces + 2 * (c.resolution - 1) * 2) * 3;

let mut positions = Vec::with_capacity(num_vertices as usize);
let mut normals = Vec::with_capacity(num_vertices as usize);
let mut uvs = Vec::with_capacity(num_vertices as usize);
let mut indices = Vec::with_capacity(num_indices as usize);

let step_theta = std::f32::consts::TAU / c.resolution as f32;
let step_y = c.height / c.segments as f32;

// rings

for ring in 0..num_rings {
let y = -c.height / 2.0 + ring as f32 * step_y;

for segment in 0..=c.resolution {
let theta = segment as f32 * step_theta;
let (sin, cos) = theta.sin_cos();

positions.push([c.radius * cos, y, c.radius * sin]);
normals.push([cos, 0., sin]);
uvs.push([
segment as f32 / c.resolution as f32,
ring as f32 / c.segments as f32,
]);
}
Cone {
top_radius: c.radius,
bottom_radius: c.radius,
height: c.height,
resolution: c.resolution,
segments: c.segments,
}

// barrel skin

for i in 0..c.segments {
let ring = i * (c.resolution + 1);
let next_ring = (i + 1) * (c.resolution + 1);

for j in 0..c.resolution {
indices.extend_from_slice(&[
ring + j,
next_ring + j,
ring + j + 1,
next_ring + j,
next_ring + j + 1,
ring + j + 1,
]);
}
}

// caps

let mut build_cap = |top: bool| {
let offset = positions.len() as u32;
let (y, normal_y, winding) = if top {
(c.height / 2., 1., (1, 0))
} else {
(c.height / -2., -1., (0, 1))
};

for i in 0..c.resolution {
let theta = i as f32 * step_theta;
let (sin, cos) = theta.sin_cos();

positions.push([cos * c.radius, y, sin * c.radius]);
normals.push([0.0, normal_y, 0.0]);
uvs.push([0.5 * (cos + 1.0), 1.0 - 0.5 * (sin + 1.0)]);
}

for i in 1..(c.resolution - 1) {
indices.extend_from_slice(&[
offset,
offset + i + winding.0,
offset + i + winding.1,
]);
}
};

// top

build_cap(true);
build_cap(false);

Mesh::new(PrimitiveTopology::TriangleList)
.with_indices(Some(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)
.into()
}
}
2 changes: 2 additions & 0 deletions crates/bevy_render/src/mesh/shape/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,13 +262,15 @@ impl From<Plane> for Mesh {
}

mod capsule;
mod cone;
mod cylinder;
mod icosphere;
mod regular_polygon;
mod torus;
mod uvsphere;

pub use capsule::{Capsule, CapsuleUvProfile};
pub use cone::Cone;
pub use cylinder::Cylinder;
pub use icosphere::Icosphere;
pub use regular_polygon::{Circle, RegularPolygon};
Expand Down
Loading