hiukim / mind-ar-js

Web Augmented Reality. Image Tracking, Face Tracking. Tensorflow.js
MIT License
2.13k stars 394 forks source link

First GLTF load without scaling #394

Closed adilsonv77 closed 1 year ago

adilsonv77 commented 1 year ago

In gltf-viewer the GLB file is loaded to fit the page. This is done automatically with no need to define the scale anywhere. How can we do this with MindAR without needing to use scene.scale.set(...)?

adilsonv77 commented 1 year ago

I found a solution:

 // creates MindARThree and light objects and initialize them

  const glb = await loadGLTF('filename.glb');

  const glbScene = glb.scene;

  const box = new THREE.Box3().setFromObject(glbScene);
  const sceneSize = box.getSize(new THREE.Vector3());
  const sceneCenter = box.getCenter(new THREE.Vector3());

  //Rescale the object to normalized space
  var maxAxis = Math.max(sceneSize.x, sceneSize.y, sceneSize.z);
  glbScene.scale.multiplyScalar(1.0 / maxAxis);
  box.setFromObject(glbScene);
  box.getCenter(sceneCenter);
  box.getSize(sceneSize);

  //Reposition to 0,halfY,0
  glbScene.position.copy(sceneCenter).multiplyScalar(-1);
  glbScene.position.y-= (sceneSize.y * 0.5);

 // reminder code

Thanks for https://stackoverflow.com/a/52271526/5913327