2026-10-20 17:20:57 +13:00
|
|
|
import { alias } from "@blitzkit/core";
|
2026-10-22 16:12:03 +13:00
|
|
|
import { useEffect } from "react";
|
2026-10-20 18:41:01 +13:00
|
|
|
import {
|
2026-10-31 22:15:57 +13:00
|
|
|
AnimationMixer,
|
2026-10-20 18:41:01 +13:00
|
|
|
Mesh,
|
|
|
|
|
SkinnedMesh,
|
|
|
|
|
type Group,
|
|
|
|
|
type Material,
|
|
|
|
|
type Object3D,
|
|
|
|
|
} from "three";
|
2026-10-20 17:20:57 +13:00
|
|
|
import { GLTFLoader } from "three-stdlib";
|
|
|
|
|
import { TRANSITION_ANIMATION_TIME } from "../components/Tankopedia/HeroSection/components/TankSandbox/components/Lighting";
|
2026-10-31 22:15:57 +13:00
|
|
|
import { prepareMaterial } from "../core/blitzkit/jsxTree";
|
|
|
|
|
import { nameOf } from "../core/blitzkit/skinTree";
|
2026-10-20 17:20:57 +13:00
|
|
|
import { Tankopedia } from "../stores/tankopedia";
|
|
|
|
|
|
|
|
|
|
export interface Skin {
|
|
|
|
|
scene: Group;
|
|
|
|
|
materials: Map<string, Material>;
|
2026-10-20 18:41:01 +13:00
|
|
|
/** customization parts by the tank node they attach to */
|
|
|
|
|
parts: Map<string, Object3D[]>;
|
2026-10-31 22:15:57 +13:00
|
|
|
mixer?: AnimationMixer;
|
2026-10-20 17:20:57 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const skins = new Map<string, Promise<Skin>>();
|
|
|
|
|
const loadedSkins = new Map<string, Skin>();
|
|
|
|
|
|
|
|
|
|
async function loadSkin(path: string) {
|
|
|
|
|
const gltf = await new GLTFLoader().loadAsync(path);
|
|
|
|
|
const materials: Material[] = await gltf.parser.getDependencies("material");
|
|
|
|
|
const skin = {
|
|
|
|
|
scene: gltf.scene,
|
2026-10-28 20:49:14 +13:00
|
|
|
materials: new Map(
|
|
|
|
|
materials
|
|
|
|
|
.filter((material) => material.userData.camouflage)
|
|
|
|
|
.map((material) => [material.name, material]),
|
|
|
|
|
),
|
2026-10-31 22:15:57 +13:00
|
|
|
// taken now because rendering moves the parts out of the scene
|
2026-10-20 18:41:01 +13:00
|
|
|
parts: new Map(
|
|
|
|
|
gltf.scene.children.map((target) => [target.name, [...target.children]]),
|
|
|
|
|
),
|
2026-10-31 22:15:57 +13:00
|
|
|
mixer:
|
|
|
|
|
gltf.animations.length > 0 ? new AnimationMixer(gltf.scene) : undefined,
|
2026-10-20 17:20:57 +13:00
|
|
|
};
|
|
|
|
|
|
2026-10-31 22:15:57 +13:00
|
|
|
// bound now for the same reason, every exported clip loops in the garage
|
|
|
|
|
for (const clip of gltf.animations) skin.mixer!.clipAction(clip).play();
|
2026-10-20 18:41:01 +13:00
|
|
|
|
2026-10-31 22:15:57 +13:00
|
|
|
for (const parts of skin.parts.values()) {
|
2026-11-07 15:35:12 +13:00
|
|
|
for (const part of parts) preparePart(part);
|
2026-10-20 18:41:01 +13:00
|
|
|
}
|
|
|
|
|
|
2026-10-20 17:20:57 +13:00
|
|
|
loadedSkins.set(path, skin);
|
|
|
|
|
|
|
|
|
|
return skin;
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-31 22:15:57 +13:00
|
|
|
// parts render as they are, so what jsxTree and skinTree would do happens here
|
2026-11-07 15:35:12 +13:00
|
|
|
function preparePart(part: Object3D) {
|
2026-10-31 22:15:57 +13:00
|
|
|
part.traverse((object) => {
|
|
|
|
|
if (object.userData.state !== undefined) {
|
|
|
|
|
for (const child of object.children) {
|
2026-11-07 15:35:12 +13:00
|
|
|
child.visible = nameOf(child) === object.userData.state;
|
2026-10-20 17:20:57 +13:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-31 22:15:57 +13:00
|
|
|
if (!(object instanceof Mesh)) return;
|
2026-10-20 17:20:57 +13:00
|
|
|
|
2026-10-31 22:15:57 +13:00
|
|
|
object.castShadow = true;
|
|
|
|
|
object.receiveShadow = true;
|
|
|
|
|
// bounds are from the bind pose
|
|
|
|
|
if (object instanceof SkinnedMesh) object.frustumCulled = false;
|
|
|
|
|
|
|
|
|
|
prepareMaterial(object.material);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-22 16:12:03 +13:00
|
|
|
function skinPath(id: number, skin: number) {
|
2026-10-21 22:59:12 -05:00
|
|
|
return alias("models_legendary", `/${id}/skins/${skin}.glb`);
|
2026-10-22 16:12:03 +13:00
|
|
|
}
|
|
|
|
|
|
2026-10-28 20:49:14 +13:00
|
|
|
// attachments load the same way as camouflages and can be shown with one
|
2026-10-30 07:11:31 +13:00
|
|
|
export function useSkin(id: number, loader: "skin" | "attachment" = "skin") {
|
|
|
|
|
const requested = Tankopedia.use((state) => state.loaders[loader].requested);
|
|
|
|
|
const loaded = Tankopedia.use((state) => state.loaders[loader].loaded);
|
2026-10-22 16:12:03 +13:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
2026-10-30 07:11:31 +13:00
|
|
|
if (requested === Tankopedia.state.loaders[loader].loaded) return;
|
|
|
|
|
|
|
|
|
|
const path = requested === undefined ? undefined : skinPath(id, requested);
|
|
|
|
|
const cached = path === undefined || loadedSkins.has(path);
|
2026-10-22 16:12:03 +13:00
|
|
|
let settled = false;
|
|
|
|
|
|
2026-10-30 07:11:31 +13:00
|
|
|
if (!cached) {
|
2026-10-22 16:12:03 +13:00
|
|
|
Tankopedia.mutate((draft) => {
|
2026-10-30 07:11:31 +13:00
|
|
|
draft.revealed = false;
|
2026-10-22 16:12:03 +13:00
|
|
|
});
|
2026-10-30 07:11:31 +13:00
|
|
|
|
|
|
|
|
if (!skins.has(path)) skins.set(path, loadSkin(path));
|
2026-10-20 17:20:57 +13:00
|
|
|
}
|
|
|
|
|
|
2026-10-30 07:11:31 +13:00
|
|
|
// swaps halfway through the lighting transition
|
2026-10-20 17:20:57 +13:00
|
|
|
Promise.all([
|
2026-10-30 07:11:31 +13:00
|
|
|
path && skins.get(path),
|
2026-10-20 17:20:57 +13:00
|
|
|
new Promise((resolve) =>
|
|
|
|
|
setTimeout(resolve, TRANSITION_ANIMATION_TIME * 1e3),
|
|
|
|
|
),
|
2026-10-22 16:12:03 +13:00
|
|
|
]).then(() => {
|
2026-10-20 17:20:57 +13:00
|
|
|
if (settled) return;
|
|
|
|
|
|
|
|
|
|
settled = true;
|
|
|
|
|
Tankopedia.mutate((draft) => {
|
2026-10-30 07:11:31 +13:00
|
|
|
draft.loaders[loader].loaded = requested;
|
|
|
|
|
if (!cached) draft.revealed = true;
|
2026-10-20 17:20:57 +13:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
|
if (settled) return;
|
|
|
|
|
|
|
|
|
|
settled = true;
|
2026-10-30 07:11:31 +13:00
|
|
|
|
|
|
|
|
if (cached) return;
|
|
|
|
|
|
2026-10-20 17:20:57 +13:00
|
|
|
Tankopedia.mutate((draft) => {
|
|
|
|
|
draft.revealed = true;
|
|
|
|
|
});
|
|
|
|
|
};
|
2026-10-30 07:11:31 +13:00
|
|
|
}, [id, loader, requested]);
|
2026-10-20 17:20:57 +13:00
|
|
|
|
2026-10-30 07:11:31 +13:00
|
|
|
return loaded === undefined
|
|
|
|
|
? undefined
|
|
|
|
|
: loadedSkins.get(skinPath(id, loaded));
|
2026-10-20 17:20:57 +13:00
|
|
|
}
|