bevy_mesh/primitives/dim3/
sphere.rs1use crate::{Indices, Mesh, MeshBuilder, Meshable, PrimitiveTopology};
2use bevy_asset::RenderAssetUsages;
3use bevy_math::{ops, primitives::Sphere};
4use bevy_reflect::prelude::*;
5use core::f32::consts::PI;
6use hexasphere::shapes::IcoSphere;
7use thiserror::Error;
8
9#[derive(Clone, Copy, Debug, Error)]
11pub enum IcosphereError {
12 #[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)")]
14 TooManyVertices {
15 subdivisions: u32,
17 number_of_resulting_points: u32,
19 },
20}
21
22#[derive(Clone, Copy, Debug, Reflect)]
24#[reflect(Default, Debug, Clone)]
25pub enum SphereKind {
26 Ico {
28 subdivisions: u32,
31 },
32 Uv {
35 #[doc(alias = "horizontal_resolution")]
37 sectors: u32,
38 #[doc(alias = "vertical_resolution")]
40 stacks: u32,
41 },
42}
43
44impl Default for SphereKind {
45 fn default() -> Self {
46 Self::Ico { subdivisions: 5 }
47 }
48}
49
50#[derive(Clone, Copy, Debug, Default, Reflect)]
52#[reflect(Default, Debug, Clone)]
53pub struct SphereMeshBuilder {
54 pub sphere: Sphere,
56 pub kind: SphereKind,
58}
59
60impl SphereMeshBuilder {
61 #[inline]
63 pub const fn new(radius: f32, kind: SphereKind) -> Self {
64 Self {
65 sphere: Sphere { radius },
66 kind,
67 }
68 }
69
70 #[inline]
72 pub const fn kind(mut self, kind: SphereKind) -> Self {
73 self.kind = kind;
74 self
75 }
76
77 pub fn ico(&self, subdivisions: u32) -> Result<Mesh, IcosphereError> {
85 if subdivisions >= 80 {
86 let temp = subdivisions + 1;
115 let number_of_resulting_points = temp * temp * 10 + 2;
116 return Err(IcosphereError::TooManyVertices {
117 subdivisions,
118 number_of_resulting_points,
119 });
120 }
121 let generated = IcoSphere::new(subdivisions as usize, |point| {
122 let inclination = ops::acos(point.y);
123 let azimuth = ops::atan2(point.z, point.x);
124
125 let norm_inclination = inclination / PI;
126 let norm_azimuth = 0.5 - (azimuth / core::f32::consts::TAU);
127
128 [norm_azimuth, norm_inclination]
129 });
130
131 let raw_points = generated.raw_points();
132
133 let points = raw_points
134 .iter()
135 .map(|&p| (p * self.sphere.radius).into())
136 .collect::<Vec<[f32; 3]>>();
137
138 let normals = raw_points
139 .iter()
140 .copied()
141 .map(Into::into)
142 .collect::<Vec<[f32; 3]>>();
143
144 let uvs = generated.raw_data().to_owned();
145
146 let mut indices = Vec::with_capacity(generated.indices_per_main_triangle() * 20);
147
148 for i in 0..20 {
149 generated.get_indices(i, &mut indices);
150 }
151
152 let indices = Indices::U32(indices);
153
154 Ok(Mesh::new(
155 PrimitiveTopology::TriangleList,
156 RenderAssetUsages::default(),
157 )
158 .with_inserted_indices(indices)
159 .with_inserted_attribute(Mesh::ATTRIBUTE_POSITION, points)
160 .with_inserted_attribute(Mesh::ATTRIBUTE_NORMAL, normals)
161 .with_inserted_attribute(Mesh::ATTRIBUTE_UV_0, uvs))
162 }
163
164 pub fn uv(&self, sectors: u32, stacks: u32) -> Mesh {
169 let sectors_f32 = sectors as f32;
172 let stacks_f32 = stacks as f32;
173 let length_inv = 1. / self.sphere.radius;
174 let sector_step = 2. * PI / sectors_f32;
175 let stack_step = PI / stacks_f32;
176
177 let n_vertices = (stacks * sectors) as usize;
178 let mut vertices: Vec<[f32; 3]> = Vec::with_capacity(n_vertices);
179 let mut normals: Vec<[f32; 3]> = Vec::with_capacity(n_vertices);
180 let mut uvs: Vec<[f32; 2]> = Vec::with_capacity(n_vertices);
181 let mut indices: Vec<u32> = Vec::with_capacity(n_vertices * 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 * ops::cos(stack_angle);
186 let z = self.sphere.radius * ops::sin(stack_angle);
187
188 for j in 0..sectors + 1 {
189 let sector_angle = (j as f32) * sector_step;
190 let x = xy * ops::cos(sector_angle);
191 let y = xy * ops::sin(sector_angle);
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);
210 indices.push(k2);
211 indices.push(k1 + 1);
212 }
213 if i != stacks - 1 {
214 indices.push(k1 + 1);
215 indices.push(k2);
216 indices.push(k2 + 1);
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}