Files
blitzkit/packages/website/src/components/TankModel.tsx
T

371 lines
11 KiB
TypeScript
Raw Normal View History

2026-10-17 23:21:47 -05:00
import {
invalidate,
useThree,
type ObjectMap,
type ThreeEvent,
} from "@react-three/fiber";
2026-10-17 23:41:23 -05:00
import { useRef, type ComponentProps } from "react";
import type { Texture } from "three";
2026-10-17 23:21:47 -05:00
import {
BatchedMesh,
Group,
Mesh,
MeshStandardMaterial,
Object3D,
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";
import { useModel } from "../hooks/useModel";
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";
2024-01-10 13:00:37 -06:00
2026-10-17 23:41:23 -05:00
interface BatchedProps extends ComponentProps<"group"> {
slicer: (node: Object3D) => boolean;
2026-10-18 00:02:48 -05:00
mutator?: (mesh: Mesh) => void;
2026-10-17 23:41:23 -05:00
}
2026-10-18 00:02:48 -05:00
function useBatched(map: ObjectMap, shadows = false) {
2026-10-17 23:41:23 -05:00
return function Batched({ slicer, mutator, ...props }: BatchedProps) {
2026-10-17 23:21:47 -05:00
const groups = new Map<MeshStandardMaterial, Mesh[]>();
2026-10-17 23:41:23 -05:00
for (const node of Object.values(map.nodes)) {
2026-10-17 23:21:47 -05:00
if (!slicer(node)) continue;
node.traverse((object) => {
if (!(object instanceof Mesh)) return;
let material = object.material;
if (!(material instanceof MeshStandardMaterial)) return;
if (!groups.has(object.material)) {
material = object.material.clone();
groups.set(material, []);
}
groups.get(material)!.push(object.clone());
});
}
const wrapper = new Group();
for (const [material, meshes] of groups) {
let vertices = 0;
for (const mesh of meshes) {
vertices += mesh.geometry.attributes.position.count;
}
const batched = new BatchedMesh(
meshes.length,
vertices,
undefined,
material,
);
for (const mesh of meshes) {
2026-10-18 00:02:48 -05:00
if (shadows) {
mesh.castShadow = true;
mesh.receiveShadow = true;
}
mutator?.(mesh);
2026-10-17 23:21:47 -05:00
batched.add(mesh);
}
wrapper.add(batched);
}
2026-10-17 23:41:23 -05:00
return (
<group {...props}>
<primitive object={wrapper} />
</group>
);
};
2026-10-17 23:21:47 -05:00
}
2026-10-17 23:41:23 -05:00
interface SectionProps {
map: ObjectMap;
}
2026-10-17 23:21:47 -05:00
2026-10-17 23:41:23 -05:00
function Tracks({ map }: SectionProps) {
2026-10-18 00:02:48 -05:00
const Batched = useBatched(map, true);
2026-10-17 23:41:23 -05:00
const position = useRef(new Vector2());
const delta = useRef(new Vector2());
const textures = useRef(new Set<Texture | null>());
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);
const deltaX = delta.current.x / window.innerWidth;
const deltaY = delta.current.y / window.innerHeight;
const offset = deltaX + deltaY;
for (const texture of textures.current) {
if (!texture) continue;
texture.offset.y += offset;
}
invalidate();
}
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>) {
if (Tankopedia.state.display !== TankopediaDisplay.Model) {
return;
}
const material = (event.object as Mesh).material as MeshStandardMaterial;
textures.current = new Set([
material.map,
material.aoMap,
material.normalMap,
material.roughnessMap,
material.metalnessMap,
]);
position.current.set(event.clientX, event.clientY);
event.stopPropagation();
controlsEnabledEvent.dispatch(false);
window.addEventListener("pointermove", handlePointerMove);
window.addEventListener("pointerup", handlePointerUp);
}
2026-10-18 00:02:48 -05:00
return (
<Batched
onPointerDown={handlePointerDown}
slicer={(node) => {
return node.name.startsWith("chassis_track_");
}}
/>
);
}
2026-10-17 23:41:23 -05:00
2026-10-18 00:02:48 -05:00
function Hull({ map }: SectionProps) {
const Batched = useBatched(map, true);
2026-10-17 23:41:23 -05:00
2026-10-18 00:02:48 -05:00
function handlePointerDown(event: ThreeEvent<PointerEvent>) {
event.stopPropagation();
2026-10-17 23:41:23 -05:00
}
return (
<Batched
onPointerDown={handlePointerDown}
slicer={(node) => {
const isHull = node.name === "hull";
const isWheel = node.name.startsWith("chassis_wheel_");
2026-10-18 00:02:48 -05:00
return isHull || isWheel;
2026-10-17 23:41:23 -05:00
}}
2026-10-18 00:02:48 -05:00
/>
);
}
function Turret({ map }: 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 Batched = useBatched(map, true);
const position = useRef(new Vector2());
const delta = useRef(new Vector2());
const canvas = useThree((state) => state.gl.domElement);
function onPointerDown(event: ThreeEvent<PointerEvent>) {
event.stopPropagation();
position.current.set(event.clientX, event.clientY);
controlsEnabledEvent.dispatch(false);
Tankopedia.mutate((draft) => {
draft.shot = undefined;
draft.highlightArmor = undefined;
});
window.addEventListener("pointermove", handlePointerMove);
window.addEventListener("pointerup", handlePointerUp);
}
function handlePointerMove(event: PointerEvent) {
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();
delta.current.set(event.clientX, event.clientY).sub(position.current);
position.current.set(event.clientX, event.clientY);
const [pitch, yaw] = applyPitchYawLimits(
modelTransformEvent.last!.pitch,
modelTransformEvent.last!.yaw +
delta.current.x * (Math.PI / boundingRect.width),
gunModelDefinition.pitch!,
turretModelDefinition.yaw,
hasImprovedVerticalStabilizer,
hasDownImprovedVerticalStabilizer,
);
modelTransformEvent.dispatch({ pitch, yaw });
}
function handlePointerUp() {
controlsEnabledEvent.dispatch(true);
window.removeEventListener("pointermove", handlePointerMove);
window.removeEventListener("pointerup", handlePointerUp);
}
return (
<Batched
onPointerDown={onPointerDown}
slicer={(node) => {
const isCurrentTurret =
node.name ===
`turret_${turretModelDefinition.model_id
.toString()
.padStart(2, "0")}`;
return isCurrentTurret;
}}
/>
);
}
function Gun({ map }: SectionProps) {
const Batched = useBatched(map, true);
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 position = useRef(new Vector2());
const delta = useRef(new Vector2());
const pitch = useRef(0);
const yaw = useRef(0);
const canvas = useThree((state) => state.gl.domElement);
function handlePointerMove(event: PointerEvent) {
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();
delta.current.set(event.clientX, event.clientY).sub(position.current);
position.current.set(event.clientX, event.clientY);
[pitch.current, yaw.current] = applyPitchYawLimits(
pitch.current - delta.current.y * (Math.PI / boundingRect.height),
yaw.current + delta.current.x * (Math.PI / boundingRect.width),
gunModelDefinition.pitch!,
turretModelDefinition.yaw,
hasImprovedVerticalStabilizer,
hasDownImprovedVerticalStabilizer,
);
modelTransformEvent.dispatch({ pitch: pitch.current, yaw: yaw.current });
}
function handlePointerUp() {
controlsEnabledEvent.dispatch(true);
window.removeEventListener("pointermove", handlePointerMove);
window.removeEventListener("pointerup", handlePointerUp);
}
function onPointerDown(event: ThreeEvent<PointerEvent>) {
event.stopPropagation();
controlsEnabledEvent.dispatch(false);
Tankopedia.mutate((draft) => {
draft.shot = undefined;
draft.highlightArmor = undefined;
});
position.current.set(event.clientX, event.clientY);
pitch.current = modelTransformEvent.last!.pitch;
yaw.current = modelTransformEvent.last!.yaw;
window.addEventListener("pointermove", handlePointerMove);
window.addEventListener("pointerup", handlePointerUp);
}
return (
<Batched
onPointerDown={onPointerDown}
slicer={(node) => {
const isCurrentMantlet =
node.name ===
`gun_${gunModelDefinition.model_id.toString().padStart(2, "0")}_mask`;
const isCurrentGun =
node.name ===
`gun_${gunModelDefinition.model_id.toString().padStart(2, "0")}`;
return isCurrentGun || isCurrentMantlet;
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);
const track = Duel.use((state) => state.protagonist.track);
const turret = Duel.use((state) => state.protagonist.turret);
2024-01-13 18:24:04 -06:00
const canvas = useThree((state) => state.gl.domElement);
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!);
2024-07-30 13:24:55 -05:00
const tankModelDefinition = useTankModelDefinition();
2024-01-13 18:24:04 -06:00
const turretModelDefinition =
tankModelDefinition.turrets[protagonist.turret.id];
2026-10-17 23:41:23 -05:00
2024-01-22 15:42:49 -06:00
const { gltf } = useModel(protagonist.tank.id);
2024-01-10 13:00:37 -06:00
useTankTransform(track, turret, turretContainer, gunContainer);
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-10-17 23:41:23 -05:00
<Tracks map={gltf} />
2026-10-18 00:02:48 -05:00
<Hull map={gltf} />
2024-01-10 13:00:37 -06:00
2024-01-13 18:24:04 -06:00
<group ref={turretContainer}>
2026-10-18 00:02:48 -05:00
<Turret map={gltf} />
2024-01-10 13:00:37 -06:00
2024-01-13 18:24:04 -06:00
<group ref={gunContainer}>
2026-10-18 00:02:48 -05:00
<Gun map={gltf} />
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
}