bevy_mesh/primitives/dim3/
conical_frustum.rs

1use crate::{Indices, Mesh, MeshBuilder, Meshable};
2use bevy_asset::RenderAssetUsages;
3use bevy_math::{ops, primitives::ConicalFrustum, Vec3};
4use wgpu::PrimitiveTopology;
5
6/// A builder used for creating a [`Mesh`] with a [`ConicalFrustum`] shape.
7#[derive(Clone, Copy, Debug)]
8pub struct ConicalFrustumMeshBuilder {
9    /// The [`ConicalFrustum`] shape.
10    pub frustum: ConicalFrustum,
11    /// The number of vertices used for the top and bottom of the conical frustum.
12    ///
13    /// The default is `32`.
14    pub resolution: u32,
15    /// The number of horizontal lines subdividing the lateral surface of the conical frustum.
16    ///
17    /// The default is `1`.
18    pub segments: u32,
19}
20
21impl Default for ConicalFrustumMeshBuilder {
22    fn default() -> Self {
23        Self {
24            frustum: ConicalFrustum::default(),
25            resolution: 32,
26            segments: 1,
27        }
28    }
29}
30
31impl ConicalFrustumMeshBuilder {
32    /// Creates a new [`ConicalFrustumMeshBuilder`] from the given top and bottom radii, a height,
33    /// and a resolution used for the top and bottom.
34    #[inline]
35    pub const fn new(radius_top: f32, radius_bottom: f32, height: f32, resolution: u32) -> Self {
36        Self {
37            frustum: ConicalFrustum {
38                radius_top,
39                radius_bottom,
40                height,
41            },
42            resolution,
43            segments: 1,
44        }
45    }
46
47    /// Sets the number of vertices used for the top and bottom of the conical frustum.
48    #[inline]
49    pub const fn resolution(mut self, resolution: u32) -> Self {
50        self.resolution = resolution;
51        self
52    }
53
54    /// Sets the number of horizontal lines subdividing the lateral surface of the conical frustum.
55    #[inline]
56    pub const fn segments(mut self, segments: u32) -> Self {
57        self.segments = segments;
58        self
59    }
60}
61
62impl MeshBuilder for ConicalFrustumMeshBuilder {
63    fn build(&self) -> Mesh {
64        debug_assert!(self.resolution > 2);
65        debug_assert!(self.segments > 0);
66
67        let ConicalFrustum {
68            radius_top,
69            radius_bottom,
70            height,
71        } = self.frustum;
72        let half_height = height / 2.0;
73
74        let num_rings = self.segments + 1;
75        let num_vertices = (self.resolution * 2 + num_rings * (self.resolution + 1)) as usize;
76        let num_faces = self.resolution * (num_rings - 2);
77        let num_indices = ((2 * num_faces + 2 * (self.resolution - 1) * 2) * 3) as usize;
78
79        let mut positions = Vec::with_capacity(num_vertices);
80        let mut normals = Vec::with_capacity(num_vertices);
81        let mut uvs = Vec::with_capacity(num_vertices);
82        let mut indices = Vec::with_capacity(num_indices);
83
84        let step_theta = core::f32::consts::TAU / self.resolution as f32;
85        let step_y = height / self.segments as f32;
86        let step_radius = (radius_top - radius_bottom) / self.segments as f32;
87
88        // Rings
89        for ring in 0..num_rings {
90            let y = -half_height + ring as f32 * step_y;
91            let radius = radius_bottom + ring as f32 * step_radius;
92
93            for segment in 0..=self.resolution {
94                let theta = segment as f32 * step_theta;
95                let (sin, cos) = ops::sin_cos(theta);
96
97                positions.push([radius * cos, y, radius * sin]);
98                normals.push(
99                    Vec3::new(cos, (radius_bottom - radius_top) / height, sin)
100                        .normalize()
101                        .to_array(),
102                );
103                uvs.push([
104                    segment as f32 / self.resolution as f32,
105                    ring as f32 / self.segments as f32,
106                ]);
107            }
108        }
109
110        // Lateral surface
111        for i in 0..self.segments {
112            let ring = i * (self.resolution + 1);
113            let next_ring = (i + 1) * (self.resolution + 1);
114
115            for j in 0..self.resolution {
116                indices.extend_from_slice(&[
117                    ring + j,
118                    next_ring + j,
119                    ring + j + 1,
120                    next_ring + j,
121                    next_ring + j + 1,
122                    ring + j + 1,
123                ]);
124            }
125        }
126
127        // Caps
128        let mut build_cap = |top: bool, radius: f32| {
129            let offset = positions.len() as u32;
130            let (y, normal_y, winding) = if top {
131                (half_height, 1.0, (1, 0))
132            } else {
133                (-half_height, -1.0, (0, 1))
134            };
135
136            for i in 0..self.resolution {
137                let theta = i as f32 * step_theta;
138                let (sin, cos) = ops::sin_cos(theta);
139
140                positions.push([cos * radius, y, sin * radius]);
141                normals.push([0.0, normal_y, 0.0]);
142                uvs.push([0.5 * (cos + 1.0), 1.0 - 0.5 * (sin + 1.0)]);
143            }
144
145            for i in 1..(self.resolution - 1) {
146                indices.extend_from_slice(&[
147                    offset,
148                    offset + i + winding.0,
149                    offset + i + winding.1,
150                ]);
151            }
152        };
153
154        build_cap(true, radius_top);
155        build_cap(false, radius_bottom);
156
157        Mesh::new(
158            PrimitiveTopology::TriangleList,
159            RenderAssetUsages::default(),
160        )
161        .with_inserted_indices(Indices::U32(indices))
162        .with_inserted_attribute(Mesh::ATTRIBUTE_POSITION, positions)
163        .with_inserted_attribute(Mesh::ATTRIBUTE_NORMAL, normals)
164        .with_inserted_attribute(Mesh::ATTRIBUTE_UV_0, uvs)
165    }
166}
167
168impl Meshable for ConicalFrustum {
169    type Output = ConicalFrustumMeshBuilder;
170
171    fn mesh(&self) -> Self::Output {
172        ConicalFrustumMeshBuilder {
173            frustum: *self,
174            ..Default::default()
175        }
176    }
177}
178
179impl From<ConicalFrustum> for Mesh {
180    fn from(frustum: ConicalFrustum) -> Self {
181        frustum.mesh().build()
182    }
183}