Refactor asset loading and distance calculation logic for improved performance and clarity
This commit is contained in:
21
app/src/services/factoryBuilder/webWorkers/distanceWorker.js
Normal file
21
app/src/services/factoryBuilder/webWorkers/distanceWorker.js
Normal file
@@ -0,0 +1,21 @@
|
||||
onmessage = function (e) {
|
||||
const { assetPosition, cameraPosition, limitDistance, renderDistance, isRendered } = e.data;
|
||||
|
||||
if (limitDistance && assetPosition) {
|
||||
const distance = Math.sqrt(
|
||||
Math.pow(assetPosition.x - cameraPosition.x, 2) +
|
||||
Math.pow(assetPosition.y - cameraPosition.y, 2) +
|
||||
Math.pow(assetPosition.z - cameraPosition.z, 2)
|
||||
);
|
||||
|
||||
if (!isRendered && distance <= renderDistance) {
|
||||
postMessage({ shouldRender: true });
|
||||
} else if (isRendered && distance > renderDistance) {
|
||||
postMessage({ shouldRender: false });
|
||||
}
|
||||
} else {
|
||||
if (!isRendered) {
|
||||
postMessage({ shouldRender: true });
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -1,12 +1,5 @@
|
||||
import * as THREE from 'three';
|
||||
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader';
|
||||
import { DRACOLoader } from 'three/examples/jsm/loaders/DRACOLoader';
|
||||
import { retrieveGLTF, storeGLTF } from '../../../utils/indexDB/idbUtils';
|
||||
|
||||
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);
|
||||
let url_Backend_dwinzo = `http://${process.env.REACT_APP_SERVER_MARKETPLACE_URL}`;
|
||||
|
||||
onmessage = async (event) => {
|
||||
@@ -17,8 +10,8 @@ onmessage = async (event) => {
|
||||
);
|
||||
|
||||
for (const item of uniqueItems) {
|
||||
if(item.assetId === null || item.assetId === undefined) {
|
||||
continue; // Skip items without a valid assetId
|
||||
if (item.assetId === null || item.assetId === undefined) {
|
||||
continue;
|
||||
}
|
||||
const modelID = item.assetId;
|
||||
const indexedDBModel = await retrieveGLTF(modelID);
|
||||
@@ -37,5 +30,5 @@ onmessage = async (event) => {
|
||||
}
|
||||
}
|
||||
|
||||
postMessage({ message: 'done' })
|
||||
postMessage({ message: 'done' });
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user