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

334 lines
10 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-11-29 17:46:24 -06:00
import type { QuicklimeEvent } from "quicklime";
2026-10-18 00:10:24 -05:00
import { useRef } from "react";
2026-10-17 23:41:23 -05:00
import type { 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-10-18 00:10:24 -05:00
import { useBatched } from "../hooks/useBatched";
2026-10-17 23:41:23 -05:00
import { useModel } from "../hooks/useModel";
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 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 {
map: ObjectMap;
2026-11-29 17:46:24 -06:00
skins: Skin[];
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(
(material) => material.emissiveMap
);
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);
};
}
function Tracks({ map, skins }: SectionProps) {
const Batched = useBatched(map, true, skins);
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
2026-11-29 17:46:24 -06:00
onMaterials={fadeEmissive}
2026-10-18 00:02:48 -05:00
onPointerDown={handlePointerDown}
slicer={(node) => {
return node.name.startsWith("chassis_track_");
}}
/>
);
}
2026-10-17 23:41:23 -05:00
2026-11-29 17:46:24 -06:00
function Hull({ map, skins }: SectionProps) {
const Batched = useBatched(map, true, skins);
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
2026-11-29 17:46:24 -06:00
onMaterials={fadeEmissive}
2026-10-17 23:41:23 -05:00
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
/>
);
}
2026-11-29 17:46:24 -06:00
function Turret({ map, skins }: SectionProps) {
2026-10-18 00:02:48 -05:00
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];
2026-11-29 17:46:24 -06:00
const Batched = useBatched(map, true, skins);
2026-10-18 00:02:48 -05:00
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,
2026-11-29 17:46:24 -06:00
Duel.state.protagonist.equipmentMatrix
2026-10-18 00:02:48 -05:00
);
const hasDownImprovedVerticalStabilizer = hasEquipment(
124,
Duel.state.protagonist.tank.equipment_preset,
2026-11-29 17:46:24 -06:00
Duel.state.protagonist.equipmentMatrix
2026-10-18 00:02:48 -05:00
);
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,
2026-11-29 17:46:24 -06:00
hasDownImprovedVerticalStabilizer
2026-10-18 00:02:48 -05:00
);
modelTransformEvent.dispatch({ pitch, yaw });
}
function handlePointerUp() {
controlsEnabledEvent.dispatch(true);
window.removeEventListener("pointermove", handlePointerMove);
window.removeEventListener("pointerup", handlePointerUp);
}
return (
<Batched
2026-11-29 17:46:24 -06:00
onMaterials={fadeEmissive}
2026-10-18 00:02:48 -05:00
onPointerDown={onPointerDown}
slicer={(node) => {
const isCurrentTurret =
node.name ===
`turret_${turretModelDefinition.model_id
.toString()
.padStart(2, "0")}`;
return isCurrentTurret;
}}
/>
);
}
2026-11-29 17:46:24 -06:00
function Gun({ map, skins }: SectionProps) {
const Batched = useBatched(map, true, skins);
2026-10-18 00:02:48 -05:00
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,
2026-11-29 17:46:24 -06:00
Duel.state.protagonist.equipmentMatrix
2026-10-18 00:02:48 -05:00
);
const hasDownImprovedVerticalStabilizer = hasEquipment(
124,
Duel.state.protagonist.tank.equipment_preset,
2026-11-29 17:46:24 -06:00
Duel.state.protagonist.equipmentMatrix
2026-10-18 00:02:48 -05:00
);
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,
2026-11-29 17:46:24 -06:00
hasDownImprovedVerticalStabilizer
2026-10-18 00:02:48 -05:00
);
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
2026-11-29 17:46:24 -06:00
onMaterials={fadeEmissive}
2026-10-18 00:02:48 -05:00
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);
2026-10-18 00:10:24 -05: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
2024-01-22 15:42:49 -06:00
const { gltf } = useModel(protagonist.tank.id);
2026-11-29 17:46:24 -06:00
const skinGltf = useSkin(protagonist.tank.id);
const attachmentGltf = useSkin(protagonist.tank.id, "attachment");
const skins = [skinGltf, attachmentGltf].filter((skin) => skin !== undefined);
2024-01-10 13:00:37 -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 17:46:24 -06:00
<Tracks map={gltf} skins={skins} />
<Hull map={gltf} skins={skins} />
2024-01-10 13:00:37 -06:00
2024-01-13 18:24:04 -06:00
<group ref={turretContainer}>
2026-11-29 17:46:24 -06:00
<Turret map={gltf} skins={skins} />
2024-01-10 13:00:37 -06:00
2024-01-13 18:24:04 -06:00
<group ref={gunContainer}>
2026-11-29 17:46:24 -06:00
<Gun map={gltf} skins={skins} />
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
}