Aframe how to reset default scale after loading the GLTF model

风格不统一 提交于 2019-12-06 16:17:22

Use the three.js API within A-Frame components to compute a bounding box of the model, then scale it down to the size you prefer. Example:

AFRAME.registerComponent('autoscale', {
  schema: {type: 'number', default: 1},
  init: function () {
    this.scale();
    this.el.addEventListener('object3dset', () => this.scale());
  },
  scale: function () {
    const el = this.el;
    const span = this.data;
    const mesh = el.getObject3D('mesh');

    if (!mesh) return;

    // Compute bounds.
    const bbox = new THREE.Box3().setFromObject(mesh);

    // Normalize scale.
    const scale = span / bbox.getSize().length();
    mesh.scale.set(scale, scale, scale);

    // Recenter.
    const offset = bbox.getCenter().multiplyScalar(scale);
    mesh.position.sub(offset);
  }
});

HTML:

<a-entity autoscale="2" gltf-model="stereo.gltf"></a-entity>

The code above will fit your model to a ~2m box, and re-center it. For more information see THREE.Object3D documentation.

three.js r89, A-Frame 0.8.0.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!