在Three.js中获取Object3D的大小 [英] Get size of Object3D in Three.js

查看:1097
本文介绍了在Three.js中获取Object3D的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have this 3D object in my scene:

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);

我得到了这个对象的位置:

I manged to get the position of this object:

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

但我无法获得该对象的大小。我试过类似的东西:

But I can't get the size of that object. I tried something like that:

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

但得到此输出:尺寸:undefined,undefined

有没有办法获得该对象的大小?

Is there any way to get the size of that object?

推荐答案

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

THREE.Sprite() has a geometry property so you can do:

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

您可以从 .boundingBox中获取所需的所有信息来自其属性的属性 .boundingBox.min .boundingBox.max

and you can get all the info you need from the .boundingBox property from its own properties .boundingBox.min and .boundingBox.max.

这篇关于在Three.js中获取Object3D的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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