2024-09-07 20:40:06 -05:00
|
|
|
import { asset } from '@blitzkit/core';
|
2024-09-21 20:26:58 -05:00
|
|
|
import { type ObjectMap, useLoader } from '@react-three/fiber';
|
2024-01-22 15:42:49 -06:00
|
|
|
import { Mesh, MeshStandardMaterial } from 'three';
|
2024-09-21 20:26:58 -05:00
|
|
|
import { type GLTF, GLTFLoader } from 'three-stdlib';
|
2024-10-30 10:42:52 -05:00
|
|
|
import { DRACOLoader } from 'three/examples/jsm/loaders/DRACOLoader.js';
|
2024-01-16 13:16:06 -06:00
|
|
|
|
2024-01-22 15:42:49 -06:00
|
|
|
const cache: Record<
|
|
|
|
|
number,
|
|
|
|
|
{
|
|
|
|
|
gltf: GLTF & ObjectMap;
|
|
|
|
|
hasPbr: boolean;
|
|
|
|
|
}
|
|
|
|
|
> = {};
|
|
|
|
|
|
2024-01-16 13:16:06 -06:00
|
|
|
export function useModel(id: number) {
|
2024-10-29 16:21:44 -05:00
|
|
|
const gltf = useLoader(
|
|
|
|
|
GLTFLoader,
|
|
|
|
|
asset(`3d/tanks/models/${id}.glb`),
|
|
|
|
|
(loader) => {
|
|
|
|
|
const dracoLoader = new DRACOLoader();
|
|
|
|
|
dracoLoader.setDecoderPath('https://www.gstatic.com/draco/v1/decoders/');
|
2024-10-30 10:42:52 -05:00
|
|
|
// @ts-ignore
|
2024-10-29 16:21:44 -05:00
|
|
|
loader.setDRACOLoader(dracoLoader);
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
2024-01-22 15:42:49 -06:00
|
|
|
if (!cache[id]) {
|
|
|
|
|
cache[id] = {
|
|
|
|
|
gltf,
|
|
|
|
|
hasPbr: Object.values(gltf.nodes).some(
|
|
|
|
|
(node) =>
|
|
|
|
|
node instanceof Mesh &&
|
|
|
|
|
node.material instanceof MeshStandardMaterial &&
|
|
|
|
|
node.material.roughnessMap !== null,
|
|
|
|
|
),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
return cache[id];
|
2024-01-16 13:16:06 -06:00
|
|
|
}
|