Refactor asset loading and distance calculation logic for improved performance and clarity

This commit is contained in:
2025-07-28 15:00:24 +05:30
parent 2ac6bbeb9d
commit 11ace1977a
6 changed files with 42 additions and 41 deletions

View 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 });
}
}
};

View File

@@ -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' });
};