icy3dw/src/client/object/resource/pony-loader.ts

62 lines
1.9 KiB
TypeScript
Raw Normal View History

2022-04-09 11:29:54 +00:00
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader';
import { DRACOLoader } from 'three/examples/jsm/loaders/DRACOLoader';
2022-04-16 06:50:46 +00:00
import { LoadingManagerWrapper } from './loading-manager';
2022-04-09 11:29:54 +00:00
// Instantiate a loader
2022-04-16 06:50:46 +00:00
const loader = new GLTFLoader(LoadingManagerWrapper.getInstance().manager);
2022-04-09 11:29:54 +00:00
// Optional: Provide a DRACOLoader instance to decode compressed mesh data
2022-04-16 06:50:46 +00:00
const dracoLoader = new DRACOLoader(
LoadingManagerWrapper.getInstance().manager,
);
2022-04-09 11:29:54 +00:00
dracoLoader.setDecoderPath('/examples/js/libs/draco/');
loader.setDRACOLoader(dracoLoader);
2022-04-12 15:24:05 +00:00
let instance: PonyModelLoader;
2022-04-11 19:57:54 +00:00
2022-04-12 15:24:05 +00:00
export class PonyModelLoader {
2022-04-09 11:29:54 +00:00
public ponyModel!: THREE.Group;
public animations!: THREE.AnimationClip[];
2022-04-16 10:21:07 +00:00
initialize(): Promise<THREE.Group> {
2022-04-09 11:29:54 +00:00
return new Promise((resolve, reject) => {
// Load a glTF resource
loader.load(
// resource URL
'/assets/clean-pony2fix.glb',
// called when the resource is loaded
(gltf) => {
this.ponyModel = gltf.scene;
this.animations = gltf.animations;
2022-04-10 06:03:31 +00:00
// temp: disable frustum culling for eyes
this.ponyModel.children[0].children[2].frustumCulled = false;
2022-04-09 11:29:54 +00:00
resolve(gltf.scene);
// gltf.animations; // Array<THREE.AnimationClip>
// gltf.scene; // THREE.Group
// gltf.scenes; // Array<THREE.Group>
// gltf.cameras; // Array<THREE.Camera>
// gltf.asset; // Object
},
// called while loading is progressing
(xhr) => {
console.log((xhr.loaded / xhr.total) * 100 + '% loaded');
},
// called when loading has errors
(error) => {
console.log('An error happened');
reject(error);
},
);
});
}
2022-04-12 15:24:05 +00:00
public static getInstance(): PonyModelLoader {
2022-04-11 19:57:54 +00:00
if (!instance) {
2022-04-12 15:24:05 +00:00
instance = new PonyModelLoader();
2022-04-11 19:57:54 +00:00
}
return instance;
}
}