1use super::*;
2
3impl Default for Vertex3D {
5 fn default() -> Self {
11 Self::from_pos_normal(Vec3::zero(), Vec3::new(0.0, 1.0, 0.0))
12 }
13}
14
15impl Vertex3D {
17 #[must_use]
28 pub const fn new(position: Vec3, normal: Vec3) -> Self {
29 Self::from_pos_normal(position, normal)
30 }
31
32 #[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 #[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
92impl Default for PbrMaterial {
94 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
111impl PbrMaterial {
113 #[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 #[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 #[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 #[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 #[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 #[must_use]
229 pub fn with_emissive(mut self, emissive: Vec3) -> Self {
230 self.set_emissive(emissive);
231 self
232 }
233
234 #[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 #[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 #[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 #[must_use]
293 pub fn with_surface_detail(mut self, mode: SurfaceDetailMode) -> Self {
294 self.set_surface_detail(mode);
295 self
296 }
297}
298
299impl BoxMeshDesc {
301 #[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 #[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 #[must_use]
348 pub fn with_material(mut self, material: PbrMaterial) -> Self {
349 self.set_material(material);
350 self
351 }
352}
353
354impl GroundPlaneDesc {
356 #[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 #[must_use]
389 pub fn with_subdivisions(mut self, subdivisions: usize) -> Self {
390 self.set_subdivisions(subdivisions);
391 self
392 }
393
394 #[must_use]
405 pub fn with_material(mut self, material: PbrMaterial) -> Self {
406 self.set_material(material);
407 self
408 }
409}
410
411impl Mesh3D {
413 #[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 #[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 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 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 #[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 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 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 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 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 fn push_stepped_polygon_shell(
842 vertices: &mut Vec<Vertex3D>,
843 indices: &mut Vec<[u32; 3]>,
844 tri_materials: &mut Vec<u16>,
845 profile_rings: &[(&[Vec3], u16)],
846 cap_slots: (u16, u16),
847 ) {
848 if profile_rings.len() < 2 {
849 return;
850 }
851 let mut ring_len: usize = usize::MAX;
852 for &(ring, _) in profile_rings.iter() {
853 ring_len = ring_len.min(ring.len());
854 }
855 if ring_len < 3 {
856 return;
857 }
858 for ring_idx in 1..profile_rings.len() {
859 let prev_ring: &[Vec3] = profile_rings[ring_idx - 1].0;
860 let (curr_ring, strip_slot): (&[Vec3], u16) = profile_rings[ring_idx];
861 for corner_idx in 0..ring_len {
862 let next_corner: usize = (corner_idx + 1) % ring_len;
863 Self::push_flat_quad(
864 vertices,
865 indices,
866 tri_materials,
867 [
868 prev_ring[corner_idx],
869 prev_ring[next_corner],
870 curr_ring[next_corner],
871 curr_ring[corner_idx],
872 ],
873 strip_slot,
874 );
875 }
876 }
877 let first_ring: &[Vec3] = profile_rings[0].0;
878 let last_ring: &[Vec3] = profile_rings[profile_rings.len() - 1].0;
879 let (bottom_cap_slot, top_cap_slot): (u16, u16) = cap_slots;
880 let inv_count: f32 = 1.0 / (ring_len as f32);
881 let mut bottom_center: Vec3 = Vec3::zero();
882 let mut top_center: Vec3 = Vec3::zero();
883 for corner_idx in 0..ring_len {
884 bottom_center = bottom_center.add(first_ring[corner_idx]);
885 top_center = top_center.add(last_ring[corner_idx]);
886 }
887 bottom_center = bottom_center.scale(inv_count);
888 top_center = top_center.scale(inv_count);
889 for corner_idx in 0..ring_len {
890 let next_corner: usize = (corner_idx + 1) % ring_len;
891 Self::push_flat_tri(
892 vertices,
893 indices,
894 tri_materials,
895 (top_center, last_ring[corner_idx], last_ring[next_corner]),
896 top_cap_slot,
897 );
898 Self::push_flat_tri(
899 vertices,
900 indices,
901 tri_materials,
902 (
903 bottom_center,
904 first_ring[next_corner],
905 first_ring[corner_idx],
906 ),
907 bottom_cap_slot,
908 );
909 }
910 }
911
912 #[must_use]
927 pub fn box_mesh(
928 name: &str,
929 width_val: f32,
930 height_val: f32,
931 delta_val: f32,
932 ground_pivot: bool,
933 material: PbrMaterial,
934 ) -> Self {
935 let hx: f32 = width_val * 0.5;
936 let hz: f32 = delta_val * 0.5;
937 let y0: f32 = if ground_pivot { 0.0 } else { -height_val * 0.5 };
938 let y1: f32 = if ground_pivot {
939 height_val
940 } else {
941 height_val * 0.5
942 };
943 let faces: [([Vec3; 4], Vec3); 6] = [
944 (
945 [
946 Vec3::new(-hx, y0, hz),
947 Vec3::new(hx, y0, hz),
948 Vec3::new(hx, y1, hz),
949 Vec3::new(-hx, y1, hz),
950 ],
951 Vec3::new(0.0, 0.0, 1.0),
952 ),
953 (
954 [
955 Vec3::new(hx, y0, -hz),
956 Vec3::new(-hx, y0, -hz),
957 Vec3::new(-hx, y1, -hz),
958 Vec3::new(hx, y1, -hz),
959 ],
960 Vec3::new(0.0, 0.0, -1.0),
961 ),
962 (
963 [
964 Vec3::new(hx, y0, hz),
965 Vec3::new(hx, y0, -hz),
966 Vec3::new(hx, y1, -hz),
967 Vec3::new(hx, y1, hz),
968 ],
969 Vec3::new(1.0, 0.0, 0.0),
970 ),
971 (
972 [
973 Vec3::new(-hx, y0, -hz),
974 Vec3::new(-hx, y0, hz),
975 Vec3::new(-hx, y1, hz),
976 Vec3::new(-hx, y1, -hz),
977 ],
978 Vec3::new(-1.0, 0.0, 0.0),
979 ),
980 (
981 [
982 Vec3::new(-hx, y1, hz),
983 Vec3::new(hx, y1, hz),
984 Vec3::new(hx, y1, -hz),
985 Vec3::new(-hx, y1, -hz),
986 ],
987 Vec3::new(0.0, 1.0, 0.0),
988 ),
989 (
990 [
991 Vec3::new(-hx, y0, -hz),
992 Vec3::new(hx, y0, -hz),
993 Vec3::new(hx, y0, hz),
994 Vec3::new(-hx, y0, hz),
995 ],
996 Vec3::new(0.0, -1.0, 0.0),
997 ),
998 ];
999 let mut vertices: Vec<Vertex3D> = Vec::with_capacity(24);
1000 let mut indices: Vec<[u32; 3]> = Vec::with_capacity(12);
1001 for (quad, item_count) in faces.into_iter() {
1002 let base: u32 = vertices.len() as u32;
1003 for point_val in quad.into_iter() {
1004 vertices.push(Vertex3D::new(point_val, item_count));
1005 }
1006 indices.push([base, base + 1, base + 2]);
1007 indices.push([base, base + 2, base + 3]);
1008 }
1009 let mut mesh: Self = Self::from_parts(name.to_string(), vertices, indices, material);
1010 mesh.set_is_closed(true);
1011 mesh
1012 }
1013
1014 #[must_use]
1028 pub fn ground_plane(
1029 name: &str,
1030 width_val: f32,
1031 delta_val: f32,
1032 subdivisions: usize,
1033 material: PbrMaterial,
1034 ) -> Self {
1035 let segs: usize = subdivisions.clamp(1, 16);
1036 let mut vertices: Vec<Vertex3D> = Vec::with_capacity((segs + 1) * (segs + 1));
1037 let mut indices: Vec<[u32; 3]> = Vec::with_capacity(segs * segs * 2);
1038 let up: Vec3 = Vec3::new(0.0, 1.0, 0.0);
1039 for iz in 0..=segs {
1040 let vz: f32 = ((iz as f32) / (segs as f32) - 0.5) * delta_val;
1041 for ix in 0..=segs {
1042 let vx: f32 = ((ix as f32) / (segs as f32) - 0.5) * width_val;
1043 vertices.push(Vertex3D::new(Vec3::new(vx, 0.0, vz), up));
1044 }
1045 }
1046 let stride: u32 = (segs + 1) as u32;
1047 for iz in 0..segs {
1048 for ix in 0..segs {
1049 let i0: u32 = (iz as u32) * stride + (ix as u32);
1050 let i1: u32 = i0 + 1;
1051 let i2: u32 = i0 + stride;
1052 let i3: u32 = i2 + 1;
1053 indices.push([i0, i2, i1]);
1054 indices.push([i1, i2, i3]);
1055 }
1056 }
1057 Self::from_parts(name.to_string(), vertices, indices, material)
1058 }
1059
1060 #[must_use]
1075 pub fn coastal_ocean_ring(
1076 name: &str,
1077 outer_span: f32,
1078 inner_half_x: f32,
1079 inner_half_z: f32,
1080 subdivisions: usize,
1081 material: PbrMaterial,
1082 ) -> Self {
1083 let seg_count: usize = subdivisions.clamp(8, 32);
1084 let cut_half_x: f32 = (inner_half_x - 2.0).max(0.0);
1085 let cut_half_z: f32 = (inner_half_z - 2.0).max(0.0);
1086 let mut vertices: Vec<Vertex3D> = Vec::with_capacity((seg_count + 1) * (seg_count + 1));
1087 let mut indices: Vec<[u32; 3]> = Vec::with_capacity(seg_count * seg_count * 2);
1088 let up_normal: Vec3 = Vec3::new(0.0, 1.0, 0.0);
1089 let inv_segs: f32 = 1.0 / (seg_count as f32);
1090 for grid_z in 0..=seg_count {
1091 let pos_z: f32 = ((grid_z as f32) * inv_segs - 0.5) * outer_span;
1092 for grid_x in 0..=seg_count {
1093 let pos_x: f32 = ((grid_x as f32) * inv_segs - 0.5) * outer_span;
1094 vertices.push(Vertex3D::new(Vec3::new(pos_x, 0.0, pos_z), up_normal));
1095 }
1096 }
1097 let row_stride: u32 = (seg_count + 1) as u32;
1098 let mut block_z: usize = 0;
1099 while block_z < seg_count {
1100 let end_z: usize = (block_z + 4).min(seg_count);
1101 let mut block_x: usize = 0;
1102 while block_x < seg_count {
1103 let end_x: usize = (block_x + 4).min(seg_count);
1104 for grid_z in block_z..end_z {
1105 let quad_z0: f32 = ((grid_z as f32) * inv_segs - 0.5) * outer_span;
1106 let quad_z1: f32 = (((grid_z + 1) as f32) * inv_segs - 0.5) * outer_span;
1107 for grid_x in block_x..end_x {
1108 let quad_x0: f32 = ((grid_x as f32) * inv_segs - 0.5) * outer_span;
1109 let quad_x1: f32 = (((grid_x + 1) as f32) * inv_segs - 0.5) * outer_span;
1110 let inside_x: bool = quad_x0 >= -cut_half_x && quad_x1 <= cut_half_x;
1111 let inside_z: bool = quad_z0 >= -cut_half_z && quad_z1 <= cut_half_z;
1112 if inside_x && inside_z {
1113 continue;
1114 }
1115 let idx0: u32 = (grid_z as u32) * row_stride + (grid_x as u32);
1116 let idx1: u32 = idx0 + 1;
1117 let idx2: u32 = idx0 + row_stride;
1118 let idx3: u32 = idx2 + 1;
1119 indices.push([idx0, idx2, idx1]);
1120 indices.push([idx1, idx2, idx3]);
1121 }
1122 }
1123 block_x += 4;
1124 }
1125 block_z += 4;
1126 }
1127 Self::from_parts(name.to_string(), vertices, indices, material)
1128 }
1129
1130 #[must_use]
1141 pub fn sports_car(name: &str, material: PbrMaterial) -> Self {
1142 Self::aventador_svj(name, material)
1143 }
1144
1145 #[must_use]
1156 pub fn aventador_svj(name: &str, material: PbrMaterial) -> Self {
1157 build_lamborghini_supercar(name, material)
1158 }
1159
1160 #[must_use]
1171 pub fn lamborghini_revuelto(name: &str, material: PbrMaterial) -> Self {
1172 build_lamborghini_supercar(name, material)
1173 }
1174
1175 #[must_use]
1186 pub fn shelby_cobra(name: &str, material: PbrMaterial) -> Self {
1187 build_mustang_s650_supercar(name, material)
1188 }
1189
1190 #[must_use]
1201 pub fn mustang_s650(name: &str, material: PbrMaterial) -> Self {
1202 build_mustang_s650_supercar(name, material)
1203 }
1204
1205 #[must_use]
1216 pub fn npc_sedan(name: &str, material: PbrMaterial) -> Self {
1217 build_npc_sedan(name, material)
1218 }
1219
1220 #[must_use]
1231 pub fn npc_coupe(name: &str, material: PbrMaterial) -> Self {
1232 build_npc_coupe(name, material)
1233 }
1234
1235 #[must_use]
1246 pub fn npc_suv(name: &str, material: PbrMaterial) -> Self {
1247 build_npc_suv(name, material)
1248 }
1249
1250 fn building_chamfered_ring(
1263 y_val: f32,
1264 half_extent: f32,
1265 chamfer_inner: f32,
1266 twist_rad: f32,
1267 ) -> [Vec3; 8] {
1268 let raw: [(f32, f32); 8] = [
1269 (half_extent, chamfer_inner),
1270 (half_extent, -chamfer_inner),
1271 (chamfer_inner, -half_extent),
1272 (-chamfer_inner, -half_extent),
1273 (-half_extent, -chamfer_inner),
1274 (-half_extent, chamfer_inner),
1275 (-chamfer_inner, half_extent),
1276 (chamfer_inner, half_extent),
1277 ];
1278 let (sin_t, cos_t): (f32, f32) = twist_rad.sin_cos();
1279 let mut ring: [Vec3; 8] = [Vec3::zero(); 8];
1280 for (idx, &(rx, rz)) in raw.iter().enumerate() {
1281 let rot_x: f32 = rx * cos_t + rz * sin_t;
1282 let rot_z: f32 = -rx * sin_t + rz * cos_t;
1283 ring[idx] = Vec3::new(rot_x, y_val, rot_z);
1284 }
1285 ring
1286 }
1287
1288 fn building_cylinder_ring(
1300 y_base: f32,
1301 radius: f32,
1302 slope_x: f32,
1303 ) -> [Vec3; MESH_CYLINDER_TOWER_SEGMENTS] {
1304 let mut ring: [Vec3; MESH_CYLINDER_TOWER_SEGMENTS] =
1305 [Vec3::zero(); MESH_CYLINDER_TOWER_SEGMENTS];
1306 let step_rad: f32 = (2.0 * PI) / (MESH_CYLINDER_TOWER_SEGMENTS as f32);
1307 for (idx, dst) in ring.iter_mut().enumerate() {
1308 let angle: f32 = (idx as f32) * step_rad;
1309 let (sin_a, cos_a): (f32, f32) = angle.sin_cos();
1310 let px: f32 = cos_a * radius;
1311 let pz: f32 = -sin_a * radius;
1312 let py: f32 = y_base + cos_a * slope_x;
1313 *dst = Vec3::new(px, py, pz);
1314 }
1315 ring
1316 }
1317
1318 fn building_sub_materials(
1330 trim_color: Color,
1331 metal_color: Color,
1332 glow_color: Color,
1333 ) -> Vec<PbrMaterial> {
1334 vec![
1335 PbrMaterial::pbr(trim_color, 0.22, 0.42, 0.32),
1336 PbrMaterial::pbr(Color::hex(0x161D29), 0.36, 0.56, 0.24),
1337 PbrMaterial::metallic(metal_color, 0.18),
1338 PbrMaterial::neon(glow_color, 1.15),
1339 ]
1340 }
1341
1342 fn building_rect_ring(
1356 center_x: f32,
1357 y_val: f32,
1358 center_z: f32,
1359 half_x: f32,
1360 half_z: f32,
1361 ) -> [Vec3; 4] {
1362 [
1363 Vec3::new(center_x + half_x, y_val, center_z + half_z),
1364 Vec3::new(center_x + half_x, y_val, center_z - half_z),
1365 Vec3::new(center_x - half_x, y_val, center_z - half_z),
1366 Vec3::new(center_x - half_x, y_val, center_z + half_z),
1367 ]
1368 }
1369
1370 fn building_rib_ring(
1383 anchor: Vec3,
1384 radial_offset: f32,
1385 half_radial: f32,
1386 half_tangent: f32,
1387 ) -> [Vec3; 4] {
1388 let radial_horiz: Vec3 = Vec3::new(anchor.get_x(), 0.0, anchor.get_z());
1389 let radial_dir: Vec3 = if radial_horiz.length_sq() > 1.0e-8 {
1390 radial_horiz.normalize()
1391 } else {
1392 Vec3::new(1.0, 0.0, 0.0)
1393 };
1394 let tangent_dir: Vec3 = Vec3::new(radial_dir.get_z(), 0.0, -radial_dir.get_x());
1395 let center: Vec3 = anchor.add(radial_dir.scale(radial_offset));
1396 let radial_vec: Vec3 = radial_dir.scale(half_radial);
1397 let tangent_vec: Vec3 = tangent_dir.scale(half_tangent);
1398 [
1399 center.add(radial_vec).sub(tangent_vec),
1400 center.add(radial_vec).add(tangent_vec),
1401 center.sub(radial_vec).add(tangent_vec),
1402 center.sub(radial_vec).sub(tangent_vec),
1403 ]
1404 }
1405
1406 #[must_use]
1417 pub fn building_art_deco_tower(name: &str, material: PbrMaterial) -> Self {
1418 let sub_materials: Vec<PbrMaterial> = Self::building_sub_materials(
1419 Color::hex(0x9A8970),
1420 Color::hex(0xB89456),
1421 Color::hex(0xFFCB6B),
1422 );
1423 let slot_facade: u16 = 0;
1424 let slot_stone: u16 = 1;
1425 let slot_roof: u16 = 2;
1426 let slot_metal: u16 = 3;
1427 let slot_glow: u16 = 4;
1428 let mut vertices: Vec<Vertex3D> = Vec::with_capacity(640);
1429 let mut indices: Vec<[u32; 3]> = Vec::with_capacity(300);
1430 let mut tri_materials: Vec<u16> = Vec::with_capacity(300);
1431 let ring_base_bot: [Vec3; 4] = Self::building_rect_ring(0.0, 0.000, 0.0, 0.480, 0.480);
1432 let ring_base_top: [Vec3; 4] = Self::building_rect_ring(0.0, 0.160, 0.0, 0.480, 0.480);
1433 let ring_pod_bot: [Vec3; 4] = Self::building_rect_ring(0.0, 0.160, 0.0, 0.500, 0.500);
1434 let ring_pod_top: [Vec3; 4] = Self::building_rect_ring(0.0, 0.188, 0.0, 0.500, 0.500);
1435 let ring_mid_bot: [Vec3; 4] = Self::building_rect_ring(0.0, 0.188, 0.0, 0.425, 0.425);
1436 let ring_mid_top: [Vec3; 4] = Self::building_rect_ring(0.0, 0.550, 0.0, 0.425, 0.425);
1437 let ring_mid_ledge_bot: [Vec3; 4] = Self::building_rect_ring(0.0, 0.550, 0.0, 0.472, 0.472);
1438 let ring_mid_ledge_top: [Vec3; 4] = Self::building_rect_ring(0.0, 0.578, 0.0, 0.472, 0.472);
1439 let ring_upper_bot: [Vec3; 4] = Self::building_rect_ring(0.0, 0.578, 0.0, 0.335, 0.335);
1440 let ring_upper_top: [Vec3; 4] = Self::building_rect_ring(0.0, 0.750, 0.0, 0.335, 0.335);
1441 let ring_upper_ledge_bot: [Vec3; 4] =
1442 Self::building_rect_ring(0.0, 0.750, 0.0, 0.382, 0.382);
1443 let ring_upper_ledge_top: [Vec3; 4] =
1444 Self::building_rect_ring(0.0, 0.778, 0.0, 0.382, 0.382);
1445 let ring_crown_bot: [Vec3; 4] = Self::building_rect_ring(0.0, 0.778, 0.0, 0.235, 0.235);
1446 let ring_crown_top: [Vec3; 4] = Self::building_rect_ring(0.0, 0.854, 0.0, 0.235, 0.235);
1447 let ring_metal_bot: [Vec3; 4] = Self::building_rect_ring(0.0, 0.854, 0.0, 0.255, 0.255);
1448 let ring_metal_top: [Vec3; 4] = Self::building_rect_ring(0.0, 0.870, 0.0, 0.255, 0.255);
1449 let ring_glow_bot: [Vec3; 4] = Self::building_rect_ring(0.0, 0.870, 0.0, 0.160, 0.160);
1450 let ring_glow_top: [Vec3; 4] = Self::building_rect_ring(0.0, 0.916, 0.0, 0.160, 0.160);
1451 let ring_roof_bot: [Vec3; 4] = Self::building_rect_ring(0.0, 0.916, 0.0, 0.176, 0.176);
1452 let ring_roof_top: [Vec3; 4] = Self::building_rect_ring(0.0, 0.932, 0.0, 0.176, 0.176);
1453 let ring_spire_bot: [Vec3; 4] = Self::building_rect_ring(0.0, 0.932, 0.0, 0.072, 0.072);
1454 let ring_spire_top: [Vec3; 4] = Self::building_rect_ring(0.0, 0.984, 0.0, 0.016, 0.016);
1455 let ring_beacon_bot: [Vec3; 4] = Self::building_rect_ring(0.0, 0.984, 0.0, 0.022, 0.022);
1456 let ring_beacon_top: [Vec3; 4] = Self::building_rect_ring(0.0, 1.000, 0.0, 0.018, 0.018);
1457 let shell_rings: [(&[Vec3], u16); 24] = [
1458 (&ring_base_bot, slot_roof),
1459 (&ring_base_top, slot_facade),
1460 (&ring_pod_bot, slot_stone),
1461 (&ring_pod_top, slot_stone),
1462 (&ring_mid_bot, slot_stone),
1463 (&ring_mid_top, slot_facade),
1464 (&ring_mid_ledge_bot, slot_stone),
1465 (&ring_mid_ledge_top, slot_stone),
1466 (&ring_upper_bot, slot_stone),
1467 (&ring_upper_top, slot_facade),
1468 (&ring_upper_ledge_bot, slot_stone),
1469 (&ring_upper_ledge_top, slot_stone),
1470 (&ring_crown_bot, slot_stone),
1471 (&ring_crown_top, slot_facade),
1472 (&ring_metal_bot, slot_metal),
1473 (&ring_metal_top, slot_metal),
1474 (&ring_glow_bot, slot_metal),
1475 (&ring_glow_top, slot_glow),
1476 (&ring_roof_bot, slot_roof),
1477 (&ring_roof_top, slot_roof),
1478 (&ring_spire_bot, slot_roof),
1479 (&ring_spire_top, slot_metal),
1480 (&ring_beacon_bot, slot_glow),
1481 (&ring_beacon_top, slot_glow),
1482 ];
1483 Self::push_stepped_polygon_shell(
1484 &mut vertices,
1485 &mut indices,
1486 &mut tri_materials,
1487 &shell_rings,
1488 (slot_roof, slot_glow),
1489 );
1490 for sign_x in [-1.0_f32, 1.0_f32] {
1491 for sign_z in [-1.0_f32, 1.0_f32] {
1492 Self::push_box_part(
1493 &mut vertices,
1494 &mut indices,
1495 &mut tri_materials,
1496 Vec3::new(sign_x * 0.426, 0.369, sign_z * 0.426),
1497 Vec3::new(0.060, 0.390, 0.060),
1498 slot_metal,
1499 );
1500 Self::push_box_part(
1501 &mut vertices,
1502 &mut indices,
1503 &mut tri_materials,
1504 Vec3::new(sign_x * 0.336, 0.664, sign_z * 0.336),
1505 Vec3::new(0.056, 0.200, 0.056),
1506 slot_metal,
1507 );
1508 }
1509 }
1510 let mut mesh: Self = Self::from_parts(name.to_string(), vertices, indices, material);
1511 mesh.set_sub_materials(sub_materials);
1512 mesh.set_tri_materials(tri_materials);
1513 mesh.set_is_closed(true);
1514 mesh
1515 }
1516
1517 #[must_use]
1528 pub fn building_chamfered_glass_tower(name: &str, material: PbrMaterial) -> Self {
1529 let sub_materials: Vec<PbrMaterial> = Self::building_sub_materials(
1530 Color::hex(0x64748B),
1531 Color::hex(0x94A3B8),
1532 Color::hex(0x7DD3FC),
1533 );
1534 let slot_facade: u16 = 0;
1535 let slot_slab: u16 = 1;
1536 let slot_roof: u16 = 2;
1537 let slot_metal: u16 = 3;
1538 let slot_glow: u16 = 4;
1539 let mut vertices: Vec<Vertex3D> = Vec::with_capacity(960);
1540 let mut indices: Vec<[u32; 3]> = Vec::with_capacity(460);
1541 let mut tri_materials: Vec<u16> = Vec::with_capacity(460);
1542 let ring_00: [Vec3; 8] = Self::building_chamfered_ring(0.000, 0.480, 0.315, 0.0);
1543 let ring_01: [Vec3; 8] = Self::building_chamfered_ring(0.140, 0.480, 0.315, 0.0);
1544 let ring_02: [Vec3; 8] = Self::building_chamfered_ring(0.140, 0.500, 0.335, 0.0);
1545 let ring_03: [Vec3; 8] = Self::building_chamfered_ring(0.164, 0.500, 0.335, 0.0);
1546 let ring_04: [Vec3; 8] = Self::building_chamfered_ring(0.164, 0.480, 0.315, 0.0);
1547 let ring_05: [Vec3; 8] = Self::building_chamfered_ring(0.330, 0.480, 0.315, 0.0);
1548 let ring_06: [Vec3; 8] = Self::building_chamfered_ring(0.330, 0.500, 0.335, 0.0);
1549 let ring_07: [Vec3; 8] = Self::building_chamfered_ring(0.354, 0.500, 0.335, 0.0);
1550 let ring_08: [Vec3; 8] = Self::building_chamfered_ring(0.354, 0.480, 0.315, 0.0);
1551 let ring_09: [Vec3; 8] = Self::building_chamfered_ring(0.508, 0.480, 0.315, 0.0);
1552 let ring_10: [Vec3; 8] = Self::building_chamfered_ring(0.508, 0.500, 0.335, 0.0);
1553 let ring_11: [Vec3; 8] = Self::building_chamfered_ring(0.536, 0.500, 0.335, 0.0);
1554 let ring_12: [Vec3; 8] = Self::building_chamfered_ring(0.536, 0.380, 0.250, 0.0);
1555 let ring_13: [Vec3; 8] = Self::building_chamfered_ring(0.614, 0.380, 0.250, 0.0);
1556 let ring_14: [Vec3; 8] = Self::building_chamfered_ring(0.614, 0.495, 0.330, 0.0);
1557 let ring_15: [Vec3; 8] = Self::building_chamfered_ring(0.642, 0.495, 0.330, 0.0);
1558 let ring_16: [Vec3; 8] = Self::building_chamfered_ring(0.642, 0.475, 0.310, 0.0);
1559 let ring_17: [Vec3; 8] = Self::building_chamfered_ring(0.760, 0.475, 0.310, 0.0);
1560 let ring_18: [Vec3; 8] = Self::building_chamfered_ring(0.760, 0.495, 0.330, 0.0);
1561 let ring_19: [Vec3; 8] = Self::building_chamfered_ring(0.784, 0.495, 0.330, 0.0);
1562 let ring_20: [Vec3; 8] = Self::building_chamfered_ring(0.784, 0.475, 0.310, 0.0);
1563 let ring_21: [Vec3; 8] = Self::building_chamfered_ring(0.884, 0.475, 0.310, 0.0);
1564 let ring_22: [Vec3; 8] = Self::building_chamfered_ring(0.884, 0.492, 0.324, 0.0);
1565 let ring_23: [Vec3; 8] = Self::building_chamfered_ring(0.912, 0.492, 0.324, 0.0);
1566 let mut ring_24: [Vec3; 8] = Self::building_chamfered_ring(0.962, 0.475, 0.310, 0.0);
1567 for corner in ring_24.iter_mut() {
1568 let slope_y: f32 = corner.get_y() + corner.get_x() * 0.075;
1569 corner.set_y(slope_y.min(1.0));
1570 }
1571 let shell_rings: [(&[Vec3], u16); 25] = [
1572 (&ring_00, slot_roof),
1573 (&ring_01, slot_facade),
1574 (&ring_02, slot_slab),
1575 (&ring_03, slot_slab),
1576 (&ring_04, slot_slab),
1577 (&ring_05, slot_facade),
1578 (&ring_06, slot_slab),
1579 (&ring_07, slot_slab),
1580 (&ring_08, slot_slab),
1581 (&ring_09, slot_facade),
1582 (&ring_10, slot_slab),
1583 (&ring_11, slot_slab),
1584 (&ring_12, slot_slab),
1585 (&ring_13, slot_facade),
1586 (&ring_14, slot_glow),
1587 (&ring_15, slot_slab),
1588 (&ring_16, slot_slab),
1589 (&ring_17, slot_facade),
1590 (&ring_18, slot_slab),
1591 (&ring_19, slot_slab),
1592 (&ring_20, slot_slab),
1593 (&ring_21, slot_facade),
1594 (&ring_22, slot_glow),
1595 (&ring_23, slot_glow),
1596 (&ring_24, slot_metal),
1597 ];
1598 Self::push_stepped_polygon_shell(
1599 &mut vertices,
1600 &mut indices,
1601 &mut tri_materials,
1602 &shell_rings,
1603 (slot_roof, slot_roof),
1604 );
1605 for sign_x in [-1.0_f32, 1.0_f32] {
1606 for sign_z in [-1.0_f32, 1.0_f32] {
1607 Self::push_box_part(
1608 &mut vertices,
1609 &mut indices,
1610 &mut tri_materials,
1611 Vec3::new(sign_x * 0.365, 0.575, sign_z * 0.365),
1612 Vec3::new(0.058, 0.106, 0.058),
1613 slot_metal,
1614 );
1615 }
1616 }
1617 let mut mesh: Self = Self::from_parts(name.to_string(), vertices, indices, material);
1618 mesh.set_sub_materials(sub_materials);
1619 mesh.set_tri_materials(tri_materials);
1620 mesh.set_is_closed(true);
1621 mesh
1622 }
1623
1624 #[must_use]
1635 pub fn building_twisted_spire_tower(name: &str, material: PbrMaterial) -> Self {
1636 let sub_materials: Vec<PbrMaterial> = Self::building_sub_materials(
1637 Color::hex(0x334155),
1638 Color::hex(0xD8E2EE),
1639 Color::hex(0x38BDF8),
1640 );
1641 let slot_facade: u16 = 0;
1642 let slot_base: u16 = 1;
1643 let slot_roof: u16 = 2;
1644 let slot_metal: u16 = 3;
1645 let slot_glow: u16 = 4;
1646 let mut vertices: Vec<Vertex3D> = Vec::with_capacity(820);
1647 let mut indices: Vec<[u32; 3]> = Vec::with_capacity(380);
1648 let mut tri_materials: Vec<u16> = Vec::with_capacity(380);
1649 let base_lo: [Vec3; 8] = Self::building_chamfered_ring(0.000, 0.500, 0.340, 0.00);
1650 let base_hi: [Vec3; 8] = Self::building_chamfered_ring(0.120, 0.490, 0.330, 0.00);
1651 let tier_0: [Vec3; 8] = Self::building_chamfered_ring(0.120, 0.450, 0.300, 0.00);
1652 let tier_1: [Vec3; 8] = Self::building_chamfered_ring(0.320, 0.410, 0.270, 0.16);
1653 let tier_2: [Vec3; 8] = Self::building_chamfered_ring(0.520, 0.360, 0.240, 0.32);
1654 let tier_3: [Vec3; 8] = Self::building_chamfered_ring(0.700, 0.310, 0.200, 0.48);
1655 let tier_4: [Vec3; 8] = Self::building_chamfered_ring(0.836, 0.260, 0.170, 0.62);
1656 let glow_lo: [Vec3; 8] = Self::building_chamfered_ring(0.836, 0.278, 0.184, 0.62);
1657 let glow_hi: [Vec3; 8] = Self::building_chamfered_ring(0.864, 0.278, 0.184, 0.62);
1658 let cap_hi: [Vec3; 8] = Self::building_chamfered_ring(0.892, 0.256, 0.168, 0.62);
1659 let shell_rings: [(&[Vec3], u16); 10] = [
1660 (&base_lo, slot_roof),
1661 (&base_hi, slot_base),
1662 (&tier_0, slot_roof),
1663 (&tier_1, slot_facade),
1664 (&tier_2, slot_facade),
1665 (&tier_3, slot_facade),
1666 (&tier_4, slot_facade),
1667 (&glow_lo, slot_glow),
1668 (&glow_hi, slot_glow),
1669 (&cap_hi, slot_base),
1670 ];
1671 Self::push_stepped_polygon_shell(
1672 &mut vertices,
1673 &mut indices,
1674 &mut tri_materials,
1675 &shell_rings,
1676 (slot_roof, slot_roof),
1677 );
1678 let rib_anchor_bot: [Vec3; 8] = Self::building_chamfered_ring(0.105, 0.453, 0.302, 0.00);
1679 let rib_anchor_top: [Vec3; 8] = Self::building_chamfered_ring(0.850, 0.258, 0.169, 0.62);
1680 for rib_idx in [0usize, 2usize, 4usize, 6usize] {
1681 let rib_r0: [Vec3; 4] =
1682 Self::building_rib_ring(rib_anchor_bot[rib_idx], 0.006, 0.018, 0.018);
1683 let rib_r1: [Vec3; 4] = Self::building_rib_ring(tier_1[rib_idx], 0.006, 0.018, 0.018);
1684 let rib_r2: [Vec3; 4] = Self::building_rib_ring(tier_2[rib_idx], 0.006, 0.018, 0.018);
1685 let rib_r3: [Vec3; 4] = Self::building_rib_ring(tier_3[rib_idx], 0.006, 0.018, 0.018);
1686 let rib_r4: [Vec3; 4] =
1687 Self::building_rib_ring(rib_anchor_top[rib_idx], 0.006, 0.018, 0.018);
1688 let rib_rings: [(&[Vec3], u16); 5] = [
1689 (&rib_r0, slot_metal),
1690 (&rib_r1, slot_metal),
1691 (&rib_r2, slot_metal),
1692 (&rib_r3, slot_metal),
1693 (&rib_r4, slot_metal),
1694 ];
1695 Self::push_stepped_polygon_shell(
1696 &mut vertices,
1697 &mut indices,
1698 &mut tri_materials,
1699 &rib_rings,
1700 (slot_metal, slot_metal),
1701 );
1702 }
1703 let fin_bot: [Vec3; 4] = [
1704 Vec3::new(-0.18, 0.878, 0.075),
1705 Vec3::new(0.20, 0.878, 0.075),
1706 Vec3::new(0.20, 0.878, -0.075),
1707 Vec3::new(-0.18, 0.878, -0.075),
1708 ];
1709 let fin_top: [Vec3; 4] = [
1710 Vec3::new(0.06, 1.000, 0.045),
1711 Vec3::new(0.20, 0.990, 0.045),
1712 Vec3::new(0.20, 0.990, -0.045),
1713 Vec3::new(0.06, 1.000, -0.045),
1714 ];
1715 Self::push_tapered_prism(
1716 &mut vertices,
1717 &mut indices,
1718 &mut tri_materials,
1719 fin_bot,
1720 fin_top,
1721 [slot_metal; 6],
1722 );
1723 let mut mesh: Self = Self::from_parts(name.to_string(), vertices, indices, material);
1724 mesh.set_sub_materials(sub_materials);
1725 mesh.set_tri_materials(tri_materials);
1726 mesh.set_is_closed(true);
1727 mesh
1728 }
1729
1730 #[must_use]
1741 pub fn building_cantilever_tech_tower(name: &str, material: PbrMaterial) -> Self {
1742 let sub_materials: Vec<PbrMaterial> = Self::building_sub_materials(
1743 Color::hex(0x475569),
1744 Color::hex(0xCBD5E1),
1745 Color::hex(0xFBBF24),
1746 );
1747 let slot_facade: u16 = 0;
1748 let slot_frame: u16 = 1;
1749 let slot_roof: u16 = 2;
1750 let slot_metal: u16 = 3;
1751 let slot_glow: u16 = 4;
1752 let mut vertices: Vec<Vertex3D> = Vec::with_capacity(680);
1753 let mut indices: Vec<[u32; 3]> = Vec::with_capacity(320);
1754 let mut tri_materials: Vec<u16> = Vec::with_capacity(320);
1755 let lower_r0: [Vec3; 4] = Self::building_rect_ring(0.0, 0.000, 0.0, 0.480, 0.480);
1756 let lower_r1: [Vec3; 4] = Self::building_rect_ring(0.0, 0.208, 0.0, 0.480, 0.480);
1757 let lower_r2: [Vec3; 4] = Self::building_rect_ring(0.0, 0.208, 0.0, 0.500, 0.500);
1758 let lower_r3: [Vec3; 4] = Self::building_rect_ring(0.0, 0.236, 0.0, 0.500, 0.500);
1759 let lower_r4: [Vec3; 4] = Self::building_rect_ring(0.0, 0.236, 0.0, 0.310, 0.310);
1760 let lower_r5: [Vec3; 4] = Self::building_rect_ring(0.0, 0.436, 0.0, 0.310, 0.310);
1761 let mid_r0: [Vec3; 4] = Self::building_rect_ring(-0.06, 0.436, 0.02, 0.400, 0.440);
1762 let mid_r1: [Vec3; 4] = Self::building_rect_ring(-0.06, 0.456, 0.02, 0.400, 0.440);
1763 let mid_r2: [Vec3; 4] = Self::building_rect_ring(-0.06, 0.456, 0.02, 0.415, 0.455);
1764 let mid_r3: [Vec3; 4] = Self::building_rect_ring(-0.06, 0.690, 0.02, 0.415, 0.455);
1765 let mid_r4: [Vec3; 4] = Self::building_rect_ring(-0.06, 0.690, 0.02, 0.430, 0.460);
1766 let mid_r5: [Vec3; 4] = Self::building_rect_ring(-0.06, 0.718, 0.02, 0.430, 0.460);
1767 let lower_mid_rings: [(&[Vec3], u16); 12] = [
1768 (&lower_r0, slot_roof),
1769 (&lower_r1, slot_facade),
1770 (&lower_r2, slot_frame),
1771 (&lower_r3, slot_frame),
1772 (&lower_r4, slot_frame),
1773 (&lower_r5, slot_facade),
1774 (&mid_r0, slot_glow),
1775 (&mid_r1, slot_glow),
1776 (&mid_r2, slot_facade),
1777 (&mid_r3, slot_facade),
1778 (&mid_r4, slot_frame),
1779 (&mid_r5, slot_frame),
1780 ];
1781 Self::push_stepped_polygon_shell(
1782 &mut vertices,
1783 &mut indices,
1784 &mut tri_materials,
1785 &lower_mid_rings,
1786 (slot_roof, slot_frame),
1787 );
1788 let upper_r0: [Vec3; 4] = Self::building_rect_ring(0.08, 0.704, -0.02, 0.380, 0.440);
1789 let upper_r1: [Vec3; 4] = Self::building_rect_ring(0.08, 0.732, -0.02, 0.380, 0.440);
1790 let upper_r2: [Vec3; 4] = Self::building_rect_ring(0.08, 0.732, -0.02, 0.400, 0.460);
1791 let upper_r3: [Vec3; 4] = Self::building_rect_ring(0.08, 0.922, -0.02, 0.400, 0.460);
1792 let upper_r4: [Vec3; 4] = Self::building_rect_ring(0.08, 0.922, -0.02, 0.420, 0.480);
1793 let upper_r5: [Vec3; 4] = Self::building_rect_ring(0.08, 0.950, -0.02, 0.420, 0.480);
1794 let upper_rings: [(&[Vec3], u16); 6] = [
1795 (&upper_r0, slot_glow),
1796 (&upper_r1, slot_glow),
1797 (&upper_r2, slot_facade),
1798 (&upper_r3, slot_facade),
1799 (&upper_r4, slot_roof),
1800 (&upper_r5, slot_roof),
1801 ];
1802 Self::push_stepped_polygon_shell(
1803 &mut vertices,
1804 &mut indices,
1805 &mut tri_materials,
1806 &upper_rings,
1807 (slot_glow, slot_roof),
1808 );
1809 for post_x in [-0.26_f32, 0.42_f32] {
1810 for post_z in [-0.40_f32, 0.36_f32] {
1811 Self::push_box_part(
1812 &mut vertices,
1813 &mut indices,
1814 &mut tri_materials,
1815 Vec3::new(post_x, 0.9615, post_z),
1816 Vec3::new(0.040, 0.051, 0.040),
1817 slot_metal,
1818 );
1819 }
1820 }
1821 for beam_idx in 0..5usize {
1822 let beam_z: f32 = -0.38 + (beam_idx as f32) * 0.19;
1823 Self::push_box_part(
1824 &mut vertices,
1825 &mut indices,
1826 &mut tri_materials,
1827 Vec3::new(0.08, 0.987, beam_z),
1828 Vec3::new(0.76, 0.026, 0.044),
1829 slot_metal,
1830 );
1831 }
1832 let mut mesh: Self = Self::from_parts(name.to_string(), vertices, indices, material);
1833 mesh.set_sub_materials(sub_materials);
1834 mesh.set_tri_materials(tri_materials);
1835 mesh.set_is_closed(true);
1836 mesh
1837 }
1838
1839 #[must_use]
1850 pub fn building_cylindrical_landmark(name: &str, material: PbrMaterial) -> Self {
1851 let sub_materials: Vec<PbrMaterial> = Self::building_sub_materials(
1852 Color::hex(0xCBD5E1),
1853 Color::hex(0xE2E8F0),
1854 Color::hex(0x34D399),
1855 );
1856 let slot_facade: u16 = 0;
1857 let slot_stone: u16 = 1;
1858 let slot_roof: u16 = 2;
1859 let slot_metal: u16 = 3;
1860 let slot_glow: u16 = 4;
1861 let mut vertices: Vec<Vertex3D> = Vec::with_capacity(1400);
1862 let mut indices: Vec<[u32; 3]> = Vec::with_capacity(680);
1863 let mut tri_materials: Vec<u16> = Vec::with_capacity(680);
1864 let ring_00: [Vec3; MESH_CYLINDER_TOWER_SEGMENTS] =
1865 Self::building_cylinder_ring(0.000, 0.380, 0.0);
1866 let ring_01: [Vec3; MESH_CYLINDER_TOWER_SEGMENTS] =
1867 Self::building_cylinder_ring(0.136, 0.380, 0.0);
1868 let ring_02: [Vec3; MESH_CYLINDER_TOWER_SEGMENTS] =
1869 Self::building_cylinder_ring(0.136, 0.500, 0.0);
1870 let ring_03: [Vec3; MESH_CYLINDER_TOWER_SEGMENTS] =
1871 Self::building_cylinder_ring(0.166, 0.500, 0.0);
1872 let ring_04: [Vec3; MESH_CYLINDER_TOWER_SEGMENTS] =
1873 Self::building_cylinder_ring(0.166, 0.478, 0.0);
1874 let ring_05: [Vec3; MESH_CYLINDER_TOWER_SEGMENTS] =
1875 Self::building_cylinder_ring(0.376, 0.478, 0.0);
1876 let ring_06: [Vec3; MESH_CYLINDER_TOWER_SEGMENTS] =
1877 Self::building_cylinder_ring(0.376, 0.500, 0.0);
1878 let ring_07: [Vec3; MESH_CYLINDER_TOWER_SEGMENTS] =
1879 Self::building_cylinder_ring(0.398, 0.500, 0.0);
1880 let ring_08: [Vec3; MESH_CYLINDER_TOWER_SEGMENTS] =
1881 Self::building_cylinder_ring(0.398, 0.478, 0.0);
1882 let ring_09: [Vec3; MESH_CYLINDER_TOWER_SEGMENTS] =
1883 Self::building_cylinder_ring(0.596, 0.478, 0.0);
1884 let ring_10: [Vec3; MESH_CYLINDER_TOWER_SEGMENTS] =
1885 Self::building_cylinder_ring(0.596, 0.500, 0.0);
1886 let ring_11: [Vec3; MESH_CYLINDER_TOWER_SEGMENTS] =
1887 Self::building_cylinder_ring(0.618, 0.500, 0.0);
1888 let ring_12: [Vec3; MESH_CYLINDER_TOWER_SEGMENTS] =
1889 Self::building_cylinder_ring(0.618, 0.478, 0.0);
1890 let ring_13: [Vec3; MESH_CYLINDER_TOWER_SEGMENTS] =
1891 Self::building_cylinder_ring(0.806, 0.478, 0.0);
1892 let ring_14: [Vec3; MESH_CYLINDER_TOWER_SEGMENTS] =
1893 Self::building_cylinder_ring(0.806, 0.500, 0.0);
1894 let ring_15: [Vec3; MESH_CYLINDER_TOWER_SEGMENTS] =
1895 Self::building_cylinder_ring(0.828, 0.500, 0.0);
1896 let ring_16: [Vec3; MESH_CYLINDER_TOWER_SEGMENTS] =
1897 Self::building_cylinder_ring(0.828, 0.478, 0.0);
1898 let ring_17: [Vec3; MESH_CYLINDER_TOWER_SEGMENTS] =
1899 Self::building_cylinder_ring(0.850, 0.478, 0.0);
1900 let ring_18: [Vec3; MESH_CYLINDER_TOWER_SEGMENTS] =
1901 Self::building_cylinder_ring(0.850, 0.498, 0.0);
1902 let ring_19: [Vec3; MESH_CYLINDER_TOWER_SEGMENTS] =
1903 Self::building_cylinder_ring(0.870, 0.498, 0.0);
1904 let ring_20: [Vec3; MESH_CYLINDER_TOWER_SEGMENTS] =
1905 Self::building_cylinder_ring(0.870, 0.482, 0.0);
1906 let ring_21: [Vec3; MESH_CYLINDER_TOWER_SEGMENTS] =
1907 Self::building_cylinder_ring(0.945, 0.456, 0.052);
1908 let shell_rings: [(&[Vec3], u16); 22] = [
1909 (&ring_00, slot_roof),
1910 (&ring_01, slot_facade),
1911 (&ring_02, slot_stone),
1912 (&ring_03, slot_stone),
1913 (&ring_04, slot_stone),
1914 (&ring_05, slot_facade),
1915 (&ring_06, slot_metal),
1916 (&ring_07, slot_metal),
1917 (&ring_08, slot_metal),
1918 (&ring_09, slot_facade),
1919 (&ring_10, slot_metal),
1920 (&ring_11, slot_metal),
1921 (&ring_12, slot_metal),
1922 (&ring_13, slot_facade),
1923 (&ring_14, slot_metal),
1924 (&ring_15, slot_metal),
1925 (&ring_16, slot_metal),
1926 (&ring_17, slot_facade),
1927 (&ring_18, slot_glow),
1928 (&ring_19, slot_glow),
1929 (&ring_20, slot_glow),
1930 (&ring_21, slot_metal),
1931 ];
1932 Self::push_stepped_polygon_shell(
1933 &mut vertices,
1934 &mut indices,
1935 &mut tri_materials,
1936 &shell_rings,
1937 (slot_roof, slot_roof),
1938 );
1939 let column_step: f32 = (2.0 * PI) / 8.0;
1940 for column_idx in 0..8usize {
1941 let column_angle: f32 = (column_idx as f32) * column_step;
1942 let (sin_col, cos_col): (f32, f32) = column_angle.sin_cos();
1943 Self::push_box_part(
1944 &mut vertices,
1945 &mut indices,
1946 &mut tri_materials,
1947 Vec3::new(cos_col * 0.440, 0.0755, -sin_col * 0.440),
1948 Vec3::new(0.060, 0.151, 0.060),
1949 slot_stone,
1950 );
1951 }
1952 let mut mesh: Self = Self::from_parts(name.to_string(), vertices, indices, material);
1953 mesh.set_sub_materials(sub_materials);
1954 mesh.set_tri_materials(tri_materials);
1955 mesh.set_is_closed(true);
1956 mesh
1957 }
1958
1959 #[must_use]
1970 pub fn building_boutique_podium_block(name: &str, material: PbrMaterial) -> Self {
1971 let sub_materials: Vec<PbrMaterial> = Self::building_sub_materials(
1972 Color::hex(0xA6957C),
1973 Color::hex(0x9B7B4A),
1974 Color::hex(0xFDE68A),
1975 );
1976 let slot_facade: u16 = 0;
1977 let slot_stone: u16 = 1;
1978 let slot_roof: u16 = 2;
1979 let slot_metal: u16 = 3;
1980 let slot_glow: u16 = 4;
1981 let mut vertices: Vec<Vertex3D> = Vec::with_capacity(760);
1982 let mut indices: Vec<[u32; 3]> = Vec::with_capacity(360);
1983 let mut tri_materials: Vec<u16> = Vec::with_capacity(360);
1984 let ring_00: [Vec3; 4] = Self::building_rect_ring(0.0, 0.000, 0.0, 0.380, 0.380);
1985 let ring_01: [Vec3; 4] = Self::building_rect_ring(0.0, 0.220, 0.0, 0.380, 0.380);
1986 let ring_02: [Vec3; 4] = Self::building_rect_ring(0.0, 0.220, 0.0, 0.396, 0.396);
1987 let ring_03: [Vec3; 4] = Self::building_rect_ring(0.0, 0.256, 0.0, 0.396, 0.396);
1988 let ring_04: [Vec3; 4] = Self::building_rect_ring(0.0, 0.256, 0.0, 0.380, 0.380);
1989 let ring_05: [Vec3; 4] = Self::building_rect_ring(0.0, 0.280, 0.0, 0.380, 0.380);
1990 let ring_06: [Vec3; 4] = Self::building_rect_ring(0.0, 0.280, 0.0, 0.500, 0.500);
1991 let ring_07: [Vec3; 4] = Self::building_rect_ring(0.0, 0.330, 0.0, 0.500, 0.500);
1992 let ring_08: [Vec3; 4] = Self::building_rect_ring(0.0, 0.330, 0.0, 0.455, 0.455);
1993 let ring_09: [Vec3; 4] = Self::building_rect_ring(0.0, 0.750, 0.0, 0.455, 0.455);
1994 let ring_10: [Vec3; 4] = Self::building_rect_ring(0.0, 0.750, 0.0, 0.500, 0.500);
1995 let ring_11: [Vec3; 4] = Self::building_rect_ring(0.0, 0.800, 0.0, 0.500, 0.500);
1996 let ring_12: [Vec3; 4] = Self::building_rect_ring(0.0, 0.800, 0.0, 0.330, 0.330);
1997 let ring_13: [Vec3; 4] = Self::building_rect_ring(0.0, 0.932, 0.0, 0.330, 0.330);
1998 let ring_14: [Vec3; 4] = Self::building_rect_ring(0.0, 0.932, 0.0, 0.352, 0.352);
1999 let ring_15: [Vec3; 4] = Self::building_rect_ring(0.0, 0.954, 0.0, 0.352, 0.352);
2000 let ring_16: [Vec3; 4] = Self::building_rect_ring(0.0, 0.954, 0.0, 0.320, 0.320);
2001 let ring_17: [Vec3; 4] = Self::building_rect_ring(0.0, 0.982, 0.0, 0.320, 0.320);
2002 let shell_rings: [(&[Vec3], u16); 18] = [
2003 (&ring_00, slot_roof),
2004 (&ring_01, slot_facade),
2005 (&ring_02, slot_glow),
2006 (&ring_03, slot_glow),
2007 (&ring_04, slot_glow),
2008 (&ring_05, slot_facade),
2009 (&ring_06, slot_stone),
2010 (&ring_07, slot_stone),
2011 (&ring_08, slot_stone),
2012 (&ring_09, slot_facade),
2013 (&ring_10, slot_stone),
2014 (&ring_11, slot_stone),
2015 (&ring_12, slot_stone),
2016 (&ring_13, slot_facade),
2017 (&ring_14, slot_roof),
2018 (&ring_15, slot_roof),
2019 (&ring_16, slot_roof),
2020 (&ring_17, slot_glow),
2021 ];
2022 Self::push_stepped_polygon_shell(
2023 &mut vertices,
2024 &mut indices,
2025 &mut tri_materials,
2026 &shell_rings,
2027 (slot_roof, slot_roof),
2028 );
2029 let pier_coords: [f32; 3] = [-0.448, 0.0, 0.448];
2030 for &pier_x in pier_coords.iter() {
2031 for &pier_z in pier_coords.iter() {
2032 if pier_x.abs() < 0.01 && pier_z.abs() < 0.01 {
2033 continue;
2034 }
2035 Self::push_box_part(
2036 &mut vertices,
2037 &mut indices,
2038 &mut tri_materials,
2039 Vec3::new(pier_x, 0.148, pier_z),
2040 Vec3::new(0.072, 0.296, 0.072),
2041 slot_stone,
2042 );
2043 }
2044 }
2045 for sign_x in [-1.0_f32, 1.0_f32] {
2046 for sign_z in [-1.0_f32, 1.0_f32] {
2047 Self::push_box_part(
2048 &mut vertices,
2049 &mut indices,
2050 &mut tri_materials,
2051 Vec3::new(sign_x * 0.454, 0.5445, sign_z * 0.454),
2052 Vec3::new(0.064, 0.461, 0.064),
2053 slot_stone,
2054 );
2055 }
2056 }
2057 for rail_x in [-0.280_f32, 0.280_f32] {
2058 Self::push_box_part(
2059 &mut vertices,
2060 &mut indices,
2061 &mut tri_materials,
2062 Vec3::new(rail_x, 0.984, 0.0),
2063 Vec3::new(0.036, 0.032, 0.600),
2064 slot_metal,
2065 );
2066 }
2067 let mut mesh: Self = Self::from_parts(name.to_string(), vertices, indices, material);
2068 mesh.set_sub_materials(sub_materials);
2069 mesh.set_tri_materials(tri_materials);
2070 mesh.set_is_closed(true);
2071 mesh
2072 }
2073
2074 #[must_use]
2084 pub fn palm_tree(name: &str) -> Self {
2085 let bark_mat: PbrMaterial = PbrMaterial::dielectric(Color::hex(0x4E392B), 0.88);
2086 let frond_top_mat: PbrMaterial = PbrMaterial::pbr(Color::hex(0x227848), 0.05, 0.56, 0.08);
2087 let frond_under_mat: PbrMaterial = PbrMaterial::pbr(Color::hex(0x134A2D), 0.04, 0.68, 0.05);
2088 let sub_materials: Vec<PbrMaterial> = vec![frond_top_mat, frond_under_mat];
2089 let mut vertices: Vec<Vertex3D> = Vec::with_capacity(520);
2090 let mut indices: Vec<[u32; 3]> = Vec::with_capacity(260);
2091 let mut tri_materials: Vec<u16> = Vec::with_capacity(260);
2092 let ring_specs: [(f32, f32, f32, f32); 5] = [
2093 (0.0, 0.0, 0.0, 0.30),
2094 (1.9, 0.14, 0.05, 0.25),
2095 (3.9, 0.32, 0.08, 0.21),
2096 (5.8, 0.44, 0.06, 0.17),
2097 (7.5, 0.48, 0.02, 0.14),
2098 ];
2099 let trunk_step: f32 = (2.0 * PI) / (MESH_PALM_TRUNK_SEGMENTS as f32);
2100 for seg_idx in 0..4usize {
2101 let (y0, ox0, oz0, r0): (f32, f32, f32, f32) = ring_specs[seg_idx];
2102 let (y1, ox1, oz1, r1): (f32, f32, f32, f32) = ring_specs[seg_idx + 1];
2103 for side_idx in 0..MESH_PALM_TRUNK_SEGMENTS {
2104 let a0: f32 = (side_idx as f32) * trunk_step;
2105 let a1: f32 = ((side_idx + 1) as f32) * trunk_step;
2106 let (s0, c0): (f32, f32) = a0.sin_cos();
2107 let (s1, c1): (f32, f32) = a1.sin_cos();
2108 let p0: Vec3 = Vec3::new(ox0 + c0 * r0, y0, oz0 + s0 * r0);
2109 let p1: Vec3 = Vec3::new(ox0 + c1 * r0, y0, oz0 + s1 * r0);
2110 let p2: Vec3 = Vec3::new(ox1 + c1 * r1, y1, oz1 + s1 * r1);
2111 let p3: Vec3 = Vec3::new(ox1 + c0 * r1, y1, oz1 + s0 * r1);
2112 Self::push_flat_quad(
2113 &mut vertices,
2114 &mut indices,
2115 &mut tri_materials,
2116 [p0, p1, p2, p3],
2117 0,
2118 );
2119 }
2120 }
2121 let crown: Vec3 = Vec3::new(0.48, 7.45, 0.02);
2122 let frond_step: f32 = (2.0 * PI) / (MESH_PALM_FROND_COUNT as f32);
2123 for tier_idx in 0..2usize {
2124 let tier_count: usize = if tier_idx == 0 {
2125 MESH_PALM_FROND_COUNT
2126 } else {
2127 5
2128 };
2129 let tier_step: f32 = if tier_idx == 0 {
2130 frond_step
2131 } else {
2132 (2.0 * PI) / 5.0
2133 };
2134 let tier_yaw_off: f32 = if tier_idx == 0 { 0.25 } else { 0.62 };
2135 let tier_origin: Vec3 = crown.add(Vec3::new(0.0, (tier_idx as f32) * 0.22, 0.0));
2136 for frond_idx in 0..tier_count {
2137 let yaw: f32 = (frond_idx as f32) * tier_step + tier_yaw_off;
2138 let (sy, cy): (f32, f32) = yaw.sin_cos();
2139 let dir: Vec3 = Vec3::new(cy, 0.0, sy);
2140 let right: Vec3 = Vec3::new(-sy, 0.0, cy);
2141 let reach: f32 = if tier_idx == 0 {
2142 if frond_idx % 2 == 0 { 2.85 } else { 2.45 }
2143 } else {
2144 1.85
2145 };
2146 let lift: f32 = if tier_idx == 0 { 0.48 } else { 0.92 };
2147 let drop_tip: f32 = if tier_idx == 0 { -1.18 } else { -0.18 };
2148 let p_mid: Vec3 = tier_origin
2149 .add(dir.scale(reach * 0.44))
2150 .add(Vec3::new(0.0, lift, 0.0));
2151 let p_arch: Vec3 = tier_origin.add(dir.scale(reach * 0.78)).add(Vec3::new(
2152 0.0,
2153 lift * 0.15 - 0.12,
2154 0.0,
2155 ));
2156 let p_tip: Vec3 = tier_origin
2157 .add(dir.scale(reach))
2158 .add(Vec3::new(0.0, drop_tip, 0.0));
2159 let w_mid: Vec3 = right.scale(if tier_idx == 0 { 0.34 } else { 0.26 });
2160 let w_arch: Vec3 = right.scale(if tier_idx == 0 { 0.24 } else { 0.18 });
2161 let sag_mid: Vec3 = Vec3::new(0.0, -0.16, 0.0);
2162 let sag_arch: Vec3 = Vec3::new(0.0, -0.20, 0.0);
2163 let mid_l: Vec3 = p_mid.sub(w_mid).add(sag_mid);
2164 let mid_r: Vec3 = p_mid.add(w_mid).add(sag_mid);
2165 let arch_l: Vec3 = p_arch.sub(w_arch).add(sag_arch);
2166 let arch_r: Vec3 = p_arch.add(w_arch).add(sag_arch);
2167 Self::push_flat_tri(
2168 &mut vertices,
2169 &mut indices,
2170 &mut tri_materials,
2171 (tier_origin, p_mid, mid_l),
2172 1,
2173 );
2174 Self::push_flat_tri(
2175 &mut vertices,
2176 &mut indices,
2177 &mut tri_materials,
2178 (tier_origin, mid_r, p_mid),
2179 1,
2180 );
2181 Self::push_flat_tri(
2182 &mut vertices,
2183 &mut indices,
2184 &mut tri_materials,
2185 (tier_origin, mid_l, p_mid),
2186 2,
2187 );
2188 Self::push_flat_tri(
2189 &mut vertices,
2190 &mut indices,
2191 &mut tri_materials,
2192 (tier_origin, p_mid, mid_r),
2193 2,
2194 );
2195 Self::push_flat_quad(
2196 &mut vertices,
2197 &mut indices,
2198 &mut tri_materials,
2199 [mid_l, p_mid, p_arch, arch_l],
2200 1,
2201 );
2202 Self::push_flat_quad(
2203 &mut vertices,
2204 &mut indices,
2205 &mut tri_materials,
2206 [p_mid, mid_r, arch_r, p_arch],
2207 1,
2208 );
2209 Self::push_flat_quad(
2210 &mut vertices,
2211 &mut indices,
2212 &mut tri_materials,
2213 [arch_l, p_arch, p_mid, mid_l],
2214 2,
2215 );
2216 Self::push_flat_quad(
2217 &mut vertices,
2218 &mut indices,
2219 &mut tri_materials,
2220 [p_arch, arch_r, mid_r, p_mid],
2221 2,
2222 );
2223 Self::push_flat_tri(
2224 &mut vertices,
2225 &mut indices,
2226 &mut tri_materials,
2227 (arch_l, p_arch, p_tip),
2228 1,
2229 );
2230 Self::push_flat_tri(
2231 &mut vertices,
2232 &mut indices,
2233 &mut tri_materials,
2234 (p_arch, arch_r, p_tip),
2235 1,
2236 );
2237 Self::push_flat_tri(
2238 &mut vertices,
2239 &mut indices,
2240 &mut tri_materials,
2241 (p_arch, arch_l, p_tip),
2242 2,
2243 );
2244 Self::push_flat_tri(
2245 &mut vertices,
2246 &mut indices,
2247 &mut tri_materials,
2248 (arch_r, p_arch, p_tip),
2249 2,
2250 );
2251 }
2252 }
2253 let mut mesh: Self = Self::from_parts(name.to_string(), vertices, indices, bark_mat);
2254 mesh.set_sub_materials(sub_materials);
2255 mesh.set_tri_materials(tri_materials);
2256 mesh.set_is_closed(false);
2257 mesh
2258 }
2259
2260 #[must_use]
2270 pub fn from_box_desc(desc: &BoxMeshDesc) -> Self {
2271 let size: Vec3 = desc.get_size();
2272 Self::box_mesh(
2273 desc.get_name(),
2274 size.get_x(),
2275 size.get_y(),
2276 size.get_z(),
2277 desc.get_ground_pivot(),
2278 desc.get_material(),
2279 )
2280 }
2281
2282 #[must_use]
2292 pub fn from_ground_desc(desc: &GroundPlaneDesc) -> Self {
2293 Self::ground_plane(
2294 desc.get_name(),
2295 desc.get_width(),
2296 desc.get_depth(),
2297 desc.get_subdivisions(),
2298 desc.get_material(),
2299 )
2300 }
2301
2302 #[must_use]
2313 pub fn build_lod_proxy(&self, suffix: &str) -> Self {
2314 let blue_val: AxisAlignedBox3D = self.get_bounds();
2315 let size: Vec3 = blue_val.get_max().sub(blue_val.get_min());
2316 let center: Vec3 = blue_val.center();
2317 let mut proxy: Self = Self::box_mesh(
2318 &format!("{}_{suffix}", self.get_name()),
2319 size.get_x().max(0.2),
2320 size.get_y().max(0.2),
2321 size.get_z().max(0.2),
2322 false,
2323 self.get_material(),
2324 );
2325 for item_val in proxy.get_mut_vertices().iter_mut() {
2326 item_val.set_position(item_val.get_position().add(center));
2327 }
2328 proxy.set_bounds(blue_val);
2329 let meshlets: Vec<Meshlet3D> =
2330 Self::build_meshlets_from_parts(proxy.get_vertices(), proxy.get_indices());
2331 proxy.set_meshlets(meshlets);
2332 proxy
2333 }
2334
2335 #[must_use]
2346 pub fn build_mid_lod(&self, suffix: &str) -> Self {
2347 let verts: &[Vertex3D] = self.get_vertices().as_slice();
2348 let tris: &[[u32; 3]] = self.get_indices().as_slice();
2349 if tris.len() <= 24 {
2350 return self.build_lod_proxy(suffix);
2351 }
2352 let mut scored: Vec<(f32, [u32; 3], u16)> = Vec::with_capacity(tris.len());
2353 let default_mat: PbrMaterial = self.get_material();
2354 for (tri_idx, tri) in tris.iter().copied().enumerate() {
2355 let (i0, i1, i2): (usize, usize, usize) =
2356 (tri[0] as usize, tri[1] as usize, tri[2] as usize);
2357 if i0 < verts.len() && i1 < verts.len() && i2 < verts.len() {
2358 let p0: Vec3 = verts[i0].get_position();
2359 let p1: Vec3 = verts[i1].get_position();
2360 let p2: Vec3 = verts[i2].get_position();
2361 let area2: f32 = p1.sub(p0).cross(p2.sub(p0)).length_sq();
2362 let (resolved_mat, slot): (PbrMaterial, u16) =
2363 self.resolve_triangle_material(tri_idx, default_mat);
2364 let emissive_boost: f32 = if resolved_mat.get_emissive().length_sq() > 0.25 {
2365 8.0
2366 } else {
2367 1.0
2368 };
2369 scored.push((area2 * emissive_boost, tri, slot));
2370 }
2371 }
2372 scored.sort_unstable_by(
2373 |alpha_val: &(f32, [u32; 3], u16), blue_val: &(f32, [u32; 3], u16)| {
2374 blue_val.0.total_cmp(&alpha_val.0)
2375 },
2376 );
2377 let keep: usize = (scored.len() / 2).max(12);
2378 let mut reduced: Vec<[u32; 3]> = Vec::with_capacity(keep);
2379 let mut reduced_slots: Vec<u16> = if self.get_tri_materials().is_empty() {
2380 Vec::new()
2381 } else {
2382 Vec::with_capacity(keep)
2383 };
2384 for (_, tri, slot) in scored.into_iter().take(keep) {
2385 reduced.push(tri);
2386 if !self.get_tri_materials().is_empty() {
2387 reduced_slots.push(slot);
2388 }
2389 }
2390 let mut mid_mesh: Self = Self::from_parts(
2391 format!("{}_{suffix}", self.get_name()),
2392 self.get_vertices().clone(),
2393 reduced,
2394 self.get_material(),
2395 );
2396 if !reduced_slots.is_empty() {
2397 mid_mesh.set_sub_materials(self.get_sub_materials().clone());
2398 mid_mesh.set_tri_materials(reduced_slots);
2399 }
2400 mid_mesh.set_is_closed(self.get_is_closed());
2401 mid_mesh
2402 }
2403}
2404
2405impl MeshLodChain {
2407 #[must_use]
2417 pub fn from_mesh(mesh: Mesh3D) -> Self {
2418 let tri_count: usize = mesh.get_indices().len();
2419 if tri_count > 32 {
2420 let lod1: Mesh3D = mesh.build_mid_lod(MESH_LOD1_SUFFIX);
2421 let lod2: Mesh3D = mesh.build_lod_proxy(MESH_LOD2_SUFFIX);
2422 Self {
2423 levels: vec![mesh, lod1, lod2],
2424 }
2425 } else if tri_count > 16 {
2426 let lod1: Mesh3D = mesh.build_lod_proxy(MESH_LOD1_SUFFIX);
2427 Self {
2428 levels: vec![mesh, lod1],
2429 }
2430 } else {
2431 let clone_lod1: Mesh3D = mesh.clone();
2432 Self {
2433 levels: vec![mesh, clone_lod1],
2434 }
2435 }
2436 }
2437
2438 #[must_use]
2448 pub fn single_level(mesh: Mesh3D) -> Self {
2449 Self { levels: vec![mesh] }
2450 }
2451
2452 #[must_use]
2464 pub fn select(&self, distance: f32, lod_bias: f32) -> (&Mesh3D, usize) {
2465 let levels: &[Mesh3D] = self.get_levels().as_slice();
2466 let effective_dist: f32 = distance * lod_bias.max(0.25);
2467 if levels.len() > 2 && effective_dist > 135.0 {
2468 (&levels[2], 2)
2469 } else if levels.len() > 1 && effective_dist > 78.0 {
2470 (&levels[1], 1)
2471 } else {
2472 (&levels[0], 0)
2473 }
2474 }
2475
2476 #[must_use]
2490 pub fn select_screen_error(
2491 &self,
2492 distance: f32,
2493 bounds_radius: f32,
2494 focal_px: f32,
2495 lod_bias: f32,
2496 ) -> (&Mesh3D, usize) {
2497 let levels: &[Mesh3D] = self.get_levels().as_slice();
2498 let safe_dist: f32 = distance.max(0.5);
2499 let projected_px: f32 =
2500 (bounds_radius.max(0.2) * focal_px) / (safe_dist * lod_bias.max(0.25));
2501 if levels.len() > 2 && projected_px < 3.5 {
2502 (&levels[2], 2)
2503 } else if levels.len() > 1 && projected_px < 9.0 {
2504 (&levels[1], 1)
2505 } else {
2506 (&levels[0], 0)
2507 }
2508 }
2509}
2510
2511impl ModelImporter3D {
2513 pub fn import(
2525 format: ModelFormat,
2526 source: &str,
2527 fallback_material: PbrMaterial,
2528 ) -> Result<ImportedModel3D, String> {
2529 match format {
2530 ModelFormat::Obj => Self::parse_obj(source, fallback_material),
2531 ModelFormat::GltfJson => Self::parse_gltf_json(source, fallback_material),
2532 ModelFormat::GlbBinary | ModelFormat::Euv3dBinary | ModelFormat::BlendInspect => {
2533 Err(ERR_TEXT_FORMAT_UNSUPPORTED.to_string())
2534 }
2535 }
2536 }
2537
2538 pub fn import_bytes(
2550 format: ModelFormat,
2551 bytes: &[u8],
2552 fallback_material: PbrMaterial,
2553 ) -> Result<ImportedModel3D, String> {
2554 match format {
2555 ModelFormat::Obj => {
2556 let text: &str = from_utf8(bytes)
2557 .ok()
2558 .ok_or_else(|| ERR_OBJ_EMPTY.to_string())?;
2559 Self::parse_obj(text, fallback_material)
2560 }
2561 ModelFormat::GltfJson => {
2562 let text: &str = from_utf8(bytes)
2563 .ok()
2564 .ok_or_else(|| ERR_GLTF_EMPTY.to_string())?;
2565 Self::parse_gltf_json(text, fallback_material)
2566 }
2567 ModelFormat::GlbBinary => {
2568 let scene: ImportedScene3D = parse_glb_bytes(bytes)?;
2569 if scene.get_meshes().is_empty() {
2570 return Err(ERR_GLTF_EMPTY.to_string());
2571 }
2572 let mut model: ImportedModel3D = scene.to_imported_model(ModelFormat::GlbBinary);
2573 if scene.get_materials().is_empty() {
2574 for chain in model.get_mut_meshes().iter_mut() {
2575 for lvl in chain.get_mut_levels().iter_mut() {
2576 lvl.set_material(fallback_material);
2577 }
2578 }
2579 }
2580 Ok(model)
2581 }
2582 ModelFormat::Euv3dBinary => {
2583 let bundle: BakedAssetBundle3D = Euv3dPackage::decode_bytes(bytes)?;
2584 if bundle.get_lod_chains().is_empty() {
2585 return Err(ERR_GLTF_EMPTY.to_string());
2586 }
2587 let total_vertices: usize = bundle
2588 .get_lod_chains()
2589 .iter()
2590 .filter_map(|c: &MeshLodChain| c.get_levels().first())
2591 .map(|m: &Mesh3D| m.get_vertices().len())
2592 .sum();
2593 let total_triangles: usize = bundle
2594 .get_lod_chains()
2595 .iter()
2596 .filter_map(|c: &MeshLodChain| c.get_levels().first())
2597 .map(|m: &Mesh3D| m.get_indices().len())
2598 .sum();
2599 Ok(ImportedModel3D {
2600 name: bundle.get_scene().get_name().clone(),
2601 format: ModelFormat::Euv3dBinary,
2602 meshes: bundle.get_lod_chains().clone(),
2603 total_vertices,
2604 total_triangles,
2605 })
2606 }
2607 ModelFormat::BlendInspect => Err(ERR_BINARY_FORMAT_UNSUPPORTED.to_string()),
2608 }
2609 }
2610
2611 #[must_use]
2622 fn resolve_obj_index(raw: i32, len: usize) -> Option<usize> {
2623 if raw > 0 {
2624 let idx: usize = (raw - 1) as usize;
2625 if idx < len { Some(idx) } else { None }
2626 } else if raw < 0 {
2627 let idx: isize = (len as isize) + (raw as isize);
2628 if idx >= 0 && (idx as usize) < len {
2629 Some(idx as usize)
2630 } else {
2631 None
2632 }
2633 } else {
2634 None
2635 }
2636 }
2637
2638 #[must_use]
2649 pub fn parse_mtl(mtl_source: &str, fallback: PbrMaterial) -> HashMap<String, PbrMaterial> {
2650 let mut table: HashMap<String, PbrMaterial> = HashMap::new();
2651 let mut active_name: Option<String> = None;
2652 let mut active_mat: PbrMaterial = fallback;
2653 for raw_line in mtl_source.lines() {
2654 let line: &str = raw_line.trim();
2655 if line.is_empty() || line.starts_with('#') {
2656 continue;
2657 }
2658 let parts: Vec<&str> = line.split_whitespace().collect();
2659 if parts.is_empty() {
2660 continue;
2661 }
2662 if parts[0] == MTL_DIRECTIVE_NEWMTL && parts.len() >= 2 {
2663 if let Some(prev_name) = active_name.take() {
2664 let _: Option<PbrMaterial> = table.insert(prev_name, active_mat);
2665 }
2666 active_name = Some(parts[1].to_string());
2667 active_mat = fallback;
2668 } else if parts[0] == "Kd" && parts.len() >= 4 {
2669 let r: f32 = parts[1].parse::<f32>().unwrap_or(0.8).clamp(0.0, 1.0);
2670 let g: f32 = parts[2].parse::<f32>().unwrap_or(0.8).clamp(0.0, 1.0);
2671 let b: f32 = parts[3].parse::<f32>().unwrap_or(0.8).clamp(0.0, 1.0);
2672 active_mat.set_base_color(Vec3::new(r, g, b));
2673 } else if parts[0] == "Ke" && parts.len() >= 4 {
2674 let r: f32 = parts[1].parse::<f32>().unwrap_or(0.0).max(0.0);
2675 let g: f32 = parts[2].parse::<f32>().unwrap_or(0.0).max(0.0);
2676 let b: f32 = parts[3].parse::<f32>().unwrap_or(0.0).max(0.0);
2677 active_mat.set_emissive(Vec3::new(r, g, b));
2678 } else if parts[0] == "Pm" && parts.len() >= 2 {
2679 let metal: f32 = parts[1].parse::<f32>().unwrap_or(0.0).clamp(0.0, 1.0);
2680 active_mat.set_metallic(metal);
2681 } else if parts[0] == "Pr" && parts.len() >= 2 {
2682 let rough: f32 = parts[1].parse::<f32>().unwrap_or(0.5).clamp(0.04, 1.0);
2683 active_mat.set_roughness(rough);
2684 active_mat.set_reflectivity((1.0 - rough).clamp(0.05, 0.95));
2685 } else if parts[0] == "Ns" && parts.len() >= 2 {
2686 let ns: f32 = parts[1].parse::<f32>().unwrap_or(64.0).clamp(0.0, 1000.0);
2687 let rough: f32 = (1.0 - (ns / 1000.0).sqrt()).clamp(0.04, 1.0);
2688 active_mat.set_roughness(rough);
2689 active_mat.set_reflectivity((1.0 - rough).clamp(0.05, 0.95));
2690 }
2691 }
2692 if let Some(last_name) = active_name {
2693 let _: Option<PbrMaterial> = table.insert(last_name, active_mat);
2694 }
2695 table
2696 }
2697
2698 pub fn parse_obj_with_mtl(
2710 source: &str,
2711 mtl_source: Option<&str>,
2712 material: PbrMaterial,
2713 ) -> Result<ImportedModel3D, String> {
2714 let mtl_table: HashMap<String, PbrMaterial> = mtl_source
2715 .map(|text: &str| Self::parse_mtl(text, material))
2716 .unwrap_or_default();
2717 let mut positions: Vec<Vec3> = Vec::new();
2718 let mut uvs: Vec<[f32; 2]> = Vec::new();
2719 let mut normals: Vec<Vec3> = Vec::new();
2720 let mut vertices: Vec<Vertex3D> = Vec::new();
2721 let mut indices: Vec<[u32; 3]> = Vec::new();
2722 let mut dedup: HashMap<(usize, Option<usize>, Option<usize>), u32> = HashMap::new();
2723 let mut sub_materials: Vec<PbrMaterial> = Vec::new();
2724 let mut sub_mat_slots: HashMap<String, u16> = HashMap::new();
2725 let mut tri_materials: Vec<u16> = Vec::new();
2726 let mut active_slot: u16 = 0;
2727 let mut model_name: String = MESH_OBJ_DEFAULT_NAME.to_string();
2728 let mut saw_any_uv: bool = false;
2729 let mut saw_any_normal: bool = false;
2730 for raw_line in source.lines() {
2731 let line: &str = raw_line.trim();
2732 if line.is_empty() || line.starts_with('#') {
2733 continue;
2734 }
2735 let parts: Vec<&str> = line.split_whitespace().collect();
2736 if parts.is_empty() {
2737 continue;
2738 }
2739 match parts[0] {
2740 "o" | "g" if parts.len() >= 2 => {
2741 model_name = parts[1].to_string();
2742 }
2743 token if token == OBJ_DIRECTIVE_MTLLIB => {}
2744 token if token == OBJ_DIRECTIVE_USEMTL && parts.len() >= 2 => {
2745 let mat_name: &str = parts[1];
2746 if let Some(&slot) = sub_mat_slots.get(mat_name) {
2747 active_slot = slot;
2748 } else if let Some(&parsed_mat) = mtl_table.get(mat_name) {
2749 sub_materials.push(parsed_mat);
2750 let new_slot: u16 = sub_materials.len() as u16;
2751 let _: Option<u16> = sub_mat_slots.insert(mat_name.to_string(), new_slot);
2752 active_slot = new_slot;
2753 } else {
2754 active_slot = 0;
2755 }
2756 }
2757 "v" if parts.len() >= 4 => {
2758 let pos_x: f32 = parts[1].parse::<f32>().unwrap_or(0.0);
2759 let pos_y: f32 = parts[2].parse::<f32>().unwrap_or(0.0);
2760 let pos_z: f32 = parts[3].parse::<f32>().unwrap_or(0.0);
2761 positions.push(Vec3::new(pos_x, pos_y, pos_z));
2762 }
2763 "vt" if parts.len() >= 3 => {
2764 let coord_u: f32 = parts[1].parse::<f32>().unwrap_or(0.0);
2765 let coord_v: f32 = parts[2].parse::<f32>().unwrap_or(0.0);
2766 uvs.push([coord_u, coord_v]);
2767 saw_any_uv = true;
2768 }
2769 "vn" if parts.len() >= 4 => {
2770 let coord_x: f32 = parts[1].parse::<f32>().unwrap_or(0.0);
2771 let coord_y: f32 = parts[2].parse::<f32>().unwrap_or(1.0);
2772 let coord_z: f32 = parts[3].parse::<f32>().unwrap_or(0.0);
2773 normals.push(Vec3::new(coord_x, coord_y, coord_z).normalize());
2774 saw_any_normal = true;
2775 }
2776 "f" if parts.len() >= 4 => {
2777 let mut face_refs: Vec<(usize, Option<usize>, Option<usize>)> =
2778 Vec::with_capacity(parts.len() - 1);
2779 for token in parts.iter().skip(1) {
2780 let comps: Vec<&str> = token.split('/').collect();
2781 let v_raw: i32 = comps
2782 .first()
2783 .and_then(|s: &&str| s.parse::<i32>().ok())
2784 .unwrap_or(0);
2785 let Some(v_idx) = Self::resolve_obj_index(v_raw, positions.len()) else {
2786 continue;
2787 };
2788 let vt_idx: Option<usize> = comps
2789 .get(1)
2790 .and_then(|s: &&str| s.parse::<i32>().ok())
2791 .and_then(|raw: i32| Self::resolve_obj_index(raw, uvs.len()));
2792 let vn_idx: Option<usize> = comps
2793 .get(2)
2794 .and_then(|s: &&str| s.parse::<i32>().ok())
2795 .and_then(|raw: i32| Self::resolve_obj_index(raw, normals.len()));
2796 face_refs.push((v_idx, vt_idx, vn_idx));
2797 }
2798 if face_refs.len() >= 3 {
2799 for entry_index in 1..(face_refs.len() - 1) {
2800 let tri_keys: [(usize, Option<usize>, Option<usize>); 3] = [
2801 face_refs[0],
2802 face_refs[entry_index],
2803 face_refs[entry_index + 1],
2804 ];
2805 let p0: Vec3 = positions[tri_keys[0].0];
2806 let p1: Vec3 = positions[tri_keys[1].0];
2807 let p2: Vec3 = positions[tri_keys[2].0];
2808 let face_n: Vec3 = p1.sub(p0).cross(p2.sub(p0)).normalize();
2809 let mut out_tri: [u32; 3] = [0u32; 3];
2810 for lane in 0..3usize {
2811 let key: (usize, Option<usize>, Option<usize>) = tri_keys[lane];
2812 let vert_id: u32 = if key.2.is_some() {
2813 if let Some(&existing) = dedup.get(&key) {
2814 existing
2815 } else {
2816 let id: u32 = vertices.len() as u32;
2817 let mut v: Vertex3D = Vertex3D::new(
2818 positions[key.0],
2819 key.2
2820 .and_then(|ni: usize| normals.get(ni).copied())
2821 .unwrap_or(face_n),
2822 );
2823 if let Some(ti) = key.1
2824 && let Some(&uv_pair) = uvs.get(ti)
2825 {
2826 v.set_uv0(uv_pair);
2827 v.set_flags(1);
2828 }
2829 vertices.push(v);
2830 let _: Option<u32> = dedup.insert(key, id);
2831 id
2832 }
2833 } else {
2834 let id: u32 = vertices.len() as u32;
2835 let mut v: Vertex3D = Vertex3D::new(positions[key.0], face_n);
2836 if let Some(ti) = key.1
2837 && let Some(&uv_pair) = uvs.get(ti)
2838 {
2839 v.set_uv0(uv_pair);
2840 v.set_flags(1);
2841 }
2842 vertices.push(v);
2843 id
2844 };
2845 out_tri[lane] = vert_id;
2846 }
2847 indices.push(out_tri);
2848 if !sub_materials.is_empty() {
2849 tri_materials.push(active_slot);
2850 }
2851 }
2852 }
2853 }
2854 _ => {}
2855 }
2856 }
2857 if indices.is_empty() {
2858 return Err(ERR_OBJ_EMPTY.to_string());
2859 }
2860 let mut mesh: Mesh3D = Mesh3D::from_parts(model_name.clone(), vertices, indices, material);
2861 if !sub_materials.is_empty() {
2862 if tri_materials.len() < mesh.get_indices().len() {
2863 tri_materials.resize(mesh.get_indices().len(), 0);
2864 }
2865 mesh.set_sub_materials(sub_materials);
2866 mesh.set_tri_materials(tri_materials);
2867 }
2868 if !saw_any_normal && dedup.len() < mesh.get_vertices().len() {
2869 mesh.compute_smooth_normals();
2870 }
2871 if saw_any_uv {
2872 mesh.compute_lengyel_tangents();
2873 }
2874 let total_vertices: usize = mesh.get_vertices().len();
2875 let total_triangles: usize = mesh.get_indices().len();
2876 Ok(ImportedModel3D {
2877 name: model_name,
2878 format: ModelFormat::Obj,
2879 meshes: vec![MeshLodChain::from_mesh(mesh)],
2880 total_vertices,
2881 total_triangles,
2882 })
2883 }
2884
2885 pub fn parse_obj(source: &str, material: PbrMaterial) -> Result<ImportedModel3D, String> {
2896 Self::parse_obj_with_mtl(source, None, material)
2897 }
2898
2899 pub fn parse_gltf_json(
2910 source: &str,
2911 fallback_material: PbrMaterial,
2912 ) -> Result<ImportedModel3D, String> {
2913 let scene: ImportedScene3D = parse_gltf_document(source, None, None)?;
2914 if scene.get_meshes().is_empty() {
2915 return Err(ERR_GLTF_EMPTY.to_string());
2916 }
2917 let mut model: ImportedModel3D = scene.to_imported_model(ModelFormat::GltfJson);
2918 if scene.get_materials().is_empty() {
2919 for chain in model.get_mut_meshes().iter_mut() {
2920 for lvl in chain.get_mut_levels().iter_mut() {
2921 lvl.set_material(fallback_material);
2922 }
2923 }
2924 }
2925 Ok(model)
2926 }
2927}