【问题标题】:Get size of Object3D in Three.js在 Three.js 中获取 Object3D 的大小
【发布时间】:2016-02-18 21:43:24
【问题描述】:

我的场景中有这个 3D 对象:

var icon = new THREE.Object3D()

var iconTexture = THREE.ImageUtils.loadTexture('images/icon.png'),
      iconMaterial = new THREE.SpriteMaterial({
      map: iconTexture,
      color: 0xffffff,
      opacity: 1
});

var iconSprite = new THREE.Sprite(iconMaterial);

iconSprite.scale.set(14, 14, 1.0);
iconSprite.position.set(Math.random() - 0.5, Math.random() - 0.5, Math.random() - 0.75);

icon.position.setLength(0);

icon.add(iconSprite);

icon.position.y = 15;
icon.position.z = 20;
scene.add(icon);
scene.updateMatrixWorld(true);

我设法得到了这个物体的位置:

var position = new THREE.Vector3();
position.getPositionFromMatrix( icon.matrixWorld );
console.log(position.x + ',' + position.y + ',' + position.z);

但我无法获得该对象的大小。我尝试了类似的方法:

console.log("sizes: "+icon.min+", "+icon.max);

但是得到了这个输出:sizes: undefined, undefined

有什么方法可以得到那个对象的大小?

【问题讨论】:

  • 你真的是想问“THREE.Sprite 有多大”?

标签: javascript three.js


【解决方案1】:

您可以从您的对象创建边界框:

var bbox = new THREE.Box3().setFromObject(icon);

如果你有边界框,你可以得到它的最小值和最大值。

Max.z - Min.z -> 高度

Max.x - Min.x -> 宽度

Max.y - Min.y -> 深度

【讨论】:

    【解决方案2】:

    THREE.Sprite() 有一个 geometry 属性,所以你可以这样做:

    if( iconSprite.geometry.boundingBox === undefined )
        iconSprite.geometry.computeBoundingBox ();
    

    您可以从.boundingBox 属性中获取所需的所有信息,从它自己的属性.boundingBox.min.boundingBox.max 中获取。

    【讨论】:

    • 我试过这样if( iconSprite.geometry.boundingBox === undefined ) iconSprite.geometry.computeBoundingBox (); console.log("bbox max: "+iconSprite.geometry.boundingBox.max+"\nbbox min: "+iconSprite.geometry.boundingBox.min); ,但出现错误,说Uncaught TypeError: Cannot read property 'max' of null。我错过了什么?
    • 试试if(... boundingBox === null )
    • 好吧,这种工作,我得到一个具有属性的对象。但是,例如,我得到 max.x = 0.5 和 min.x = -0.5。当我尝试var size = iconSprite.geometry.boundingBox.size() 时,我得到了这个结果:TREE.Vector3 {x: 1, y: 1, z: 0} 那么我怎样才能得到这个对象的确切尺寸呢?
    • 你有对象的最小和最大范围以及它的大小。它的尺寸由@bbartosz.baczek 的答案给出,除了 y 轴代表高度,z 轴代表深度。
    • 抱歉耽搁了,我被分配了一项紧急工作,没有时间检查它。谢谢你详细的回答!再次为耽搁道歉!
    猜你喜欢
    • 1970-01-01
    • 2012-08-08
    • 2014-03-02
    • 2013-03-07
    • 2015-06-15
    • 1970-01-01
    • 2018-08-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多