bevy_render/mesh/primitives/dim3/
conical_frustum.rs

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