12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- import { Box3, Color, MeshStandardMaterial, Object3D, Vector3 } from "three";
- import { GLTFLoader } from "three/examples/jsm/Addons.js";
- const gltfLoader = new GLTFLoader().setPath("/static/models/");
- const normalized = async (model: Object3D) => {
- const parent = new Object3D();
- parent.add(model);
- const bbox = new Box3().setFromObject(parent);
- const size = bbox.getSize(new Vector3());
- parent.scale.set(1 / size.x, 1 / size.y, 1 / size.z);
- model.traverse((child: any) => {
- if (child.isMesh) {
- child.receiveShadow = true;
- child.castShadow = true;
- }
- });
- const center = new Box3().setFromObject(parent).getCenter(new Vector3());
- parent.position.sub({ x: center.x, y: center.y, z: center.z });
- return parent;
- };
- const resources: Record<string, () => Promise<Object3D>> = {
- "men_l.svg": async () => {
- const gltf = await gltfLoader.loadAsync("door_with_frame/scene.gltf");
- return await normalized(gltf.scene);
- },
- "piaochuang.svg": async () => {
- const gltf = await gltfLoader.loadAsync("window/scene.gltf");
- // gltf.scene.rotateY(Math.PI / 2);
- gltf.scene.traverse((node: any) => {
- if (node.name === '02') {
- node.parent.remove(node)
- }
- })
- return await normalized(gltf.scene);
- },
- "chuang.svg": () => {
- return getModel('piaochuang.svg') as Promise<Object3D>
- }
- };
- export const getModel = (() => {
- const typeModels: Record<string, Promise<Object3D | undefined>> = {};
- return (type: string) => {
- const ndx = type.lastIndexOf("/");
- if (~ndx) {
- type = type.substring(ndx + 1);
- }
- if (type in typeModels) {
- return typeModels[type];
- }
- if (type in resources) {
- typeModels[type] = resources[type]();
- typeModels[type].catch(() => {
- delete typeModels[type];
- });
- return typeModels[type];
- }
- };
- })();
|