2025-05-13 11:51:52 +00:00
|
|
|
import { GLTF, GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader';
|
|
|
|
import { DRACOLoader } from 'three/examples/jsm/loaders/DRACOLoader';
|
|
|
|
import * as THREE from 'three';
|
2025-04-21 06:23:42 +00:00
|
|
|
import * as Types from "../../../types/world/worldTypes";
|
|
|
|
import { getWallItems } from '../../../services/factoryBuilder/assest/wallAsset/getWallItemsApi';
|
2025-05-13 11:51:52 +00:00
|
|
|
import { retrieveGLTF, storeGLTF } from '../../../utils/indexDB/idbUtils';
|
2025-04-21 06:23:42 +00:00
|
|
|
|
|
|
|
async function loadInitialWallItems(
|
|
|
|
setWallItems: Types.setWallItemSetState,
|
2025-06-04 06:25:59 +00:00
|
|
|
projectId?:string
|
2025-04-21 06:23:42 +00:00
|
|
|
): Promise<void> {
|
2025-05-13 11:51:52 +00:00
|
|
|
try {
|
|
|
|
const email = localStorage.getItem('email');
|
|
|
|
if (!email) {
|
|
|
|
throw new Error('No email found in localStorage');
|
|
|
|
}
|
2025-04-21 06:23:42 +00:00
|
|
|
|
2025-05-13 11:51:52 +00:00
|
|
|
const organization = email.split("@")[1].split(".")[0];
|
2025-06-04 06:25:59 +00:00
|
|
|
const items = await getWallItems(organization,projectId);
|
2025-04-21 06:23:42 +00:00
|
|
|
|
2025-05-13 11:51:52 +00:00
|
|
|
let url_Backend_dwinzo = `http://${process.env.REACT_APP_SERVER_MARKETPLACE_URL}`;
|
2025-04-21 06:23:42 +00:00
|
|
|
|
2025-05-13 11:51:52 +00:00
|
|
|
if (!items || items.length === 0) {
|
|
|
|
localStorage.removeItem("WallItems");
|
|
|
|
return;
|
|
|
|
}
|
2025-04-21 06:23:42 +00:00
|
|
|
|
2025-05-13 11:51:52 +00:00
|
|
|
localStorage.setItem("WallItems", JSON.stringify(items));
|
|
|
|
|
|
|
|
const loader = new GLTFLoader();
|
|
|
|
const dracoLoader = new DRACOLoader();
|
|
|
|
dracoLoader.setDecoderPath('https://cdn.jsdelivr.net/npm/three@0.160.0/examples/jsm/libs/draco/gltf/');
|
|
|
|
loader.setDRACOLoader(dracoLoader);
|
|
|
|
|
|
|
|
const loadedWallItems = await Promise.all(items.map(async (item: Types.WallItem) => {
|
|
|
|
// Check THREE.js cache first
|
2025-05-15 04:04:55 +00:00
|
|
|
const cachedModel = THREE.Cache.get(item.modelfileID!);
|
2025-05-13 11:51:52 +00:00
|
|
|
if (cachedModel) {
|
|
|
|
return processModel(cachedModel, item);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check IndexedDB cache
|
|
|
|
const cachedModelBlob = await retrieveGLTF(item.modelfileID!);
|
|
|
|
if (cachedModelBlob) {
|
|
|
|
const blobUrl = URL.createObjectURL(cachedModelBlob);
|
|
|
|
return new Promise<Types.WallItem>((resolve) => {
|
|
|
|
loader.load(blobUrl, (gltf) => {
|
|
|
|
URL.revokeObjectURL(blobUrl);
|
2025-05-15 04:04:55 +00:00
|
|
|
THREE.Cache.add(item.modelfileID!, gltf);
|
2025-05-13 11:51:52 +00:00
|
|
|
resolve(processModel(gltf, item));
|
|
|
|
});
|
2025-04-21 06:23:42 +00:00
|
|
|
});
|
2025-05-13 11:51:52 +00:00
|
|
|
}
|
2025-04-21 06:23:42 +00:00
|
|
|
|
2025-05-13 11:51:52 +00:00
|
|
|
// Load from original URL if not cached
|
|
|
|
const modelUrl = `${url_Backend_dwinzo}/api/v2/AssetFile/${item.modelfileID!}`;
|
|
|
|
return new Promise<Types.WallItem>((resolve) => {
|
|
|
|
loader.load(modelUrl, async (gltf) => {
|
|
|
|
try {
|
|
|
|
// Cache the model
|
|
|
|
const modelBlob = await fetch(modelUrl).then((res) => res.blob());
|
2025-05-15 04:04:55 +00:00
|
|
|
await storeGLTF(item.modelfileID!, modelBlob);
|
|
|
|
THREE.Cache.add(item.modelfileID!, gltf);
|
2025-05-13 11:51:52 +00:00
|
|
|
resolve(processModel(gltf, item));
|
|
|
|
} catch (error) {
|
|
|
|
console.error('Failed to cache model:', error);
|
|
|
|
resolve(processModel(gltf, item));
|
|
|
|
}
|
2025-04-21 06:23:42 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
|
|
|
setWallItems(loadedWallItems);
|
2025-05-13 11:51:52 +00:00
|
|
|
} catch (error) {
|
|
|
|
console.error('Failed to load wall items:', error);
|
2025-04-21 06:23:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-05-13 11:51:52 +00:00
|
|
|
function processModel(gltf: GLTF, item: Types.WallItem): Types.WallItem {
|
|
|
|
const model = gltf.scene.clone();
|
|
|
|
model.uuid = item.modelUuid!;
|
|
|
|
|
|
|
|
model.children[0]?.children?.forEach((child: THREE.Object3D) => {
|
|
|
|
if (child.name !== "CSG_REF") {
|
|
|
|
child.castShadow = true;
|
|
|
|
child.receiveShadow = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
|
|
|
type: item.type,
|
|
|
|
model: model,
|
|
|
|
modelName: item.modelName,
|
|
|
|
modelfileID: item.modelfileID,
|
|
|
|
scale: item.scale,
|
|
|
|
csgscale: item.csgscale,
|
|
|
|
csgposition: item.csgposition,
|
|
|
|
position: item.position,
|
|
|
|
quaternion: item.quaternion,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export default loadInitialWallItems;
|