2026-11-29 21:57:28 -06:00
|
|
|
import { invalidate, useThree, type ThreeEvent } from "@react-three/fiber";
|
2026-11-29 17:46:24 -06:00
|
|
|
import type { QuicklimeEvent } from "quicklime";
|
2026-11-29 21:57:28 -06:00
|
|
|
import { useEffect, useMemo, useRef } from "react";
|
|
|
|
|
import type { Object3D, Texture } from "three";
|
2026-10-18 00:10:24 -05:00
|
|
|
import { Group, Mesh, MeshStandardMaterial, Vector2 } from "three";
|
2026-10-18 00:02:48 -05:00
|
|
|
import { applyPitchYawLimits } from "../core/blitz/applyPitchYawLimits";
|
|
|
|
|
import { hasEquipment } from "../core/blitzkit/hasEquipment";
|
|
|
|
|
import { modelTransformEvent } from "../core/blitzkit/modelTransform";
|
2026-10-17 23:41:23 -05:00
|
|
|
import { controlsEnabledEvent } from "../core/controlsEnabled";
|
2026-11-29 21:57:28 -06:00
|
|
|
import { useBatchGroups } from "../hooks/useBatchGroups";
|
|
|
|
|
import { useModelNodes } from "../hooks/useModelNodes";
|
2026-11-29 17:46:24 -06:00
|
|
|
import { useSkin, type Skin } from "../hooks/useSkin";
|
|
|
|
|
import { useSkinAnimation } from "../hooks/useSkinAnimation";
|
2026-10-17 23:41:23 -05:00
|
|
|
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";
|
2026-11-29 21:57:28 -06:00
|
|
|
import { Batch } from "./Batch";
|
2026-11-29 17:46:24 -06:00
|
|
|
import { transitionEvent } from "./Tankopedia/HeroSection/components/TankSandbox/components/Lighting";
|
2024-01-10 13:00:37 -06:00
|
|
|
|
2026-10-17 23:41:23 -05:00
|
|
|
interface SectionProps {
|
2026-11-29 21:57:28 -06:00
|
|
|
nodes: Object3D[];
|
2026-11-29 17:46:24 -06:00
|
|
|
skins: Skin[];
|
2026-11-29 21:57:28 -06:00
|
|
|
// dragging scrolls the track textures
|
|
|
|
|
scrollTracks?: boolean;
|
|
|
|
|
// dragging turns the turret
|
|
|
|
|
yaw?: boolean;
|
|
|
|
|
// dragging raises and lowers the gun
|
|
|
|
|
pitch?: boolean;
|
2026-10-17 23:41:23 -05:00
|
|
|
}
|
2026-10-17 23:21:47 -05:00
|
|
|
|
2026-11-29 17:46:24 -06:00
|
|
|
// emissive parts fade with the lights instead of glowing in the dark
|
|
|
|
|
function fadeEmissive(materials: MeshStandardMaterial[]) {
|
|
|
|
|
const emissiveMaterials = materials.filter(
|
2026-11-29 21:57:28 -06:00
|
|
|
(material) => material.emissiveMap,
|
2026-11-29 17:46:24 -06:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
function applyIntensity(intensity: number) {
|
|
|
|
|
for (const material of emissiveMaterials) {
|
|
|
|
|
material.emissiveIntensity = intensity;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function handleTransitionEvent(event: QuicklimeEvent<number>) {
|
|
|
|
|
applyIntensity(event.data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
applyIntensity(transitionEvent.last!);
|
|
|
|
|
transitionEvent.on(handleTransitionEvent);
|
|
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
|
transitionEvent.off(handleTransitionEvent);
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-29 21:57:28 -06:00
|
|
|
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);
|
2026-10-17 23:41:23 -05:00
|
|
|
|
|
|
|
|
const position = useRef(new Vector2());
|
|
|
|
|
const delta = useRef(new Vector2());
|
2026-11-29 21:57:28 -06:00
|
|
|
const pitchAngle = useRef(0);
|
|
|
|
|
const yawAngle = useRef(0);
|
2026-10-17 23:41:23 -05:00
|
|
|
const textures = useRef(new Set<Texture | null>());
|
|
|
|
|
|
2026-11-29 21:57:28 -06:00
|
|
|
const canvas = useThree((state) => state.gl.domElement);
|
|
|
|
|
const rotates = yaw || pitch;
|
|
|
|
|
|
2026-10-18 00:02:48 -05:00
|
|
|
function handlePointerMove(event: PointerEvent) {
|
|
|
|
|
delta.current.set(event.clientX, event.clientY).sub(position.current);
|
|
|
|
|
position.current.set(event.clientX, event.clientY);
|
|
|
|
|
|
2026-11-29 21:57:28 -06:00
|
|
|
if (scrollTracks) {
|
|
|
|
|
const offset =
|
|
|
|
|
delta.current.x / window.innerWidth +
|
|
|
|
|
delta.current.y / window.innerHeight;
|
2026-10-18 00:02:48 -05:00
|
|
|
|
2026-11-29 21:57:28 -06:00
|
|
|
for (const texture of textures.current) {
|
|
|
|
|
if (!texture) continue;
|
|
|
|
|
texture.offset.y += offset;
|
|
|
|
|
}
|
2026-10-18 00:02:48 -05:00
|
|
|
|
2026-11-29 21:57:28 -06:00
|
|
|
invalidate();
|
2026-10-18 00:02:48 -05:00
|
|
|
}
|
|
|
|
|
|
2026-11-29 21:57:28 -06:00
|
|
|
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,
|
|
|
|
|
});
|
|
|
|
|
}
|
2026-10-18 00:02:48 -05:00
|
|
|
}
|
|
|
|
|
function handlePointerUp() {
|
|
|
|
|
controlsEnabledEvent.dispatch(true);
|
|
|
|
|
|
|
|
|
|
window.removeEventListener("pointermove", handlePointerMove);
|
|
|
|
|
window.removeEventListener("pointerup", handlePointerUp);
|
|
|
|
|
}
|
2026-10-17 23:41:23 -05:00
|
|
|
function handlePointerDown(event: ThreeEvent<PointerEvent>) {
|
2026-11-29 21:57:28 -06:00
|
|
|
// tracks only scroll while looking at the model, not the armor
|
|
|
|
|
if (scrollTracks && Tankopedia.state.display !== TankopediaDisplay.Model) {
|
2026-10-17 23:41:23 -05:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
event.stopPropagation();
|
|
|
|
|
|
2026-11-29 21:57:28 -06:00
|
|
|
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);
|
2026-10-17 23:41:23 -05:00
|
|
|
controlsEnabledEvent.dispatch(false);
|
|
|
|
|
|
|
|
|
|
window.addEventListener("pointermove", handlePointerMove);
|
|
|
|
|
window.addEventListener("pointerup", handlePointerUp);
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-29 21:57:28 -06:00
|
|
|
useEffect(() => fadeEmissive(materials), [materials]);
|
2026-10-17 23:41:23 -05:00
|
|
|
|
|
|
|
|
return (
|
2026-11-29 21:57:28 -06:00
|
|
|
<group onPointerDown={handlePointerDown}>
|
|
|
|
|
{groups.map(({ material, items }) => (
|
|
|
|
|
<Batch
|
|
|
|
|
key={material.uuid}
|
|
|
|
|
items={items}
|
|
|
|
|
material={material}
|
|
|
|
|
castShadow
|
|
|
|
|
receiveShadow
|
|
|
|
|
/>
|
|
|
|
|
))}
|
2026-10-17 23:41:23 -05:00
|
|
|
|
2026-11-29 21:57:28 -06:00
|
|
|
{parts.map(({ part, matrix }) => (
|
|
|
|
|
<group key={part.uuid} matrix={matrix} matrixAutoUpdate={false}>
|
|
|
|
|
<primitive object={part} />
|
|
|
|
|
</group>
|
|
|
|
|
))}
|
|
|
|
|
</group>
|
2026-10-17 23:41:23 -05:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function TankModel() {
|
2024-07-10 13:11:09 -05:00
|
|
|
const protagonist = Duel.use((draft) => draft.protagonist);
|
2026-10-18 00:10:24 -05:00
|
|
|
|
2024-11-10 21:54:20 -06:00
|
|
|
const track = Duel.use((state) => state.protagonist.track);
|
|
|
|
|
const turret = Duel.use((state) => state.protagonist.turret);
|
2026-10-18 00:10:24 -05:00
|
|
|
|
2024-01-11 21:44:44 -06:00
|
|
|
const hullContainer = useRef<Group>(null);
|
2025-09-27 16:07:45 -05:00
|
|
|
const turretContainer = useRef<Group>(null!);
|
|
|
|
|
const gunContainer = useRef<Group>(null!);
|
2026-10-17 23:41:23 -05:00
|
|
|
|
2026-11-29 21:57:28 -06:00
|
|
|
const nodes = useModelNodes();
|
2026-11-29 17:46:24 -06:00
|
|
|
const skinGltf = useSkin(protagonist.tank.id);
|
|
|
|
|
const attachmentGltf = useSkin(protagonist.tank.id, "attachment");
|
2026-11-29 21:57:28 -06:00
|
|
|
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]);
|
2024-01-10 13:00:37 -06:00
|
|
|
|
2024-11-10 21:54:20 -06:00
|
|
|
useTankTransform(track, turret, turretContainer, gunContainer);
|
2026-11-29 17:46:24 -06:00
|
|
|
useSkinAnimation(skins);
|
2024-07-30 13:24:55 -05:00
|
|
|
|
2024-01-10 13:00:37 -06:00
|
|
|
return (
|
2024-07-24 20:46:42 -05:00
|
|
|
<ModelTankWrapper ref={hullContainer}>
|
2026-11-29 21:57:28 -06:00
|
|
|
<Section nodes={nodes.tracks} skins={skins} scrollTracks />
|
|
|
|
|
<Section nodes={hullNodes} skins={skins} />
|
2024-01-10 13:00:37 -06:00
|
|
|
|
2024-01-13 18:24:04 -06:00
|
|
|
<group ref={turretContainer}>
|
2026-11-29 21:57:28 -06:00
|
|
|
<Section nodes={nodes.turret} skins={skins} yaw />
|
2024-01-10 13:00:37 -06:00
|
|
|
|
2024-01-13 18:24:04 -06:00
|
|
|
<group ref={gunContainer}>
|
2026-11-29 21:57:28 -06:00
|
|
|
<Section nodes={gunNodes} skins={skins} yaw pitch />
|
2024-01-10 13:00:37 -06:00
|
|
|
</group>
|
2026-10-18 00:02:48 -05:00
|
|
|
</group>
|
2024-07-24 20:46:42 -05:00
|
|
|
</ModelTankWrapper>
|
2024-01-10 13:00:37 -06:00
|
|
|
);
|
2024-12-12 18:32:27 -06:00
|
|
|
}
|