feat: Refactor asset loading and model handling; remove unused loadInitialWallItems function and streamline GLTFLoader usage across components
This commit is contained in:
@@ -1,110 +0,0 @@
|
||||
// import { GLTF, GLTFLoader } from "three/examples/jsm/loaders/GLTFLoader";
|
||||
// import { DRACOLoader } from "three/examples/jsm/loaders/DRACOLoader";
|
||||
// import * as THREE from "three";
|
||||
// import * as Types from "../../../types/world/worldTypes";
|
||||
// import { getWallItems } from "../../../services/factoryBuilder/asset/wallAsset/getWallItemsApi";
|
||||
// import { retrieveGLTF, storeGLTF } from "../../../utils/indexDB/idbUtils";
|
||||
// import { getUserData } from "../../../functions/getUserData";
|
||||
|
||||
// async function loadInitialWallItems(
|
||||
// setWallItems: Types.setWallItemSetState,
|
||||
// projectId?: string,
|
||||
// versionId?: string
|
||||
// ): Promise<void> {
|
||||
// if (!projectId || !versionId) return;
|
||||
// try {
|
||||
// const { organization, email } = getUserData();
|
||||
|
||||
// if (!email) {
|
||||
// console.error("No email found in localStorage");
|
||||
// }
|
||||
|
||||
// const items = await getWallItems(organization, projectId, versionId);
|
||||
|
||||
// let url_Backend_dwinzo = `http://${process.env.REACT_APP_SERVER_MARKETPLACE_URL}`;
|
||||
|
||||
// if (!items || items.length === 0) {
|
||||
// localStorage.removeItem("WallItems");
|
||||
// return;
|
||||
// }
|
||||
|
||||
// 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
|
||||
// const cachedModel = THREE.Cache.get(item.assetId!);
|
||||
// if (cachedModel) {
|
||||
// return processModel(cachedModel, item);
|
||||
// }
|
||||
|
||||
// // Check IndexedDB cache
|
||||
// const cachedModelBlob = await retrieveGLTF(item.assetId!);
|
||||
// if (cachedModelBlob) {
|
||||
// const blobUrl = URL.createObjectURL(cachedModelBlob);
|
||||
// return new Promise<Types.WallItem>((resolve) => {
|
||||
// loader.load(blobUrl, (gltf) => {
|
||||
// URL.revokeObjectURL(blobUrl);
|
||||
// THREE.Cache.add(item.assetId!, gltf);
|
||||
// resolve(processModel(gltf, item));
|
||||
// });
|
||||
// });
|
||||
// }
|
||||
|
||||
// // Load from original URL if not cached
|
||||
// const modelUrl = `${url_Backend_dwinzo}/api/v2/AssetFile/${item.assetId!}`;
|
||||
// return new Promise<Types.WallItem>((resolve) => {
|
||||
// loader.load(modelUrl, async (gltf) => {
|
||||
// try {
|
||||
// // Cache the model
|
||||
// const modelBlob = await fetch(modelUrl).then((res) => res.blob());
|
||||
// await storeGLTF(item.assetId!, modelBlob);
|
||||
// THREE.Cache.add(item.assetId!, gltf);
|
||||
// resolve(processModel(gltf, item));
|
||||
// } catch (error) {
|
||||
// console.error("Failed to cache model:", error);
|
||||
// resolve(processModel(gltf, item));
|
||||
// }
|
||||
// });
|
||||
// });
|
||||
// })
|
||||
// );
|
||||
|
||||
// setWallItems(loadedWallItems);
|
||||
// } catch (error) {
|
||||
// console.error("Failed to load wall items:", error);
|
||||
// }
|
||||
// }
|
||||
|
||||
// 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,
|
||||
// assetId: item.assetId,
|
||||
// scale: item.scale,
|
||||
// csgscale: item.csgscale,
|
||||
// csgposition: item.csgposition,
|
||||
// position: item.position,
|
||||
// quaternion: item.quaternion,
|
||||
// };
|
||||
// }
|
||||
|
||||
// export default loadInitialWallItems;
|
||||
@@ -44,6 +44,7 @@ function AssetsGroup({ plane }: { readonly plane: RefMesh }) {
|
||||
|
||||
useEffect(() => {
|
||||
if (!projectId || !selectedVersion) return;
|
||||
|
||||
clearEvents();
|
||||
|
||||
let totalAssets = 0;
|
||||
@@ -304,7 +305,7 @@ function AssetsGroup({ plane }: { readonly plane: RefMesh }) {
|
||||
pointer.x = (event.clientX / window.innerWidth) * 2 - 1;
|
||||
pointer.y = -(event.clientY / window.innerHeight) * 2 + 1;
|
||||
|
||||
addAssetModel(scene, raycaster, camera, pointer, socket, selectedItem, setSelectedItem, addEvent, addAsset, plane, selectedVersion, projectId, userId);
|
||||
addAssetModel(scene, raycaster, camera, pointer, socket, selectedItem, setSelectedItem, addEvent, addAsset, plane, loader, selectedVersion, projectId, userId);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -348,7 +349,7 @@ function AssetsGroup({ plane }: { readonly plane: RefMesh }) {
|
||||
}, [selectedItem, camera, activeModule, controls, isRenameMode]);
|
||||
|
||||
return (
|
||||
<Models />
|
||||
<Models loader={loader} />
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -20,6 +20,7 @@ async function addAssetModel(
|
||||
addEvent: (event: EventsSchema) => void,
|
||||
addAsset: (asset: Asset) => void,
|
||||
plane: Types.RefMesh,
|
||||
loader: GLTFLoader,
|
||||
selectedVersion?: Version | null,
|
||||
projectId?: string,
|
||||
userId?: string
|
||||
@@ -29,12 +30,6 @@ async function addAssetModel(
|
||||
let url_Backend_dwinzo = `http://${process.env.REACT_APP_SERVER_MARKETPLACE_URL}`;
|
||||
|
||||
try {
|
||||
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);
|
||||
|
||||
raycaster.setFromCamera(pointer, camera);
|
||||
const wallFloorsGroup = scene.getObjectByName("Walls-Floors-Group") as Types.Group | null;
|
||||
const floorsGroup = scene.getObjectByName("Floors-Group") as Types.Group | null;
|
||||
|
||||
@@ -2,7 +2,6 @@ import * as THREE from 'three';
|
||||
import { useCallback, useEffect, useRef, useState } from 'react';
|
||||
import { retrieveGLTF, storeGLTF } from '../../../../../utils/indexDB/idbUtils';
|
||||
import { GLTF, GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader';
|
||||
import { DRACOLoader } from 'three/examples/jsm/loaders/DRACOLoader';
|
||||
import { ThreeEvent, useThree } from '@react-three/fiber';
|
||||
import { useActiveTool, useDeletableFloorItem, useSelectedAssets, useSelectedFloorItem, useSocketStore, useToggleView, useToolMode } from '../../../../../store/builder/store';
|
||||
import { AssetBoundingBox } from '../../functions/assetBoundingBox';
|
||||
@@ -21,8 +20,7 @@ import { upsertProductOrEventApi } from '../../../../../services/simulation/prod
|
||||
import { getAssetIksApi } from '../../../../../services/simulation/ik/getAssetIKs';
|
||||
import { ModelAnimator } from './animator/modelAnimator';
|
||||
|
||||
|
||||
function Model({ asset, isRendered }: { readonly asset: Asset, isRendered: boolean }) {
|
||||
function Model({ asset, isRendered, loader }: { readonly asset: Asset, isRendered: boolean, loader: GLTFLoader }) {
|
||||
const url_Backend_dwinzo = `http://${process.env.REACT_APP_SERVER_MARKETPLACE_URL}`;
|
||||
const savedTheme: string = localStorage.getItem("theme") || "light";
|
||||
const { controls, gl } = useThree();
|
||||
@@ -109,11 +107,6 @@ function Model({ asset, isRendered }: { readonly asset: Asset, isRendered: boole
|
||||
}, [isRendered, selectedFloorItem])
|
||||
|
||||
useEffect(() => {
|
||||
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 loadModel = async () => {
|
||||
try {
|
||||
|
||||
|
||||
@@ -7,10 +7,11 @@ import { useSelectedAsset } from '../../../../store/simulation/useSimulationStor
|
||||
import { useSceneContext } from '../../../scene/sceneContext';
|
||||
|
||||
import Model from './model/model';
|
||||
import { GLTFLoader } from "three/examples/jsm/Addons";
|
||||
|
||||
const distanceWorker = new Worker(new URL("../../../../services/factoryBuilder/webWorkers/distanceWorker.js", import.meta.url));
|
||||
|
||||
function Models() {
|
||||
function Models({ loader }: { loader: GLTFLoader }) {
|
||||
const { controls, camera } = useThree();
|
||||
const { assetStore } = useSceneContext();
|
||||
const { assets } = assetStore();
|
||||
@@ -57,7 +58,7 @@ function Models() {
|
||||
}}
|
||||
>
|
||||
{assets.map((asset) => (
|
||||
<Model key={asset.modelUuid} asset={asset} isRendered={renderMap[asset.modelUuid] ?? false} />
|
||||
<Model key={asset.modelUuid} asset={asset} isRendered={renderMap[asset.modelUuid] ?? false} loader={loader} />
|
||||
))}
|
||||
</group>
|
||||
);
|
||||
|
||||
@@ -44,6 +44,7 @@ function WallAssetInstance({ wallAsset }: { wallAsset: WallAsset }) {
|
||||
|
||||
dracoLoader.setDecoderPath('https://cdn.jsdelivr.net/npm/three@0.160.0/examples/jsm/libs/draco/gltf/');
|
||||
loader.setDRACOLoader(dracoLoader);
|
||||
|
||||
const loadModel = async () => {
|
||||
try {
|
||||
// Check Cache
|
||||
|
||||
@@ -19,7 +19,7 @@ import { getUserData } from "../../../functions/getUserData";
|
||||
const CamModelsGroup = () => {
|
||||
const navigate = useNavigate();
|
||||
const groupRef = useRef<THREE.Group>(null);
|
||||
const { userId, organization, email } = getUserData();
|
||||
const { organization, email } = getUserData();
|
||||
const { setActiveUsers } = useActiveUsers();
|
||||
const { socket } = useSocketStore();
|
||||
const { activeModule } = useModuleStore();
|
||||
@@ -31,14 +31,11 @@ const CamModelsGroup = () => {
|
||||
dracoLoader.setDecoderPath("three/examples/jsm/libs/draco/gltf/");
|
||||
loader.setDRACOLoader(dracoLoader);
|
||||
|
||||
|
||||
const { camMode } = useCamMode();
|
||||
const { camera, controls } = useThree(); // Access R3F camera and controls
|
||||
const { camera, controls } = useThree();
|
||||
|
||||
useEffect(() => {
|
||||
if (camMode !== "FollowPerson") return;
|
||||
// If a user is selected, set the camera view to their location
|
||||
// and update the camera and controls accordingly
|
||||
if (selectedUser?.location) {
|
||||
const { position, rotation, target } = selectedUser.location;
|
||||
if (rotation && target)
|
||||
|
||||
@@ -79,17 +79,17 @@ export default function PostProcessing() {
|
||||
denoiseRadius={6}
|
||||
denoiseSamples={16}
|
||||
/>
|
||||
<DepthOfField
|
||||
{/* <DepthOfField
|
||||
focusDistance={0}
|
||||
focalLength={0.15}
|
||||
bokehScale={2}
|
||||
/>
|
||||
<Bloom
|
||||
/> */}
|
||||
{/* <Bloom
|
||||
intensity={0.1}
|
||||
luminanceThreshold={0.9}
|
||||
luminanceSmoothing={0.025}
|
||||
mipmapBlur={false}
|
||||
/>
|
||||
/> */}
|
||||
{selectedWallAsset && (
|
||||
<Outline
|
||||
selection={flattenChildren(selectedWallAsset.children)}
|
||||
|
||||
@@ -55,7 +55,6 @@ const ArmBotUI = () => {
|
||||
})
|
||||
}
|
||||
|
||||
// Fetch and setup selected ArmBot data
|
||||
useEffect(() => {
|
||||
if (selectedEventSphere) {
|
||||
const selectedArmBot = getEventByModelUuid(selectedProduct.productUuid, selectedEventSphere.userData.modelUuid);
|
||||
@@ -86,13 +85,11 @@ const ArmBotUI = () => {
|
||||
const modelData = getEventByModelUuid(selectedProduct.productUuid, modelUuid);
|
||||
|
||||
if (modelData?.type === "roboticArm") {
|
||||
const baseX = modelData.point.position?.[0] || 0;
|
||||
const baseY = modelData.point.position?.[1] || 0;;
|
||||
const baseZ = modelData.point.position?.[2] || 0;
|
||||
const baseY = modelData.point.position?.[1] || 0;
|
||||
return {
|
||||
pick: [baseX, baseY, baseZ + 0.5],
|
||||
drop: [baseX, baseY, baseZ - 0.5],
|
||||
default: [baseX, baseY, baseZ],
|
||||
pick: [0, baseY, 0 + 0.5],
|
||||
drop: [0, baseY, 0 - 0.5],
|
||||
default: [0, baseY, 0],
|
||||
};
|
||||
}
|
||||
|
||||
@@ -225,7 +222,7 @@ const ArmBotUI = () => {
|
||||
</React.Fragment>
|
||||
);
|
||||
} else {
|
||||
return null; // important! must return something
|
||||
return null;
|
||||
}
|
||||
})}
|
||||
</>
|
||||
|
||||
Reference in New Issue
Block a user