mirror of
https://github.com/blitzkit/blitzkit.git
synced 2026-12-01 05:43:22 +01:00
Feature: Legendary camouflages in the Tankopedia viewer
This commit is contained in:
1 parent
05648ea040
commit
85e03d3b42
17 files changed
+897
-172
No files matched your search
@@ -0,0 +1,85 @@
|
||||
import { alias } from "@blitzkit/core";
|
||||
import { useEffect, useRef, useState } from "react";
|
||||
import type { Group, Material } from "three";
|
||||
import { GLTFLoader } from "three-stdlib";
|
||||
import { TRANSITION_ANIMATION_TIME } from "../components/Tankopedia/HeroSection/components/TankSandbox/components/Lighting";
|
||||
import { Tankopedia } from "../stores/tankopedia";
|
||||
|
||||
export interface Skin {
|
||||
scene: Group;
|
||||
materials: Map<string, Material>;
|
||||
}
|
||||
|
||||
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,
|
||||
materials: new Map(materials.map((material) => [material.name, material])),
|
||||
};
|
||||
|
||||
loadedSkins.set(path, skin);
|
||||
|
||||
return skin;
|
||||
}
|
||||
|
||||
function skinPath(id: number, skin: number) {
|
||||
return alias("api", `/tanks/${id}/skins/${skin}.glb`);
|
||||
}
|
||||
|
||||
export function useSkin(id: number, skin?: number) {
|
||||
const [loaded, setLoaded] = useState<Skin>();
|
||||
const mounted = useRef(false);
|
||||
|
||||
useEffect(() => {
|
||||
const path = skin === undefined ? undefined : skinPath(id, skin);
|
||||
const cached = path === undefined ? undefined : loadedSkins.get(path);
|
||||
let settled = false;
|
||||
|
||||
if (!mounted.current) {
|
||||
mounted.current = true;
|
||||
|
||||
if (path === undefined || cached) {
|
||||
setLoaded(cached);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
Tankopedia.mutate((draft) => {
|
||||
draft.revealed = false;
|
||||
});
|
||||
|
||||
if (path !== undefined && !skins.has(path)) {
|
||||
skins.set(path, loadSkin(path));
|
||||
}
|
||||
|
||||
Promise.all([
|
||||
path === undefined ? undefined : skins.get(path),
|
||||
new Promise((resolve) =>
|
||||
setTimeout(resolve, TRANSITION_ANIMATION_TIME * 1e3),
|
||||
),
|
||||
]).then(([loaded]) => {
|
||||
if (settled) return;
|
||||
|
||||
settled = true;
|
||||
setLoaded(loaded);
|
||||
Tankopedia.mutate((draft) => {
|
||||
draft.revealed = true;
|
||||
});
|
||||
});
|
||||
|
||||
return () => {
|
||||
if (settled) return;
|
||||
|
||||
settled = true;
|
||||
Tankopedia.mutate((draft) => {
|
||||
draft.revealed = true;
|
||||
});
|
||||
};
|
||||
}, [id, skin]);
|
||||
|
||||
return loaded;
|
||||
}
|
||||
Reference in new issue
Block a user