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

416 lines
13 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-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;
mutator: (mesh: Mesh) => void;
}
function useBatched(map: ObjectMap) {
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) {
mutator(mesh);
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 Hull({ map }: SectionProps) {
const Batched = useBatched(map);
function handlePointerDown(event: ThreeEvent<PointerEvent>) {
// event.stopPropagation();
}
2026-10-17 23:21:47 -05:00
return (
2026-10-17 23:41:23 -05:00
<Batched
onPointerDown={handlePointerDown}
slicer={(node) => {
const isHull = node.name === "hull";
const isWheel = node.name.startsWith("chassis_wheel_");
const isTrack = node.name.startsWith("chassis_track_");
2026-10-17 23:21:47 -05:00
2026-10-17 23:41:23 -05:00
return isHull || isWheel || isTrack;
}}
mutator={(mesh) => {
mesh.castShadow = true;
mesh.receiveShadow = true;
}}
/>
2026-10-17 23:21:47 -05:00
);
}
2026-10-17 23:41:23 -05:00
function Tracks({ map }: SectionProps) {
const Batched = useBatched(map);
const position = useRef(new Vector2());
const delta = useRef(new Vector2());
const textures = useRef(new Set<Texture | null>());
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);
}
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);
}
return (
<Batched
onPointerDown={handlePointerDown}
slicer={(node) => {
const isHull = node.name === "hull";
const isWheel = node.name.startsWith("chassis_wheel_");
const isTrack = node.name.startsWith("chassis_track_");
return isHull || isWheel || isTrack;
}}
mutator={(mesh) => {
mesh.castShadow = true;
mesh.receiveShadow = true;
}}
/>
);
}
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];
2025-08-18 12:06:14 -05:00
const gunModelDefinition = turretModelDefinition.guns[protagonist.gun.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
<Hull map={gltf} />
<Tracks map={gltf} />
{/* {nodes.map((node) => {
2025-09-01 22:56:39 -05:00
const isHull = node.name === "hull";
const isWheel = node.name.startsWith("chassis_wheel_");
const isTrack = node.name.startsWith("chassis_track_");
2024-01-10 13:00:37 -06:00
const isVisible = isHull || isWheel || isTrack;
2024-06-29 13:15:24 -05:00
const position = new Vector2();
const delta = new Vector2();
2024-01-10 13:00:37 -06:00
if (!isVisible) return null;
2024-01-11 21:44:44 -06:00
2024-06-29 13:15:24 -05:00
function translateTexture(offset: number) {
const mesh = node.children[0] as Mesh;
const material = mesh?.material as MeshStandardMaterial;
if (!material) return;
2024-11-10 22:19:05 -06:00
invalidate();
2024-08-28 18:02:20 -05:00
2026-10-04 20:00:51 +13:00
const textures = new Set([
material.map,
material.aoMap,
material.normalMap,
material.roughnessMap,
material.metalnessMap,
]);
for (const texture of textures) {
if (texture) texture.offset.y += offset;
}
2024-06-29 13:15:24 -05:00
}
2025-04-15 00:11:49 -05:00
return jsxTree(node, {
mesh(_, props, key) {
return (
<mesh
{...props}
key={key}
onPointerDown={onPointerDown}
castShadow
2025-08-28 16:52:23 -05:00
receiveShadow
2025-04-15 00:11:49 -05:00
/>
);
2024-04-21 11:09:24 -05:00
},
2025-04-15 00:11:49 -05:00
});
2024-01-10 13:00:37 -06:00
})}
2024-01-13 18:24:04 -06:00
<group ref={turretContainer}>
2024-01-10 13:00:37 -06:00
{nodes.map((node) => {
2025-09-01 22:56:39 -05:00
const isTurret = node.name.startsWith("turret_");
2024-01-10 13:00:37 -06:00
const isCurrentTurret =
node.name ===
2025-11-26 21:10:03 -06:00
`turret_${turretModelDefinition.model_id
.toString()
.padStart(2, "0")}`;
2024-01-10 13:00:37 -06:00
const isVisible = isCurrentTurret;
2024-06-05 13:28:45 -05:00
const position = new Vector2();
const delta = new Vector2();
2024-02-22 12:20:29 -06:00
if (!isVisible) return null;
function onPointerDown(event: ThreeEvent<PointerEvent>) {
2024-01-10 13:00:37 -06:00
event.stopPropagation();
2025-08-06 12:23:57 -05:00
if (!isTurret) return;
2024-01-10 13:00:37 -06:00
2024-08-01 20:38:37 -05:00
position.set(event.clientX, event.clientY);
2025-10-02 10:19:50 -05:00
controlsEnabledEvent.dispatch(false);
2025-09-01 22:56:39 -05:00
Tankopedia.mutate((draft) => {
2024-08-01 20:38:37 -05:00
draft.shot = undefined;
draft.highlightArmor = undefined;
});
2025-09-01 22:56:39 -05:00
window.addEventListener("pointermove", handlePointerMove);
window.addEventListener("pointerup", handlePointerUp);
2024-01-10 13:00:37 -06:00
}
2025-08-29 13:42:44 -05:00
function handlePointerMove(event: PointerEvent) {
const hasImprovedVerticalStabilizer = hasEquipment(
2024-04-04 13:16:12 -05:00
122,
2025-09-02 16:24:13 -05:00
Duel.state.protagonist.tank.equipment_preset,
2026-03-31 14:11:45 -05:00
Duel.state.protagonist.equipmentMatrix,
2024-04-04 13:16:12 -05:00
);
2025-08-29 13:42:44 -05:00
const hasDownImprovedVerticalStabilizer = hasEquipment(
2025-03-11 10:13:02 -05:00
124,
2025-09-02 16:24:13 -05:00
Duel.state.protagonist.tank.equipment_preset,
2026-03-31 14:11:45 -05:00
Duel.state.protagonist.equipmentMatrix,
2025-03-11 10:13:02 -05:00
);
2024-06-04 17:37:59 -05:00
const boundingRect = canvas.getBoundingClientRect();
2024-06-05 13:28:45 -05:00
delta.set(event.clientX, event.clientY).sub(position);
position.set(event.clientX, event.clientY);
2025-08-12 13:43:11 -05:00
const [pitch, yaw] = applyPitchYawLimits(
modelTransformEvent.last!.pitch,
modelTransformEvent.last!.yaw +
delta.x * (Math.PI / boundingRect.width),
2026-03-31 14:11:45 -05:00
gunModelDefinition.pitch!,
2024-01-10 13:00:37 -06:00
turretModelDefinition.yaw,
hasImprovedVerticalStabilizer,
2026-03-31 14:11:45 -05:00
hasDownImprovedVerticalStabilizer,
2024-01-10 13:00:37 -06:00
);
2025-08-10 20:36:48 -05:00
modelTransformEvent.dispatch({ pitch, yaw });
2024-01-10 13:00:37 -06:00
}
function handlePointerUp() {
2025-10-02 10:19:50 -05:00
controlsEnabledEvent.dispatch(true);
2025-09-01 22:56:39 -05:00
window.removeEventListener("pointermove", handlePointerMove);
window.removeEventListener("pointerup", handlePointerUp);
2024-01-10 13:00:37 -06:00
}
2025-04-15 00:11:49 -05:00
return jsxTree(node, {
mesh(_, props, key) {
return (
<mesh
{...props}
key={key}
onPointerDown={onPointerDown}
castShadow
2025-08-28 16:52:23 -05:00
receiveShadow
2025-04-15 00:11:49 -05:00
/>
);
2024-04-21 11:09:24 -05:00
},
2025-04-15 00:11:49 -05:00
});
2024-01-10 13:00:37 -06:00
})}
2024-01-13 18:24:04 -06:00
<group ref={gunContainer}>
2024-01-10 13:00:37 -06:00
{nodes.map((node) => {
const isCurrentMantlet =
node.name ===
2024-10-07 18:45:11 -05:00
`gun_${gunModelDefinition.model_id
2024-01-10 13:00:37 -06:00
.toString()
2025-09-01 22:56:39 -05:00
.padStart(2, "0")}_mask`;
2024-01-10 13:00:37 -06:00
const isCurrentGun =
node.name ===
2025-09-01 22:56:39 -05:00
`gun_${gunModelDefinition.model_id.toString().padStart(2, "0")}`;
2024-01-10 13:00:37 -06:00
const isVisible = isCurrentGun || isCurrentMantlet;
2024-06-05 13:28:45 -05:00
const position = new Vector2();
const delta = new Vector2();
2024-02-22 12:20:29 -06:00
if (!isVisible) return null;
2024-01-13 18:24:04 -06:00
let pitch = 0;
let yaw = 0;
2024-01-10 13:00:37 -06:00
2024-02-22 12:20:29 -06:00
function onPointerDown(event: ThreeEvent<PointerEvent>) {
2024-01-10 13:00:37 -06:00
event.stopPropagation();
2025-10-02 10:19:50 -05:00
controlsEnabledEvent.dispatch(false);
2025-09-01 22:56:39 -05:00
Tankopedia.mutate((draft) => {
2024-02-02 16:08:41 -06:00
draft.shot = undefined;
2024-07-27 14:43:33 -05:00
draft.highlightArmor = undefined;
2024-02-02 16:08:41 -06:00
});
2024-06-05 13:28:45 -05:00
position.set(event.clientX, event.clientY);
2025-08-12 13:43:11 -05:00
pitch = modelTransformEvent.last!.pitch;
yaw = modelTransformEvent.last!.yaw;
2024-06-05 13:28:45 -05:00
2025-09-01 22:56:39 -05:00
window.addEventListener("pointermove", handlePointerMove);
window.addEventListener("pointerup", handlePointerUp);
2024-01-10 13:00:37 -06:00
}
2025-08-29 13:42:44 -05:00
function handlePointerMove(event: PointerEvent) {
const hasImprovedVerticalStabilizer = hasEquipment(
2024-04-04 13:16:12 -05:00
122,
2025-09-02 16:24:13 -05:00
Duel.state.protagonist.tank.equipment_preset,
2026-03-31 14:11:45 -05:00
Duel.state.protagonist.equipmentMatrix,
2024-04-04 13:16:12 -05:00
);
2025-08-29 13:42:44 -05:00
const hasDownImprovedVerticalStabilizer = hasEquipment(
2025-03-11 10:13:02 -05:00
124,
2025-09-02 16:24:13 -05:00
Duel.state.protagonist.tank.equipment_preset,
2026-03-31 14:11:45 -05:00
Duel.state.protagonist.equipmentMatrix,
2025-03-11 10:13:02 -05:00
);
2024-06-04 17:37:59 -05:00
const boundingRect = canvas.getBoundingClientRect();
2024-06-05 13:28:45 -05:00
delta.set(event.clientX, event.clientY).sub(position);
position.set(event.clientX, event.clientY);
2024-01-13 18:24:04 -06:00
[pitch, yaw] = applyPitchYawLimits(
2024-06-05 13:28:45 -05:00
pitch - delta.y * (Math.PI / boundingRect.height),
yaw + delta.x * (Math.PI / boundingRect.width),
2026-03-31 14:11:45 -05:00
gunModelDefinition.pitch!,
2024-01-10 13:00:37 -06:00
turretModelDefinition.yaw,
hasImprovedVerticalStabilizer,
2026-03-31 14:11:45 -05:00
hasDownImprovedVerticalStabilizer,
2024-01-10 13:00:37 -06:00
);
2025-08-10 20:36:48 -05:00
modelTransformEvent.dispatch({ pitch, yaw });
2024-01-10 13:00:37 -06:00
}
function handlePointerUp() {
2025-10-02 10:19:50 -05:00
controlsEnabledEvent.dispatch(true);
2025-09-01 22:56:39 -05:00
window.removeEventListener("pointermove", handlePointerMove);
window.removeEventListener("pointerup", handlePointerUp);
2024-01-10 13:00:37 -06:00
}
2025-04-15 00:11:49 -05:00
return jsxTree(node, {
mesh(_, props, key) {
return (
<mesh
{...props}
key={key}
onPointerDown={onPointerDown}
castShadow
2025-08-28 16:52:23 -05:00
receiveShadow
2025-04-15 00:11:49 -05:00
/>
);
2024-04-21 11:09:24 -05:00
},
2025-04-15 00:11:49 -05:00
});
2024-01-10 13:00:37 -06:00
})}
</group>
2026-10-17 23:41:23 -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
}