What is the proper use of threejs Box3().getSize() method?
Asked Answered
M

1

1

Everywhere on stackoverflow et al seem to agree that the proper use of the Box3() getSize() method is as follows:

const geometry = new THREE.BoxGeometry( 10, 10, 10 );
const material = new THREE.MeshBasicMaterial( {color: 0x00ff00} );
const cube = new THREE.Mesh( geometry, material );
scene.add( cube );

let cubeBoundingBox = new THREE.Box3().setFromObject( cube );
let boxSize = cubeBoundingBox.getSize();
console.log("BoxSize: " + boxSize);

However this doesn't seem to be the case. See the error in this example: https://jsfiddle.net/8L5dczmf/3/

I can see in the three.js documentation that getSize() is in fact supposed to be used this way... unless I'm reading .getSize ( target : Vector3 ) : Vector3 incorrectly. What is wrong with the code in the fiddle above then?

Marigolde answered 10/4, 2022 at 19:16 Comment(1)
You should put the error directly in your question body. I thought the issue was the missing target param to getSize, but appears that it's actually that getSize() doesn't exist (althought size() does)Haphtarah
H
4

You're expected to pass in a vector into which the result will be written:

// ...

let boxSize = new THREE.Vector3();
cubeBoundingBox.getSize(boxSize);

console.log("BoxSize: " + boxSize);

The intention here is that you can pool your Vector3 objects, and re-use them cheaply without making tons of garbage for the GC to clean up.

Haphtarah answered 10/4, 2022 at 19:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.