bevy_render/mesh/primitives/dim3/
sphere.rs1use std::f32::consts::PI;
2
3use crate::{
4 mesh::{Indices, Mesh, MeshBuilder, Meshable},
5 render_asset::RenderAssetUsages,
6};
7use bevy_math::primitives::Sphere;
8use hexasphere::shapes::IcoSphere;
9use thiserror::Error;
10use wgpu::PrimitiveTopology;
11
12#[derive(Clone, Copy, Debug, Error)]
14pub enum IcosphereError {
15 #[error("Cannot create an icosphere of {subdivisions} subdivisions due to there being too many vertices being generated: {number_of_resulting_points}. (Limited to 65535 vertices or 79 subdivisions)")]
17 TooManyVertices {
18 subdivisions: usize,
20 number_of_resulting_points: usize,
22 },
23}
24
25#[derive(Clone, Copy, Debug)]
27pub enum SphereKind {
28 Ico {
30 subdivisions: usize,
33 },
34 Uv {
37 #[doc(alias = "horizontal_resolution")]
39 sectors: usize,
40 #[doc(alias = "vertical_resolution")]
42 stacks: usize,
43 },
44}
45
46impl Default for SphereKind {
47 fn default() -> Self {
48 Self::Ico { subdivisions: 5 }
49 }
50}
51
52#[derive(Clone, Copy, Debug, Default)]
54pub struct SphereMeshBuilder {
55 pub sphere: Sphere,
57 pub kind: SphereKind,
59}
60
61impl SphereMeshBuilder {
62 #[inline]
64 pub const fn new(radius: f32, kind: SphereKind) -> Self {
65 Self {
66 sphere: Sphere { radius },
67 kind,
68 }
69 }
70
71 #[inline]
73 pub const fn kind(mut self, kind: SphereKind) -> Self {
74 self.kind = kind;
75 self
76 }
77
78 pub fn ico(&self, subdivisions: usize) -> Result<Mesh, IcosphereError> {
86 if subdivisions >= 80 {
87 let temp = subdivisions + 1;
116 let number_of_resulting_points = temp * temp * 10 + 2;
117 return Err(IcosphereError::TooManyVertices {
118 subdivisions,
119 number_of_resulting_points,
120 });
121 }
122 let generated = IcoSphere::new(subdivisions, |point| {
123 let inclination = point.y.acos();
124 let azimuth = point.z.atan2(point.x);
125
126 let norm_inclination = inclination / std::f32::consts::PI;
127 let norm_azimuth = 0.5 - (azimuth / std::f32::consts::TAU);
128
129 [norm_azimuth, norm_inclination]
130 });
131
132 let raw_points = generated.raw_points();
133
134 let points = raw_points
135 .iter()
136 .map(|&p| (p * self.sphere.radius).into())
137 .collect::<Vec<[f32; 3]>>();
138
139 let normals = raw_points
140 .iter()
141 .copied()
142 .map(Into::into)
143 .collect::<Vec<[f32; 3]>>();
144
145 let uvs = generated.raw_data().to_owned();
146
147 let mut indices = Vec::with_capacity(generated.indices_per_main_triangle() * 20);
148
149 for i in 0..20 {
150 generated.get_indices(i, &mut indices);
151 }
152
153 let indices = Indices::U32(indices);
154
155 Ok(Mesh::new(
156 PrimitiveTopology::TriangleList,
157 RenderAssetUsages::default(),
158 )
159 .with_inserted_indices(indices)
160 .with_inserted_attribute(Mesh::ATTRIBUTE_POSITION, points)
161 .with_inserted_attribute(Mesh::ATTRIBUTE_NORMAL, normals)
162 .with_inserted_attribute(Mesh::ATTRIBUTE_UV_0, uvs))
163 }
164
165 pub fn uv(&self, sectors: usize, stacks: usize) -> Mesh {
170 let sectors_f32 = sectors as f32;
173 let stacks_f32 = stacks as f32;
174 let length_inv = 1. / self.sphere.radius;
175 let sector_step = 2. * PI / sectors_f32;
176 let stack_step = PI / stacks_f32;
177
178 let mut vertices: Vec<[f32; 3]> = Vec::with_capacity(stacks * sectors);
179 let mut normals: Vec<[f32; 3]> = Vec::with_capacity(stacks * sectors);
180 let mut uvs: Vec<[f32; 2]> = Vec::with_capacity(stacks * sectors);
181 let mut indices: Vec<u32> = Vec::with_capacity(stacks * sectors * 2 * 3);
182
183 for i in 0..stacks + 1 {
184 let stack_angle = PI / 2. - (i as f32) * stack_step;
185 let xy = self.sphere.radius * stack_angle.cos();
186 let z = self.sphere.radius * stack_angle.sin();
187
188 for j in 0..sectors + 1 {
189 let sector_angle = (j as f32) * sector_step;
190 let x = xy * sector_angle.cos();
191 let y = xy * sector_angle.sin();
192
193 vertices.push([x, y, z]);
194 normals.push([x * length_inv, y * length_inv, z * length_inv]);
195 uvs.push([(j as f32) / sectors_f32, (i as f32) / stacks_f32]);
196 }
197 }
198
199 for i in 0..stacks {
205 let mut k1 = i * (sectors + 1);
206 let mut k2 = k1 + sectors + 1;
207 for _j in 0..sectors {
208 if i != 0 {
209 indices.push(k1 as u32);
210 indices.push(k2 as u32);
211 indices.push((k1 + 1) as u32);
212 }
213 if i != stacks - 1 {
214 indices.push((k1 + 1) as u32);
215 indices.push(k2 as u32);
216 indices.push((k2 + 1) as u32);
217 }
218 k1 += 1;
219 k2 += 1;
220 }
221 }
222
223 Mesh::new(
224 PrimitiveTopology::TriangleList,
225 RenderAssetUsages::default(),
226 )
227 .with_inserted_indices(Indices::U32(indices))
228 .with_inserted_attribute(Mesh::ATTRIBUTE_POSITION, vertices)
229 .with_inserted_attribute(Mesh::ATTRIBUTE_NORMAL, normals)
230 .with_inserted_attribute(Mesh::ATTRIBUTE_UV_0, uvs)
231 }
232}
233
234impl MeshBuilder for SphereMeshBuilder {
235 fn build(&self) -> Mesh {
242 match self.kind {
243 SphereKind::Ico { subdivisions } => self.ico(subdivisions).unwrap(),
244 SphereKind::Uv { sectors, stacks } => self.uv(sectors, stacks),
245 }
246 }
247}
248
249impl Meshable for Sphere {
250 type Output = SphereMeshBuilder;
251
252 fn mesh(&self) -> Self::Output {
253 SphereMeshBuilder {
254 sphere: *self,
255 ..Default::default()
256 }
257 }
258}
259
260impl From<Sphere> for Mesh {
261 fn from(sphere: Sphere) -> Self {
262 sphere.mesh().build()
263 }
264}