Skip to main content

euv_core/game/engine3d/mesh/
impl.rs

1use super::*;
2
3/// Default neutral vertex positioned at origin with `+Y` normal, `+X` tangent, and white vertex color.
4impl Default for Vertex3D {
5    /// Returns a default 64-byte `Vertex3D`.
6    ///
7    /// # Returns
8    ///
9    /// - `Self` - Default `Vertex3D`.
10    fn default() -> Self {
11        Self::from_pos_normal(Vec3::zero(), Vec3::new(0.0, 1.0, 0.0))
12    }
13}
14
15/// Compact and full attribute constructors for `Vertex3D`.
16impl Vertex3D {
17    /// Constructs a `Vertex3D` from `position` and `normal` with identity tangent, zero UVs, and opaque white color.
18    ///
19    /// # Arguments
20    ///
21    /// - `Vec3` - Local-space 3D position.
22    /// - `Vec3` - Local-space unit surface normal.
23    ///
24    /// # Returns
25    ///
26    /// - `Self` - Constructed 64-byte vertex.
27    #[must_use]
28    pub const fn new(position: Vec3, normal: Vec3) -> Self {
29        Self::from_pos_normal(position, normal)
30    }
31
32    /// Constructs a `Vertex3D` from `position` and `normal` with default tangent, UV, and color attributes.
33    ///
34    /// # Arguments
35    ///
36    /// - `Vec3` - Local-space 3D position.
37    /// - `Vec3` - Local-space unit surface normal.
38    ///
39    /// # Returns
40    ///
41    /// - `Self` - Constructed 64-byte vertex.
42    #[must_use]
43    pub const fn from_pos_normal(position: Vec3, normal: Vec3) -> Self {
44        Self {
45            position,
46            normal,
47            tangent: [1.0, 0.0, 0.0, 1.0],
48            uv0: [0.0, 0.0],
49            uv1: [0.0, 0.0],
50            color0: [255, 255, 255, 255],
51            flags: 0,
52        }
53    }
54
55    /// Constructs a fully populated 64-byte `Vertex3D` with tangent, dual UVs, vertex color, and attribute flags.
56    ///
57    /// # Arguments
58    ///
59    /// - `Vec3` - Local-space 3D position.
60    /// - `Vec3` - Local-space unit surface normal.
61    /// - `[f32; 4]` - Tangent vector `(tx, ty, tz, handedness_sign)`.
62    /// - `[f32; 2]` - Primary UV coordinate (`TEXCOORD_0`).
63    /// - `[f32; 2]` - Secondary UV coordinate (`TEXCOORD_1`).
64    /// - `[u8; 4]` - Packed UNORM8 RGBA vertex color (`COLOR_0`).
65    /// - `u32` - Attribute bitmask flags.
66    ///
67    /// # Returns
68    ///
69    /// - `Self` - Fully populated 64-byte vertex.
70    #[must_use]
71    pub const fn from_full(
72        position: Vec3,
73        normal: Vec3,
74        tangent: [f32; 4],
75        uv0: [f32; 2],
76        uv1: [f32; 2],
77        color0: [u8; 4],
78        flags: u32,
79    ) -> Self {
80        Self {
81            position,
82            normal,
83            tangent,
84            uv0,
85            uv1,
86            color0,
87            flags,
88        }
89    }
90}
91
92/// Default neutral matte PBR material.
93impl Default for PbrMaterial {
94    /// Returns a mid-gray dielectric PBR material.
95    ///
96    /// # Returns
97    ///
98    /// - `Self` - Default `PbrMaterial`.
99    fn default() -> Self {
100        Self {
101            base_color: Vec3::new(0.72, 0.75, 0.80),
102            metallic: 0.1,
103            roughness: 0.55,
104            emissive: Vec3::zero(),
105            reflectivity: 0.12,
106            surface_detail: SurfaceDetailMode::None,
107        }
108    }
109}
110
111/// Material presets and fluent builders for metallic conductors, dielectrics, architectural glass, and neon signage.
112impl PbrMaterial {
113    /// Creates a PBR material from an sRGB `Color`, `metallic`, `roughness`, and `reflectivity`.
114    ///
115    /// # Arguments
116    ///
117    /// - `Color` - Base albedo color.
118    /// - `f32` - Metallic scalar `[0.0, 1.0]`.
119    /// - `f32` - Roughness scalar `[0.04, 1.0]`.
120    /// - `f32` - Ray-traced reflectivity weight `[0.0, 1.0]`.
121    ///
122    /// # Returns
123    ///
124    /// - `Self` - Configured PBR material.
125    #[must_use]
126    pub fn pbr(color: Color, metallic: f32, roughness: f32, reflectivity: f32) -> Self {
127        Self {
128            base_color: Vec3::from_color(color),
129            metallic: metallic.clamp(0.0, 1.0),
130            roughness: roughness.clamp(0.04, 1.0),
131            emissive: Vec3::zero(),
132            reflectivity: reflectivity.clamp(0.0, 1.0),
133            surface_detail: SurfaceDetailMode::None,
134        }
135    }
136
137    /// Creates a metallic conductor PBR material (chrome, painted car body, brushed steel).
138    ///
139    /// # Arguments
140    ///
141    /// - `Color` - Base metallic reflectance color.
142    /// - `f32` - Surface roughness in `[0.04, 1.0]`.
143    ///
144    /// # Returns
145    ///
146    /// - `Self` - Conductor PBR material.
147    #[must_use]
148    pub fn metallic(color: Color, roughness: f32) -> Self {
149        let red_val: f32 = roughness.clamp(0.04, 1.0);
150        Self::pbr(
151            color,
152            0.92,
153            red_val,
154            (1.0 - red_val * 0.8).clamp(0.15, 0.96),
155        )
156    }
157
158    /// Creates a non-metallic dielectric PBR material (asphalt, concrete, matte plastic, cloth).
159    ///
160    /// # Arguments
161    ///
162    /// - `Color` - Base diffuse albedo color.
163    /// - `f32` - Surface roughness in `[0.04, 1.0]`.
164    ///
165    /// # Returns
166    ///
167    /// - `Self` - Dielectric PBR material.
168    #[must_use]
169    pub fn dielectric(color: Color, roughness: f32) -> Self {
170        let red_val: f32 = roughness.clamp(0.04, 1.0);
171        Self::pbr(
172            color,
173            0.04,
174            red_val,
175            (0.35 * (1.0 - red_val)).clamp(0.04, 0.35),
176        )
177    }
178
179    /// Creates a highly reflective architectural glass PBR material.
180    ///
181    /// # Arguments
182    ///
183    /// - `Color` - Subtle glass tint color.
184    /// - `f32` - Surface roughness in `[0.04, 0.4]`.
185    ///
186    /// # Returns
187    ///
188    /// - `Self` - Reflective glass PBR material.
189    #[must_use]
190    pub fn glass(color: Color, roughness: f32) -> Self {
191        let red_val: f32 = roughness.clamp(0.04, 0.4);
192        Self::pbr(color, 0.12, red_val, 0.88)
193    }
194
195    /// Creates an emissive neon PBR material for city signage, vehicle tail-lights, and beacons.
196    ///
197    /// # Arguments
198    ///
199    /// - `Color` - Base and emissive neon color.
200    /// - `f32` - Emissive intensity multiplier.
201    ///
202    /// # Returns
203    ///
204    /// - `Self` - Emissive PBR material.
205    #[must_use]
206    pub fn neon(color: Color, intensity: f32) -> Self {
207        let rgb: Vec3 = Vec3::from_color(color);
208        Self {
209            base_color: rgb,
210            metallic: 0.2,
211            roughness: 0.2,
212            emissive: rgb.scale(intensity.max(0.2)),
213            reflectivity: 0.3,
214            surface_detail: SurfaceDetailMode::None,
215        }
216    }
217
218    /// Returns a copy of this material with custom linear HDR emissive radiance.
219    ///
220    /// # Arguments
221    ///
222    /// - `Self` - Base material.
223    /// - `Vec3` - Emissive HDR RGB radiance.
224    ///
225    /// # Returns
226    ///
227    /// - `Self` - Updated material.
228    #[must_use]
229    pub fn with_emissive(mut self, emissive: Vec3) -> Self {
230        self.set_emissive(emissive);
231        self
232    }
233
234    /// Returns a copy of this material with adjusted surface roughness.
235    ///
236    /// # Arguments
237    ///
238    /// - `Self` - Base material.
239    /// - `f32` - Surface roughness clamped to `[0.04, 1.0]`.
240    ///
241    /// # Returns
242    ///
243    /// - `Self` - Updated material.
244    #[must_use]
245    pub fn with_roughness(mut self, roughness: f32) -> Self {
246        self.set_roughness(roughness.clamp(0.04, 1.0));
247        self
248    }
249
250    /// Returns a copy of this material with adjusted metallic factor.
251    ///
252    /// # Arguments
253    ///
254    /// - `Self` - Base material.
255    /// - `f32` - Metallic factor clamped to `[0.0, 1.0]`.
256    ///
257    /// # Returns
258    ///
259    /// - `Self` - Updated material.
260    #[must_use]
261    pub fn with_metallic(mut self, metallic: f32) -> Self {
262        self.set_metallic(metallic.clamp(0.0, 1.0));
263        self
264    }
265
266    /// Returns a copy of this material with adjusted ray-traced reflectivity.
267    ///
268    /// # Arguments
269    ///
270    /// - `Self` - Base material.
271    /// - `f32` - Reflectivity weight clamped to `[0.0, 1.0]`.
272    ///
273    /// # Returns
274    ///
275    /// - `Self` - Updated material.
276    #[must_use]
277    pub fn with_reflectivity(mut self, reflectivity: f32) -> Self {
278        self.set_reflectivity(reflectivity.clamp(0.0, 1.0));
279        self
280    }
281
282    /// Returns a copy of this material with the requested procedural surface detail overlay.
283    ///
284    /// # Arguments
285    ///
286    /// - `Self` - Base material.
287    /// - `SurfaceDetailMode` - Procedural surface detail mode.
288    ///
289    /// # Returns
290    ///
291    /// - `Self` - Updated material.
292    #[must_use]
293    pub fn with_surface_detail(mut self, mode: SurfaceDetailMode) -> Self {
294        self.set_surface_detail(mode);
295        self
296    }
297}
298
299/// Fluent constructors for `BoxMeshDesc`.
300impl BoxMeshDesc {
301    /// Creates a box mesh descriptor with default ground pivot and neutral PBR material.
302    ///
303    /// # Arguments
304    ///
305    /// - `&str` - Asset name.
306    /// - `Vec3` - Box extents `(width, height, depth)`.
307    ///
308    /// # Returns
309    ///
310    /// - `Self` - Ready descriptor.
311    #[must_use]
312    pub fn new(name: &str, size: Vec3) -> Self {
313        Self {
314            name: name.to_string(),
315            size,
316            ground_pivot: true,
317            material: PbrMaterial::default(),
318        }
319    }
320
321    /// Sets whether the box rests on `y = 0` or is centered at the origin.
322    ///
323    /// # Arguments
324    ///
325    /// - `Self` - Base descriptor.
326    /// - `bool` - Ground pivot toggle.
327    ///
328    /// # Returns
329    ///
330    /// - `Self` - Updated descriptor.
331    #[must_use]
332    pub fn with_ground_pivot(mut self, ground_pivot: bool) -> Self {
333        self.set_ground_pivot(ground_pivot);
334        self
335    }
336
337    /// Assigns the PBR material for the generated box mesh.
338    ///
339    /// # Arguments
340    ///
341    /// - `Self` - Base descriptor.
342    /// - `PbrMaterial` - Surface material.
343    ///
344    /// # Returns
345    ///
346    /// - `Self` - Updated descriptor.
347    #[must_use]
348    pub fn with_material(mut self, material: PbrMaterial) -> Self {
349        self.set_material(material);
350        self
351    }
352}
353
354/// Fluent constructors for `GroundPlaneDesc`.
355impl GroundPlaneDesc {
356    /// Creates a ground plane descriptor with default subdivisions and neutral PBR material.
357    ///
358    /// # Arguments
359    ///
360    /// - `&str` - Asset name.
361    /// - `f32` - Width along X.
362    /// - `f32` - Depth along Z.
363    ///
364    /// # Returns
365    ///
366    /// - `Self` - Ready descriptor.
367    #[must_use]
368    pub fn new(name: &str, width: f32, depth: f32) -> Self {
369        Self {
370            name: name.to_string(),
371            width,
372            depth,
373            subdivisions: 8,
374            material: PbrMaterial::default(),
375        }
376    }
377
378    /// Sets the grid subdivision count along each axis.
379    ///
380    /// # Arguments
381    ///
382    /// - `Self` - Base descriptor.
383    /// - `usize` - Subdivision count.
384    ///
385    /// # Returns
386    ///
387    /// - `Self` - Updated descriptor.
388    #[must_use]
389    pub fn with_subdivisions(mut self, subdivisions: usize) -> Self {
390        self.set_subdivisions(subdivisions);
391        self
392    }
393
394    /// Assigns the PBR material for the generated ground plane.
395    ///
396    /// # Arguments
397    ///
398    /// - `Self` - Base descriptor.
399    /// - `PbrMaterial` - Surface material.
400    ///
401    /// # Returns
402    ///
403    /// - `Self` - Updated descriptor.
404    #[must_use]
405    pub fn with_material(mut self, material: PbrMaterial) -> Self {
406        self.set_material(material);
407        self
408    }
409}
410
411/// Mesh construction, Nanite-style meshlet clustering, decimation, and procedural asset generators for `Mesh3D`.
412impl Mesh3D {
413    /// Partitions indexed triangles into Nanite-style `Meshlet3D` clusters with tight axis-aligned bounding boxes and normal cones.
414    ///
415    /// # Arguments
416    ///
417    /// - `&[Vertex3D]` - Source vertex buffer.
418    /// - `&[[u32; 3]]` - Source triangle indices.
419    ///
420    /// # Returns
421    ///
422    /// - `Vec<Meshlet3D>` - Contiguous cluster descriptors covering all triangles.
423    #[must_use]
424    pub fn build_meshlets_from_parts(
425        vertices: &[Vertex3D],
426        indices: &[[u32; 3]],
427    ) -> Vec<Meshlet3D> {
428        if indices.is_empty() {
429            return Vec::new();
430        }
431        let cluster_count: usize = indices.len().div_ceil(MESHLET_MAX_TRIS);
432        let mut out: Vec<Meshlet3D> = Vec::with_capacity(cluster_count);
433        let mut start: usize = 0;
434        while start < indices.len() {
435            let end: usize = (start + MESHLET_MAX_TRIS).min(indices.len());
436            let mut cluster_box: AxisAlignedBox3D = AxisAlignedBox3D::empty();
437            let mut avg_n: Vec3 = Vec3::zero();
438            let mut valid_tris: usize = 0;
439            for tri in &indices[start..end] {
440                let (i0, i1, i2): (usize, usize, usize) =
441                    (tri[0] as usize, tri[1] as usize, tri[2] as usize);
442                if i0 < vertices.len() && i1 < vertices.len() && i2 < vertices.len() {
443                    let p0: Vec3 = vertices[i0].get_position();
444                    let p1: Vec3 = vertices[i1].get_position();
445                    let p2: Vec3 = vertices[i2].get_position();
446                    cluster_box.expand_point(p0);
447                    cluster_box.expand_point(p1);
448                    cluster_box.expand_point(p2);
449                    let fn_vec: Vec3 = p1.sub(p0).cross(p2.sub(p0)).normalize();
450                    avg_n = avg_n.add(fn_vec);
451                    valid_tris += 1;
452                }
453            }
454            if valid_tris == 0 {
455                cluster_box = AxisAlignedBox3D::new(Vec3::zero(), Vec3::zero());
456            }
457            let cone_axis: Vec3 = avg_n.normalize();
458            let mut min_dot: f32 = 1.0;
459            for tri in &indices[start..end] {
460                let (i0, i1, i2): (usize, usize, usize) =
461                    (tri[0] as usize, tri[1] as usize, tri[2] as usize);
462                if i0 < vertices.len() && i1 < vertices.len() && i2 < vertices.len() {
463                    let p0: Vec3 = vertices[i0].get_position();
464                    let p1: Vec3 = vertices[i1].get_position();
465                    let p2: Vec3 = vertices[i2].get_position();
466                    let fn_vec: Vec3 = p1.sub(p0).cross(p2.sub(p0)).normalize();
467                    min_dot = min_dot.min(fn_vec.dot(cone_axis));
468                }
469            }
470            let radius: f32 = cluster_box.radius().max(0.05);
471            let screen_error: f32 = radius / ((end - start).max(1) as f32).sqrt();
472            out.push(Meshlet3D::new(
473                start as u32,
474                (end - start) as u32,
475                cluster_box,
476                cone_axis,
477                min_dot,
478                screen_error,
479            ));
480            start = end;
481        }
482        out
483    }
484
485    /// Constructs a `Mesh3D`, computes its local `AxisAlignedBox3D`, and partitions Nanite-style `Meshlet3D` clusters.
486    ///
487    /// # Arguments
488    ///
489    /// - `String` - Asset name.
490    /// - `Vec<Vertex3D>` - Vertex list.
491    /// - `Vec<[u32; 3]>` - Triangle indices.
492    /// - `PbrMaterial` - Default material.
493    ///
494    /// # Returns
495    ///
496    /// - `Self` - Ready indexed mesh with meshlets.
497    #[must_use]
498    pub fn from_parts(
499        name: String,
500        vertices: Vec<Vertex3D>,
501        indices: Vec<[u32; 3]>,
502        material: PbrMaterial,
503    ) -> Self {
504        let mut bounds: AxisAlignedBox3D = AxisAlignedBox3D::empty();
505        for item_val in vertices.iter() {
506            bounds.expand_point(item_val.get_position());
507        }
508        if vertices.is_empty() {
509            bounds = AxisAlignedBox3D::new(Vec3::zero(), Vec3::zero());
510        }
511        let meshlets: Vec<Meshlet3D> = Self::build_meshlets_from_parts(&vertices, &indices);
512        Self {
513            name,
514            vertices,
515            indices,
516            bounds,
517            material,
518            sub_materials: Vec::new(),
519            tri_materials: Vec::new(),
520            meshlets,
521            skin_influences: Vec::new(),
522            morph_targets: Vec::new(),
523            material_index: None,
524            is_closed: false,
525        }
526    }
527
528    /// Recomputes area-weighted smooth per-vertex normals across all indexed triangles.
529    ///
530    /// # Arguments
531    ///
532    /// - `&Self` - Mutable 3D mesh.
533    pub fn compute_smooth_normals(&mut self) {
534        let vert_count: usize = self.get_vertices().len();
535        if vert_count == 0 {
536            return;
537        }
538        let mut accum: Vec<Vec3> = vec![Vec3::zero(); vert_count];
539        let verts: &[Vertex3D] = self.get_vertices().as_slice();
540        for tri in self.get_indices().iter() {
541            let (i0, i1, i2): (usize, usize, usize) =
542                (tri[0] as usize, tri[1] as usize, tri[2] as usize);
543            if i0 < vert_count && i1 < vert_count && i2 < vert_count {
544                let p0: Vec3 = verts[i0].get_position();
545                let p1: Vec3 = verts[i1].get_position();
546                let p2: Vec3 = verts[i2].get_position();
547                let weighted_n: Vec3 = p1.sub(p0).cross(p2.sub(p0));
548                accum[i0] = accum[i0].add(weighted_n);
549                accum[i1] = accum[i1].add(weighted_n);
550                accum[i2] = accum[i2].add(weighted_n);
551            }
552        }
553        for (idx, vert) in self.get_mut_vertices().iter_mut().enumerate() {
554            let sum_n: Vec3 = accum[idx];
555            if sum_n.length_sq() > 1e-10 {
556                vert.set_normal(sum_n.normalize());
557            }
558        }
559    }
560
561    /// Computes MikkTSpace-compatible Lengyel tangent-space vectors `(tx, ty, tz, handedness)` from positions, normals, and `uv0`.
562    ///
563    /// # Arguments
564    ///
565    /// - `&Self` - Mutable 3D mesh.
566    pub fn compute_lengyel_tangents(&mut self) {
567        let vert_count: usize = self.get_vertices().len();
568        if vert_count == 0 {
569            return;
570        }
571        let mut tan1: Vec<Vec3> = vec![Vec3::zero(); vert_count];
572        let mut tan2: Vec<Vec3> = vec![Vec3::zero(); vert_count];
573        let verts: &[Vertex3D] = self.get_vertices().as_slice();
574        for tri in self.get_indices().iter() {
575            let (i0, i1, i2): (usize, usize, usize) =
576                (tri[0] as usize, tri[1] as usize, tri[2] as usize);
577            if i0 >= vert_count || i1 >= vert_count || i2 >= vert_count {
578                continue;
579            }
580            let p0: Vec3 = verts[i0].get_position();
581            let p1: Vec3 = verts[i1].get_position();
582            let p2: Vec3 = verts[i2].get_position();
583            let w0: [f32; 2] = verts[i0].get_uv0();
584            let w1: [f32; 2] = verts[i1].get_uv0();
585            let w2: [f32; 2] = verts[i2].get_uv0();
586            let e1: Vec3 = p1.sub(p0);
587            let e2: Vec3 = p2.sub(p0);
588            let s1: f32 = w1[0] - w0[0];
589            let s2: f32 = w2[0] - w0[0];
590            let t1: f32 = w1[1] - w0[1];
591            let t2: f32 = w2[1] - w0[1];
592            let det: f32 = s1 * t2 - s2 * t1;
593            if det.abs() < 1e-10 {
594                continue;
595            }
596            let inv_det: f32 = 1.0 / det;
597            let sdir: Vec3 = Vec3::new(
598                (t2 * e1.get_x() - t1 * e2.get_x()) * inv_det,
599                (t2 * e1.get_y() - t1 * e2.get_y()) * inv_det,
600                (t2 * e1.get_z() - t1 * e2.get_z()) * inv_det,
601            );
602            let tdir: Vec3 = Vec3::new(
603                (s1 * e2.get_x() - s2 * e1.get_x()) * inv_det,
604                (s1 * e2.get_y() - s2 * e1.get_y()) * inv_det,
605                (s1 * e2.get_z() - s2 * e1.get_z()) * inv_det,
606            );
607            tan1[i0] = tan1[i0].add(sdir);
608            tan1[i1] = tan1[i1].add(sdir);
609            tan1[i2] = tan1[i2].add(sdir);
610            tan2[i0] = tan2[i0].add(tdir);
611            tan2[i1] = tan2[i1].add(tdir);
612            tan2[i2] = tan2[i2].add(tdir);
613        }
614        for (idx, vert) in self.get_mut_vertices().iter_mut().enumerate() {
615            let n: Vec3 = vert.get_normal().normalize();
616            let t: Vec3 = tan1[idx];
617            let ortho: Vec3 = t.sub(n.scale(n.dot(t)));
618            let unit_t: Vec3 = if ortho.length_sq() > 1e-10 {
619                ortho.normalize()
620            } else {
621                let fallback_axis: Vec3 = if n.get_y().abs() < 0.99 {
622                    Vec3::new(0.0, 1.0, 0.0)
623                } else {
624                    Vec3::new(1.0, 0.0, 0.0)
625                };
626                fallback_axis.cross(n).normalize()
627            };
628            let handedness: f32 = if n.cross(t).dot(tan2[idx]) < 0.0 {
629                -1.0
630            } else {
631                1.0
632            };
633            vert.set_tangent([unit_t.get_x(), unit_t.get_y(), unit_t.get_z(), handedness]);
634            let next_flags: u32 = vert.get_flags() | 4;
635            vert.set_flags(next_flags);
636        }
637    }
638
639    /// Resolves the effective PBR material and sub-material slot for triangle `tri_index`.
640    ///
641    /// # Arguments
642    ///
643    /// - `usize` - Triangle index inside `indices`.
644    /// - `PbrMaterial` - Instance primary PBR material used when slot is `0`.
645    ///
646    /// # Returns
647    ///
648    /// - `(PbrMaterial, u16)` - Effective PBR material and resolved material slot.
649    #[cfg_attr(not(target_arch = "wasm32"), inline)]
650    #[must_use]
651    pub fn resolve_triangle_material(
652        &self,
653        tri_index: usize,
654        primary_material: PbrMaterial,
655    ) -> (PbrMaterial, u16) {
656        let Self {
657            sub_materials,
658            tri_materials,
659            ..
660        } = self;
661        if tri_materials.is_empty() {
662            return (primary_material, 0);
663        }
664        let slot: u16 = tri_materials.get(tri_index).copied().unwrap_or(0);
665        if slot == 0 {
666            (primary_material, 0)
667        } else if let Some(&sub_mat) = sub_materials.get((slot - 1) as usize) {
668            (sub_mat, slot)
669        } else {
670            (primary_material, 0)
671        }
672    }
673
674    /// Appends a single outward-oriented flat-shaded triangle tagged with `mat_slot`.
675    ///
676    /// # Arguments
677    ///
678    /// - `&mut Vec<Vertex3D>` - Destination vertex buffer.
679    /// - `&mut Vec<[u32; 3]>` - Destination triangle index buffer.
680    /// - `&mut Vec<u16>` - Destination per-triangle material slot table.
681    /// - `(Vec3, Vec3, Vec3)` - Triangle vertices `(vert_a, vert_b, vert_c)` in CCW outward order.
682    /// - `u16` - Sub-material slot index (`0` = primary material).
683    fn push_flat_tri(
684        vertices: &mut Vec<Vertex3D>,
685        indices: &mut Vec<[u32; 3]>,
686        tri_materials: &mut Vec<u16>,
687        corners: (Vec3, Vec3, Vec3),
688        mat_slot: u16,
689    ) {
690        let (vert_a, vert_b, vert_c): (Vec3, Vec3, Vec3) = corners;
691        let face_normal: Vec3 = vert_b.sub(vert_a).cross(vert_c.sub(vert_a)).normalize();
692        let base_idx: u32 = vertices.len() as u32;
693        vertices.push(Vertex3D::new(vert_a, face_normal));
694        vertices.push(Vertex3D::new(vert_b, face_normal));
695        vertices.push(Vertex3D::new(vert_c, face_normal));
696        indices.push([base_idx, base_idx + 1, base_idx + 2]);
697        tri_materials.push(mat_slot);
698    }
699
700    /// Appends a flat-shaded CCW quad split into two triangles tagged with `mat_slot`.
701    ///
702    /// # Arguments
703    ///
704    /// - `&mut Vec<Vertex3D>` - Destination vertex buffer.
705    /// - `&mut Vec<[u32; 3]>` - Destination triangle index buffer.
706    /// - `&mut Vec<u16>` - Destination per-triangle material slot table.
707    /// - `[Vec3; 4]` - Quad vertices in CCW outward order.
708    /// - `u16` - Sub-material slot index.
709    fn push_flat_quad(
710        vertices: &mut Vec<Vertex3D>,
711        indices: &mut Vec<[u32; 3]>,
712        tri_materials: &mut Vec<u16>,
713        quad: [Vec3; 4],
714        mat_slot: u16,
715    ) {
716        let edge_u: Vec3 = quad[1].sub(quad[0]);
717        let edge_v: Vec3 = quad[2].sub(quad[0]);
718        let face_normal: Vec3 = edge_u.cross(edge_v).normalize();
719        let base_idx: u32 = vertices.len() as u32;
720        for corner in quad.into_iter() {
721            vertices.push(Vertex3D::new(corner, face_normal));
722        }
723        indices.push([base_idx, base_idx + 1, base_idx + 2]);
724        indices.push([base_idx, base_idx + 2, base_idx + 3]);
725        tri_materials.push(mat_slot);
726        tri_materials.push(mat_slot);
727    }
728
729    /// Appends a closed 6-face tapered frustum/box prism with explicit bottom and top corner quads.
730    ///
731    /// Bottom corners are ordered `[(-x,+z), (+x,+z), (+x,-z), (-x,-z)]` and top corners match identically.
732    ///
733    /// # Arguments
734    ///
735    /// - `&mut Vec<Vertex3D>` - Destination vertex buffer.
736    /// - `&mut Vec<[u32; 3]>` - Destination triangle index buffer.
737    /// - `&mut Vec<u16>` - Destination per-triangle material slot table.
738    /// - `[Vec3; 4]` - Bottom loop corners `[bl_rear, br_rear, br_front, bl_front]`.
739    /// - `[Vec3; 4]` - Top loop corners `[tl_rear, tr_rear, tr_front, tl_front]`.
740    /// - `[u16; 6]` - Per-face material slots `[rear(+z), front(-z), right(+x), left(-x), top(+y), bottom(-y)]`.
741    fn push_tapered_prism(
742        vertices: &mut Vec<Vertex3D>,
743        indices: &mut Vec<[u32; 3]>,
744        tri_materials: &mut Vec<u16>,
745        bottom: [Vec3; 4],
746        top: [Vec3; 4],
747        face_slots: [u16; 6],
748    ) {
749        Self::push_flat_quad(
750            vertices,
751            indices,
752            tri_materials,
753            [bottom[0], bottom[1], top[1], top[0]],
754            face_slots[0],
755        );
756        Self::push_flat_quad(
757            vertices,
758            indices,
759            tri_materials,
760            [bottom[2], bottom[3], top[3], top[2]],
761            face_slots[1],
762        );
763        Self::push_flat_quad(
764            vertices,
765            indices,
766            tri_materials,
767            [bottom[1], bottom[2], top[2], top[1]],
768            face_slots[2],
769        );
770        Self::push_flat_quad(
771            vertices,
772            indices,
773            tri_materials,
774            [bottom[3], bottom[0], top[0], top[3]],
775            face_slots[3],
776        );
777        Self::push_flat_quad(
778            vertices,
779            indices,
780            tri_materials,
781            [top[0], top[1], top[2], top[3]],
782            face_slots[4],
783        );
784        Self::push_flat_quad(
785            vertices,
786            indices,
787            tri_materials,
788            [bottom[3], bottom[2], bottom[1], bottom[0]],
789            face_slots[5],
790        );
791    }
792
793    /// Appends an axis-aligned box prism centered at `center` with `(width, height, depth)` tagged uniformly with `mat_slot`.
794    ///
795    /// # Arguments
796    ///
797    /// - `&mut Vec<Vertex3D>` - Destination vertex buffer.
798    /// - `&mut Vec<[u32; 3]>` - Destination triangle index buffer.
799    /// - `&mut Vec<u16>` - Destination per-triangle material slot table.
800    /// - `Vec3` - Box center position.
801    /// - `Vec3` - Box extents `(width, height, depth)`.
802    /// - `u16` - Sub-material slot applied to all six faces.
803    fn push_box_part(
804        vertices: &mut Vec<Vertex3D>,
805        indices: &mut Vec<[u32; 3]>,
806        tri_materials: &mut Vec<u16>,
807        center: Vec3,
808        size: Vec3,
809        mat_slot: u16,
810    ) {
811        let half_x: f32 = size.get_x() * 0.5;
812        let half_y: f32 = size.get_y() * 0.5;
813        let half_z: f32 = size.get_z() * 0.5;
814        let cx: f32 = center.get_x();
815        let cy: f32 = center.get_y();
816        let cz: f32 = center.get_z();
817        let bottom: [Vec3; 4] = [
818            Vec3::new(cx - half_x, cy - half_y, cz + half_z),
819            Vec3::new(cx + half_x, cy - half_y, cz + half_z),
820            Vec3::new(cx + half_x, cy - half_y, cz - half_z),
821            Vec3::new(cx - half_x, cy - half_y, cz - half_z),
822        ];
823        let top: [Vec3; 4] = [
824            Vec3::new(cx - half_x, cy + half_y, cz + half_z),
825            Vec3::new(cx + half_x, cy + half_y, cz + half_z),
826            Vec3::new(cx + half_x, cy + half_y, cz - half_z),
827            Vec3::new(cx - half_x, cy + half_y, cz - half_z),
828        ];
829        Self::push_tapered_prism(vertices, indices, tri_materials, bottom, top, [mat_slot; 6]);
830    }
831
832    /// Appends a closed cylindrical supercar wheel assembly aligned along the X axle with matte rubber tire tread, recessed metallic brake rotor, contrasting brake caliper, and forged alloy Y-spoke or 5-twin-spoke face.
833    ///
834    /// # Arguments
835    ///
836    /// - `&mut Vec<Vertex3D>` - Destination vertex buffer.
837    /// - `&mut Vec<[u32; 3]>` - Destination triangle index buffer.
838    /// - `&mut Vec<u16>` - Destination per-triangle material slot table.
839    /// - `Vec3` - Wheel hub center `(center_x, center_y, center_z)`.
840    /// - `(f32, f32, f32)` - `(tire_radius, rim_radius, tire_width)`.
841    /// - `(u16, u16, u16, u16)` - `(tire_slot, rim_slot, rotor_slot, caliper_slot)` sub-material slots.
842    /// - `bool` - When `true`, renders Lamborghini Y-spokes; when `false`, renders Shelby 5-twin-spokes.
843    fn push_supercar_wheel_assembly(
844        vertices: &mut Vec<Vertex3D>,
845        indices: &mut Vec<[u32; 3]>,
846        tri_materials: &mut Vec<u16>,
847        center: Vec3,
848        dims: (f32, f32, f32),
849        slots: (u16, u16, u16, u16),
850        y_spoke_style: bool,
851    ) {
852        let (tire_radius, rim_radius, tire_width): (f32, f32, f32) = dims;
853        let (tire_slot, rim_slot, rotor_slot, caliper_slot): (u16, u16, u16, u16) = slots;
854        let half_w: f32 = tire_width * 0.5;
855        let cx: f32 = center.get_x();
856        let cy: f32 = center.get_y();
857        let cz: f32 = center.get_z();
858        let x_left: f32 = cx - half_w;
859        let x_right: f32 = cx + half_w;
860        let left_hub: Vec3 = Vec3::new(x_left, cy, cz);
861        let right_hub: Vec3 = Vec3::new(x_right, cy, cz);
862        let outer_sign: f32 = if cx >= 0.0 { 1.0 } else { -1.0 };
863        let barrel_x: f32 = cx + outer_sign * (half_w + 0.004);
864        let rotor_x: f32 = cx + outer_sign * (half_w + 0.009);
865        let rim_x: f32 = cx + outer_sign * (half_w + 0.018);
866        let barrel_hub: Vec3 = Vec3::new(barrel_x, cy, cz);
867        let rotor_hub: Vec3 = Vec3::new(rotor_x, cy, cz);
868        let rim_hub: Vec3 = Vec3::new(rim_x, cy, cz);
869        let step_rad: f32 = (2.0 * PI) / (MESH_CAR_WHEEL_SEGMENTS as f32);
870        for seg_idx in 0..MESH_CAR_WHEEL_SEGMENTS {
871            let a0: f32 = (seg_idx as f32) * step_rad;
872            let a1: f32 = ((seg_idx + 1) as f32) * step_rad;
873            let (s0, c0): (f32, f32) = a0.sin_cos();
874            let (s1, c1): (f32, f32) = a1.sin_cos();
875            let l0: Vec3 = Vec3::new(x_left, cy + s0 * tire_radius, cz + c0 * tire_radius);
876            let l1: Vec3 = Vec3::new(x_left, cy + s1 * tire_radius, cz + c1 * tire_radius);
877            let r0: Vec3 = Vec3::new(x_right, cy + s0 * tire_radius, cz + c0 * tire_radius);
878            let r1: Vec3 = Vec3::new(x_right, cy + s1 * tire_radius, cz + c1 * tire_radius);
879            Self::push_flat_quad(
880                vertices,
881                indices,
882                tri_materials,
883                [l0, r0, r1, l1],
884                tire_slot,
885            );
886            Self::push_flat_tri(
887                vertices,
888                indices,
889                tri_materials,
890                (right_hub, r1, r0),
891                tire_slot,
892            );
893            Self::push_flat_tri(
894                vertices,
895                indices,
896                tri_materials,
897                (left_hub, l0, l1),
898                tire_slot,
899            );
900            let bm0: Vec3 = Vec3::new(barrel_x, cy + s0 * rim_radius, cz + c0 * rim_radius);
901            let bm1: Vec3 = Vec3::new(barrel_x, cy + s1 * rim_radius, cz + c1 * rim_radius);
902            let rotor_r: f32 = rim_radius * 0.72;
903            let rt0: Vec3 = Vec3::new(rotor_x, cy + s0 * rotor_r, cz + c0 * rotor_r);
904            let rt1: Vec3 = Vec3::new(rotor_x, cy + s1 * rotor_r, cz + c1 * rotor_r);
905            if outer_sign > 0.0 {
906                Self::push_flat_tri(
907                    vertices,
908                    indices,
909                    tri_materials,
910                    (barrel_hub, bm1, bm0),
911                    tire_slot,
912                );
913                Self::push_flat_tri(
914                    vertices,
915                    indices,
916                    tri_materials,
917                    (rotor_hub, rt1, rt0),
918                    rotor_slot,
919                );
920            } else {
921                Self::push_flat_tri(
922                    vertices,
923                    indices,
924                    tri_materials,
925                    (barrel_hub, bm0, bm1),
926                    tire_slot,
927                );
928                Self::push_flat_tri(
929                    vertices,
930                    indices,
931                    tri_materials,
932                    (rotor_hub, rt0, rt1),
933                    rotor_slot,
934                );
935            }
936            let lip_in: f32 = rim_radius * 0.84;
937            let rm0: Vec3 = Vec3::new(rim_x, cy + s0 * rim_radius, cz + c0 * rim_radius);
938            let rm1: Vec3 = Vec3::new(rim_x, cy + s1 * rim_radius, cz + c1 * rim_radius);
939            let ri0: Vec3 = Vec3::new(rim_x, cy + s0 * lip_in, cz + c0 * lip_in);
940            let ri1: Vec3 = Vec3::new(rim_x, cy + s1 * lip_in, cz + c1 * lip_in);
941            if outer_sign > 0.0 {
942                Self::push_flat_quad(
943                    vertices,
944                    indices,
945                    tri_materials,
946                    [ri0, rm0, rm1, ri1],
947                    rim_slot,
948                );
949            } else {
950                Self::push_flat_quad(
951                    vertices,
952                    indices,
953                    tri_materials,
954                    [ri1, rm1, rm0, ri0],
955                    rim_slot,
956                );
957            }
958        }
959        let cal_x: f32 = cx + outer_sign * (half_w + 0.013);
960        let cal_z_off: f32 = if cz < 0.0 {
961            rim_radius * 0.56
962        } else {
963            -rim_radius * 0.56
964        };
965        Self::push_box_part(
966            vertices,
967            indices,
968            tri_materials,
969            Vec3::new(cal_x, cy + rim_radius * 0.14, cz + cal_z_off),
970            Vec3::new(0.022, rim_radius * 0.62, rim_radius * 0.28),
971            caliper_slot,
972        );
973        let spoke_count: usize = 5;
974        let spoke_step: f32 = (2.0 * PI) / (spoke_count as f32);
975        let spoke_outer: f32 = rim_radius * 0.88;
976        let spoke_mid: f32 = rim_radius * 0.44;
977        let hub_r: f32 = rim_radius * 0.24;
978        for spoke_idx in 0..spoke_count {
979            let base_a: f32 = (spoke_idx as f32) * spoke_step + 0.18;
980            let (sb, cb): (f32, f32) = base_a.sin_cos();
981            let (s_hi, c_hi): (f32, f32) = (base_a + 0.12).sin_cos();
982            let (s_lo, c_lo): (f32, f32) = (base_a - 0.12).sin_cos();
983            if y_spoke_style {
984                let (s_fl, c_fl): (f32, f32) = (base_a - 0.24).sin_cos();
985                let (s_fr, c_fr): (f32, f32) = (base_a + 0.24).sin_cos();
986                let (s_fl2, c_fl2): (f32, f32) = (base_a - 0.16).sin_cos();
987                let (s_fr2, c_fr2): (f32, f32) = (base_a + 0.16).sin_cos();
988                let stem_a: Vec3 = Vec3::new(rim_x, cy + s_lo * hub_r, cz + c_lo * hub_r);
989                let stem_b: Vec3 = Vec3::new(rim_x, cy + s_hi * hub_r, cz + c_hi * hub_r);
990                let fork_c: Vec3 = Vec3::new(rim_x, cy + sb * spoke_mid, cz + cb * spoke_mid);
991                let tip_l0: Vec3 =
992                    Vec3::new(rim_x, cy + s_fl * spoke_outer, cz + c_fl * spoke_outer);
993                let tip_l1: Vec3 =
994                    Vec3::new(rim_x, cy + s_fl2 * spoke_outer, cz + c_fl2 * spoke_outer);
995                let tip_r0: Vec3 =
996                    Vec3::new(rim_x, cy + s_fr2 * spoke_outer, cz + c_fr2 * spoke_outer);
997                let tip_r1: Vec3 =
998                    Vec3::new(rim_x, cy + s_fr * spoke_outer, cz + c_fr * spoke_outer);
999                if outer_sign > 0.0 {
1000                    Self::push_flat_tri(
1001                        vertices,
1002                        indices,
1003                        tri_materials,
1004                        (stem_a, fork_c, stem_b),
1005                        rim_slot,
1006                    );
1007                    Self::push_flat_tri(
1008                        vertices,
1009                        indices,
1010                        tri_materials,
1011                        (fork_c, tip_l0, tip_l1),
1012                        rim_slot,
1013                    );
1014                    Self::push_flat_tri(
1015                        vertices,
1016                        indices,
1017                        tri_materials,
1018                        (fork_c, tip_r0, tip_r1),
1019                        rim_slot,
1020                    );
1021                } else {
1022                    Self::push_flat_tri(
1023                        vertices,
1024                        indices,
1025                        tri_materials,
1026                        (stem_a, stem_b, fork_c),
1027                        rim_slot,
1028                    );
1029                    Self::push_flat_tri(
1030                        vertices,
1031                        indices,
1032                        tri_materials,
1033                        (fork_c, tip_l1, tip_l0),
1034                        rim_slot,
1035                    );
1036                    Self::push_flat_tri(
1037                        vertices,
1038                        indices,
1039                        tri_materials,
1040                        (fork_c, tip_r1, tip_r0),
1041                        rim_slot,
1042                    );
1043                }
1044            } else {
1045                for pair_off in [-0.13_f32, 0.13_f32] {
1046                    let a_p: f32 = base_a + pair_off;
1047                    let (sp0, cp0): (f32, f32) = (a_p - 0.05).sin_cos();
1048                    let (sp1, cp1): (f32, f32) = (a_p + 0.05).sin_cos();
1049                    let p_o0: Vec3 =
1050                        Vec3::new(rim_x, cy + sp0 * spoke_outer, cz + cp0 * spoke_outer);
1051                    let p_o1: Vec3 =
1052                        Vec3::new(rim_x, cy + sp1 * spoke_outer, cz + cp1 * spoke_outer);
1053                    if outer_sign > 0.0 {
1054                        Self::push_flat_tri(
1055                            vertices,
1056                            indices,
1057                            tri_materials,
1058                            (rim_hub, p_o1, p_o0),
1059                            rim_slot,
1060                        );
1061                    } else {
1062                        Self::push_flat_tri(
1063                            vertices,
1064                            indices,
1065                            tri_materials,
1066                            (rim_hub, p_o0, p_o1),
1067                            rim_slot,
1068                        );
1069                    }
1070                }
1071            }
1072        }
1073        Self::push_box_part(
1074            vertices,
1075            indices,
1076            tri_materials,
1077            Vec3::new(rim_x + outer_sign * 0.005, cy, cz),
1078            Vec3::new(0.016, hub_r * 1.15, hub_r * 1.15),
1079            rim_slot,
1080        );
1081    }
1082
1083    /// Appends a single closed, watertight stepped multi-tier polygon shell connecting consecutive CCW rings with zero internal caps and strictly outward-facing wall, terrace, and soffit normals.
1084    ///
1085    /// # Arguments
1086    ///
1087    /// - `&mut Vec<Vertex3D>` - Destination vertex buffer.
1088    /// - `&mut Vec<[u32; 3]>` - Destination triangle index buffer.
1089    /// - `&mut Vec<u16>` - Destination per-triangle material slot table.
1090    /// - `&[(&[Vec3], u16)]` - Ordered bottom-to-top CCW polygon rings paired with the material slot for the strip connecting the previous ring to the current ring.
1091    /// - `(u16, u16)` - `(bottom_cap_slot, top_cap_slot)` material slots for the outer base and roof caps.
1092    fn push_stepped_polygon_shell(
1093        vertices: &mut Vec<Vertex3D>,
1094        indices: &mut Vec<[u32; 3]>,
1095        tri_materials: &mut Vec<u16>,
1096        profile_rings: &[(&[Vec3], u16)],
1097        cap_slots: (u16, u16),
1098    ) {
1099        if profile_rings.len() < 2 {
1100            return;
1101        }
1102        let mut ring_len: usize = usize::MAX;
1103        for &(ring, _) in profile_rings.iter() {
1104            ring_len = ring_len.min(ring.len());
1105        }
1106        if ring_len < 3 {
1107            return;
1108        }
1109        for ring_idx in 1..profile_rings.len() {
1110            let prev_ring: &[Vec3] = profile_rings[ring_idx - 1].0;
1111            let (curr_ring, strip_slot): (&[Vec3], u16) = profile_rings[ring_idx];
1112            for corner_idx in 0..ring_len {
1113                let next_corner: usize = (corner_idx + 1) % ring_len;
1114                Self::push_flat_quad(
1115                    vertices,
1116                    indices,
1117                    tri_materials,
1118                    [
1119                        prev_ring[corner_idx],
1120                        prev_ring[next_corner],
1121                        curr_ring[next_corner],
1122                        curr_ring[corner_idx],
1123                    ],
1124                    strip_slot,
1125                );
1126            }
1127        }
1128        let first_ring: &[Vec3] = profile_rings[0].0;
1129        let last_ring: &[Vec3] = profile_rings[profile_rings.len() - 1].0;
1130        let (bottom_cap_slot, top_cap_slot): (u16, u16) = cap_slots;
1131        let inv_count: f32 = 1.0 / (ring_len as f32);
1132        let mut bottom_center: Vec3 = Vec3::zero();
1133        let mut top_center: Vec3 = Vec3::zero();
1134        for corner_idx in 0..ring_len {
1135            bottom_center = bottom_center.add(first_ring[corner_idx]);
1136            top_center = top_center.add(last_ring[corner_idx]);
1137        }
1138        bottom_center = bottom_center.scale(inv_count);
1139        top_center = top_center.scale(inv_count);
1140        for corner_idx in 0..ring_len {
1141            let next_corner: usize = (corner_idx + 1) % ring_len;
1142            Self::push_flat_tri(
1143                vertices,
1144                indices,
1145                tri_materials,
1146                (top_center, last_ring[corner_idx], last_ring[next_corner]),
1147                top_cap_slot,
1148            );
1149            Self::push_flat_tri(
1150                vertices,
1151                indices,
1152                tri_materials,
1153                (
1154                    bottom_center,
1155                    first_ring[next_corner],
1156                    first_ring[corner_idx],
1157                ),
1158                bottom_cap_slot,
1159            );
1160        }
1161    }
1162
1163    /// Appends a crisp oriented rectangular 3D beam spanning from `start_pt` to `end_pt` with cross-section `(half_width, half_height)`.
1164    ///
1165    /// # Arguments
1166    ///
1167    /// - `&mut Vec<Vertex3D>` - Destination vertex buffer.
1168    /// - `&mut Vec<[u32; 3]>` - Destination triangle index buffer.
1169    /// - `&mut Vec<u16>` - Destination per-triangle material slot table.
1170    /// - `(Vec3, Vec3)` - Beam endpoints `(start_pt, end_pt)`.
1171    /// - `(f32, f32)` - Half cross-section `(half_right, half_up)`.
1172    /// - `u16` - Material slot assigned to all six faces.
1173    fn push_angled_bar(
1174        vertices: &mut Vec<Vertex3D>,
1175        indices: &mut Vec<[u32; 3]>,
1176        tri_materials: &mut Vec<u16>,
1177        endpoints: (Vec3, Vec3),
1178        half_extents: (f32, f32),
1179        mat_slot: u16,
1180    ) {
1181        let (start_pt, end_pt): (Vec3, Vec3) = endpoints;
1182        let (half_right, half_up): (f32, f32) = half_extents;
1183        let axis_vec: Vec3 = end_pt.sub(start_pt);
1184        if axis_vec.length_sq() <= 1.0e-8 {
1185            return;
1186        }
1187        let fwd: Vec3 = axis_vec.normalize();
1188        let ref_up: Vec3 = if fwd.get_y().abs() > 0.92 {
1189            Vec3::new(0.0, 0.0, 1.0)
1190        } else {
1191            Vec3::new(0.0, 1.0, 0.0)
1192        };
1193        let right: Vec3 = fwd.cross(ref_up).normalize().scale(half_right);
1194        let up: Vec3 = right.cross(fwd).normalize().scale(half_up);
1195        let ring_a: [Vec3; 4] = [
1196            start_pt.sub(right).sub(up),
1197            start_pt.add(right).sub(up),
1198            start_pt.add(right).add(up),
1199            start_pt.sub(right).add(up),
1200        ];
1201        let ring_b: [Vec3; 4] = [
1202            end_pt.sub(right).sub(up),
1203            end_pt.add(right).sub(up),
1204            end_pt.add(right).add(up),
1205            end_pt.sub(right).add(up),
1206        ];
1207        for idx in 0..4usize {
1208            let next_idx: usize = (idx + 1) % 4;
1209            Self::push_flat_quad(
1210                vertices,
1211                indices,
1212                tri_materials,
1213                [ring_a[idx], ring_b[idx], ring_b[next_idx], ring_a[next_idx]],
1214                mat_slot,
1215            );
1216        }
1217        Self::push_flat_quad(
1218            vertices,
1219            indices,
1220            tri_materials,
1221            [ring_a[3], ring_a[2], ring_a[1], ring_a[0]],
1222            mat_slot,
1223        );
1224        Self::push_flat_quad(
1225            vertices,
1226            indices,
1227            tri_materials,
1228            [ring_b[0], ring_b[1], ring_b[2], ring_b[3]],
1229            mat_slot,
1230        );
1231    }
1232
1233    /// Generates an axis-aligned box mesh of dimensions `(w, h, d)` resting on `y = 0` or centered.
1234    ///
1235    /// # Arguments
1236    ///
1237    /// - `&str` - Mesh name.
1238    /// - `f32` - Width along X.
1239    /// - `f32` - Height along Y.
1240    /// - `f32` - Depth along Z.
1241    /// - `bool` - When `true`, base rests on `y = 0`; otherwise centered at origin.
1242    /// - `PbrMaterial` - Surface PBR material.
1243    ///
1244    /// # Returns
1245    ///
1246    /// - `Self` - Indexed 12-triangle box mesh with crisp flat face normals.
1247    #[must_use]
1248    pub fn box_mesh(
1249        name: &str,
1250        width_val: f32,
1251        height_val: f32,
1252        delta_val: f32,
1253        ground_pivot: bool,
1254        material: PbrMaterial,
1255    ) -> Self {
1256        let hx: f32 = width_val * 0.5;
1257        let hz: f32 = delta_val * 0.5;
1258        let y0: f32 = if ground_pivot { 0.0 } else { -height_val * 0.5 };
1259        let y1: f32 = if ground_pivot {
1260            height_val
1261        } else {
1262            height_val * 0.5
1263        };
1264        let faces: [([Vec3; 4], Vec3); 6] = [
1265            (
1266                [
1267                    Vec3::new(-hx, y0, hz),
1268                    Vec3::new(hx, y0, hz),
1269                    Vec3::new(hx, y1, hz),
1270                    Vec3::new(-hx, y1, hz),
1271                ],
1272                Vec3::new(0.0, 0.0, 1.0),
1273            ),
1274            (
1275                [
1276                    Vec3::new(hx, y0, -hz),
1277                    Vec3::new(-hx, y0, -hz),
1278                    Vec3::new(-hx, y1, -hz),
1279                    Vec3::new(hx, y1, -hz),
1280                ],
1281                Vec3::new(0.0, 0.0, -1.0),
1282            ),
1283            (
1284                [
1285                    Vec3::new(hx, y0, hz),
1286                    Vec3::new(hx, y0, -hz),
1287                    Vec3::new(hx, y1, -hz),
1288                    Vec3::new(hx, y1, hz),
1289                ],
1290                Vec3::new(1.0, 0.0, 0.0),
1291            ),
1292            (
1293                [
1294                    Vec3::new(-hx, y0, -hz),
1295                    Vec3::new(-hx, y0, hz),
1296                    Vec3::new(-hx, y1, hz),
1297                    Vec3::new(-hx, y1, -hz),
1298                ],
1299                Vec3::new(-1.0, 0.0, 0.0),
1300            ),
1301            (
1302                [
1303                    Vec3::new(-hx, y1, hz),
1304                    Vec3::new(hx, y1, hz),
1305                    Vec3::new(hx, y1, -hz),
1306                    Vec3::new(-hx, y1, -hz),
1307                ],
1308                Vec3::new(0.0, 1.0, 0.0),
1309            ),
1310            (
1311                [
1312                    Vec3::new(-hx, y0, -hz),
1313                    Vec3::new(hx, y0, -hz),
1314                    Vec3::new(hx, y0, hz),
1315                    Vec3::new(-hx, y0, hz),
1316                ],
1317                Vec3::new(0.0, -1.0, 0.0),
1318            ),
1319        ];
1320        let mut vertices: Vec<Vertex3D> = Vec::with_capacity(24);
1321        let mut indices: Vec<[u32; 3]> = Vec::with_capacity(12);
1322        for (quad, item_count) in faces.into_iter() {
1323            let base: u32 = vertices.len() as u32;
1324            for point_val in quad.into_iter() {
1325                vertices.push(Vertex3D::new(point_val, item_count));
1326            }
1327            indices.push([base, base + 1, base + 2]);
1328            indices.push([base, base + 2, base + 3]);
1329        }
1330        let mut mesh: Self = Self::from_parts(name.to_string(), vertices, indices, material);
1331        mesh.set_is_closed(true);
1332        mesh
1333    }
1334
1335    /// Generates a horizontal ground/road quad plane at `y = 0` subdivided for crisp specular/shadow falloff.
1336    ///
1337    /// # Arguments
1338    ///
1339    /// - `&str` - Mesh name.
1340    /// - `f32` - Width along X.
1341    /// - `f32` - Depth along Z.
1342    /// - `usize` - Grid subdivisions per axis.
1343    /// - `PbrMaterial` - Surface PBR material.
1344    ///
1345    /// # Returns
1346    ///
1347    /// - `Self` - Subdivided ground plane mesh.
1348    #[must_use]
1349    pub fn ground_plane(
1350        name: &str,
1351        width_val: f32,
1352        delta_val: f32,
1353        subdivisions: usize,
1354        material: PbrMaterial,
1355    ) -> Self {
1356        let segs: usize = subdivisions.clamp(1, 16);
1357        let mut vertices: Vec<Vertex3D> = Vec::with_capacity((segs + 1) * (segs + 1));
1358        let mut indices: Vec<[u32; 3]> = Vec::with_capacity(segs * segs * 2);
1359        let up: Vec3 = Vec3::new(0.0, 1.0, 0.0);
1360        for iz in 0..=segs {
1361            let vz: f32 = ((iz as f32) / (segs as f32) - 0.5) * delta_val;
1362            for ix in 0..=segs {
1363                let vx: f32 = ((ix as f32) / (segs as f32) - 0.5) * width_val;
1364                vertices.push(Vertex3D::new(Vec3::new(vx, 0.0, vz), up));
1365            }
1366        }
1367        let stride: u32 = (segs + 1) as u32;
1368        for iz in 0..segs {
1369            for ix in 0..segs {
1370                let i0: u32 = (iz as u32) * stride + (ix as u32);
1371                let i1: u32 = i0 + 1;
1372                let i2: u32 = i0 + stride;
1373                let i3: u32 = i2 + 1;
1374                indices.push([i0, i2, i1]);
1375                indices.push([i1, i2, i3]);
1376            }
1377        }
1378        Self::from_parts(name.to_string(), vertices, indices, material)
1379    }
1380
1381    /// Builds a subdivided coastal ocean water ring mesh spanning `outer_span` while omitting submerged interior quads strictly inside the solid island footprint and grouping triangles into spatially coherent 4x4 blocks for Nanite frustum culling.
1382    ///
1383    /// # Arguments
1384    ///
1385    /// - `&str` - Mesh asset name.
1386    /// - `f32` - Total horizontal outer span in meters along X and Z.
1387    /// - `f32` - Solid island half-width along X in meters.
1388    /// - `f32` - Solid island half-depth along Z in meters.
1389    /// - `usize` - Grid subdivisions along each axis (`8..=32`).
1390    /// - `PbrMaterial` - Ocean water PBR material.
1391    ///
1392    /// # Returns
1393    ///
1394    /// - `Self` - Watertight coastal ocean ring mesh with spatial meshlets.
1395    #[must_use]
1396    pub fn coastal_ocean_ring(
1397        name: &str,
1398        outer_span: f32,
1399        inner_half_x: f32,
1400        inner_half_z: f32,
1401        subdivisions: usize,
1402        material: PbrMaterial,
1403    ) -> Self {
1404        let seg_count: usize = subdivisions.clamp(8, 32);
1405        let cut_half_x: f32 = (inner_half_x - 2.0).max(0.0);
1406        let cut_half_z: f32 = (inner_half_z - 2.0).max(0.0);
1407        let mut vertices: Vec<Vertex3D> = Vec::with_capacity((seg_count + 1) * (seg_count + 1));
1408        let mut indices: Vec<[u32; 3]> = Vec::with_capacity(seg_count * seg_count * 2);
1409        let up_normal: Vec3 = Vec3::new(0.0, 1.0, 0.0);
1410        let inv_segs: f32 = 1.0 / (seg_count as f32);
1411        for grid_z in 0..=seg_count {
1412            let pos_z: f32 = ((grid_z as f32) * inv_segs - 0.5) * outer_span;
1413            for grid_x in 0..=seg_count {
1414                let pos_x: f32 = ((grid_x as f32) * inv_segs - 0.5) * outer_span;
1415                vertices.push(Vertex3D::new(Vec3::new(pos_x, 0.0, pos_z), up_normal));
1416            }
1417        }
1418        let row_stride: u32 = (seg_count + 1) as u32;
1419        let mut block_z: usize = 0;
1420        while block_z < seg_count {
1421            let end_z: usize = (block_z + 4).min(seg_count);
1422            let mut block_x: usize = 0;
1423            while block_x < seg_count {
1424                let end_x: usize = (block_x + 4).min(seg_count);
1425                for grid_z in block_z..end_z {
1426                    let quad_z0: f32 = ((grid_z as f32) * inv_segs - 0.5) * outer_span;
1427                    let quad_z1: f32 = (((grid_z + 1) as f32) * inv_segs - 0.5) * outer_span;
1428                    for grid_x in block_x..end_x {
1429                        let quad_x0: f32 = ((grid_x as f32) * inv_segs - 0.5) * outer_span;
1430                        let quad_x1: f32 = (((grid_x + 1) as f32) * inv_segs - 0.5) * outer_span;
1431                        let inside_x: bool = quad_x0 >= -cut_half_x && quad_x1 <= cut_half_x;
1432                        let inside_z: bool = quad_z0 >= -cut_half_z && quad_z1 <= cut_half_z;
1433                        if inside_x && inside_z {
1434                            continue;
1435                        }
1436                        let idx0: u32 = (grid_z as u32) * row_stride + (grid_x as u32);
1437                        let idx1: u32 = idx0 + 1;
1438                        let idx2: u32 = idx0 + row_stride;
1439                        let idx3: u32 = idx2 + 1;
1440                        indices.push([idx0, idx2, idx1]);
1441                        indices.push([idx1, idx2, idx3]);
1442                    }
1443                }
1444                block_x += 4;
1445            }
1446            block_z += 4;
1447        }
1448        Self::from_parts(name.to_string(), vertices, indices, material)
1449    }
1450
1451    /// Builds the default sculpted GT supercar mesh (Lamborghini Aventador SVJ V12 stealth wedge).
1452    ///
1453    /// # Arguments
1454    ///
1455    /// - `&str` - Asset name.
1456    /// - `PbrMaterial` - Primary car body clearcoat paint PBR material.
1457    ///
1458    /// # Returns
1459    ///
1460    /// - `Self` - Composite multi-material vehicle mesh.
1461    #[must_use]
1462    pub fn sports_car(name: &str, material: PbrMaterial) -> Self {
1463        Self::aventador_svj(name, material)
1464    }
1465
1466    /// Builds a sculpted Lamborghini Aventador SVJ V12 stealth-fighter wedge supercar with hexagonal low nose, Y-shaped xenon DRL headlights, scissor-door mid-engine air intakes, louvered V12 engine bay with metallic plenums, swan-neck dual-pylon carbon rear wing, Y-motif crimson LED taillights, central triple hexagonal exhausts, and Y-spoke dark forged wheels with yellow ceramic brake calipers.
1467    ///
1468    /// # Arguments
1469    ///
1470    /// - `&str` - Asset name.
1471    /// - `PbrMaterial` - Primary metallic pearlescent body paint PBR material.
1472    ///
1473    /// # Returns
1474    ///
1475    /// - `Self` - Closed multi-material Lamborghini Aventador SVJ mesh.
1476    #[must_use]
1477    pub fn aventador_svj(name: &str, material: PbrMaterial) -> Self {
1478        let sub_materials: Vec<PbrMaterial> = vec![
1479            PbrMaterial::glass(Color::hex(0x070B14), 0.04),
1480            PbrMaterial::dielectric(Color::hex(0x0B0E13), 0.90),
1481            PbrMaterial::metallic(Color::hex(0x94A3B8), 0.16),
1482            PbrMaterial::pbr(Color::hex(0x11151E), 0.34, 0.30, 0.28),
1483            PbrMaterial::neon(Color::hex(0xFF1744), 3.4),
1484            PbrMaterial::neon(Color::hex(0xF8FCFF), 3.3),
1485            PbrMaterial::dielectric(Color::hex(0x04060A), 0.96),
1486            PbrMaterial::pbr(Color::hex(0xFACC15), 0.16, 0.24, 0.38),
1487            PbrMaterial::metallic(Color::hex(0xCBD5E1), 0.14),
1488            PbrMaterial::neon(Color::hex(0xFF7A00), 2.1),
1489        ];
1490        let slot_body: u16 = 0;
1491        let slot_glass: u16 = 1;
1492        let slot_tire: u16 = 2;
1493        let slot_rim: u16 = 3;
1494        let slot_carbon: u16 = 4;
1495        let slot_taillight: u16 = 5;
1496        let slot_headlight: u16 = 6;
1497        let slot_underbody: u16 = 7;
1498        let slot_caliper: u16 = 8;
1499        let slot_chrome: u16 = 9;
1500        let slot_amber: u16 = 10;
1501        let mut vertices: Vec<Vertex3D> = Vec::with_capacity(2400);
1502        let mut indices: Vec<[u32; 3]> = Vec::with_capacity(900);
1503        let mut tri_materials: Vec<u16> = Vec::with_capacity(900);
1504        Self::push_box_part(
1505            &mut vertices,
1506            &mut indices,
1507            &mut tri_materials,
1508            Vec3::new(0.0, 0.13, 0.02),
1509            Vec3::new(2.04, 0.16, 4.42),
1510            slot_underbody,
1511        );
1512        let splitter_bot: [Vec3; 4] = [
1513            Vec3::new(-1.08, 0.16, -1.96),
1514            Vec3::new(1.08, 0.16, -1.96),
1515            Vec3::new(0.92, 0.17, -2.42),
1516            Vec3::new(-0.92, 0.17, -2.42),
1517        ];
1518        let splitter_top: [Vec3; 4] = [
1519            Vec3::new(-1.06, 0.23, -1.96),
1520            Vec3::new(1.06, 0.23, -1.96),
1521            Vec3::new(0.88, 0.23, -2.40),
1522            Vec3::new(-0.88, 0.23, -2.40),
1523        ];
1524        Self::push_tapered_prism(
1525            &mut vertices,
1526            &mut indices,
1527            &mut tri_materials,
1528            splitter_bot,
1529            splitter_top,
1530            [slot_carbon; 6],
1531        );
1532        let nose_bot: [Vec3; 4] = [
1533            Vec3::new(-1.06, 0.22, -1.62),
1534            Vec3::new(1.06, 0.22, -1.62),
1535            Vec3::new(0.76, 0.24, -2.38),
1536            Vec3::new(-0.76, 0.24, -2.38),
1537        ];
1538        let nose_top: [Vec3; 4] = [
1539            Vec3::new(-0.98, 0.66, -1.62),
1540            Vec3::new(0.98, 0.66, -1.62),
1541            Vec3::new(0.56, 0.42, -2.36),
1542            Vec3::new(-0.56, 0.42, -2.36),
1543        ];
1544        Self::push_tapered_prism(
1545            &mut vertices,
1546            &mut indices,
1547            &mut tri_materials,
1548            nose_bot,
1549            nose_top,
1550            [
1551                slot_body,
1552                slot_carbon,
1553                slot_body,
1554                slot_body,
1555                slot_body,
1556                slot_underbody,
1557            ],
1558        );
1559        let hood_bot: [Vec3; 4] = [
1560            Vec3::new(-1.08, 0.22, -0.82),
1561            Vec3::new(1.08, 0.22, -0.82),
1562            Vec3::new(1.06, 0.22, -1.64),
1563            Vec3::new(-1.06, 0.22, -1.64),
1564        ];
1565        let hood_top: [Vec3; 4] = [
1566            Vec3::new(-1.02, 0.80, -0.82),
1567            Vec3::new(1.02, 0.80, -0.82),
1568            Vec3::new(0.98, 0.66, -1.64),
1569            Vec3::new(-0.98, 0.66, -1.64),
1570        ];
1571        Self::push_tapered_prism(
1572            &mut vertices,
1573            &mut indices,
1574            &mut tri_materials,
1575            hood_bot,
1576            hood_top,
1577            [
1578                slot_body,
1579                slot_body,
1580                slot_body,
1581                slot_body,
1582                slot_body,
1583                slot_underbody,
1584            ],
1585        );
1586        let crease_bot: [Vec3; 4] = [
1587            Vec3::new(-0.48, 0.78, -0.90),
1588            Vec3::new(0.48, 0.78, -0.90),
1589            Vec3::new(0.28, 0.44, -2.28),
1590            Vec3::new(-0.28, 0.44, -2.28),
1591        ];
1592        let crease_top: [Vec3; 4] = [
1593            Vec3::new(-0.36, 0.84, -0.94),
1594            Vec3::new(0.36, 0.84, -0.94),
1595            Vec3::new(0.18, 0.48, -2.22),
1596            Vec3::new(-0.18, 0.48, -2.22),
1597        ];
1598        Self::push_tapered_prism(
1599            &mut vertices,
1600            &mut indices,
1601            &mut tri_materials,
1602            crease_bot,
1603            crease_top,
1604            [slot_body; 6],
1605        );
1606        Self::push_box_part(
1607            &mut vertices,
1608            &mut indices,
1609            &mut tri_materials,
1610            Vec3::new(0.0, 0.34, -2.37),
1611            Vec3::new(0.22, 0.16, 0.10),
1612            slot_body,
1613        );
1614        let mid_bot: [Vec3; 4] = [
1615            Vec3::new(-1.02, 0.22, 0.96),
1616            Vec3::new(1.02, 0.22, 0.96),
1617            Vec3::new(1.02, 0.22, -0.84),
1618            Vec3::new(-1.02, 0.22, -0.84),
1619        ];
1620        let mid_top: [Vec3; 4] = [
1621            Vec3::new(-1.06, 0.92, 0.96),
1622            Vec3::new(1.06, 0.92, 0.96),
1623            Vec3::new(0.98, 0.80, -0.84),
1624            Vec3::new(-0.98, 0.80, -0.84),
1625        ];
1626        Self::push_tapered_prism(
1627            &mut vertices,
1628            &mut indices,
1629            &mut tri_materials,
1630            mid_bot,
1631            mid_top,
1632            [
1633                slot_body,
1634                slot_body,
1635                slot_body,
1636                slot_body,
1637                slot_body,
1638                slot_underbody,
1639            ],
1640        );
1641        let rear_bot: [Vec3; 4] = [
1642            Vec3::new(-1.08, 0.36, 2.34),
1643            Vec3::new(1.08, 0.36, 2.34),
1644            Vec3::new(1.18, 0.24, 0.94),
1645            Vec3::new(-1.18, 0.24, 0.94),
1646        ];
1647        let rear_top: [Vec3; 4] = [
1648            Vec3::new(-1.02, 0.96, 2.26),
1649            Vec3::new(1.02, 0.96, 2.26),
1650            Vec3::new(1.14, 0.94, 0.94),
1651            Vec3::new(-1.14, 0.94, 0.94),
1652        ];
1653        Self::push_tapered_prism(
1654            &mut vertices,
1655            &mut indices,
1656            &mut tri_materials,
1657            rear_bot,
1658            rear_top,
1659            [
1660                slot_carbon,
1661                slot_body,
1662                slot_body,
1663                slot_body,
1664                slot_body,
1665                slot_underbody,
1666            ],
1667        );
1668        for side in [-1.0_f32, 1.0_f32] {
1669            Self::push_box_part(
1670                &mut vertices,
1671                &mut indices,
1672                &mut tri_materials,
1673                Vec3::new(side * 0.48, 0.31, -2.30),
1674                Vec3::new(0.46, 0.14, 0.12),
1675                slot_carbon,
1676            );
1677            Self::push_angled_bar(
1678                &mut vertices,
1679                &mut indices,
1680                &mut tri_materials,
1681                (
1682                    Vec3::new(side * 0.20, 0.39, -2.36),
1683                    Vec3::new(side * 0.78, 0.24, -2.28),
1684                ),
1685                (0.028, 0.028),
1686                slot_body,
1687            );
1688            Self::push_box_part(
1689                &mut vertices,
1690                &mut indices,
1691                &mut tri_materials,
1692                Vec3::new(side * 1.11, 0.22, -0.02),
1693                Vec3::new(0.16, 0.12, 2.12),
1694                slot_carbon,
1695            );
1696            Self::push_box_part(
1697                &mut vertices,
1698                &mut indices,
1699                &mut tri_materials,
1700                Vec3::new(side * 1.04, 0.52, 0.64),
1701                Vec3::new(0.14, 0.32, 0.64),
1702                slot_carbon,
1703            );
1704            Self::push_angled_bar(
1705                &mut vertices,
1706                &mut indices,
1707                &mut tri_materials,
1708                (
1709                    Vec3::new(side * 1.02, 0.40, 0.28),
1710                    Vec3::new(side * 1.12, 0.68, 0.94),
1711                ),
1712                (0.032, 0.030),
1713                slot_body,
1714            );
1715            Self::push_box_part(
1716                &mut vertices,
1717                &mut indices,
1718                &mut tri_materials,
1719                Vec3::new(side * 0.94, 0.93, 0.66),
1720                Vec3::new(0.14, 0.06, 0.42),
1721                slot_carbon,
1722            );
1723            for (fender_z, flare_w, h_rear, h_front) in [
1724                (-1.42_f32, 1.15_f32, 0.75_f32, 0.56_f32),
1725                (1.44_f32, 1.21_f32, 0.96_f32, 0.95_f32),
1726            ] {
1727                let x_in: f32 = side * 0.92;
1728                let x_out_lo: f32 = side * flare_w;
1729                let x_out_hi: f32 = side * (flare_w - 0.14);
1730                let (xa, xb): (f32, f32) = if side > 0.0 {
1731                    (x_in, x_out_lo)
1732                } else {
1733                    (x_out_lo, x_in)
1734                };
1735                let (ta, tb): (f32, f32) = if side > 0.0 {
1736                    (x_in, x_out_hi)
1737                } else {
1738                    (x_out_hi, x_in)
1739                };
1740                let arch_bot: [Vec3; 4] = [
1741                    Vec3::new(xa, 0.50, fender_z + 0.54),
1742                    Vec3::new(xb, 0.50, fender_z + 0.54),
1743                    Vec3::new(xb, 0.44, fender_z - 0.54),
1744                    Vec3::new(xa, 0.44, fender_z - 0.54),
1745                ];
1746                let arch_top: [Vec3; 4] = [
1747                    Vec3::new(ta, h_rear, fender_z + 0.40),
1748                    Vec3::new(tb, h_rear, fender_z + 0.40),
1749                    Vec3::new(tb, h_front, fender_z - 0.40),
1750                    Vec3::new(ta, h_front, fender_z - 0.40),
1751                ];
1752                Self::push_tapered_prism(
1753                    &mut vertices,
1754                    &mut indices,
1755                    &mut tri_materials,
1756                    arch_bot,
1757                    arch_top,
1758                    [
1759                        slot_body,
1760                        slot_body,
1761                        slot_body,
1762                        slot_body,
1763                        slot_body,
1764                        slot_underbody,
1765                    ],
1766                );
1767                Self::push_box_part(
1768                    &mut vertices,
1769                    &mut indices,
1770                    &mut tri_materials,
1771                    Vec3::new(side * 0.92, 0.40, fender_z),
1772                    Vec3::new(0.28, 0.48, 0.86),
1773                    slot_underbody,
1774                );
1775            }
1776            Self::push_angled_bar(
1777                &mut vertices,
1778                &mut indices,
1779                &mut tri_materials,
1780                (
1781                    Vec3::new(side * 0.94, 0.84, -0.66),
1782                    Vec3::new(side * 1.12, 0.93, -0.62),
1783                ),
1784                (0.025, 0.020),
1785                slot_carbon,
1786            );
1787            Self::push_box_part(
1788                &mut vertices,
1789                &mut indices,
1790                &mut tri_materials,
1791                Vec3::new(side * 1.12, 0.94, -0.60),
1792                Vec3::new(0.16, 0.07, 0.14),
1793                slot_body,
1794            );
1795            let butt_bot: [Vec3; 4] = [
1796                Vec3::new(if side > 0.0 { 0.54 } else { -0.92 }, 0.94, 1.82),
1797                Vec3::new(if side > 0.0 { 0.92 } else { -0.54 }, 0.94, 1.82),
1798                Vec3::new(if side > 0.0 { 0.84 } else { -0.54 }, 0.92, 0.56),
1799                Vec3::new(if side > 0.0 { 0.54 } else { -0.84 }, 0.92, 0.56),
1800            ];
1801            let butt_top: [Vec3; 4] = [
1802                Vec3::new(if side > 0.0 { 0.54 } else { -0.78 }, 0.99, 1.74),
1803                Vec3::new(if side > 0.0 { 0.78 } else { -0.54 }, 0.99, 1.74),
1804                Vec3::new(if side > 0.0 { 0.66 } else { -0.50 }, 1.21, 0.56),
1805                Vec3::new(if side > 0.0 { 0.50 } else { -0.66 }, 1.21, 0.56),
1806            ];
1807            Self::push_tapered_prism(
1808                &mut vertices,
1809                &mut indices,
1810                &mut tri_materials,
1811                butt_bot,
1812                butt_top,
1813                [slot_body; 6],
1814            );
1815            let hl_in: f32 = side * 0.44;
1816            let hl_out: f32 = side * 0.84;
1817            let (hx0, hx1): (f32, f32) = if side > 0.0 {
1818                (hl_in, hl_out)
1819            } else {
1820                (hl_out, hl_in)
1821            };
1822            let hl_bot: [Vec3; 4] = [
1823                Vec3::new(hx0, 0.56, -1.86),
1824                Vec3::new(hx1, 0.56, -1.86),
1825                Vec3::new(hx1 * 0.90, 0.43, -2.28),
1826                Vec3::new(hx0 * 0.90, 0.43, -2.28),
1827            ];
1828            let hl_top: [Vec3; 4] = [
1829                Vec3::new(hx0, 0.61, -1.86),
1830                Vec3::new(hx1, 0.61, -1.86),
1831                Vec3::new(hx1 * 0.90, 0.47, -2.28),
1832                Vec3::new(hx0 * 0.90, 0.47, -2.28),
1833            ];
1834            Self::push_tapered_prism(
1835                &mut vertices,
1836                &mut indices,
1837                &mut tri_materials,
1838                hl_bot,
1839                hl_top,
1840                [slot_carbon; 6],
1841            );
1842            Self::push_angled_bar(
1843                &mut vertices,
1844                &mut indices,
1845                &mut tri_materials,
1846                (
1847                    Vec3::new(side * 0.48, 0.48, -2.26),
1848                    Vec3::new(side * 0.78, 0.61, -1.90),
1849                ),
1850                (0.024, 0.020),
1851                slot_headlight,
1852            );
1853            Self::push_angled_bar(
1854                &mut vertices,
1855                &mut indices,
1856                &mut tri_materials,
1857                (
1858                    Vec3::new(side * 0.62, 0.55, -2.08),
1859                    Vec3::new(side * 0.50, 0.62, -1.92),
1860                ),
1861                (0.020, 0.018),
1862                slot_headlight,
1863            );
1864            Self::push_angled_bar(
1865                &mut vertices,
1866                &mut indices,
1867                &mut tri_materials,
1868                (
1869                    Vec3::new(side * 0.62, 0.55, -2.08),
1870                    Vec3::new(side * 0.78, 0.52, -2.14),
1871                ),
1872                (0.020, 0.018),
1873                slot_headlight,
1874            );
1875            Self::push_angled_bar(
1876                &mut vertices,
1877                &mut indices,
1878                &mut tri_materials,
1879                (
1880                    Vec3::new(side * 0.42, 0.79, 2.35),
1881                    Vec3::new(side * 0.76, 0.79, 2.35),
1882                ),
1883                (0.036, 0.036),
1884                slot_taillight,
1885            );
1886            Self::push_angled_bar(
1887                &mut vertices,
1888                &mut indices,
1889                &mut tri_materials,
1890                (
1891                    Vec3::new(side * 0.74, 0.79, 2.35),
1892                    Vec3::new(side * 0.95, 0.88, 2.32),
1893                ),
1894                (0.032, 0.032),
1895                slot_taillight,
1896            );
1897            Self::push_angled_bar(
1898                &mut vertices,
1899                &mut indices,
1900                &mut tri_materials,
1901                (
1902                    Vec3::new(side * 0.74, 0.79, 2.35),
1903                    Vec3::new(side * 0.95, 0.70, 2.32),
1904                ),
1905                (0.032, 0.032),
1906                slot_taillight,
1907            );
1908            Self::push_box_part(
1909                &mut vertices,
1910                &mut indices,
1911                &mut tri_materials,
1912                Vec3::new(side * 0.24, 0.92, 1.24),
1913                Vec3::new(0.16, 0.08, 0.94),
1914                slot_chrome,
1915            );
1916        }
1917        let cabin_bot: [Vec3; 4] = [
1918            Vec3::new(-0.84, 0.92, 0.86),
1919            Vec3::new(0.84, 0.92, 0.86),
1920            Vec3::new(0.86, 0.78, -1.04),
1921            Vec3::new(-0.86, 0.78, -1.04),
1922        ];
1923        let cabin_top: [Vec3; 4] = [
1924            Vec3::new(-0.56, 1.22, 0.56),
1925            Vec3::new(0.56, 1.22, 0.56),
1926            Vec3::new(0.58, 1.19, -0.14),
1927            Vec3::new(-0.58, 1.19, -0.14),
1928        ];
1929        Self::push_tapered_prism(
1930            &mut vertices,
1931            &mut indices,
1932            &mut tri_materials,
1933            cabin_bot,
1934            cabin_top,
1935            [
1936                slot_glass,
1937                slot_glass,
1938                slot_glass,
1939                slot_glass,
1940                slot_body,
1941                slot_underbody,
1942            ],
1943        );
1944        let roof_bot: [Vec3; 4] = [
1945            Vec3::new(-0.54, 1.225, 0.57),
1946            Vec3::new(0.54, 1.225, 0.57),
1947            Vec3::new(0.56, 1.195, -0.15),
1948            Vec3::new(-0.56, 1.195, -0.15),
1949        ];
1950        let roof_top: [Vec3; 4] = [
1951            Vec3::new(-0.42, 1.265, 0.48),
1952            Vec3::new(0.42, 1.265, 0.48),
1953            Vec3::new(0.44, 1.240, -0.06),
1954            Vec3::new(-0.44, 1.240, -0.06),
1955        ];
1956        Self::push_tapered_prism(
1957            &mut vertices,
1958            &mut indices,
1959            &mut tri_materials,
1960            roof_bot,
1961            roof_top,
1962            [slot_body; 6],
1963        );
1964        Self::push_box_part(
1965            &mut vertices,
1966            &mut indices,
1967            &mut tri_materials,
1968            Vec3::new(0.0, 0.88, 1.24),
1969            Vec3::new(0.96, 0.08, 1.18),
1970            slot_underbody,
1971        );
1972        for (louver_z, louver_y, louver_w) in [
1973            (0.84_f32, 1.10_f32, 0.86_f32),
1974            (1.22_f32, 1.04_f32, 0.92_f32),
1975            (1.60_f32, 0.99_f32, 0.96_f32),
1976        ] {
1977            let lv_bot: [Vec3; 4] = [
1978                Vec3::new(-louver_w * 0.5, louver_y - 0.03, louver_z + 0.13),
1979                Vec3::new(louver_w * 0.5, louver_y - 0.03, louver_z + 0.13),
1980                Vec3::new(louver_w * 0.46, louver_y + 0.01, louver_z - 0.12),
1981                Vec3::new(-louver_w * 0.46, louver_y + 0.01, louver_z - 0.12),
1982            ];
1983            let lv_top: [Vec3; 4] = [
1984                Vec3::new(-louver_w * 0.48, louver_y, louver_z + 0.12),
1985                Vec3::new(louver_w * 0.48, louver_y, louver_z + 0.12),
1986                Vec3::new(louver_w * 0.44, louver_y + 0.04, louver_z - 0.11),
1987                Vec3::new(-louver_w * 0.44, louver_y + 0.04, louver_z - 0.11),
1988            ];
1989            Self::push_tapered_prism(
1990                &mut vertices,
1991                &mut indices,
1992                &mut tri_materials,
1993                lv_bot,
1994                lv_top,
1995                [slot_carbon; 6],
1996            );
1997        }
1998        Self::push_box_part(
1999            &mut vertices,
2000            &mut indices,
2001            &mut tri_materials,
2002            Vec3::new(0.0, 0.28, 2.31),
2003            Vec3::new(1.82, 0.24, 0.24),
2004            slot_carbon,
2005        );
2006        for fin_x in [-0.62_f32, -0.22_f32, 0.22_f32, 0.62_f32] {
2007            Self::push_box_part(
2008                &mut vertices,
2009                &mut indices,
2010                &mut tri_materials,
2011                Vec3::new(fin_x, 0.26, 2.39),
2012                Vec3::new(0.055, 0.22, 0.16),
2013                slot_carbon,
2014            );
2015        }
2016        Self::push_box_part(
2017            &mut vertices,
2018            &mut indices,
2019            &mut tri_materials,
2020            Vec3::new(0.0, 0.49, 2.34),
2021            Vec3::new(0.66, 0.18, 0.12),
2022            slot_carbon,
2023        );
2024        for pipe_x in [-0.18_f32, 0.0_f32, 0.18_f32] {
2025            Self::push_box_part(
2026                &mut vertices,
2027                &mut indices,
2028                &mut tri_materials,
2029                Vec3::new(pipe_x, 0.49, 2.39),
2030                Vec3::new(0.12, 0.11, 0.12),
2031                slot_chrome,
2032            );
2033            Self::push_box_part(
2034                &mut vertices,
2035                &mut indices,
2036                &mut tri_materials,
2037                Vec3::new(pipe_x, 0.49, 2.43),
2038                Vec3::new(0.07, 0.06, 0.04),
2039                slot_amber,
2040            );
2041        }
2042        let wing_bot: [Vec3; 4] = [
2043            Vec3::new(-1.04, 1.24, 2.24),
2044            Vec3::new(1.04, 1.24, 2.24),
2045            Vec3::new(0.98, 1.20, 1.90),
2046            Vec3::new(-0.98, 1.20, 1.90),
2047        ];
2048        let wing_top: [Vec3; 4] = [
2049            Vec3::new(-1.04, 1.29, 2.24),
2050            Vec3::new(1.04, 1.29, 2.24),
2051            Vec3::new(0.98, 1.24, 1.90),
2052            Vec3::new(-0.98, 1.24, 1.90),
2053        ];
2054        Self::push_tapered_prism(
2055            &mut vertices,
2056            &mut indices,
2057            &mut tri_materials,
2058            wing_bot,
2059            wing_top,
2060            [slot_carbon; 6],
2061        );
2062        Self::push_box_part(
2063            &mut vertices,
2064            &mut indices,
2065            &mut tri_materials,
2066            Vec3::new(0.0, 1.12, 2.04),
2067            Vec3::new(0.04, 0.24, 0.34),
2068            slot_carbon,
2069        );
2070        for wing_side in [-1.0_f32, 1.0_f32] {
2071            Self::push_box_part(
2072                &mut vertices,
2073                &mut indices,
2074                &mut tri_materials,
2075                Vec3::new(wing_side * 1.04, 1.24, 2.08),
2076                Vec3::new(0.045, 0.18, 0.44),
2077                slot_carbon,
2078            );
2079            Self::push_angled_bar(
2080                &mut vertices,
2081                &mut indices,
2082                &mut tri_materials,
2083                (
2084                    Vec3::new(wing_side * 0.48, 0.94, 1.94),
2085                    Vec3::new(wing_side * 0.48, 1.24, 2.08),
2086                ),
2087                (0.025, 0.065),
2088                slot_carbon,
2089            );
2090        }
2091        let wheel_specs: [(f32, f32, f32, f32, f32); 4] = [
2092            (-1.03, -1.42, 0.34, 0.24, 0.34),
2093            (1.03, -1.42, 0.34, 0.24, 0.34),
2094            (-1.06, 1.44, 0.36, 0.26, 0.44),
2095            (1.06, 1.44, 0.36, 0.26, 0.44),
2096        ];
2097        for (wheel_x, wheel_z, tire_r, rim_r, tire_w) in wheel_specs.into_iter() {
2098            Self::push_supercar_wheel_assembly(
2099                &mut vertices,
2100                &mut indices,
2101                &mut tri_materials,
2102                Vec3::new(wheel_x, tire_r, wheel_z),
2103                (tire_r, rim_r, tire_w),
2104                (slot_tire, slot_rim, slot_underbody, slot_caliper),
2105                true,
2106            );
2107        }
2108        let mut mesh: Self = Self::from_parts(name.to_string(), vertices, indices, material);
2109        mesh.set_sub_materials(sub_materials);
2110        mesh.set_tri_materials(tri_materials);
2111        mesh.set_is_closed(true);
2112        mesh
2113    }
2114
2115    /// Builds a sculpted Ford Mustang Shelby GT500 Cobra fastback muscle car with long sculpted hood, recessed power-dome heat extractor, nose-to-tail twin Le Mans racing stripes, deep hexagonal Cobra grille, tri-bar fang LED headlights, fastback quarter-window louvers, blacked-out decklid panel with iconic vertical tri-bar crimson LED taillights, Gurney-flap track spoiler, quad metallic exhausts, and 5-twin-spoke forged alloy wheels with crimson Brembo brake calipers.
2116    ///
2117    /// # Arguments
2118    ///
2119    /// - `&str` - Asset name.
2120    /// - `PbrMaterial` - Primary metallic muscle-car paint PBR material.
2121    ///
2122    /// # Returns
2123    ///
2124    /// - `Self` - Closed multi-material Ford Mustang Shelby GT500 Cobra mesh.
2125    #[must_use]
2126    pub fn shelby_cobra(name: &str, material: PbrMaterial) -> Self {
2127        let sub_materials: Vec<PbrMaterial> = vec![
2128            PbrMaterial::glass(Color::hex(0x0B1220), 0.05),
2129            PbrMaterial::dielectric(Color::hex(0x0B0E13), 0.90),
2130            PbrMaterial::metallic(Color::hex(0xDCE6F2), 0.14),
2131            PbrMaterial::pbr(Color::hex(0x11151E), 0.30, 0.32, 0.26),
2132            PbrMaterial::neon(Color::hex(0xFF1744), 4.2),
2133            PbrMaterial::neon(Color::hex(0xF8FCFF), 3.9),
2134            PbrMaterial::dielectric(Color::hex(0x04060A), 0.96),
2135            PbrMaterial::pbr(Color::hex(0xF4F7FB), 0.16, 0.16, 0.55),
2136            PbrMaterial::pbr(Color::hex(0xE11D48), 0.24, 0.22, 0.42),
2137            PbrMaterial::neon(Color::hex(0xFF8800), 1.9),
2138        ];
2139        let slot_body: u16 = 0;
2140        let slot_glass: u16 = 1;
2141        let slot_tire: u16 = 2;
2142        let slot_rim: u16 = 3;
2143        let slot_carbon: u16 = 4;
2144        let slot_taillight: u16 = 5;
2145        let slot_headlight: u16 = 6;
2146        let slot_underbody: u16 = 7;
2147        let slot_stripe: u16 = 8;
2148        let slot_caliper: u16 = 9;
2149        let slot_amber: u16 = 10;
2150        let mut vertices: Vec<Vertex3D> = Vec::with_capacity(2400);
2151        let mut indices: Vec<[u32; 3]> = Vec::with_capacity(900);
2152        let mut tri_materials: Vec<u16> = Vec::with_capacity(900);
2153        Self::push_box_part(
2154            &mut vertices,
2155            &mut indices,
2156            &mut tri_materials,
2157            Vec3::new(0.0, 0.15, 0.0),
2158            Vec3::new(2.02, 0.18, 4.46),
2159            slot_underbody,
2160        );
2161        let chin_bot: [Vec3; 4] = [
2162            Vec3::new(-1.10, 0.18, -1.96),
2163            Vec3::new(1.10, 0.18, -1.96),
2164            Vec3::new(1.04, 0.19, -2.42),
2165            Vec3::new(-1.04, 0.19, -2.42),
2166        ];
2167        let chin_top: [Vec3; 4] = [
2168            Vec3::new(-1.08, 0.25, -1.96),
2169            Vec3::new(1.08, 0.25, -1.96),
2170            Vec3::new(1.02, 0.26, -2.40),
2171            Vec3::new(-1.02, 0.26, -2.40),
2172        ];
2173        Self::push_tapered_prism(
2174            &mut vertices,
2175            &mut indices,
2176            &mut tri_materials,
2177            chin_bot,
2178            chin_top,
2179            [slot_carbon; 6],
2180        );
2181        let hood_bot: [Vec3; 4] = [
2182            Vec3::new(-1.08, 0.24, -0.52),
2183            Vec3::new(1.08, 0.24, -0.52),
2184            Vec3::new(0.98, 0.26, -2.30),
2185            Vec3::new(-0.98, 0.26, -2.30),
2186        ];
2187        let hood_top: [Vec3; 4] = [
2188            Vec3::new(-1.02, 0.96, -0.52),
2189            Vec3::new(1.02, 0.96, -0.52),
2190            Vec3::new(0.92, 0.78, -2.38),
2191            Vec3::new(-0.92, 0.78, -2.38),
2192        ];
2193        Self::push_tapered_prism(
2194            &mut vertices,
2195            &mut indices,
2196            &mut tri_materials,
2197            hood_bot,
2198            hood_top,
2199            [
2200                slot_body,
2201                slot_body,
2202                slot_body,
2203                slot_body,
2204                slot_body,
2205                slot_underbody,
2206            ],
2207        );
2208        let dome_bot: [Vec3; 4] = [
2209            Vec3::new(-0.48, 0.93, -0.62),
2210            Vec3::new(0.48, 0.93, -0.62),
2211            Vec3::new(0.42, 0.80, -2.02),
2212            Vec3::new(-0.42, 0.80, -2.02),
2213        ];
2214        let dome_top: [Vec3; 4] = [
2215            Vec3::new(-0.40, 1.00, -0.66),
2216            Vec3::new(0.40, 1.00, -0.66),
2217            Vec3::new(0.34, 0.86, -1.94),
2218            Vec3::new(-0.34, 0.86, -1.94),
2219        ];
2220        Self::push_tapered_prism(
2221            &mut vertices,
2222            &mut indices,
2223            &mut tri_materials,
2224            dome_bot,
2225            dome_top,
2226            [slot_body; 6],
2227        );
2228        Self::push_box_part(
2229            &mut vertices,
2230            &mut indices,
2231            &mut tri_materials,
2232            Vec3::new(0.0, 0.945, -1.34),
2233            Vec3::new(0.56, 0.05, 0.64),
2234            slot_carbon,
2235        );
2236        for vent_z in [-1.54_f32, -1.34_f32, -1.14_f32] {
2237            Self::push_box_part(
2238                &mut vertices,
2239                &mut indices,
2240                &mut tri_materials,
2241                Vec3::new(0.0, 0.975, vent_z),
2242                Vec3::new(0.50, 0.025, 0.07),
2243                slot_underbody,
2244            );
2245        }
2246        Self::push_box_part(
2247            &mut vertices,
2248            &mut indices,
2249            &mut tri_materials,
2250            Vec3::new(0.0, 0.50, -2.36),
2251            Vec3::new(1.36, 0.42, 0.09),
2252            slot_carbon,
2253        );
2254        Self::push_box_part(
2255            &mut vertices,
2256            &mut indices,
2257            &mut tri_materials,
2258            Vec3::new(0.0, 0.50, -2.39),
2259            Vec3::new(1.42, 0.09, 0.06),
2260            slot_carbon,
2261        );
2262        Self::push_box_part(
2263            &mut vertices,
2264            &mut indices,
2265            &mut tri_materials,
2266            Vec3::new(0.32, 0.61, -2.41),
2267            Vec3::new(0.08, 0.11, 0.03),
2268            slot_rim,
2269        );
2270        let mid_bot: [Vec3; 4] = [
2271            Vec3::new(-1.06, 0.24, 1.08),
2272            Vec3::new(1.06, 0.24, 1.08),
2273            Vec3::new(1.06, 0.24, -0.54),
2274            Vec3::new(-1.06, 0.24, -0.54),
2275        ];
2276        let mid_top: [Vec3; 4] = [
2277            Vec3::new(-1.04, 1.00, 1.08),
2278            Vec3::new(1.04, 1.00, 1.08),
2279            Vec3::new(1.02, 0.96, -0.54),
2280            Vec3::new(-1.02, 0.96, -0.54),
2281        ];
2282        Self::push_tapered_prism(
2283            &mut vertices,
2284            &mut indices,
2285            &mut tri_materials,
2286            mid_bot,
2287            mid_top,
2288            [
2289                slot_body,
2290                slot_body,
2291                slot_body,
2292                slot_body,
2293                slot_body,
2294                slot_underbody,
2295            ],
2296        );
2297        let rear_bot: [Vec3; 4] = [
2298            Vec3::new(-1.08, 0.34, 2.34),
2299            Vec3::new(1.08, 0.34, 2.34),
2300            Vec3::new(1.16, 0.26, 1.06),
2301            Vec3::new(-1.16, 0.26, 1.06),
2302        ];
2303        let rear_top: [Vec3; 4] = [
2304            Vec3::new(-1.02, 1.02, 2.28),
2305            Vec3::new(1.02, 1.02, 2.28),
2306            Vec3::new(1.12, 1.02, 1.06),
2307            Vec3::new(-1.12, 1.02, 1.06),
2308        ];
2309        Self::push_tapered_prism(
2310            &mut vertices,
2311            &mut indices,
2312            &mut tri_materials,
2313            rear_bot,
2314            rear_top,
2315            [
2316                slot_body,
2317                slot_body,
2318                slot_body,
2319                slot_body,
2320                slot_body,
2321                slot_underbody,
2322            ],
2323        );
2324        let cabin_bot: [Vec3; 4] = [
2325            Vec3::new(-0.92, 1.00, 1.82),
2326            Vec3::new(0.92, 1.00, 1.82),
2327            Vec3::new(0.90, 0.94, -0.58),
2328            Vec3::new(-0.90, 0.94, -0.58),
2329        ];
2330        let cabin_top: [Vec3; 4] = [
2331            Vec3::new(-0.68, 1.38, 0.80),
2332            Vec3::new(0.68, 1.38, 0.80),
2333            Vec3::new(0.68, 1.36, 0.04),
2334            Vec3::new(-0.68, 1.36, 0.04),
2335        ];
2336        Self::push_tapered_prism(
2337            &mut vertices,
2338            &mut indices,
2339            &mut tri_materials,
2340            cabin_bot,
2341            cabin_top,
2342            [
2343                slot_glass,
2344                slot_glass,
2345                slot_glass,
2346                slot_glass,
2347                slot_body,
2348                slot_underbody,
2349            ],
2350        );
2351        let roof_bot: [Vec3; 4] = [
2352            Vec3::new(-0.66, 1.385, 0.81),
2353            Vec3::new(0.66, 1.385, 0.81),
2354            Vec3::new(0.66, 1.365, 0.03),
2355            Vec3::new(-0.66, 1.365, 0.03),
2356        ];
2357        let roof_top: [Vec3; 4] = [
2358            Vec3::new(-0.58, 1.425, 0.75),
2359            Vec3::new(0.58, 1.425, 0.75),
2360            Vec3::new(0.58, 1.410, 0.10),
2361            Vec3::new(-0.58, 1.410, 0.10),
2362        ];
2363        Self::push_tapered_prism(
2364            &mut vertices,
2365            &mut indices,
2366            &mut tri_materials,
2367            roof_bot,
2368            roof_top,
2369            [slot_body; 6],
2370        );
2371        for stripe_side in [-1.0_f32, 1.0_f32] {
2372            let sx0: f32 = if stripe_side > 0.0 { 0.07 } else { -0.25 };
2373            let sx1: f32 = if stripe_side > 0.0 { 0.25 } else { -0.07 };
2374            let hood_st_bot: [Vec3; 4] = [
2375                Vec3::new(sx0, 0.995, -0.60),
2376                Vec3::new(sx1, 0.995, -0.60),
2377                Vec3::new(sx1, 0.790, -2.32),
2378                Vec3::new(sx0, 0.790, -2.32),
2379            ];
2380            let hood_st_top: [Vec3; 4] = [
2381                Vec3::new(sx0, 1.015, -0.60),
2382                Vec3::new(sx1, 1.015, -0.60),
2383                Vec3::new(sx1, 0.810, -2.32),
2384                Vec3::new(sx0, 0.810, -2.32),
2385            ];
2386            Self::push_tapered_prism(
2387                &mut vertices,
2388                &mut indices,
2389                &mut tri_materials,
2390                hood_st_bot,
2391                hood_st_top,
2392                [slot_stripe; 6],
2393            );
2394            Self::push_box_part(
2395                &mut vertices,
2396                &mut indices,
2397                &mut tri_materials,
2398                Vec3::new((sx0 + sx1) * 0.5, 1.428, 0.42),
2399                Vec3::new(0.18, 0.022, 0.68),
2400                slot_stripe,
2401            );
2402            Self::push_box_part(
2403                &mut vertices,
2404                &mut indices,
2405                &mut tri_materials,
2406                Vec3::new((sx0 + sx1) * 0.5, 1.032, 2.04),
2407                Vec3::new(0.18, 0.022, 0.46),
2408                slot_stripe,
2409            );
2410            Self::push_box_part(
2411                &mut vertices,
2412                &mut indices,
2413                &mut tri_materials,
2414                Vec3::new((sx0 + sx1) * 0.5, 0.74, -2.35),
2415                Vec3::new(0.18, 0.12, 0.04),
2416                slot_stripe,
2417            );
2418        }
2419        Self::push_box_part(
2420            &mut vertices,
2421            &mut indices,
2422            &mut tri_materials,
2423            Vec3::new(0.0, 0.79, 2.33),
2424            Vec3::new(1.96, 0.30, 0.07),
2425            slot_carbon,
2426        );
2427        Self::push_box_part(
2428            &mut vertices,
2429            &mut indices,
2430            &mut tri_materials,
2431            Vec3::new(0.0, 0.79, 2.37),
2432            Vec3::new(0.14, 0.14, 0.03),
2433            slot_rim,
2434        );
2435        for side in [-1.0_f32, 1.0_f32] {
2436            Self::push_box_part(
2437                &mut vertices,
2438                &mut indices,
2439                &mut tri_materials,
2440                Vec3::new(side * 1.08, 0.24, 0.02),
2441                Vec3::new(0.14, 0.12, 2.08),
2442                slot_carbon,
2443            );
2444            Self::push_angled_bar(
2445                &mut vertices,
2446                &mut indices,
2447                &mut tri_materials,
2448                (
2449                    Vec3::new(side * 1.00, 0.34, -2.32),
2450                    Vec3::new(side * 1.12, 0.40, -2.14),
2451                ),
2452                (0.025, 0.020),
2453                slot_carbon,
2454            );
2455            Self::push_box_part(
2456                &mut vertices,
2457                &mut indices,
2458                &mut tri_materials,
2459                Vec3::new(side * 1.06, 0.64, -0.82),
2460                Vec3::new(0.08, 0.24, 0.16),
2461                slot_carbon,
2462            );
2463            Self::push_box_part(
2464                &mut vertices,
2465                &mut indices,
2466                &mut tri_materials,
2467                Vec3::new(side * 1.08, 0.64, 0.86),
2468                Vec3::new(0.10, 0.26, 0.44),
2469                slot_carbon,
2470            );
2471            for (fender_z, flare_w, h_rear, h_front) in [
2472                (-1.42_f32, 1.14_f32, 0.90_f32, 0.79_f32),
2473                (1.44_f32, 1.20_f32, 1.02_f32, 1.01_f32),
2474            ] {
2475                let x_in: f32 = side * 0.92;
2476                let x_out_lo: f32 = side * flare_w;
2477                let x_out_hi: f32 = side * (flare_w - 0.10);
2478                let (xa, xb): (f32, f32) = if side > 0.0 {
2479                    (x_in, x_out_lo)
2480                } else {
2481                    (x_out_lo, x_in)
2482                };
2483                let (ta, tb): (f32, f32) = if side > 0.0 {
2484                    (x_in, x_out_hi)
2485                } else {
2486                    (x_out_hi, x_in)
2487                };
2488                let arch_bot: [Vec3; 4] = [
2489                    Vec3::new(xa, 0.58, fender_z + 0.54),
2490                    Vec3::new(xb, 0.58, fender_z + 0.54),
2491                    Vec3::new(xb, 0.54, fender_z - 0.54),
2492                    Vec3::new(xa, 0.54, fender_z - 0.54),
2493                ];
2494                let arch_top: [Vec3; 4] = [
2495                    Vec3::new(ta, h_rear, fender_z + 0.42),
2496                    Vec3::new(tb, h_rear, fender_z + 0.42),
2497                    Vec3::new(tb, h_front, fender_z - 0.42),
2498                    Vec3::new(ta, h_front, fender_z - 0.42),
2499                ];
2500                Self::push_tapered_prism(
2501                    &mut vertices,
2502                    &mut indices,
2503                    &mut tri_materials,
2504                    arch_bot,
2505                    arch_top,
2506                    [
2507                        slot_body,
2508                        slot_body,
2509                        slot_body,
2510                        slot_body,
2511                        slot_body,
2512                        slot_underbody,
2513                    ],
2514                );
2515                Self::push_box_part(
2516                    &mut vertices,
2517                    &mut indices,
2518                    &mut tri_materials,
2519                    Vec3::new(side * 0.92, 0.44, fender_z),
2520                    Vec3::new(0.28, 0.54, 0.88),
2521                    slot_underbody,
2522                );
2523            }
2524            Self::push_box_part(
2525                &mut vertices,
2526                &mut indices,
2527                &mut tri_materials,
2528                Vec3::new(side * 1.04, 1.03, -0.34),
2529                Vec3::new(0.18, 0.08, 0.14),
2530                slot_body,
2531            );
2532            for louver_i in 0..3usize {
2533                let lz: f32 = 0.72 + (louver_i as f32) * 0.16;
2534                Self::push_angled_bar(
2535                    &mut vertices,
2536                    &mut indices,
2537                    &mut tri_materials,
2538                    (
2539                        Vec3::new(side * 0.84, 1.04, lz + 0.06),
2540                        Vec3::new(side * 0.74, 1.24, lz - 0.04),
2541                    ),
2542                    (0.020, 0.025),
2543                    slot_carbon,
2544                );
2545            }
2546            Self::push_box_part(
2547                &mut vertices,
2548                &mut indices,
2549                &mut tri_materials,
2550                Vec3::new(side * 0.74, 0.68, -2.33),
2551                Vec3::new(0.36, 0.14, 0.08),
2552                slot_carbon,
2553            );
2554            for gill_idx in 0..3usize {
2555                let gx: f32 = side * (0.61 + (gill_idx as f32) * 0.085);
2556                Self::push_box_part(
2557                    &mut vertices,
2558                    &mut indices,
2559                    &mut tri_materials,
2560                    Vec3::new(gx, 0.68, -2.37),
2561                    Vec3::new(0.045, 0.11, 0.04),
2562                    slot_headlight,
2563                );
2564            }
2565            for bar_idx in 0..3usize {
2566                let bx: f32 = side * (0.56 + (bar_idx as f32) * 0.16);
2567                Self::push_box_part(
2568                    &mut vertices,
2569                    &mut indices,
2570                    &mut tri_materials,
2571                    Vec3::new(bx, 0.79, 2.37),
2572                    Vec3::new(0.08, 0.24, 0.05),
2573                    slot_taillight,
2574                );
2575            }
2576        }
2577        Self::push_box_part(
2578            &mut vertices,
2579            &mut indices,
2580            &mut tri_materials,
2581            Vec3::new(0.0, 0.28, 2.31),
2582            Vec3::new(1.78, 0.22, 0.22),
2583            slot_carbon,
2584        );
2585        for fin_x in [-0.36_f32, -0.12_f32, 0.12_f32, 0.36_f32] {
2586            Self::push_box_part(
2587                &mut vertices,
2588                &mut indices,
2589                &mut tri_materials,
2590                Vec3::new(fin_x, 0.26, 2.38),
2591                Vec3::new(0.05, 0.20, 0.14),
2592                slot_carbon,
2593            );
2594        }
2595        for pipe_x in [-0.76_f32, -0.58_f32, 0.58_f32, 0.76_f32] {
2596            Self::push_box_part(
2597                &mut vertices,
2598                &mut indices,
2599                &mut tri_materials,
2600                Vec3::new(pipe_x, 0.30, 2.38),
2601                Vec3::new(0.11, 0.11, 0.14),
2602                slot_rim,
2603            );
2604            Self::push_box_part(
2605                &mut vertices,
2606                &mut indices,
2607                &mut tri_materials,
2608                Vec3::new(pipe_x, 0.30, 2.43),
2609                Vec3::new(0.06, 0.06, 0.04),
2610                slot_amber,
2611            );
2612        }
2613        Self::push_box_part(
2614            &mut vertices,
2615            &mut indices,
2616            &mut tri_materials,
2617            Vec3::new(0.0, 1.12, 2.24),
2618            Vec3::new(1.94, 0.05, 0.26),
2619            slot_carbon,
2620        );
2621        Self::push_box_part(
2622            &mut vertices,
2623            &mut indices,
2624            &mut tri_materials,
2625            Vec3::new(0.0, 1.16, 2.35),
2626            Vec3::new(1.92, 0.07, 0.04),
2627            slot_carbon,
2628        );
2629        for post_side in [-1.0_f32, 1.0_f32] {
2630            Self::push_box_part(
2631                &mut vertices,
2632                &mut indices,
2633                &mut tri_materials,
2634                Vec3::new(post_side * 0.74, 1.06, 2.22),
2635                Vec3::new(0.09, 0.12, 0.18),
2636                slot_carbon,
2637            );
2638            Self::push_box_part(
2639                &mut vertices,
2640                &mut indices,
2641                &mut tri_materials,
2642                Vec3::new(post_side * 0.96, 1.13, 2.24),
2643                Vec3::new(0.04, 0.12, 0.28),
2644                slot_carbon,
2645            );
2646        }
2647        let wheel_specs: [(f32, f32, f32, f32, f32); 4] = [
2648            (-1.03, -1.42, 0.35, 0.24, 0.34),
2649            (1.03, -1.42, 0.35, 0.24, 0.34),
2650            (-1.06, 1.44, 0.36, 0.25, 0.42),
2651            (1.06, 1.44, 0.36, 0.25, 0.42),
2652        ];
2653        for (wheel_x, wheel_z, tire_r, rim_r, tire_w) in wheel_specs.into_iter() {
2654            Self::push_supercar_wheel_assembly(
2655                &mut vertices,
2656                &mut indices,
2657                &mut tri_materials,
2658                Vec3::new(wheel_x, tire_r, wheel_z),
2659                (tire_r, rim_r, tire_w),
2660                (slot_tire, slot_rim, slot_carbon, slot_caliper),
2661                false,
2662            );
2663        }
2664        let mut mesh: Self = Self::from_parts(name.to_string(), vertices, indices, material);
2665        mesh.set_sub_materials(sub_materials);
2666        mesh.set_tri_materials(tri_materials);
2667        mesh.set_is_closed(true);
2668        mesh
2669    }
2670
2671    /// Generates an 8-vertex CCW chamfered octagon ring centered at `(0, y, 0)` with flat cardinal walls at `±half_extent` and 45-degree corner bevels cut at `±chamfer_inner`, optionally rotated by `twist_rad` around `+Y`.
2672    ///
2673    /// # Arguments
2674    ///
2675    /// - `f32` - Elevation along Y.
2676    /// - `f32` - Half-extent to flat cardinal walls.
2677    /// - `f32` - Coordinate where diagonal corner bevel begins (`<= half_extent`).
2678    /// - `f32` - Counter-clockwise rotation around `+Y` in radians.
2679    ///
2680    /// # Returns
2681    ///
2682    /// - `[Vec3; 8]` - Eight vertices ordered CCW when viewed from `+Y`.
2683    fn building_chamfered_ring(
2684        y_val: f32,
2685        half_extent: f32,
2686        chamfer_inner: f32,
2687        twist_rad: f32,
2688    ) -> [Vec3; 8] {
2689        let raw: [(f32, f32); 8] = [
2690            (half_extent, chamfer_inner),
2691            (half_extent, -chamfer_inner),
2692            (chamfer_inner, -half_extent),
2693            (-chamfer_inner, -half_extent),
2694            (-half_extent, -chamfer_inner),
2695            (-half_extent, chamfer_inner),
2696            (-chamfer_inner, half_extent),
2697            (chamfer_inner, half_extent),
2698        ];
2699        let (sin_t, cos_t): (f32, f32) = twist_rad.sin_cos();
2700        let mut ring: [Vec3; 8] = [Vec3::zero(); 8];
2701        for (idx, &(rx, rz)) in raw.iter().enumerate() {
2702            let rot_x: f32 = rx * cos_t + rz * sin_t;
2703            let rot_z: f32 = -rx * sin_t + rz * cos_t;
2704            ring[idx] = Vec3::new(rot_x, y_val, rot_z);
2705        }
2706        ring
2707    }
2708
2709    /// Generates a 16-vertex CCW circular/slanted ring centered at `(0, y_base, 0)` with vertices aligned on cardinal axes `(±radius, 0)` and `(0, ±radius)`.
2710    ///
2711    /// # Arguments
2712    ///
2713    /// - `f32` - Base elevation along Y.
2714    /// - `f32` - Ring radius in XZ (`<= 0.50`).
2715    /// - `f32` - Linear Y slope applied along `+X` (`0.0` for horizontal rings).
2716    ///
2717    /// # Returns
2718    ///
2719    /// - `[Vec3; MESH_CYLINDER_TOWER_SEGMENTS]` - Sixteen vertices ordered CCW when viewed from `+Y`.
2720    fn building_cylinder_ring(
2721        y_base: f32,
2722        radius: f32,
2723        slope_x: f32,
2724    ) -> [Vec3; MESH_CYLINDER_TOWER_SEGMENTS] {
2725        let mut ring: [Vec3; MESH_CYLINDER_TOWER_SEGMENTS] =
2726            [Vec3::zero(); MESH_CYLINDER_TOWER_SEGMENTS];
2727        let step_rad: f32 = (2.0 * PI) / (MESH_CYLINDER_TOWER_SEGMENTS as f32);
2728        for (idx, dst) in ring.iter_mut().enumerate() {
2729            let angle: f32 = (idx as f32) * step_rad;
2730            let (sin_a, cos_a): (f32, f32) = angle.sin_cos();
2731            let px: f32 = cos_a * radius;
2732            let pz: f32 = -sin_a * radius;
2733            let py: f32 = y_base + cos_a * slope_x;
2734            *dst = Vec3::new(px, py, pz);
2735        }
2736        ring
2737    }
2738
2739    /// Builds the shared 4-slot architectural accent sub-material table (`slot 1` warm stone/titanium trim, `slot 2` smoked roof/mullion, `slot 3` champagne-bronze metallic ribs/spires, `slot 4` warm illuminated crown/soffit glow).
2740    ///
2741    /// # Arguments
2742    ///
2743    /// - `Color` - Stone/trim accent color (`slot 1`).
2744    /// - `Color` - Metallic rib/spire accent color (`slot 3`).
2745    /// - `Color` - Warm illuminated architectural glow color (`slot 4`).
2746    ///
2747    /// # Returns
2748    ///
2749    /// - `Vec<PbrMaterial>` - Four architectural accent sub-materials.
2750    fn building_sub_materials(
2751        trim_color: Color,
2752        metal_color: Color,
2753        glow_color: Color,
2754    ) -> Vec<PbrMaterial> {
2755        vec![
2756            PbrMaterial::pbr(trim_color, 0.22, 0.42, 0.32),
2757            PbrMaterial::pbr(Color::hex(0x161D29), 0.36, 0.56, 0.24),
2758            PbrMaterial::metallic(metal_color, 0.18),
2759            PbrMaterial::neon(glow_color, 1.15),
2760        ]
2761    }
2762
2763    /// Generates a 4-vertex CCW horizontal rectangular ring centered at `(center_x, y_val, center_z)` with half-extents `(half_x, half_z)`.
2764    ///
2765    /// # Arguments
2766    ///
2767    /// - `f32` - Horizontal X center coordinate.
2768    /// - `f32` - Vertical elevation along Y.
2769    /// - `f32` - Horizontal Z center coordinate.
2770    /// - `f32` - Half extent along X.
2771    /// - `f32` - Half extent along Z.
2772    ///
2773    /// # Returns
2774    ///
2775    /// - `[Vec3; 4]` - Four vertices ordered CCW when viewed from `+Y`.
2776    fn building_rect_ring(
2777        center_x: f32,
2778        y_val: f32,
2779        center_z: f32,
2780        half_x: f32,
2781        half_z: f32,
2782    ) -> [Vec3; 4] {
2783        [
2784            Vec3::new(center_x + half_x, y_val, center_z + half_z),
2785            Vec3::new(center_x + half_x, y_val, center_z - half_z),
2786            Vec3::new(center_x - half_x, y_val, center_z - half_z),
2787            Vec3::new(center_x - half_x, y_val, center_z + half_z),
2788        ]
2789    }
2790
2791    /// Generates a 4-vertex CCW cross-section ring oriented radially around `anchor` for continuous helical spire ribs.
2792    ///
2793    /// # Arguments
2794    ///
2795    /// - `Vec3` - Corner anchor point on the twisted tower perimeter.
2796    /// - `f32` - Outward radial offset applied to the rib center.
2797    /// - `f32` - Half thickness along the radial axis.
2798    /// - `f32` - Half width along the horizontal tangent axis.
2799    ///
2800    /// # Returns
2801    ///
2802    /// - `[Vec3; 4]` - Four cross-section vertices ordered CCW when viewed from `+Y`.
2803    fn building_rib_ring(
2804        anchor: Vec3,
2805        radial_offset: f32,
2806        half_radial: f32,
2807        half_tangent: f32,
2808    ) -> [Vec3; 4] {
2809        let radial_horiz: Vec3 = Vec3::new(anchor.get_x(), 0.0, anchor.get_z());
2810        let radial_dir: Vec3 = if radial_horiz.length_sq() > 1.0e-8 {
2811            radial_horiz.normalize()
2812        } else {
2813            Vec3::new(1.0, 0.0, 0.0)
2814        };
2815        let tangent_dir: Vec3 = Vec3::new(radial_dir.get_z(), 0.0, -radial_dir.get_x());
2816        let center: Vec3 = anchor.add(radial_dir.scale(radial_offset));
2817        let radial_vec: Vec3 = radial_dir.scale(half_radial);
2818        let tangent_vec: Vec3 = tangent_dir.scale(half_tangent);
2819        [
2820            center.add(radial_vec).sub(tangent_vec),
2821            center.add(radial_vec).add(tangent_vec),
2822            center.sub(radial_vec).add(tangent_vec),
2823            center.sub(radial_vec).sub(tangent_vec),
2824        ]
2825    }
2826
2827    /// Builds Archetype 1: an Art-Deco stepped setback skyscraper (`[-0.5, +0.5]` unit footprint, `y in [0, 1]`) featuring a grand stone podium, three tiered setbacks with vertical corner pilasters, an illuminated warm-gold crown pavilion, and a slender tapered spire.
2828    ///
2829    /// # Arguments
2830    ///
2831    /// - `&str` - Mesh asset name.
2832    /// - `PbrMaterial` - Primary curtain-wall facade material (`slot 0`).
2833    ///
2834    /// # Returns
2835    ///
2836    /// - `Self` - Closed multi-material unit Art-Deco setback tower mesh.
2837    #[must_use]
2838    pub fn building_art_deco_tower(name: &str, material: PbrMaterial) -> Self {
2839        let sub_materials: Vec<PbrMaterial> = Self::building_sub_materials(
2840            Color::hex(0x9A8970),
2841            Color::hex(0xB89456),
2842            Color::hex(0xFFCB6B),
2843        );
2844        let slot_facade: u16 = 0;
2845        let slot_stone: u16 = 1;
2846        let slot_roof: u16 = 2;
2847        let slot_metal: u16 = 3;
2848        let slot_glow: u16 = 4;
2849        let mut vertices: Vec<Vertex3D> = Vec::with_capacity(640);
2850        let mut indices: Vec<[u32; 3]> = Vec::with_capacity(300);
2851        let mut tri_materials: Vec<u16> = Vec::with_capacity(300);
2852        let ring_base_bot: [Vec3; 4] = Self::building_rect_ring(0.0, 0.000, 0.0, 0.480, 0.480);
2853        let ring_base_top: [Vec3; 4] = Self::building_rect_ring(0.0, 0.160, 0.0, 0.480, 0.480);
2854        let ring_pod_bot: [Vec3; 4] = Self::building_rect_ring(0.0, 0.160, 0.0, 0.500, 0.500);
2855        let ring_pod_top: [Vec3; 4] = Self::building_rect_ring(0.0, 0.188, 0.0, 0.500, 0.500);
2856        let ring_mid_bot: [Vec3; 4] = Self::building_rect_ring(0.0, 0.188, 0.0, 0.425, 0.425);
2857        let ring_mid_top: [Vec3; 4] = Self::building_rect_ring(0.0, 0.550, 0.0, 0.425, 0.425);
2858        let ring_mid_ledge_bot: [Vec3; 4] = Self::building_rect_ring(0.0, 0.550, 0.0, 0.472, 0.472);
2859        let ring_mid_ledge_top: [Vec3; 4] = Self::building_rect_ring(0.0, 0.578, 0.0, 0.472, 0.472);
2860        let ring_upper_bot: [Vec3; 4] = Self::building_rect_ring(0.0, 0.578, 0.0, 0.335, 0.335);
2861        let ring_upper_top: [Vec3; 4] = Self::building_rect_ring(0.0, 0.750, 0.0, 0.335, 0.335);
2862        let ring_upper_ledge_bot: [Vec3; 4] =
2863            Self::building_rect_ring(0.0, 0.750, 0.0, 0.382, 0.382);
2864        let ring_upper_ledge_top: [Vec3; 4] =
2865            Self::building_rect_ring(0.0, 0.778, 0.0, 0.382, 0.382);
2866        let ring_crown_bot: [Vec3; 4] = Self::building_rect_ring(0.0, 0.778, 0.0, 0.235, 0.235);
2867        let ring_crown_top: [Vec3; 4] = Self::building_rect_ring(0.0, 0.854, 0.0, 0.235, 0.235);
2868        let ring_metal_bot: [Vec3; 4] = Self::building_rect_ring(0.0, 0.854, 0.0, 0.255, 0.255);
2869        let ring_metal_top: [Vec3; 4] = Self::building_rect_ring(0.0, 0.870, 0.0, 0.255, 0.255);
2870        let ring_glow_bot: [Vec3; 4] = Self::building_rect_ring(0.0, 0.870, 0.0, 0.160, 0.160);
2871        let ring_glow_top: [Vec3; 4] = Self::building_rect_ring(0.0, 0.916, 0.0, 0.160, 0.160);
2872        let ring_roof_bot: [Vec3; 4] = Self::building_rect_ring(0.0, 0.916, 0.0, 0.176, 0.176);
2873        let ring_roof_top: [Vec3; 4] = Self::building_rect_ring(0.0, 0.932, 0.0, 0.176, 0.176);
2874        let ring_spire_bot: [Vec3; 4] = Self::building_rect_ring(0.0, 0.932, 0.0, 0.072, 0.072);
2875        let ring_spire_top: [Vec3; 4] = Self::building_rect_ring(0.0, 0.984, 0.0, 0.016, 0.016);
2876        let ring_beacon_bot: [Vec3; 4] = Self::building_rect_ring(0.0, 0.984, 0.0, 0.022, 0.022);
2877        let ring_beacon_top: [Vec3; 4] = Self::building_rect_ring(0.0, 1.000, 0.0, 0.018, 0.018);
2878        let shell_rings: [(&[Vec3], u16); 24] = [
2879            (&ring_base_bot, slot_roof),
2880            (&ring_base_top, slot_facade),
2881            (&ring_pod_bot, slot_stone),
2882            (&ring_pod_top, slot_stone),
2883            (&ring_mid_bot, slot_stone),
2884            (&ring_mid_top, slot_facade),
2885            (&ring_mid_ledge_bot, slot_stone),
2886            (&ring_mid_ledge_top, slot_stone),
2887            (&ring_upper_bot, slot_stone),
2888            (&ring_upper_top, slot_facade),
2889            (&ring_upper_ledge_bot, slot_stone),
2890            (&ring_upper_ledge_top, slot_stone),
2891            (&ring_crown_bot, slot_stone),
2892            (&ring_crown_top, slot_facade),
2893            (&ring_metal_bot, slot_metal),
2894            (&ring_metal_top, slot_metal),
2895            (&ring_glow_bot, slot_metal),
2896            (&ring_glow_top, slot_glow),
2897            (&ring_roof_bot, slot_roof),
2898            (&ring_roof_top, slot_roof),
2899            (&ring_spire_bot, slot_roof),
2900            (&ring_spire_top, slot_metal),
2901            (&ring_beacon_bot, slot_glow),
2902            (&ring_beacon_top, slot_glow),
2903        ];
2904        Self::push_stepped_polygon_shell(
2905            &mut vertices,
2906            &mut indices,
2907            &mut tri_materials,
2908            &shell_rings,
2909            (slot_roof, slot_glow),
2910        );
2911        for sign_x in [-1.0_f32, 1.0_f32] {
2912            for sign_z in [-1.0_f32, 1.0_f32] {
2913                Self::push_box_part(
2914                    &mut vertices,
2915                    &mut indices,
2916                    &mut tri_materials,
2917                    Vec3::new(sign_x * 0.426, 0.369, sign_z * 0.426),
2918                    Vec3::new(0.060, 0.390, 0.060),
2919                    slot_metal,
2920                );
2921                Self::push_box_part(
2922                    &mut vertices,
2923                    &mut indices,
2924                    &mut tri_materials,
2925                    Vec3::new(sign_x * 0.336, 0.664, sign_z * 0.336),
2926                    Vec3::new(0.056, 0.200, 0.056),
2927                    slot_metal,
2928                );
2929            }
2930        }
2931        let mut mesh: Self = Self::from_parts(name.to_string(), vertices, indices, material);
2932        mesh.set_sub_materials(sub_materials);
2933        mesh.set_tri_materials(tri_materials);
2934        mesh.set_is_closed(true);
2935        mesh
2936    }
2937
2938    /// Builds Archetype 2: a chamfered octagonal modernist glass tower (`[-0.5, +0.5]` unit footprint, `y in [0, 1]`) with horizontal structural sky-deck slab bands, a recessed illuminated mid-level sky-garden notch, and a sculpted angled roof parapet.
2939    ///
2940    /// # Arguments
2941    ///
2942    /// - `&str` - Mesh asset name.
2943    /// - `PbrMaterial` - Primary glass curtain-wall material (`slot 0`).
2944    ///
2945    /// # Returns
2946    ///
2947    /// - `Self` - Closed multi-material unit chamfered glass tower mesh.
2948    #[must_use]
2949    pub fn building_chamfered_glass_tower(name: &str, material: PbrMaterial) -> Self {
2950        let sub_materials: Vec<PbrMaterial> = Self::building_sub_materials(
2951            Color::hex(0x64748B),
2952            Color::hex(0x94A3B8),
2953            Color::hex(0x7DD3FC),
2954        );
2955        let slot_facade: u16 = 0;
2956        let slot_slab: u16 = 1;
2957        let slot_roof: u16 = 2;
2958        let slot_metal: u16 = 3;
2959        let slot_glow: u16 = 4;
2960        let mut vertices: Vec<Vertex3D> = Vec::with_capacity(960);
2961        let mut indices: Vec<[u32; 3]> = Vec::with_capacity(460);
2962        let mut tri_materials: Vec<u16> = Vec::with_capacity(460);
2963        let ring_00: [Vec3; 8] = Self::building_chamfered_ring(0.000, 0.480, 0.315, 0.0);
2964        let ring_01: [Vec3; 8] = Self::building_chamfered_ring(0.140, 0.480, 0.315, 0.0);
2965        let ring_02: [Vec3; 8] = Self::building_chamfered_ring(0.140, 0.500, 0.335, 0.0);
2966        let ring_03: [Vec3; 8] = Self::building_chamfered_ring(0.164, 0.500, 0.335, 0.0);
2967        let ring_04: [Vec3; 8] = Self::building_chamfered_ring(0.164, 0.480, 0.315, 0.0);
2968        let ring_05: [Vec3; 8] = Self::building_chamfered_ring(0.330, 0.480, 0.315, 0.0);
2969        let ring_06: [Vec3; 8] = Self::building_chamfered_ring(0.330, 0.500, 0.335, 0.0);
2970        let ring_07: [Vec3; 8] = Self::building_chamfered_ring(0.354, 0.500, 0.335, 0.0);
2971        let ring_08: [Vec3; 8] = Self::building_chamfered_ring(0.354, 0.480, 0.315, 0.0);
2972        let ring_09: [Vec3; 8] = Self::building_chamfered_ring(0.508, 0.480, 0.315, 0.0);
2973        let ring_10: [Vec3; 8] = Self::building_chamfered_ring(0.508, 0.500, 0.335, 0.0);
2974        let ring_11: [Vec3; 8] = Self::building_chamfered_ring(0.536, 0.500, 0.335, 0.0);
2975        let ring_12: [Vec3; 8] = Self::building_chamfered_ring(0.536, 0.380, 0.250, 0.0);
2976        let ring_13: [Vec3; 8] = Self::building_chamfered_ring(0.614, 0.380, 0.250, 0.0);
2977        let ring_14: [Vec3; 8] = Self::building_chamfered_ring(0.614, 0.495, 0.330, 0.0);
2978        let ring_15: [Vec3; 8] = Self::building_chamfered_ring(0.642, 0.495, 0.330, 0.0);
2979        let ring_16: [Vec3; 8] = Self::building_chamfered_ring(0.642, 0.475, 0.310, 0.0);
2980        let ring_17: [Vec3; 8] = Self::building_chamfered_ring(0.760, 0.475, 0.310, 0.0);
2981        let ring_18: [Vec3; 8] = Self::building_chamfered_ring(0.760, 0.495, 0.330, 0.0);
2982        let ring_19: [Vec3; 8] = Self::building_chamfered_ring(0.784, 0.495, 0.330, 0.0);
2983        let ring_20: [Vec3; 8] = Self::building_chamfered_ring(0.784, 0.475, 0.310, 0.0);
2984        let ring_21: [Vec3; 8] = Self::building_chamfered_ring(0.884, 0.475, 0.310, 0.0);
2985        let ring_22: [Vec3; 8] = Self::building_chamfered_ring(0.884, 0.492, 0.324, 0.0);
2986        let ring_23: [Vec3; 8] = Self::building_chamfered_ring(0.912, 0.492, 0.324, 0.0);
2987        let mut ring_24: [Vec3; 8] = Self::building_chamfered_ring(0.962, 0.475, 0.310, 0.0);
2988        for corner in ring_24.iter_mut() {
2989            let slope_y: f32 = corner.get_y() + corner.get_x() * 0.075;
2990            corner.set_y(slope_y.min(1.0));
2991        }
2992        let shell_rings: [(&[Vec3], u16); 25] = [
2993            (&ring_00, slot_roof),
2994            (&ring_01, slot_facade),
2995            (&ring_02, slot_slab),
2996            (&ring_03, slot_slab),
2997            (&ring_04, slot_slab),
2998            (&ring_05, slot_facade),
2999            (&ring_06, slot_slab),
3000            (&ring_07, slot_slab),
3001            (&ring_08, slot_slab),
3002            (&ring_09, slot_facade),
3003            (&ring_10, slot_slab),
3004            (&ring_11, slot_slab),
3005            (&ring_12, slot_slab),
3006            (&ring_13, slot_facade),
3007            (&ring_14, slot_glow),
3008            (&ring_15, slot_slab),
3009            (&ring_16, slot_slab),
3010            (&ring_17, slot_facade),
3011            (&ring_18, slot_slab),
3012            (&ring_19, slot_slab),
3013            (&ring_20, slot_slab),
3014            (&ring_21, slot_facade),
3015            (&ring_22, slot_glow),
3016            (&ring_23, slot_glow),
3017            (&ring_24, slot_metal),
3018        ];
3019        Self::push_stepped_polygon_shell(
3020            &mut vertices,
3021            &mut indices,
3022            &mut tri_materials,
3023            &shell_rings,
3024            (slot_roof, slot_roof),
3025        );
3026        for sign_x in [-1.0_f32, 1.0_f32] {
3027            for sign_z in [-1.0_f32, 1.0_f32] {
3028                Self::push_box_part(
3029                    &mut vertices,
3030                    &mut indices,
3031                    &mut tri_materials,
3032                    Vec3::new(sign_x * 0.365, 0.575, sign_z * 0.365),
3033                    Vec3::new(0.058, 0.106, 0.058),
3034                    slot_metal,
3035                );
3036            }
3037        }
3038        let mut mesh: Self = Self::from_parts(name.to_string(), vertices, indices, material);
3039        mesh.set_sub_materials(sub_materials);
3040        mesh.set_tri_materials(tri_materials);
3041        mesh.set_is_closed(true);
3042        mesh
3043    }
3044
3045    /// Builds Archetype 3: a twisted tapering futuristic spire (`[-0.5, +0.5]` unit footprint, `y in [0, 1]`) with helical metallic exoskeletal ribs, a glowing observation rim band, and an asymmetrical diagonal roof fin.
3046    ///
3047    /// # Arguments
3048    ///
3049    /// - `&str` - Mesh asset name.
3050    /// - `PbrMaterial` - Primary curtain-wall facade material (`slot 0`).
3051    ///
3052    /// # Returns
3053    ///
3054    /// - `Self` - Closed multi-material unit twisted spire mesh.
3055    #[must_use]
3056    pub fn building_twisted_spire_tower(name: &str, material: PbrMaterial) -> Self {
3057        let sub_materials: Vec<PbrMaterial> = Self::building_sub_materials(
3058            Color::hex(0x334155),
3059            Color::hex(0xD8E2EE),
3060            Color::hex(0x38BDF8),
3061        );
3062        let slot_facade: u16 = 0;
3063        let slot_base: u16 = 1;
3064        let slot_roof: u16 = 2;
3065        let slot_metal: u16 = 3;
3066        let slot_glow: u16 = 4;
3067        let mut vertices: Vec<Vertex3D> = Vec::with_capacity(820);
3068        let mut indices: Vec<[u32; 3]> = Vec::with_capacity(380);
3069        let mut tri_materials: Vec<u16> = Vec::with_capacity(380);
3070        let base_lo: [Vec3; 8] = Self::building_chamfered_ring(0.000, 0.500, 0.340, 0.00);
3071        let base_hi: [Vec3; 8] = Self::building_chamfered_ring(0.120, 0.490, 0.330, 0.00);
3072        let tier_0: [Vec3; 8] = Self::building_chamfered_ring(0.120, 0.450, 0.300, 0.00);
3073        let tier_1: [Vec3; 8] = Self::building_chamfered_ring(0.320, 0.410, 0.270, 0.16);
3074        let tier_2: [Vec3; 8] = Self::building_chamfered_ring(0.520, 0.360, 0.240, 0.32);
3075        let tier_3: [Vec3; 8] = Self::building_chamfered_ring(0.700, 0.310, 0.200, 0.48);
3076        let tier_4: [Vec3; 8] = Self::building_chamfered_ring(0.836, 0.260, 0.170, 0.62);
3077        let glow_lo: [Vec3; 8] = Self::building_chamfered_ring(0.836, 0.278, 0.184, 0.62);
3078        let glow_hi: [Vec3; 8] = Self::building_chamfered_ring(0.864, 0.278, 0.184, 0.62);
3079        let cap_hi: [Vec3; 8] = Self::building_chamfered_ring(0.892, 0.256, 0.168, 0.62);
3080        let shell_rings: [(&[Vec3], u16); 10] = [
3081            (&base_lo, slot_roof),
3082            (&base_hi, slot_base),
3083            (&tier_0, slot_roof),
3084            (&tier_1, slot_facade),
3085            (&tier_2, slot_facade),
3086            (&tier_3, slot_facade),
3087            (&tier_4, slot_facade),
3088            (&glow_lo, slot_glow),
3089            (&glow_hi, slot_glow),
3090            (&cap_hi, slot_base),
3091        ];
3092        Self::push_stepped_polygon_shell(
3093            &mut vertices,
3094            &mut indices,
3095            &mut tri_materials,
3096            &shell_rings,
3097            (slot_roof, slot_roof),
3098        );
3099        let rib_anchor_bot: [Vec3; 8] = Self::building_chamfered_ring(0.105, 0.453, 0.302, 0.00);
3100        let rib_anchor_top: [Vec3; 8] = Self::building_chamfered_ring(0.850, 0.258, 0.169, 0.62);
3101        for rib_idx in [0usize, 2usize, 4usize, 6usize] {
3102            let rib_r0: [Vec3; 4] =
3103                Self::building_rib_ring(rib_anchor_bot[rib_idx], 0.006, 0.018, 0.018);
3104            let rib_r1: [Vec3; 4] = Self::building_rib_ring(tier_1[rib_idx], 0.006, 0.018, 0.018);
3105            let rib_r2: [Vec3; 4] = Self::building_rib_ring(tier_2[rib_idx], 0.006, 0.018, 0.018);
3106            let rib_r3: [Vec3; 4] = Self::building_rib_ring(tier_3[rib_idx], 0.006, 0.018, 0.018);
3107            let rib_r4: [Vec3; 4] =
3108                Self::building_rib_ring(rib_anchor_top[rib_idx], 0.006, 0.018, 0.018);
3109            let rib_rings: [(&[Vec3], u16); 5] = [
3110                (&rib_r0, slot_metal),
3111                (&rib_r1, slot_metal),
3112                (&rib_r2, slot_metal),
3113                (&rib_r3, slot_metal),
3114                (&rib_r4, slot_metal),
3115            ];
3116            Self::push_stepped_polygon_shell(
3117                &mut vertices,
3118                &mut indices,
3119                &mut tri_materials,
3120                &rib_rings,
3121                (slot_metal, slot_metal),
3122            );
3123        }
3124        let fin_bot: [Vec3; 4] = [
3125            Vec3::new(-0.18, 0.878, 0.075),
3126            Vec3::new(0.20, 0.878, 0.075),
3127            Vec3::new(0.20, 0.878, -0.075),
3128            Vec3::new(-0.18, 0.878, -0.075),
3129        ];
3130        let fin_top: [Vec3; 4] = [
3131            Vec3::new(0.06, 1.000, 0.045),
3132            Vec3::new(0.20, 0.990, 0.045),
3133            Vec3::new(0.20, 0.990, -0.045),
3134            Vec3::new(0.06, 1.000, -0.045),
3135        ];
3136        Self::push_tapered_prism(
3137            &mut vertices,
3138            &mut indices,
3139            &mut tri_materials,
3140            fin_bot,
3141            fin_top,
3142            [slot_metal; 6],
3143        );
3144        let mut mesh: Self = Self::from_parts(name.to_string(), vertices, indices, material);
3145        mesh.set_sub_materials(sub_materials);
3146        mesh.set_tri_materials(tri_materials);
3147        mesh.set_is_closed(true);
3148        mesh
3149    }
3150
3151    /// Builds Archetype 4: a cantilevered contemporary tech and cultural tower (`[-0.5, +0.5]` unit footprint, `y in [0, 1]`) with a sleek glass podium, recessed atrium waist, boldly offset upper cantilevered volumes with warm illuminated overhang soffits, and a rooftop louver pergola.
3152    ///
3153    /// # Arguments
3154    ///
3155    /// - `&str` - Mesh asset name.
3156    /// - `PbrMaterial` - Primary curtain-wall facade material (`slot 0`).
3157    ///
3158    /// # Returns
3159    ///
3160    /// - `Self` - Closed multi-material unit cantilevered tower mesh.
3161    #[must_use]
3162    pub fn building_cantilever_tech_tower(name: &str, material: PbrMaterial) -> Self {
3163        let sub_materials: Vec<PbrMaterial> = Self::building_sub_materials(
3164            Color::hex(0x475569),
3165            Color::hex(0xCBD5E1),
3166            Color::hex(0xFBBF24),
3167        );
3168        let slot_facade: u16 = 0;
3169        let slot_frame: u16 = 1;
3170        let slot_roof: u16 = 2;
3171        let slot_metal: u16 = 3;
3172        let slot_glow: u16 = 4;
3173        let mut vertices: Vec<Vertex3D> = Vec::with_capacity(680);
3174        let mut indices: Vec<[u32; 3]> = Vec::with_capacity(320);
3175        let mut tri_materials: Vec<u16> = Vec::with_capacity(320);
3176        let lower_r0: [Vec3; 4] = Self::building_rect_ring(0.0, 0.000, 0.0, 0.480, 0.480);
3177        let lower_r1: [Vec3; 4] = Self::building_rect_ring(0.0, 0.208, 0.0, 0.480, 0.480);
3178        let lower_r2: [Vec3; 4] = Self::building_rect_ring(0.0, 0.208, 0.0, 0.500, 0.500);
3179        let lower_r3: [Vec3; 4] = Self::building_rect_ring(0.0, 0.236, 0.0, 0.500, 0.500);
3180        let lower_r4: [Vec3; 4] = Self::building_rect_ring(0.0, 0.236, 0.0, 0.310, 0.310);
3181        let lower_r5: [Vec3; 4] = Self::building_rect_ring(0.0, 0.436, 0.0, 0.310, 0.310);
3182        let mid_r0: [Vec3; 4] = Self::building_rect_ring(-0.06, 0.436, 0.02, 0.400, 0.440);
3183        let mid_r1: [Vec3; 4] = Self::building_rect_ring(-0.06, 0.456, 0.02, 0.400, 0.440);
3184        let mid_r2: [Vec3; 4] = Self::building_rect_ring(-0.06, 0.456, 0.02, 0.415, 0.455);
3185        let mid_r3: [Vec3; 4] = Self::building_rect_ring(-0.06, 0.690, 0.02, 0.415, 0.455);
3186        let mid_r4: [Vec3; 4] = Self::building_rect_ring(-0.06, 0.690, 0.02, 0.430, 0.460);
3187        let mid_r5: [Vec3; 4] = Self::building_rect_ring(-0.06, 0.718, 0.02, 0.430, 0.460);
3188        let lower_mid_rings: [(&[Vec3], u16); 12] = [
3189            (&lower_r0, slot_roof),
3190            (&lower_r1, slot_facade),
3191            (&lower_r2, slot_frame),
3192            (&lower_r3, slot_frame),
3193            (&lower_r4, slot_frame),
3194            (&lower_r5, slot_facade),
3195            (&mid_r0, slot_glow),
3196            (&mid_r1, slot_glow),
3197            (&mid_r2, slot_facade),
3198            (&mid_r3, slot_facade),
3199            (&mid_r4, slot_frame),
3200            (&mid_r5, slot_frame),
3201        ];
3202        Self::push_stepped_polygon_shell(
3203            &mut vertices,
3204            &mut indices,
3205            &mut tri_materials,
3206            &lower_mid_rings,
3207            (slot_roof, slot_frame),
3208        );
3209        let upper_r0: [Vec3; 4] = Self::building_rect_ring(0.08, 0.704, -0.02, 0.380, 0.440);
3210        let upper_r1: [Vec3; 4] = Self::building_rect_ring(0.08, 0.732, -0.02, 0.380, 0.440);
3211        let upper_r2: [Vec3; 4] = Self::building_rect_ring(0.08, 0.732, -0.02, 0.400, 0.460);
3212        let upper_r3: [Vec3; 4] = Self::building_rect_ring(0.08, 0.922, -0.02, 0.400, 0.460);
3213        let upper_r4: [Vec3; 4] = Self::building_rect_ring(0.08, 0.922, -0.02, 0.420, 0.480);
3214        let upper_r5: [Vec3; 4] = Self::building_rect_ring(0.08, 0.950, -0.02, 0.420, 0.480);
3215        let upper_rings: [(&[Vec3], u16); 6] = [
3216            (&upper_r0, slot_glow),
3217            (&upper_r1, slot_glow),
3218            (&upper_r2, slot_facade),
3219            (&upper_r3, slot_facade),
3220            (&upper_r4, slot_roof),
3221            (&upper_r5, slot_roof),
3222        ];
3223        Self::push_stepped_polygon_shell(
3224            &mut vertices,
3225            &mut indices,
3226            &mut tri_materials,
3227            &upper_rings,
3228            (slot_glow, slot_roof),
3229        );
3230        for post_x in [-0.26_f32, 0.42_f32] {
3231            for post_z in [-0.40_f32, 0.36_f32] {
3232                Self::push_box_part(
3233                    &mut vertices,
3234                    &mut indices,
3235                    &mut tri_materials,
3236                    Vec3::new(post_x, 0.9615, post_z),
3237                    Vec3::new(0.040, 0.051, 0.040),
3238                    slot_metal,
3239                );
3240            }
3241        }
3242        for beam_idx in 0..5usize {
3243            let beam_z: f32 = -0.38 + (beam_idx as f32) * 0.19;
3244            Self::push_box_part(
3245                &mut vertices,
3246                &mut indices,
3247                &mut tri_materials,
3248                Vec3::new(0.08, 0.987, beam_z),
3249                Vec3::new(0.76, 0.026, 0.044),
3250                slot_metal,
3251            );
3252        }
3253        let mut mesh: Self = Self::from_parts(name.to_string(), vertices, indices, material);
3254        mesh.set_sub_materials(sub_materials);
3255        mesh.set_tri_materials(tri_materials);
3256        mesh.set_is_closed(true);
3257        mesh
3258    }
3259
3260    /// Builds Archetype 5: a 16-sided smooth cylindrical landmark glass tower (`[-0.5, +0.5]` unit footprint, `y in [0, 1]`) with a recessed lobby colonnade, horizontal metallic floor-slab rings, and a sculpted slanted crown.
3261    ///
3262    /// # Arguments
3263    ///
3264    /// - `&str` - Mesh asset name.
3265    /// - `PbrMaterial` - Primary curved glass facade material (`slot 0`).
3266    ///
3267    /// # Returns
3268    ///
3269    /// - `Self` - Closed multi-material unit cylindrical landmark mesh.
3270    #[must_use]
3271    pub fn building_cylindrical_landmark(name: &str, material: PbrMaterial) -> Self {
3272        let sub_materials: Vec<PbrMaterial> = Self::building_sub_materials(
3273            Color::hex(0xCBD5E1),
3274            Color::hex(0xE2E8F0),
3275            Color::hex(0x34D399),
3276        );
3277        let slot_facade: u16 = 0;
3278        let slot_stone: u16 = 1;
3279        let slot_roof: u16 = 2;
3280        let slot_metal: u16 = 3;
3281        let slot_glow: u16 = 4;
3282        let mut vertices: Vec<Vertex3D> = Vec::with_capacity(1400);
3283        let mut indices: Vec<[u32; 3]> = Vec::with_capacity(680);
3284        let mut tri_materials: Vec<u16> = Vec::with_capacity(680);
3285        let ring_00: [Vec3; MESH_CYLINDER_TOWER_SEGMENTS] =
3286            Self::building_cylinder_ring(0.000, 0.380, 0.0);
3287        let ring_01: [Vec3; MESH_CYLINDER_TOWER_SEGMENTS] =
3288            Self::building_cylinder_ring(0.136, 0.380, 0.0);
3289        let ring_02: [Vec3; MESH_CYLINDER_TOWER_SEGMENTS] =
3290            Self::building_cylinder_ring(0.136, 0.500, 0.0);
3291        let ring_03: [Vec3; MESH_CYLINDER_TOWER_SEGMENTS] =
3292            Self::building_cylinder_ring(0.166, 0.500, 0.0);
3293        let ring_04: [Vec3; MESH_CYLINDER_TOWER_SEGMENTS] =
3294            Self::building_cylinder_ring(0.166, 0.478, 0.0);
3295        let ring_05: [Vec3; MESH_CYLINDER_TOWER_SEGMENTS] =
3296            Self::building_cylinder_ring(0.376, 0.478, 0.0);
3297        let ring_06: [Vec3; MESH_CYLINDER_TOWER_SEGMENTS] =
3298            Self::building_cylinder_ring(0.376, 0.500, 0.0);
3299        let ring_07: [Vec3; MESH_CYLINDER_TOWER_SEGMENTS] =
3300            Self::building_cylinder_ring(0.398, 0.500, 0.0);
3301        let ring_08: [Vec3; MESH_CYLINDER_TOWER_SEGMENTS] =
3302            Self::building_cylinder_ring(0.398, 0.478, 0.0);
3303        let ring_09: [Vec3; MESH_CYLINDER_TOWER_SEGMENTS] =
3304            Self::building_cylinder_ring(0.596, 0.478, 0.0);
3305        let ring_10: [Vec3; MESH_CYLINDER_TOWER_SEGMENTS] =
3306            Self::building_cylinder_ring(0.596, 0.500, 0.0);
3307        let ring_11: [Vec3; MESH_CYLINDER_TOWER_SEGMENTS] =
3308            Self::building_cylinder_ring(0.618, 0.500, 0.0);
3309        let ring_12: [Vec3; MESH_CYLINDER_TOWER_SEGMENTS] =
3310            Self::building_cylinder_ring(0.618, 0.478, 0.0);
3311        let ring_13: [Vec3; MESH_CYLINDER_TOWER_SEGMENTS] =
3312            Self::building_cylinder_ring(0.806, 0.478, 0.0);
3313        let ring_14: [Vec3; MESH_CYLINDER_TOWER_SEGMENTS] =
3314            Self::building_cylinder_ring(0.806, 0.500, 0.0);
3315        let ring_15: [Vec3; MESH_CYLINDER_TOWER_SEGMENTS] =
3316            Self::building_cylinder_ring(0.828, 0.500, 0.0);
3317        let ring_16: [Vec3; MESH_CYLINDER_TOWER_SEGMENTS] =
3318            Self::building_cylinder_ring(0.828, 0.478, 0.0);
3319        let ring_17: [Vec3; MESH_CYLINDER_TOWER_SEGMENTS] =
3320            Self::building_cylinder_ring(0.850, 0.478, 0.0);
3321        let ring_18: [Vec3; MESH_CYLINDER_TOWER_SEGMENTS] =
3322            Self::building_cylinder_ring(0.850, 0.498, 0.0);
3323        let ring_19: [Vec3; MESH_CYLINDER_TOWER_SEGMENTS] =
3324            Self::building_cylinder_ring(0.870, 0.498, 0.0);
3325        let ring_20: [Vec3; MESH_CYLINDER_TOWER_SEGMENTS] =
3326            Self::building_cylinder_ring(0.870, 0.482, 0.0);
3327        let ring_21: [Vec3; MESH_CYLINDER_TOWER_SEGMENTS] =
3328            Self::building_cylinder_ring(0.945, 0.456, 0.052);
3329        let shell_rings: [(&[Vec3], u16); 22] = [
3330            (&ring_00, slot_roof),
3331            (&ring_01, slot_facade),
3332            (&ring_02, slot_stone),
3333            (&ring_03, slot_stone),
3334            (&ring_04, slot_stone),
3335            (&ring_05, slot_facade),
3336            (&ring_06, slot_metal),
3337            (&ring_07, slot_metal),
3338            (&ring_08, slot_metal),
3339            (&ring_09, slot_facade),
3340            (&ring_10, slot_metal),
3341            (&ring_11, slot_metal),
3342            (&ring_12, slot_metal),
3343            (&ring_13, slot_facade),
3344            (&ring_14, slot_metal),
3345            (&ring_15, slot_metal),
3346            (&ring_16, slot_metal),
3347            (&ring_17, slot_facade),
3348            (&ring_18, slot_glow),
3349            (&ring_19, slot_glow),
3350            (&ring_20, slot_glow),
3351            (&ring_21, slot_metal),
3352        ];
3353        Self::push_stepped_polygon_shell(
3354            &mut vertices,
3355            &mut indices,
3356            &mut tri_materials,
3357            &shell_rings,
3358            (slot_roof, slot_roof),
3359        );
3360        let column_step: f32 = (2.0 * PI) / 8.0;
3361        for column_idx in 0..8usize {
3362            let column_angle: f32 = (column_idx as f32) * column_step;
3363            let (sin_col, cos_col): (f32, f32) = column_angle.sin_cos();
3364            Self::push_box_part(
3365                &mut vertices,
3366                &mut indices,
3367                &mut tri_materials,
3368                Vec3::new(cos_col * 0.440, 0.0755, -sin_col * 0.440),
3369                Vec3::new(0.060, 0.151, 0.060),
3370                slot_stone,
3371            );
3372        }
3373        let mut mesh: Self = Self::from_parts(name.to_string(), vertices, indices, material);
3374        mesh.set_sub_materials(sub_materials);
3375        mesh.set_tri_materials(tri_materials);
3376        mesh.set_is_closed(true);
3377        mesh
3378    }
3379
3380    /// Builds Archetype 6: a human-scale boutique neo-classical mid-rise boulevard podium block (`[-0.5, +0.5]` unit footprint, `y in [0, 1]`) with a recessed warm-lit ground-floor retail arcade colonnade, projecting stone cornices, pilasters, terraced penthouse setback, and rooftop warm accent lighting.
3381    ///
3382    /// # Arguments
3383    ///
3384    /// - `&str` - Mesh asset name.
3385    /// - `PbrMaterial` - Primary architectural facade material (`slot 0`).
3386    ///
3387    /// # Returns
3388    ///
3389    /// - `Self` - Closed multi-material unit boutique boulevard podium mesh.
3390    #[must_use]
3391    pub fn building_boutique_podium_block(name: &str, material: PbrMaterial) -> Self {
3392        let sub_materials: Vec<PbrMaterial> = Self::building_sub_materials(
3393            Color::hex(0xA6957C),
3394            Color::hex(0x9B7B4A),
3395            Color::hex(0xFDE68A),
3396        );
3397        let slot_facade: u16 = 0;
3398        let slot_stone: u16 = 1;
3399        let slot_roof: u16 = 2;
3400        let slot_metal: u16 = 3;
3401        let slot_glow: u16 = 4;
3402        let mut vertices: Vec<Vertex3D> = Vec::with_capacity(760);
3403        let mut indices: Vec<[u32; 3]> = Vec::with_capacity(360);
3404        let mut tri_materials: Vec<u16> = Vec::with_capacity(360);
3405        let ring_00: [Vec3; 4] = Self::building_rect_ring(0.0, 0.000, 0.0, 0.380, 0.380);
3406        let ring_01: [Vec3; 4] = Self::building_rect_ring(0.0, 0.220, 0.0, 0.380, 0.380);
3407        let ring_02: [Vec3; 4] = Self::building_rect_ring(0.0, 0.220, 0.0, 0.396, 0.396);
3408        let ring_03: [Vec3; 4] = Self::building_rect_ring(0.0, 0.256, 0.0, 0.396, 0.396);
3409        let ring_04: [Vec3; 4] = Self::building_rect_ring(0.0, 0.256, 0.0, 0.380, 0.380);
3410        let ring_05: [Vec3; 4] = Self::building_rect_ring(0.0, 0.280, 0.0, 0.380, 0.380);
3411        let ring_06: [Vec3; 4] = Self::building_rect_ring(0.0, 0.280, 0.0, 0.500, 0.500);
3412        let ring_07: [Vec3; 4] = Self::building_rect_ring(0.0, 0.330, 0.0, 0.500, 0.500);
3413        let ring_08: [Vec3; 4] = Self::building_rect_ring(0.0, 0.330, 0.0, 0.455, 0.455);
3414        let ring_09: [Vec3; 4] = Self::building_rect_ring(0.0, 0.750, 0.0, 0.455, 0.455);
3415        let ring_10: [Vec3; 4] = Self::building_rect_ring(0.0, 0.750, 0.0, 0.500, 0.500);
3416        let ring_11: [Vec3; 4] = Self::building_rect_ring(0.0, 0.800, 0.0, 0.500, 0.500);
3417        let ring_12: [Vec3; 4] = Self::building_rect_ring(0.0, 0.800, 0.0, 0.330, 0.330);
3418        let ring_13: [Vec3; 4] = Self::building_rect_ring(0.0, 0.932, 0.0, 0.330, 0.330);
3419        let ring_14: [Vec3; 4] = Self::building_rect_ring(0.0, 0.932, 0.0, 0.352, 0.352);
3420        let ring_15: [Vec3; 4] = Self::building_rect_ring(0.0, 0.954, 0.0, 0.352, 0.352);
3421        let ring_16: [Vec3; 4] = Self::building_rect_ring(0.0, 0.954, 0.0, 0.320, 0.320);
3422        let ring_17: [Vec3; 4] = Self::building_rect_ring(0.0, 0.982, 0.0, 0.320, 0.320);
3423        let shell_rings: [(&[Vec3], u16); 18] = [
3424            (&ring_00, slot_roof),
3425            (&ring_01, slot_facade),
3426            (&ring_02, slot_glow),
3427            (&ring_03, slot_glow),
3428            (&ring_04, slot_glow),
3429            (&ring_05, slot_facade),
3430            (&ring_06, slot_stone),
3431            (&ring_07, slot_stone),
3432            (&ring_08, slot_stone),
3433            (&ring_09, slot_facade),
3434            (&ring_10, slot_stone),
3435            (&ring_11, slot_stone),
3436            (&ring_12, slot_stone),
3437            (&ring_13, slot_facade),
3438            (&ring_14, slot_roof),
3439            (&ring_15, slot_roof),
3440            (&ring_16, slot_roof),
3441            (&ring_17, slot_glow),
3442        ];
3443        Self::push_stepped_polygon_shell(
3444            &mut vertices,
3445            &mut indices,
3446            &mut tri_materials,
3447            &shell_rings,
3448            (slot_roof, slot_roof),
3449        );
3450        let pier_coords: [f32; 3] = [-0.448, 0.0, 0.448];
3451        for &pier_x in pier_coords.iter() {
3452            for &pier_z in pier_coords.iter() {
3453                if pier_x.abs() < 0.01 && pier_z.abs() < 0.01 {
3454                    continue;
3455                }
3456                Self::push_box_part(
3457                    &mut vertices,
3458                    &mut indices,
3459                    &mut tri_materials,
3460                    Vec3::new(pier_x, 0.148, pier_z),
3461                    Vec3::new(0.072, 0.296, 0.072),
3462                    slot_stone,
3463                );
3464            }
3465        }
3466        for sign_x in [-1.0_f32, 1.0_f32] {
3467            for sign_z in [-1.0_f32, 1.0_f32] {
3468                Self::push_box_part(
3469                    &mut vertices,
3470                    &mut indices,
3471                    &mut tri_materials,
3472                    Vec3::new(sign_x * 0.454, 0.5445, sign_z * 0.454),
3473                    Vec3::new(0.064, 0.461, 0.064),
3474                    slot_stone,
3475                );
3476            }
3477        }
3478        for rail_x in [-0.280_f32, 0.280_f32] {
3479            Self::push_box_part(
3480                &mut vertices,
3481                &mut indices,
3482                &mut tri_materials,
3483                Vec3::new(rail_x, 0.984, 0.0),
3484                Vec3::new(0.036, 0.032, 0.600),
3485                slot_metal,
3486            );
3487        }
3488        let mut mesh: Self = Self::from_parts(name.to_string(), vertices, indices, material);
3489        mesh.set_sub_materials(sub_materials);
3490        mesh.set_tri_materials(tri_materials);
3491        mesh.set_is_closed(true);
3492        mesh
3493    }
3494
3495    /// Builds an instanced Vice Boulevard coastal palm tree with a gracefully leaning segmented trunk and seven arching multi-segment tropical fronds.
3496    ///
3497    /// # Arguments
3498    ///
3499    /// - `&str` - Asset name.
3500    ///
3501    /// # Returns
3502    ///
3503    /// - `Self` - Multi-material palm tree mesh (`slot 0` warm bark trunk, `slot 1` emerald tropical fronds).
3504    #[must_use]
3505    pub fn palm_tree(name: &str) -> Self {
3506        let bark_mat: PbrMaterial = PbrMaterial::dielectric(Color::hex(0x4E392B), 0.88);
3507        let frond_top_mat: PbrMaterial = PbrMaterial::pbr(Color::hex(0x227848), 0.05, 0.56, 0.08);
3508        let frond_under_mat: PbrMaterial = PbrMaterial::pbr(Color::hex(0x134A2D), 0.04, 0.68, 0.05);
3509        let sub_materials: Vec<PbrMaterial> = vec![frond_top_mat, frond_under_mat];
3510        let mut vertices: Vec<Vertex3D> = Vec::with_capacity(520);
3511        let mut indices: Vec<[u32; 3]> = Vec::with_capacity(260);
3512        let mut tri_materials: Vec<u16> = Vec::with_capacity(260);
3513        let ring_specs: [(f32, f32, f32, f32); 5] = [
3514            (0.0, 0.0, 0.0, 0.30),
3515            (1.9, 0.14, 0.05, 0.25),
3516            (3.9, 0.32, 0.08, 0.21),
3517            (5.8, 0.44, 0.06, 0.17),
3518            (7.5, 0.48, 0.02, 0.14),
3519        ];
3520        let trunk_step: f32 = (2.0 * PI) / (MESH_PALM_TRUNK_SEGMENTS as f32);
3521        for seg_idx in 0..4usize {
3522            let (y0, ox0, oz0, r0): (f32, f32, f32, f32) = ring_specs[seg_idx];
3523            let (y1, ox1, oz1, r1): (f32, f32, f32, f32) = ring_specs[seg_idx + 1];
3524            for side_idx in 0..MESH_PALM_TRUNK_SEGMENTS {
3525                let a0: f32 = (side_idx as f32) * trunk_step;
3526                let a1: f32 = ((side_idx + 1) as f32) * trunk_step;
3527                let (s0, c0): (f32, f32) = a0.sin_cos();
3528                let (s1, c1): (f32, f32) = a1.sin_cos();
3529                let p0: Vec3 = Vec3::new(ox0 + c0 * r0, y0, oz0 + s0 * r0);
3530                let p1: Vec3 = Vec3::new(ox0 + c1 * r0, y0, oz0 + s1 * r0);
3531                let p2: Vec3 = Vec3::new(ox1 + c1 * r1, y1, oz1 + s1 * r1);
3532                let p3: Vec3 = Vec3::new(ox1 + c0 * r1, y1, oz1 + s0 * r1);
3533                Self::push_flat_quad(
3534                    &mut vertices,
3535                    &mut indices,
3536                    &mut tri_materials,
3537                    [p0, p1, p2, p3],
3538                    0,
3539                );
3540            }
3541        }
3542        let crown: Vec3 = Vec3::new(0.48, 7.45, 0.02);
3543        let frond_step: f32 = (2.0 * PI) / (MESH_PALM_FROND_COUNT as f32);
3544        for tier_idx in 0..2usize {
3545            let tier_count: usize = if tier_idx == 0 {
3546                MESH_PALM_FROND_COUNT
3547            } else {
3548                5
3549            };
3550            let tier_step: f32 = if tier_idx == 0 {
3551                frond_step
3552            } else {
3553                (2.0 * PI) / 5.0
3554            };
3555            let tier_yaw_off: f32 = if tier_idx == 0 { 0.25 } else { 0.62 };
3556            let tier_origin: Vec3 = crown.add(Vec3::new(0.0, (tier_idx as f32) * 0.22, 0.0));
3557            for frond_idx in 0..tier_count {
3558                let yaw: f32 = (frond_idx as f32) * tier_step + tier_yaw_off;
3559                let (sy, cy): (f32, f32) = yaw.sin_cos();
3560                let dir: Vec3 = Vec3::new(cy, 0.0, sy);
3561                let right: Vec3 = Vec3::new(-sy, 0.0, cy);
3562                let reach: f32 = if tier_idx == 0 {
3563                    if frond_idx % 2 == 0 { 2.85 } else { 2.45 }
3564                } else {
3565                    1.85
3566                };
3567                let lift: f32 = if tier_idx == 0 { 0.48 } else { 0.92 };
3568                let drop_tip: f32 = if tier_idx == 0 { -1.18 } else { -0.18 };
3569                let p_mid: Vec3 = tier_origin
3570                    .add(dir.scale(reach * 0.44))
3571                    .add(Vec3::new(0.0, lift, 0.0));
3572                let p_arch: Vec3 = tier_origin.add(dir.scale(reach * 0.78)).add(Vec3::new(
3573                    0.0,
3574                    lift * 0.15 - 0.12,
3575                    0.0,
3576                ));
3577                let p_tip: Vec3 = tier_origin
3578                    .add(dir.scale(reach))
3579                    .add(Vec3::new(0.0, drop_tip, 0.0));
3580                let w_mid: Vec3 = right.scale(if tier_idx == 0 { 0.34 } else { 0.26 });
3581                let w_arch: Vec3 = right.scale(if tier_idx == 0 { 0.24 } else { 0.18 });
3582                let sag_mid: Vec3 = Vec3::new(0.0, -0.16, 0.0);
3583                let sag_arch: Vec3 = Vec3::new(0.0, -0.20, 0.0);
3584                let mid_l: Vec3 = p_mid.sub(w_mid).add(sag_mid);
3585                let mid_r: Vec3 = p_mid.add(w_mid).add(sag_mid);
3586                let arch_l: Vec3 = p_arch.sub(w_arch).add(sag_arch);
3587                let arch_r: Vec3 = p_arch.add(w_arch).add(sag_arch);
3588                Self::push_flat_tri(
3589                    &mut vertices,
3590                    &mut indices,
3591                    &mut tri_materials,
3592                    (tier_origin, p_mid, mid_l),
3593                    1,
3594                );
3595                Self::push_flat_tri(
3596                    &mut vertices,
3597                    &mut indices,
3598                    &mut tri_materials,
3599                    (tier_origin, mid_r, p_mid),
3600                    1,
3601                );
3602                Self::push_flat_tri(
3603                    &mut vertices,
3604                    &mut indices,
3605                    &mut tri_materials,
3606                    (tier_origin, mid_l, p_mid),
3607                    2,
3608                );
3609                Self::push_flat_tri(
3610                    &mut vertices,
3611                    &mut indices,
3612                    &mut tri_materials,
3613                    (tier_origin, p_mid, mid_r),
3614                    2,
3615                );
3616                Self::push_flat_quad(
3617                    &mut vertices,
3618                    &mut indices,
3619                    &mut tri_materials,
3620                    [mid_l, p_mid, p_arch, arch_l],
3621                    1,
3622                );
3623                Self::push_flat_quad(
3624                    &mut vertices,
3625                    &mut indices,
3626                    &mut tri_materials,
3627                    [p_mid, mid_r, arch_r, p_arch],
3628                    1,
3629                );
3630                Self::push_flat_quad(
3631                    &mut vertices,
3632                    &mut indices,
3633                    &mut tri_materials,
3634                    [arch_l, p_arch, p_mid, mid_l],
3635                    2,
3636                );
3637                Self::push_flat_quad(
3638                    &mut vertices,
3639                    &mut indices,
3640                    &mut tri_materials,
3641                    [p_arch, arch_r, mid_r, p_mid],
3642                    2,
3643                );
3644                Self::push_flat_tri(
3645                    &mut vertices,
3646                    &mut indices,
3647                    &mut tri_materials,
3648                    (arch_l, p_arch, p_tip),
3649                    1,
3650                );
3651                Self::push_flat_tri(
3652                    &mut vertices,
3653                    &mut indices,
3654                    &mut tri_materials,
3655                    (p_arch, arch_r, p_tip),
3656                    1,
3657                );
3658                Self::push_flat_tri(
3659                    &mut vertices,
3660                    &mut indices,
3661                    &mut tri_materials,
3662                    (p_arch, arch_l, p_tip),
3663                    2,
3664                );
3665                Self::push_flat_tri(
3666                    &mut vertices,
3667                    &mut indices,
3668                    &mut tri_materials,
3669                    (arch_r, p_arch, p_tip),
3670                    2,
3671                );
3672            }
3673        }
3674        let mut mesh: Self = Self::from_parts(name.to_string(), vertices, indices, bark_mat);
3675        mesh.set_sub_materials(sub_materials);
3676        mesh.set_tri_materials(tri_materials);
3677        mesh.set_is_closed(false);
3678        mesh
3679    }
3680
3681    /// Builds a box mesh from a fluent `BoxMeshDesc`.
3682    ///
3683    /// # Arguments
3684    ///
3685    /// - `&BoxMeshDesc` - Box mesh descriptor.
3686    ///
3687    /// # Returns
3688    ///
3689    /// - `Self` - Indexed 12-triangle box mesh.
3690    #[must_use]
3691    pub fn from_box_desc(desc: &BoxMeshDesc) -> Self {
3692        let size: Vec3 = desc.get_size();
3693        Self::box_mesh(
3694            desc.get_name(),
3695            size.get_x(),
3696            size.get_y(),
3697            size.get_z(),
3698            desc.get_ground_pivot(),
3699            desc.get_material(),
3700        )
3701    }
3702
3703    /// Builds a subdivided ground plane from a fluent `GroundPlaneDesc`.
3704    ///
3705    /// # Arguments
3706    ///
3707    /// - `&GroundPlaneDesc` - Ground plane descriptor.
3708    ///
3709    /// # Returns
3710    ///
3711    /// - `Self` - Subdivided ground plane mesh.
3712    #[must_use]
3713    pub fn from_ground_desc(desc: &GroundPlaneDesc) -> Self {
3714        Self::ground_plane(
3715            desc.get_name(),
3716            desc.get_width(),
3717            desc.get_depth(),
3718            desc.get_subdivisions(),
3719            desc.get_material(),
3720        )
3721    }
3722
3723    /// Builds a simplified proxy mesh enclosing this mesh's axis-aligned bounding box for distant LOD tiers.
3724    ///
3725    /// # Arguments
3726    ///
3727    /// - `&Self` - Source mesh reference.
3728    /// - `&str` - LOD suffix label.
3729    ///
3730    /// # Returns
3731    ///
3732    /// - `Self` - Lightweight 12-triangle LOD proxy mesh.
3733    #[must_use]
3734    pub fn build_lod_proxy(&self, suffix: &str) -> Self {
3735        let blue_val: AxisAlignedBox3D = self.get_bounds();
3736        let size: Vec3 = blue_val.get_max().sub(blue_val.get_min());
3737        let center: Vec3 = blue_val.center();
3738        let mut proxy: Self = Self::box_mesh(
3739            &format!("{}_{suffix}", self.get_name()),
3740            size.get_x().max(0.2),
3741            size.get_y().max(0.2),
3742            size.get_z().max(0.2),
3743            false,
3744            self.get_material(),
3745        );
3746        for item_val in proxy.get_mut_vertices().iter_mut() {
3747            item_val.set_position(item_val.get_position().add(center));
3748        }
3749        proxy.set_bounds(blue_val);
3750        let meshlets: Vec<Meshlet3D> =
3751            Self::build_meshlets_from_parts(proxy.get_vertices(), proxy.get_indices());
3752        proxy.set_meshlets(meshlets);
3753        proxy
3754    }
3755
3756    /// Produces a mid-detail decimated mesh by retaining dominant triangles while preserving bounding silhouette.
3757    ///
3758    /// # Arguments
3759    ///
3760    /// - `&Self` - Source mesh reference.
3761    /// - `&str` - LOD suffix label.
3762    ///
3763    /// # Returns
3764    ///
3765    /// - `Self` - Decimated intermediate LOD mesh.
3766    #[must_use]
3767    pub fn build_mid_lod(&self, suffix: &str) -> Self {
3768        let verts: &[Vertex3D] = self.get_vertices().as_slice();
3769        let tris: &[[u32; 3]] = self.get_indices().as_slice();
3770        if tris.len() <= 24 {
3771            return self.build_lod_proxy(suffix);
3772        }
3773        let mut scored: Vec<(f32, [u32; 3], u16)> = Vec::with_capacity(tris.len());
3774        let default_mat: PbrMaterial = self.get_material();
3775        for (tri_idx, tri) in tris.iter().copied().enumerate() {
3776            let (i0, i1, i2): (usize, usize, usize) =
3777                (tri[0] as usize, tri[1] as usize, tri[2] as usize);
3778            if i0 < verts.len() && i1 < verts.len() && i2 < verts.len() {
3779                let p0: Vec3 = verts[i0].get_position();
3780                let p1: Vec3 = verts[i1].get_position();
3781                let p2: Vec3 = verts[i2].get_position();
3782                let area2: f32 = p1.sub(p0).cross(p2.sub(p0)).length_sq();
3783                let (resolved_mat, slot): (PbrMaterial, u16) =
3784                    self.resolve_triangle_material(tri_idx, default_mat);
3785                let emissive_boost: f32 = if resolved_mat.get_emissive().length_sq() > 0.25 {
3786                    8.0
3787                } else {
3788                    1.0
3789                };
3790                scored.push((area2 * emissive_boost, tri, slot));
3791            }
3792        }
3793        scored.sort_unstable_by(
3794            |alpha_val: &(f32, [u32; 3], u16), blue_val: &(f32, [u32; 3], u16)| {
3795                blue_val.0.total_cmp(&alpha_val.0)
3796            },
3797        );
3798        let keep: usize = (scored.len() / 2).max(12);
3799        let mut reduced: Vec<[u32; 3]> = Vec::with_capacity(keep);
3800        let mut reduced_slots: Vec<u16> = if self.get_tri_materials().is_empty() {
3801            Vec::new()
3802        } else {
3803            Vec::with_capacity(keep)
3804        };
3805        for (_, tri, slot) in scored.into_iter().take(keep) {
3806            reduced.push(tri);
3807            if !self.get_tri_materials().is_empty() {
3808                reduced_slots.push(slot);
3809            }
3810        }
3811        let mut mid_mesh: Self = Self::from_parts(
3812            format!("{}_{suffix}", self.get_name()),
3813            self.get_vertices().clone(),
3814            reduced,
3815            self.get_material(),
3816        );
3817        if !reduced_slots.is_empty() {
3818            mid_mesh.set_sub_materials(self.get_sub_materials().clone());
3819            mid_mesh.set_tri_materials(reduced_slots);
3820        }
3821        mid_mesh.set_is_closed(self.get_is_closed());
3822        mid_mesh
3823    }
3824}
3825
3826/// Multi-level continuous LOD chain creation and screen-error selection for `MeshLodChain`.
3827impl MeshLodChain {
3828    /// Builds a continuous 3-tier Nanite-style LOD chain (`LOD0` full -> `LOD1` mid -> `LOD2` proxy) automatically.
3829    ///
3830    /// # Arguments
3831    ///
3832    /// - `Mesh3D` - Full-resolution `LOD0` mesh.
3833    ///
3834    /// # Returns
3835    ///
3836    /// - `Self` - Ready LOD chain.
3837    #[must_use]
3838    pub fn from_mesh(mesh: Mesh3D) -> Self {
3839        let tri_count: usize = mesh.get_indices().len();
3840        if tri_count > 32 {
3841            let lod1: Mesh3D = mesh.build_mid_lod(MESH_LOD1_SUFFIX);
3842            let lod2: Mesh3D = mesh.build_lod_proxy(MESH_LOD2_SUFFIX);
3843            Self {
3844                levels: vec![mesh, lod1, lod2],
3845            }
3846        } else if tri_count > 16 {
3847            let lod1: Mesh3D = mesh.build_lod_proxy(MESH_LOD1_SUFFIX);
3848            Self {
3849                levels: vec![mesh, lod1],
3850            }
3851        } else {
3852            let clone_lod1: Mesh3D = mesh.clone();
3853            Self {
3854                levels: vec![mesh, clone_lod1],
3855            }
3856        }
3857    }
3858
3859    /// Wraps a sculpted hero or foliage mesh in a single-level LOD chain so it never degrades into an opaque bounding-box proxy.
3860    ///
3861    /// # Arguments
3862    ///
3863    /// - `Mesh3D` - Full-resolution `LOD0` mesh.
3864    ///
3865    /// # Returns
3866    ///
3867    /// - `Self` - Single-tier LOD chain preserving exact silhouette at all distances.
3868    #[must_use]
3869    pub fn single_level(mesh: Mesh3D) -> Self {
3870        Self { levels: vec![mesh] }
3871    }
3872
3873    /// Selects the appropriate `Mesh3D` detail level for `distance` and `lod_bias`.
3874    ///
3875    /// # Arguments
3876    ///
3877    /// - `&Self` - LOD chain reference.
3878    /// - `f32` - Camera-to-object distance in meters.
3879    /// - `f32` - Quality LOD bias multiplier.
3880    ///
3881    /// # Returns
3882    ///
3883    /// - `(&Mesh3D, usize)` - Selected mesh reference and its LOD tier index.
3884    #[must_use]
3885    pub fn select(&self, distance: f32, lod_bias: f32) -> (&Mesh3D, usize) {
3886        let levels: &[Mesh3D] = self.get_levels().as_slice();
3887        let effective_dist: f32 = distance * lod_bias.max(0.25);
3888        if levels.len() > 2 && effective_dist > 135.0 {
3889            (&levels[2], 2)
3890        } else if levels.len() > 1 && effective_dist > 78.0 {
3891            (&levels[1], 1)
3892        } else {
3893            (&levels[0], 0)
3894        }
3895    }
3896
3897    /// Nanite-style continuous LOD selection driven by projected screen-space pixel coverage.
3898    ///
3899    /// # Arguments
3900    ///
3901    /// - `&Self` - LOD chain reference.
3902    /// - `f32` - Camera-to-object distance in meters.
3903    /// - `f32` - World-space bounding sphere radius in meters.
3904    /// - `f32` - Projected viewport focal length in pixels (`height / (2 * tan(fov/2))`).
3905    /// - `f32` - Quality LOD bias multiplier (`< 1.0` higher detail, `> 1.0` aggressive simplification).
3906    ///
3907    /// # Returns
3908    ///
3909    /// - `(&Mesh3D, usize)` - Selected mesh reference and its LOD tier index.
3910    #[must_use]
3911    pub fn select_screen_error(
3912        &self,
3913        distance: f32,
3914        bounds_radius: f32,
3915        focal_px: f32,
3916        lod_bias: f32,
3917    ) -> (&Mesh3D, usize) {
3918        let levels: &[Mesh3D] = self.get_levels().as_slice();
3919        let safe_dist: f32 = distance.max(0.5);
3920        let projected_px: f32 =
3921            (bounds_radius.max(0.2) * focal_px) / (safe_dist * lod_bias.max(0.25));
3922        if levels.len() > 2 && projected_px < 3.5 {
3923            (&levels[2], 2)
3924        } else if levels.len() > 1 && projected_px < 9.0 {
3925            (&levels[1], 1)
3926        } else {
3927            (&levels[0], 0)
3928        }
3929    }
3930}
3931
3932/// Native Wavefront `.obj`, Khronos glTF 2.0 `.gltf`/`.glb`, and baked binary model importers for `ModelImporter3D`.
3933impl ModelImporter3D {
3934    /// Imports a 3D model from text source in the specified `ModelFormat` and generates LOD chains.
3935    ///
3936    /// # Arguments
3937    ///
3938    /// - `ModelFormat` - Interchange format (`Obj` or `GltfJson`).
3939    /// - `&str` - Raw `.obj` or `.gltf` text content.
3940    /// - `PbrMaterial` - Fallback PBR material when source omits material definitions.
3941    ///
3942    /// # Returns
3943    ///
3944    /// - `Result<ImportedModel3D, String>` - Imported model bundle or parse error diagnostic.
3945    pub fn import(
3946        format: ModelFormat,
3947        source: &str,
3948        fallback_material: PbrMaterial,
3949    ) -> Result<ImportedModel3D, String> {
3950        match format {
3951            ModelFormat::Obj => Self::parse_obj(source, fallback_material),
3952            ModelFormat::GltfJson => Self::parse_gltf_json(source, fallback_material),
3953            ModelFormat::GlbBinary | ModelFormat::Euv3dBinary | ModelFormat::BlendInspect => {
3954                Err(ERR_TEXT_FORMAT_UNSUPPORTED.to_string())
3955            }
3956        }
3957    }
3958
3959    /// Imports a 3D model from raw binary or UTF-8 bytes in `Obj`, `GltfJson`, or `GlbBinary` format.
3960    ///
3961    /// # Arguments
3962    ///
3963    /// - `ModelFormat` - Source interchange format.
3964    /// - `&[u8]` - Raw file bytes.
3965    /// - `PbrMaterial` - Fallback PBR material when source omits material definitions.
3966    ///
3967    /// # Returns
3968    ///
3969    /// - `Result<ImportedModel3D, String>` - Imported model bundle or parse error.
3970    pub fn import_bytes(
3971        format: ModelFormat,
3972        bytes: &[u8],
3973        fallback_material: PbrMaterial,
3974    ) -> Result<ImportedModel3D, String> {
3975        match format {
3976            ModelFormat::Obj => {
3977                let text: &str = from_utf8(bytes)
3978                    .ok()
3979                    .ok_or_else(|| ERR_OBJ_EMPTY.to_string())?;
3980                Self::parse_obj(text, fallback_material)
3981            }
3982            ModelFormat::GltfJson => {
3983                let text: &str = from_utf8(bytes)
3984                    .ok()
3985                    .ok_or_else(|| ERR_GLTF_EMPTY.to_string())?;
3986                Self::parse_gltf_json(text, fallback_material)
3987            }
3988            ModelFormat::GlbBinary => {
3989                let scene: ImportedScene3D = parse_glb_bytes(bytes)?;
3990                if scene.get_meshes().is_empty() {
3991                    return Err(ERR_GLTF_EMPTY.to_string());
3992                }
3993                let mut model: ImportedModel3D = scene.to_imported_model(ModelFormat::GlbBinary);
3994                if scene.get_materials().is_empty() {
3995                    for chain in model.get_mut_meshes().iter_mut() {
3996                        for lvl in chain.get_mut_levels().iter_mut() {
3997                            lvl.set_material(fallback_material);
3998                        }
3999                    }
4000                }
4001                Ok(model)
4002            }
4003            ModelFormat::Euv3dBinary => {
4004                let bundle: BakedAssetBundle3D = Euv3dPackage::decode_bytes(bytes)?;
4005                if bundle.get_lod_chains().is_empty() {
4006                    return Err(ERR_GLTF_EMPTY.to_string());
4007                }
4008                let total_vertices: usize = bundle
4009                    .get_lod_chains()
4010                    .iter()
4011                    .filter_map(|c: &MeshLodChain| c.get_levels().first())
4012                    .map(|m: &Mesh3D| m.get_vertices().len())
4013                    .sum();
4014                let total_triangles: usize = bundle
4015                    .get_lod_chains()
4016                    .iter()
4017                    .filter_map(|c: &MeshLodChain| c.get_levels().first())
4018                    .map(|m: &Mesh3D| m.get_indices().len())
4019                    .sum();
4020                Ok(ImportedModel3D {
4021                    name: bundle.get_scene().get_name().clone(),
4022                    format: ModelFormat::Euv3dBinary,
4023                    meshes: bundle.get_lod_chains().clone(),
4024                    total_vertices,
4025                    total_triangles,
4026                })
4027            }
4028            ModelFormat::BlendInspect => Err(ERR_BINARY_FORMAT_UNSUPPORTED.to_string()),
4029        }
4030    }
4031
4032    /// Resolves a signed 1-based Wavefront OBJ index (positive or negative relative to `len`) into a 0-based index.
4033    ///
4034    /// # Arguments
4035    ///
4036    /// - `i32` - Signed 1-based OBJ index.
4037    /// - `usize` - Current length of the corresponding attribute array.
4038    ///
4039    /// # Returns
4040    ///
4041    /// - `Option<usize>` - Resolved 0-based index if in bounds.
4042    #[must_use]
4043    fn resolve_obj_index(raw: i32, len: usize) -> Option<usize> {
4044        if raw > 0 {
4045            let idx: usize = (raw - 1) as usize;
4046            if idx < len { Some(idx) } else { None }
4047        } else if raw < 0 {
4048            let idx: isize = (len as isize) + (raw as isize);
4049            if idx >= 0 && (idx as usize) < len {
4050                Some(idx as usize)
4051            } else {
4052                None
4053            }
4054        } else {
4055            None
4056        }
4057    }
4058
4059    /// Parses a Wavefront `.mtl` material library into a named `PbrMaterial` map (`Kd`, `Ke`, `Ns`, `Pm`, `Pr`).
4060    ///
4061    /// # Arguments
4062    ///
4063    /// - `&str` - Raw `.mtl` ASCII text.
4064    /// - `PbrMaterial` - Base fallback material template.
4065    ///
4066    /// # Returns
4067    ///
4068    /// - `HashMap<String, PbrMaterial>` - Parsed material table keyed by `newmtl` identifier.
4069    #[must_use]
4070    pub fn parse_mtl(mtl_source: &str, fallback: PbrMaterial) -> HashMap<String, PbrMaterial> {
4071        let mut table: HashMap<String, PbrMaterial> = HashMap::new();
4072        let mut active_name: Option<String> = None;
4073        let mut active_mat: PbrMaterial = fallback;
4074        for raw_line in mtl_source.lines() {
4075            let line: &str = raw_line.trim();
4076            if line.is_empty() || line.starts_with('#') {
4077                continue;
4078            }
4079            let parts: Vec<&str> = line.split_whitespace().collect();
4080            if parts.is_empty() {
4081                continue;
4082            }
4083            if parts[0] == MTL_DIRECTIVE_NEWMTL && parts.len() >= 2 {
4084                if let Some(prev_name) = active_name.take() {
4085                    let _: Option<PbrMaterial> = table.insert(prev_name, active_mat);
4086                }
4087                active_name = Some(parts[1].to_string());
4088                active_mat = fallback;
4089            } else if parts[0] == "Kd" && parts.len() >= 4 {
4090                let r: f32 = parts[1].parse::<f32>().unwrap_or(0.8).clamp(0.0, 1.0);
4091                let g: f32 = parts[2].parse::<f32>().unwrap_or(0.8).clamp(0.0, 1.0);
4092                let b: f32 = parts[3].parse::<f32>().unwrap_or(0.8).clamp(0.0, 1.0);
4093                active_mat.set_base_color(Vec3::new(r, g, b));
4094            } else if parts[0] == "Ke" && parts.len() >= 4 {
4095                let r: f32 = parts[1].parse::<f32>().unwrap_or(0.0).max(0.0);
4096                let g: f32 = parts[2].parse::<f32>().unwrap_or(0.0).max(0.0);
4097                let b: f32 = parts[3].parse::<f32>().unwrap_or(0.0).max(0.0);
4098                active_mat.set_emissive(Vec3::new(r, g, b));
4099            } else if parts[0] == "Pm" && parts.len() >= 2 {
4100                let metal: f32 = parts[1].parse::<f32>().unwrap_or(0.0).clamp(0.0, 1.0);
4101                active_mat.set_metallic(metal);
4102            } else if parts[0] == "Pr" && parts.len() >= 2 {
4103                let rough: f32 = parts[1].parse::<f32>().unwrap_or(0.5).clamp(0.04, 1.0);
4104                active_mat.set_roughness(rough);
4105                active_mat.set_reflectivity((1.0 - rough).clamp(0.05, 0.95));
4106            } else if parts[0] == "Ns" && parts.len() >= 2 {
4107                let ns: f32 = parts[1].parse::<f32>().unwrap_or(64.0).clamp(0.0, 1000.0);
4108                let rough: f32 = (1.0 - (ns / 1000.0).sqrt()).clamp(0.04, 1.0);
4109                active_mat.set_roughness(rough);
4110                active_mat.set_reflectivity((1.0 - rough).clamp(0.05, 0.95));
4111            }
4112        }
4113        if let Some(last_name) = active_name {
4114            let _: Option<PbrMaterial> = table.insert(last_name, active_mat);
4115        }
4116        table
4117    }
4118
4119    /// Parses a Wavefront `.obj` mesh (`o`, `g`, `v`, `vt`, `vn`, `f`, `mtllib`, `usemtl`) with optional `.mtl` library, negative index support, vertex deduplication, and Lengyel tangents.
4120    ///
4121    /// # Arguments
4122    ///
4123    /// - `&str` - Raw `.obj` ASCII text.
4124    /// - `Option<&str>` - Optional companion `.mtl` ASCII text.
4125    /// - `PbrMaterial` - Fallback PBR material.
4126    ///
4127    /// # Returns
4128    ///
4129    /// - `Result<ImportedModel3D, String>` - Parsed model or error string.
4130    pub fn parse_obj_with_mtl(
4131        source: &str,
4132        mtl_source: Option<&str>,
4133        material: PbrMaterial,
4134    ) -> Result<ImportedModel3D, String> {
4135        let mtl_table: HashMap<String, PbrMaterial> = mtl_source
4136            .map(|text: &str| Self::parse_mtl(text, material))
4137            .unwrap_or_default();
4138        let mut positions: Vec<Vec3> = Vec::new();
4139        let mut uvs: Vec<[f32; 2]> = Vec::new();
4140        let mut normals: Vec<Vec3> = Vec::new();
4141        let mut vertices: Vec<Vertex3D> = Vec::new();
4142        let mut indices: Vec<[u32; 3]> = Vec::new();
4143        let mut dedup: HashMap<(usize, Option<usize>, Option<usize>), u32> = HashMap::new();
4144        let mut sub_materials: Vec<PbrMaterial> = Vec::new();
4145        let mut sub_mat_slots: HashMap<String, u16> = HashMap::new();
4146        let mut tri_materials: Vec<u16> = Vec::new();
4147        let mut active_slot: u16 = 0;
4148        let mut model_name: String = MESH_OBJ_DEFAULT_NAME.to_string();
4149        let mut saw_any_uv: bool = false;
4150        let mut saw_any_normal: bool = false;
4151        for raw_line in source.lines() {
4152            let line: &str = raw_line.trim();
4153            if line.is_empty() || line.starts_with('#') {
4154                continue;
4155            }
4156            let parts: Vec<&str> = line.split_whitespace().collect();
4157            if parts.is_empty() {
4158                continue;
4159            }
4160            match parts[0] {
4161                "o" | "g" if parts.len() >= 2 => {
4162                    model_name = parts[1].to_string();
4163                }
4164                token if token == OBJ_DIRECTIVE_MTLLIB => {}
4165                token if token == OBJ_DIRECTIVE_USEMTL && parts.len() >= 2 => {
4166                    let mat_name: &str = parts[1];
4167                    if let Some(&slot) = sub_mat_slots.get(mat_name) {
4168                        active_slot = slot;
4169                    } else if let Some(&parsed_mat) = mtl_table.get(mat_name) {
4170                        sub_materials.push(parsed_mat);
4171                        let new_slot: u16 = sub_materials.len() as u16;
4172                        let _: Option<u16> = sub_mat_slots.insert(mat_name.to_string(), new_slot);
4173                        active_slot = new_slot;
4174                    } else {
4175                        active_slot = 0;
4176                    }
4177                }
4178                "v" if parts.len() >= 4 => {
4179                    let pos_x: f32 = parts[1].parse::<f32>().unwrap_or(0.0);
4180                    let pos_y: f32 = parts[2].parse::<f32>().unwrap_or(0.0);
4181                    let pos_z: f32 = parts[3].parse::<f32>().unwrap_or(0.0);
4182                    positions.push(Vec3::new(pos_x, pos_y, pos_z));
4183                }
4184                "vt" if parts.len() >= 3 => {
4185                    let coord_u: f32 = parts[1].parse::<f32>().unwrap_or(0.0);
4186                    let coord_v: f32 = parts[2].parse::<f32>().unwrap_or(0.0);
4187                    uvs.push([coord_u, coord_v]);
4188                    saw_any_uv = true;
4189                }
4190                "vn" if parts.len() >= 4 => {
4191                    let coord_x: f32 = parts[1].parse::<f32>().unwrap_or(0.0);
4192                    let coord_y: f32 = parts[2].parse::<f32>().unwrap_or(1.0);
4193                    let coord_z: f32 = parts[3].parse::<f32>().unwrap_or(0.0);
4194                    normals.push(Vec3::new(coord_x, coord_y, coord_z).normalize());
4195                    saw_any_normal = true;
4196                }
4197                "f" if parts.len() >= 4 => {
4198                    let mut face_refs: Vec<(usize, Option<usize>, Option<usize>)> =
4199                        Vec::with_capacity(parts.len() - 1);
4200                    for token in parts.iter().skip(1) {
4201                        let comps: Vec<&str> = token.split('/').collect();
4202                        let v_raw: i32 = comps
4203                            .first()
4204                            .and_then(|s: &&str| s.parse::<i32>().ok())
4205                            .unwrap_or(0);
4206                        let Some(v_idx) = Self::resolve_obj_index(v_raw, positions.len()) else {
4207                            continue;
4208                        };
4209                        let vt_idx: Option<usize> = comps
4210                            .get(1)
4211                            .and_then(|s: &&str| s.parse::<i32>().ok())
4212                            .and_then(|raw: i32| Self::resolve_obj_index(raw, uvs.len()));
4213                        let vn_idx: Option<usize> = comps
4214                            .get(2)
4215                            .and_then(|s: &&str| s.parse::<i32>().ok())
4216                            .and_then(|raw: i32| Self::resolve_obj_index(raw, normals.len()));
4217                        face_refs.push((v_idx, vt_idx, vn_idx));
4218                    }
4219                    if face_refs.len() >= 3 {
4220                        for entry_index in 1..(face_refs.len() - 1) {
4221                            let tri_keys: [(usize, Option<usize>, Option<usize>); 3] = [
4222                                face_refs[0],
4223                                face_refs[entry_index],
4224                                face_refs[entry_index + 1],
4225                            ];
4226                            let p0: Vec3 = positions[tri_keys[0].0];
4227                            let p1: Vec3 = positions[tri_keys[1].0];
4228                            let p2: Vec3 = positions[tri_keys[2].0];
4229                            let face_n: Vec3 = p1.sub(p0).cross(p2.sub(p0)).normalize();
4230                            let mut out_tri: [u32; 3] = [0u32; 3];
4231                            for lane in 0..3usize {
4232                                let key: (usize, Option<usize>, Option<usize>) = tri_keys[lane];
4233                                let vert_id: u32 = if key.2.is_some() {
4234                                    if let Some(&existing) = dedup.get(&key) {
4235                                        existing
4236                                    } else {
4237                                        let id: u32 = vertices.len() as u32;
4238                                        let mut v: Vertex3D = Vertex3D::new(
4239                                            positions[key.0],
4240                                            key.2
4241                                                .and_then(|ni: usize| normals.get(ni).copied())
4242                                                .unwrap_or(face_n),
4243                                        );
4244                                        if let Some(ti) = key.1
4245                                            && let Some(&uv_pair) = uvs.get(ti)
4246                                        {
4247                                            v.set_uv0(uv_pair);
4248                                            v.set_flags(1);
4249                                        }
4250                                        vertices.push(v);
4251                                        let _: Option<u32> = dedup.insert(key, id);
4252                                        id
4253                                    }
4254                                } else {
4255                                    let id: u32 = vertices.len() as u32;
4256                                    let mut v: Vertex3D = Vertex3D::new(positions[key.0], face_n);
4257                                    if let Some(ti) = key.1
4258                                        && let Some(&uv_pair) = uvs.get(ti)
4259                                    {
4260                                        v.set_uv0(uv_pair);
4261                                        v.set_flags(1);
4262                                    }
4263                                    vertices.push(v);
4264                                    id
4265                                };
4266                                out_tri[lane] = vert_id;
4267                            }
4268                            indices.push(out_tri);
4269                            if !sub_materials.is_empty() {
4270                                tri_materials.push(active_slot);
4271                            }
4272                        }
4273                    }
4274                }
4275                _ => {}
4276            }
4277        }
4278        if indices.is_empty() {
4279            return Err(ERR_OBJ_EMPTY.to_string());
4280        }
4281        let mut mesh: Mesh3D = Mesh3D::from_parts(model_name.clone(), vertices, indices, material);
4282        if !sub_materials.is_empty() {
4283            if tri_materials.len() < mesh.get_indices().len() {
4284                tri_materials.resize(mesh.get_indices().len(), 0);
4285            }
4286            mesh.set_sub_materials(sub_materials);
4287            mesh.set_tri_materials(tri_materials);
4288        }
4289        if !saw_any_normal && dedup.len() < mesh.get_vertices().len() {
4290            mesh.compute_smooth_normals();
4291        }
4292        if saw_any_uv {
4293            mesh.compute_lengyel_tangents();
4294        }
4295        let total_vertices: usize = mesh.get_vertices().len();
4296        let total_triangles: usize = mesh.get_indices().len();
4297        Ok(ImportedModel3D {
4298            name: model_name,
4299            format: ModelFormat::Obj,
4300            meshes: vec![MeshLodChain::from_mesh(mesh)],
4301            total_vertices,
4302            total_triangles,
4303        })
4304    }
4305
4306    /// Parses a Wavefront `.obj` mesh (`o`, `g`, `v`, `vt`, `vn`, `f` with n-gon fan triangulation, negative indices, and auto-normals).
4307    ///
4308    /// # Arguments
4309    ///
4310    /// - `&str` - Raw `.obj` ASCII text.
4311    /// - `PbrMaterial` - Material assigned to the parsed mesh.
4312    ///
4313    /// # Returns
4314    ///
4315    /// - `Result<ImportedModel3D, String>` - Parsed model or error string.
4316    pub fn parse_obj(source: &str, material: PbrMaterial) -> Result<ImportedModel3D, String> {
4317        Self::parse_obj_with_mtl(source, None, material)
4318    }
4319
4320    /// Parses a Khronos glTF 2.0 JSON asset (full buffers/bufferViews/accessors/Principled BSDF plus legacy inline geometry fallback).
4321    ///
4322    /// # Arguments
4323    ///
4324    /// - `&str` - Raw `.gltf` JSON string.
4325    /// - `PbrMaterial` - Fallback PBR material when JSON omits `materials`.
4326    ///
4327    /// # Returns
4328    ///
4329    /// - `Result<ImportedModel3D, String>` - Parsed model bundle or validation error.
4330    pub fn parse_gltf_json(
4331        source: &str,
4332        fallback_material: PbrMaterial,
4333    ) -> Result<ImportedModel3D, String> {
4334        let scene: ImportedScene3D = parse_gltf_document(source, None, None)?;
4335        if scene.get_meshes().is_empty() {
4336            return Err(ERR_GLTF_EMPTY.to_string());
4337        }
4338        let mut model: ImportedModel3D = scene.to_imported_model(ModelFormat::GltfJson);
4339        if scene.get_materials().is_empty() {
4340            for chain in model.get_mut_meshes().iter_mut() {
4341                for lvl in chain.get_mut_levels().iter_mut() {
4342                    lvl.set_material(fallback_material);
4343                }
4344            }
4345        }
4346        Ok(model)
4347    }
4348}