9

Short question. How does one find the dimensions of a mesh in three.js?

I have Collada (.dae) files which I would like to know the size of in units (x,y,z). I have seen comments about using geometry.computeBoundingBox(), but I am still not exactly sure how to use it.

Also I am aware of using scale but my ultimate goal is to compare objects in the scene with realistic measurements (mm) so it helps to know the exact physical dimensions of an object so that I may change it if necessary.

Appreciate any words of wisdom :^)

1 Answers1

16

Using a bounding box sounds like a good idea. You can try something like this

    boundingBox = new THREE.Box3().setFromObject(your_object)
    size = boundingBox.getSize() // Returns Vector3

Docs:

Comment update: Now that I have the object dimensions X,Y,Z, how can I change the value of let's say 'X' and have the object scale to it?

scaleFactor = otherSize.x / mySize.x 
myObject.scale(scaleFactor, scaleFactor, scaleFactor)
jparimaa
  • 1,964
  • 15
  • 19
  • 1
    Now that I have the object dimensions X,Y,Z, how can I change the value of let's say 'X' and have the object scale to it? – Jonathan Drummond Jan 05 '18 at 10:05
  • 2
    `size = boundingBox.getSize()` doesn't appear to be correct, probably outdated use of `getSize()`? See here: https://jsfiddle.net/8L5dczmf/ – A__ Apr 10 '22 at 19:06
  • 2
    I guess its usage was updated. Now a Vector3 needs to be passed into getSize() as an argument. See here for implementation https://stackoverflow.com/a/71819811/1757149 – A__ Apr 10 '22 at 19:38