// Renders individual parts (tanks, engines, avionics, structures, shrouds). import * as THREE from "three"; import type { PlacedComponent } from "../../../shared/types.js"; import { buildOgiveFairingGeo, buildParabolicBellGeo, buildStageConnector, cappedColumnGroup, } from "./geometry.js"; import { ACCENT_STRIPE_MAT, addSelectionHalo, AVIONICS_RING_MAT, DARK_NOZZLE_MAT, FAIRING_SOLID_MAT, SHUTTLE_FOAM_MAT, } from "./materials.js"; export function buildSinglePartCopy( comp: PlacedComponent, tankMat: THREE.MeshStandardMaterial, nextStageDia: number, isSelected: boolean, stageDia: number = comp.diameter, ): THREE.Group { const g = new THREE.Group(); const r = comp.diameter / 2; if (comp.role === "engine") { const skirt = cappedColumnGroup( r * 0.55, r * 0.38, comp.length * 0.22, AVIONICS_RING_MAT, { radialSegs: 18 }, ); skirt.position.y = comp.length * 0.78; g.add(skirt); const bellH = comp.length * 0.8; const bell = new THREE.Mesh( buildParabolicBellGeo(r, bellH), DARK_NOZZLE_MAT, ); bell.castShadow = true; bell.receiveShadow = true; g.add(bell); const lip = new THREE.Mesh( new THREE.TorusGeometry(r * 0.97, Math.max(0.025, r * 0.06), 8, 22), DARK_NOZZLE_MAT, ); lip.rotation.x = Math.PI / 2; lip.position.y = 0.03; g.add(lip); } else if (comp.role === "tank") { if (comp.isDropTank) { const barrelMat = comp.part.id === "drop-tank-hydro-et" ? SHUTTLE_FOAM_MAT : tankMat; const aftH = Math.max(0.28, comp.length * 0.06); const noseH = Math.max(0.55, comp.length * 0.22); const cylLen = Math.max(0.6, comp.length - aftH - noseH); const aftCap = cappedColumnGroup(r, r * 0.62, aftH, barrelMat, { radialSegs: 32, }); aftCap.traverse((o) => { if ((o as THREE.Mesh).isMesh) { o.castShadow = true; o.receiveShadow = true; } }); g.add(aftCap); const tankMesh = cappedColumnGroup(r, r, cylLen, barrelMat, { radialSegs: 36, }); tankMesh.position.y = aftH; tankMesh.traverse((o) => { if ((o as THREE.Mesh).isMesh) { o.castShadow = true; o.receiveShadow = true; } }); g.add(tankMesh); const nose = cappedColumnGroup(r * 0.14, r, noseH, barrelMat, { radialSegs: 36, }); nose.position.y = aftH + cylLen; nose.traverse((o) => { if ((o as THREE.Mesh).isMesh) { o.castShadow = true; o.receiveShadow = true; } }); g.add(nose); const noseTip = new THREE.Mesh( new THREE.SphereGeometry(r * 0.14, 16, 10), AVIONICS_RING_MAT, ); noseTip.position.y = aftH + cylLen + noseH; g.add(noseTip); for (const frac of [0.16, 0.54, 0.92]) { const band = new THREE.Mesh( new THREE.CylinderGeometry(r * 1.018, r * 1.018, 0.14, 36), AVIONICS_RING_MAT, ); band.position.y = aftH + cylLen * frac; g.add(band); } } else { const tankMesh = cappedColumnGroup(r, r, comp.length, tankMat, { radialSegs: 48, }); tankMesh.traverse((o) => { if ((o as THREE.Mesh).isMesh) { o.castShadow = true; o.receiveShadow = true; } }); g.add(tankMesh); for (const frac of [0.15, 0.5, 0.85]) { const ring = new THREE.Mesh( new THREE.CylinderGeometry(r * 1.012, r * 1.012, 0.16, 48), AVIONICS_RING_MAT, ); ring.position.y = comp.length * frac; ring.castShadow = true; g.add(ring); } const raceway = new THREE.Mesh( new THREE.BoxGeometry( Math.max(0.12, r * 0.08), comp.length * 0.94, Math.max(0.08, r * 0.06), ), AVIONICS_RING_MAT, ); raceway.position.set(0, comp.length / 2, r * 0.995); g.add(raceway); } } else if (comp.role === "heatshield") { const shield = cappedColumnGroup( r * 1.03, r * 1.03, comp.length, new THREE.MeshStandardMaterial({ color: 0x451a03, metalness: 0.3, roughness: 0.85, }), { radialSegs: 32 }, ); g.add(shield); } else if (comp.role === "structure") { const avionics = cappedColumnGroup(r, r, comp.length, tankMat, { radialSegs: 48, }); avionics.traverse((o) => { if ((o as THREE.Mesh).isMesh) { o.castShadow = true; o.receiveShadow = true; } }); g.add(avionics); const botTrim = new THREE.Mesh( new THREE.CylinderGeometry( r * 1.008, r * 1.008, Math.min(0.24, comp.length * 0.22), 48, ), AVIONICS_RING_MAT, ); botTrim.position.y = Math.min(0.12, comp.length * 0.11); g.add(botTrim); const topTrim = new THREE.Mesh( new THREE.CylinderGeometry( r * 1.008, r * 1.008, Math.min(0.24, comp.length * 0.22), 48, ), AVIONICS_RING_MAT, ); topTrim.position.y = comp.length - Math.min(0.12, comp.length * 0.11); g.add(topTrim); const stripe = new THREE.Mesh( new THREE.CylinderGeometry(r * 1.012, r * 1.012, 0.2, 48), ACCENT_STRIPE_MAT, ); stripe.position.y = comp.length * 0.5; g.add(stripe); } else if (comp.role === "interstage") { const botR = stageDia / 2; const topR = nextStageDia > 0.2 ? nextStageDia / 2 : r; g.add(buildStageConnector(botR, topR, Math.max(0.6, comp.length), tankMat)); } else if (comp.role === "computer") { const collarH = Math.max(0.45, comp.length); const collar = cappedColumnGroup( r * 1.02, r * 1.02, collarH, new THREE.MeshStandardMaterial({ color: 0x0f172a, metalness: 0.72, roughness: 0.28, }), { radialSegs: 24 }, ); g.add(collar); const ledColor = (comp.part.radHardeningLevel ?? 1) >= 3 ? 0x34d399 : (comp.part.radHardeningLevel ?? 1) >= 2 ? 0x38bdf8 : 0xf59e0b; const busRing = new THREE.Mesh( new THREE.TorusGeometry(r * 1.03, 0.055, 8, 28), new THREE.MeshBasicMaterial({ color: ledColor }), ); busRing.rotation.x = Math.PI / 2; busRing.position.y = Math.max(0.45, comp.length) * 0.55; g.add(busRing); } else if (comp.role === "fairing") { const shroud = new THREE.Mesh( buildOgiveFairingGeo(r, comp.length), FAIRING_SOLID_MAT, ); shroud.castShadow = true; shroud.receiveShadow = true; g.add(shroud); } if (isSelected) addSelectionHalo(g, comp); return g; }