简体   繁体   English

在 Three.js 中获取 Object3D 的大小

[英]Get size of Object3D in Three.js

I have this 3D object in my scene:我的场景中有这个 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);

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

But got this output: sizes: undefined, undefined .但是得到了这个输出: sizes: undefined, undefined

Is there any way to get the size of that object?有什么方法可以获取该对象的大小?

You could possibly create bounding box from your object:您可以从您的对象创建边界框:

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

If you have bounding box you can get it's min and max.如果你有边界框,你可以得到它的最小值和最大值。

Max.z - Min.z -> height Max.z - Min.z -> 高度

Max.x - Min.x -> width Max.x - Min.x -> 宽度

Max.y - Min.y -> depth Max.y - Min.y -> 深度

THREE.Sprite() has a geometry property so you can do: THREE.Sprite()具有geometry属性,因此您可以执行以下操作:

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

and you can get all the info you need from the .boundingBox property from its own properties .boundingBox.min and .boundingBox.max .并且您可以从.boundingBox属性从其自己的属性.boundingBox.min.boundingBox.max获取您需要的所有信息。

const boundingBox = new THREE.Box3().setFromObject(object) const xSize = boundingBox.max.x - boundingBox.min.x const ySize = boundingBox.max.y - boundingBox.min.y const zSize = boundingBox.max.z - boundingBox.min.z const maxSize = Math.max(xSize, ySize, zSize)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM