From 0446f4fd14147b806960b93e2388b6c852767ed0 Mon Sep 17 00:00:00 2001 From: Lanius Trolling Date: Fri, 24 Feb 2023 12:32:31 -0500 Subject: [PATCH] Optimize model loading 3 --- src/main/resources/static/init.js | 38 +++++++++++++++---------------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/src/main/resources/static/init.js b/src/main/resources/static/init.js index b6d8673..6b3f6cd 100644 --- a/src/main/resources/static/init.js +++ b/src/main/resources/static/init.js @@ -83,30 +83,28 @@ if (modelName == null || modelName === "") continue; (async () => { - let threeData = {}; + const modelAsync = loadObj(modelName); - let modelAsync = loadObj(modelName); + const camera = new THREE.PerspectiveCamera(69, 1, 0.01, 1000.0); - threeData.camera = new THREE.PerspectiveCamera(69, 1, 0.01, 1000.0); + const scene = new THREE.Scene(); + scene.add(new THREE.AmbientLight("#555555", 1.0)); - threeData.scene = new THREE.Scene(); - threeData.scene.add(new THREE.AmbientLight("#555555", 1.0)); + const renderer = new THREE.WebGLRenderer({"canvas": canvas, "antialias": true}); - threeData.renderer = new THREE.WebGLRenderer({"canvas": canvas, "antialias": true}); - - threeData.controls = new THREE.OrbitControls(threeData.camera, canvas); + const controls = new THREE.OrbitControls(camera, canvas); function render() { - threeData.controls.update(); - threeData.renderer.render(threeData.scene, threeData.camera); + controls.update(); + renderer.render(scene, camera); window.requestAnimationFrame(render); } function onResize() { const dim = canvas.getBoundingClientRect(); - threeData.camera.aspect = dim.width / dim.height; - threeData.camera.updateProjectionMatrix(); - threeData.renderer.setSize(dim.width, dim.height, false); + camera.aspect = dim.width / dim.height; + camera.updateProjectionMatrix(); + renderer.setSize(dim.width, dim.height, false); } window.addEventListener('resize', onResize); @@ -114,9 +112,9 @@ onResize(); const model = await modelAsync; - threeData.scene.add(model); + scene.add(model); - const bbox = new THREE.Box3().setFromObject(threeData.scene); + const bbox = new THREE.Box3().setFromObject(scene); bbox.dimensions = { x: bbox.max.x - bbox.min.x, y: bbox.max.y - bbox.min.y, @@ -124,12 +122,12 @@ }; model.position.sub(new THREE.Vector3(bbox.min.x + bbox.dimensions.x / 2, bbox.min.y + bbox.dimensions.y / 2, bbox.min.z + bbox.dimensions.z / 2)); - threeData.camera.position.set(bbox.dimensions.x / 2, bbox.dimensions.y / 2, Math.max(bbox.dimensions.x, bbox.dimensions.y, bbox.dimensions.z)); + camera.position.set(bbox.dimensions.x / 2, bbox.dimensions.y / 2, Math.max(bbox.dimensions.x, bbox.dimensions.y, bbox.dimensions.z)); - threeData.light = new THREE.PointLight("#AAAAAA", 1.0); - threeData.scene.add(threeData.camera); - threeData.camera.add(threeData.light); - threeData.light.position.set(0, 0, 0); + const light = new THREE.PointLight("#AAAAAA", 1.0); + scene.add(camera); + camera.add(light); + light.position.set(0, 0, 0); render(); })().catch(reason => { -- 2.25.1