import { invalidate, useThree, type ThreeEvent } from "@react-three/fiber"; import type { QuicklimeEvent } from "quicklime"; import { useEffect, useMemo, useRef } from "react"; import type { Object3D, Texture } from "three"; import { Group, Mesh, MeshStandardMaterial, Vector2 } from "three"; import { applyPitchYawLimits } from "../core/blitz/applyPitchYawLimits"; import { hasEquipment } from "../core/blitzkit/hasEquipment"; import { modelTransformEvent } from "../core/blitzkit/modelTransform"; import { controlsEnabledEvent } from "../core/controlsEnabled"; import { useBatchGroups } from "../hooks/useBatchGroups"; import { useModelNodes } from "../hooks/useModelNodes"; import { useSkin, type Skin } from "../hooks/useSkin"; import { useSkinAnimation } from "../hooks/useSkinAnimation"; import { useTankModelDefinition } from "../hooks/useTankModelDefinition"; import { useTankTransform } from "../hooks/useTankTransform"; import { Duel } from "../stores/duel"; import { Tankopedia } from "../stores/tankopedia"; import { TankopediaDisplay } from "../stores/tankopediaPersistent/constants"; import { ModelTankWrapper } from "./Armor/components/ModelTankWrapper"; import { Batch } from "./Batch"; import { transitionEvent } from "./Tankopedia/HeroSection/components/TankSandbox/components/Lighting"; interface SectionProps { nodes: Object3D[]; skins: Skin[]; // dragging scrolls the track textures scrollTracks?: boolean; // dragging turns the turret yaw?: boolean; // dragging raises and lowers the gun pitch?: boolean; } // emissive parts fade with the lights instead of glowing in the dark function fadeEmissive(materials: MeshStandardMaterial[]) { const emissiveMaterials = materials.filter( (material) => material.emissiveMap, ); function applyIntensity(intensity: number) { for (const material of emissiveMaterials) { material.emissiveIntensity = intensity; } } function handleTransitionEvent(event: QuicklimeEvent) { applyIntensity(event.data); } applyIntensity(transitionEvent.last!); transitionEvent.on(handleTransitionEvent); return () => { transitionEvent.off(handleTransitionEvent); }; } function Section({ nodes, skins, scrollTracks, yaw, pitch }: SectionProps) { const turret = Duel.use((state) => state.protagonist.turret); const gun = Duel.use((state) => state.protagonist.gun); const tankModelDefinition = useTankModelDefinition(); const turretModelDefinition = tankModelDefinition.turrets[turret.id]; const gunModelDefinition = turretModelDefinition.guns[gun.id]; const { groups, parts, materials } = useBatchGroups(nodes, skins); const position = useRef(new Vector2()); const delta = useRef(new Vector2()); const pitchAngle = useRef(0); const yawAngle = useRef(0); const textures = useRef(new Set()); const canvas = useThree((state) => state.gl.domElement); const rotates = yaw || pitch; function handlePointerMove(event: PointerEvent) { delta.current.set(event.clientX, event.clientY).sub(position.current); position.current.set(event.clientX, event.clientY); if (scrollTracks) { const offset = delta.current.x / window.innerWidth + delta.current.y / window.innerHeight; for (const texture of textures.current) { if (!texture) continue; texture.offset.y += offset; } invalidate(); } if (rotates) { const hasImprovedVerticalStabilizer = hasEquipment( 122, Duel.state.protagonist.tank.equipment_preset, Duel.state.protagonist.equipmentMatrix, ); const hasDownImprovedVerticalStabilizer = hasEquipment( 124, Duel.state.protagonist.tank.equipment_preset, Duel.state.protagonist.equipmentMatrix, ); const boundingRect = canvas.getBoundingClientRect(); const pitchDelta = pitch ? delta.current.y * (Math.PI / boundingRect.height) : 0; const yawDelta = yaw ? delta.current.x * (Math.PI / boundingRect.width) : 0; [pitchAngle.current, yawAngle.current] = applyPitchYawLimits( pitchAngle.current - pitchDelta, yawAngle.current + yawDelta, gunModelDefinition.pitch!, turretModelDefinition.yaw, hasImprovedVerticalStabilizer, hasDownImprovedVerticalStabilizer, ); modelTransformEvent.dispatch({ pitch: pitchAngle.current, yaw: yawAngle.current, }); } } function handlePointerUp() { controlsEnabledEvent.dispatch(true); window.removeEventListener("pointermove", handlePointerMove); window.removeEventListener("pointerup", handlePointerUp); } function handlePointerDown(event: ThreeEvent) { // tracks only scroll while looking at the model, not the armor if (scrollTracks && Tankopedia.state.display !== TankopediaDisplay.Model) { return; } event.stopPropagation(); if (!scrollTracks && !rotates) return; if (scrollTracks) { const material = (event.object as Mesh).material as MeshStandardMaterial; textures.current = new Set([ material.map, material.aoMap, material.normalMap, material.roughnessMap, material.metalnessMap, ]); } if (rotates) { Tankopedia.mutate((draft) => { draft.shot = undefined; draft.highlightArmor = undefined; }); pitchAngle.current = modelTransformEvent.last!.pitch; yawAngle.current = modelTransformEvent.last!.yaw; } position.current.set(event.clientX, event.clientY); controlsEnabledEvent.dispatch(false); window.addEventListener("pointermove", handlePointerMove); window.addEventListener("pointerup", handlePointerUp); } useEffect(() => fadeEmissive(materials), [materials]); return ( {groups.map(({ material, items }) => ( ))} {parts.map(({ part, matrix }) => ( ))} ); } export function TankModel() { const protagonist = Duel.use((draft) => draft.protagonist); const track = Duel.use((state) => state.protagonist.track); const turret = Duel.use((state) => state.protagonist.turret); const hullContainer = useRef(null); const turretContainer = useRef(null!); const gunContainer = useRef(null!); const nodes = useModelNodes(); const skinGltf = useSkin(protagonist.tank.id); const attachmentGltf = useSkin(protagonist.tank.id, "attachment"); const skins = useMemo( () => [skinGltf, attachmentGltf].filter((skin) => skin !== undefined), [skinGltf, attachmentGltf], ); const hullNodes = useMemo(() => [...nodes.hull, ...nodes.wheels], [nodes]); const gunNodes = useMemo(() => [...nodes.gun, ...nodes.mask], [nodes]); useTankTransform(track, turret, turretContainer, gunContainer); useSkinAnimation(skins); return (
); }