取消加载GLTF模型后如何重置默认比例 [英] Aframe how to reset default scale after loading the GLTF model

查看:230
本文介绍了取消加载GLTF模型后如何重置默认比例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个应用程序,以GLTF格式添加来自Google poly的模型。

I am trying to make an app which adds models from google poly in GLTF format.

当场景中添加某些模型时,我会遇到一些问题我通过用边界框的最大值和最小值来计算它们的大小并设置比例值来解决。

I had the issue of some models being extremely large when added on the scene which I solved by computing their size with bounding box max and min values and setting the scale amount.

现在,当我打开检查器并拖动到场景中之后,将对象添加到场景中缩放对象,即使拖动少量对象也会变得非常大。

Now after adding the objects to the scene when I open the inspector and drag to scale objects, even with a small amount of drag the objects becomes very large.

如果有任何方法可以重置已加载对象的缩放值,以便可以使用默认值是我计算出的值,这也可以解决缩放问题。

If there is any way to reset the scale value of loaded objects so that default value can be the value which I computed and this can also solve the drag to scale issue.

注意:某些元素的计算出的缩放系数用于x,y,z为0.00001。

Note: The computed scale factor for some elements goes to 0.00001 for x, y, z.

任何输入都会受到赞赏。

Any inputs will be appreciated.

-谢谢

推荐答案

使用A-Frame组件中的three.js API计算模型的边界框,然后将其缩小为大小yo你喜欢。例如:

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>

上面的代码将您的模型拟合到约200万个框,然后重新居中。有关更多信息,请参见 THREE.Object3D文档

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帧0.8.0。

three.js r89, A-Frame 0.8.0.

这篇关于取消加载GLTF模型后如何重置默认比例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆