Halo
发布于 2023-09-27 / 102 阅读 / 0 评论 / 0 点赞

three.js自动放置模型位置

function autoLocalOneModel(object, camera, controls) {
  const box = new THREE.Box3().setFromObject(object);
  const boxSize = box.getSize(new THREE.Vector3()).length();
  const boxCenter = box.getCenter(new THREE.Vector3());

  const sizeToFitOnScreen = boxSize * 0.6;
  const halfFovY = THREE.MathUtils.degToRad(camera.fov * .5);
  const distance = sizeToFitOnScreen / Math.tan(halfFovY);
  const direction = (new THREE.Vector3())
                    .subVectors(camera.position, boxCenter)
		    .multiply(new THREE.Vector3(1, 0, 1))
		    .normalize();
                    
  camera.position.copy(direction.multiplyScalar(distance).add(boxCenter));
  camera.near = boxSize / 100;
  camera.far = boxSize * 100;
  camera.updateProjectionMatrix();
  camera.lookAt(boxCenter.x, boxCenter.y, boxCenter.z);
  
  controls.maxDistance = boxSize * 10;
  controls.target.copy(boxCenter);
  controls.update();
}

评论