Hello,
I’m bulding an animation of a glb model.
After loading it on the scene, few seconds pass by and it starts to lag.
Another few seconds pass, and the lag dissappears, then the cycle begins again.
this is the code for initializing the scene:
function initScene() {
const renderer = new THREE.WebGLRenderer({
antialias: true,
canvas: document.getElementById('three-js-canvas')
})
// const three_js_col = document.getElementById('three-js-col');
renderer.setSize(window.innerWidth,window.innerHeight);
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 1, 15000)
window.addEventListener('resize',() => {
camera.aspect = (window.innerWidth * 10/12) / window.innerHeight
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth * 10/12, window.innerHeight);
})
const environment = new RoomEnvironment(renderer);
const pmremGenerator = new THREE.PMREMGenerator(renderer);
const controls = new MapControls(camera, renderer.domElement);
scene.background = new THREE.Color(0xffffff);
scene.environment = pmremGenerator.fromScene(environment).texture;
camera.position.set(0,50,50);
camera.rotation.z = Math.PI;
controls.enableDamping = true;
controls.update();
const light = new THREE.AmbientLight(0xFFFFFF,1);
scene.add(light);
const axesHelper = new THREE.AxesHelper(20);
scene.add(axesHelper);
return {
scene: scene,
renderer: renderer,
camera: camera,
controls: controls,
}
}
This is how i’m loading the model
async function loadModel(scene,robot_name) {
async function getRobotModel(robot_name) {
...
return model.url;
}
const loader = new GLTFLoader();
const dracoLoader = new DRACOLoader();
dracoLoader.setDecoderPath('https://www.gstatic.com/draco/versioned/decoders/1.5.7/');
dracoLoader.setDecoderConfig({type: 'js'});
loader.setDRACOLoader(dracoLoader);
loader.load(
await getRobotModel(robot_name),
(glb) => addModelToScene(glb,scene),
(xhr) => {
if(xhr.lengthComputable){
loading_progress.value = (xhr.loaded/xhr.total)*100;
if(loading_progress.value >= 100){
loading_complete.value = true;
}
}
},
(err) => {
alert(err);
}
)
and this is where the code above is called
onMounted(() => {
function animateScene() {
requestAnimationFrame(animateScene);
controls.update();
renderer.render(scene,camera);
}
const {scene,renderer,camera,controls} = initScene();
loadModel(scene,robot_name);
animateScene();
})
I’ve been trying to find the issue looking for memory leaks topics, but haven’t found a solution.