@@ -165,12 +165,16 @@ impl Mesh {
165
165
}
166
166
}
167
167
168
+ /// Generation for some primitive shape meshes.
168
169
pub mod shape {
169
170
use super :: { Mesh , VertexAttribute } ;
170
171
use crate :: pipeline:: PrimitiveTopology ;
171
172
use bevy_math:: * ;
173
+ use hexasphere:: Hexasphere ;
172
174
175
+ /// A cube.
173
176
pub struct Cube {
177
+ /// Half the side length of the cube.
174
178
pub size : f32 ,
175
179
}
176
180
@@ -246,8 +250,11 @@ pub mod shape {
246
250
}
247
251
}
248
252
253
+ /// A rectangle on the XY plane.
249
254
pub struct Quad {
255
+ /// Full width and height of the rectangle.
250
256
pub size : Vec2 ,
257
+ /// Flips the texture coords of the resulting vertices.
251
258
pub flip : bool ,
252
259
}
253
260
@@ -341,7 +348,9 @@ pub mod shape {
341
348
}
342
349
}
343
350
351
+ /// A square on the XZ plane.
344
352
pub struct Plane {
353
+ /// The total side length of the square.
345
354
pub size : f32 ,
346
355
}
347
356
@@ -378,6 +387,70 @@ pub mod shape {
378
387
}
379
388
}
380
389
}
390
+
391
+ /// A sphere made from a subdivided Icosahedron.
392
+ pub struct Icosphere {
393
+ /// The radius of the sphere.
394
+ pub radius : f32 ,
395
+ /// The number of subdivisions applied.
396
+ pub subdivisions : usize ,
397
+ }
398
+
399
+ impl Default for Icosphere {
400
+ fn default ( ) -> Self {
401
+ Self {
402
+ radius : 1.0 ,
403
+ subdivisions : 5 ,
404
+ }
405
+ }
406
+ }
407
+
408
+ impl From < Icosphere > for Mesh {
409
+ fn from ( sphere : Icosphere ) -> Self {
410
+ let hexasphere = Hexasphere :: new ( sphere. subdivisions , |point| {
411
+ let inclination = point. z ( ) . acos ( ) ;
412
+ let azumith = point. y ( ) . atan2 ( point. x ( ) ) ;
413
+
414
+ let norm_inclination = 1.0 - ( inclination / std:: f32:: consts:: PI ) ;
415
+ let norm_azumith = ( azumith / std:: f32:: consts:: PI ) * 0.5 ;
416
+
417
+ [ norm_inclination, norm_azumith]
418
+ } ) ;
419
+
420
+ let raw_points = hexasphere. raw_points ( ) ;
421
+
422
+ let points = raw_points
423
+ . iter ( )
424
+ . map ( |& p| {
425
+ ( p * sphere. radius ) . into ( )
426
+ } )
427
+ . collect :: < Vec < [ f32 ; 3 ] > > ( ) ;
428
+
429
+ let normals = raw_points
430
+ . iter ( )
431
+ . copied ( )
432
+ . map ( Into :: into)
433
+ . collect :: < Vec < [ f32 ; 3 ] > > ( ) ;
434
+
435
+ let uvs = hexasphere. raw_data ( ) . to_owned ( ) ;
436
+
437
+ let mut indices = Vec :: with_capacity ( hexasphere. indices_per_main_triangle ( ) * 20 ) ;
438
+
439
+ for i in 0 ..20 {
440
+ hexasphere. get_indices ( i, & mut indices) ;
441
+ }
442
+
443
+ Mesh {
444
+ primitive_topology : PrimitiveTopology :: TriangleList ,
445
+ attributes : vec ! [
446
+ VertexAttribute :: position( points) ,
447
+ VertexAttribute :: normal( normals) ,
448
+ VertexAttribute :: uv( uvs) ,
449
+ ] ,
450
+ indices : Some ( indices)
451
+ }
452
+ }
453
+ }
381
454
}
382
455
383
456
fn remove_current_mesh_resources (
0 commit comments