How to get the width/height/length of a mesh in THREE.js [closed]
Asked Answered
C

1

52

I been researching various places about trying to get the width and height of a mesh but I have been able to find anything that works. I have a collada model that I imported and all I want to do is get the dimension of it in Webgl/Three.js Units so that I can calculate collision in my simple game. So how would I go about getting the width and height of a mesh in THREE.js?

Carlock answered 25/5, 2014 at 20:10 Comment(5)
Please show what you attempted in order for others to be able to help.Stomatitis
Can someone tell me why I got -rep? I just want to know to not do it again on my future questionsCarlock
You got negative reputation because someone down voted your question. A down vote is meant to mean "This question does not show any research effort; it is unclear or not useful."Stomatitis
Thank You! I appreciate that you answered :D Also I couldnt add the code before since I was doing this on a tablet, but vincent posted the perfect answer :DCarlock
You're welcome. You may want to still add the code because otherwise, your question is on its way to being closed.Stomatitis
B
89

If you're ok with a basic bounding box, you can use a THREE.Box3

let measure = new THREE.Vector3();
let box = colladaModel.getSize(measure);
console.log( measure );
Bullion answered 25/5, 2014 at 22:0 Comment(11)
This looks like the perfect solution, i'll try it when i have a chance and i'll tell u how it went! 😀Carlock
I used it and it worked perfectly, thanks again!Carlock
It doesn't give correct dimensionsBrittain
hi @Tarun, can you explain in which conditions it does not work ?Bullion
do I need to somehow normalize values in matrix? Id like to flip object on x axis and move its -x_width ... flip is okay, bot move seems incorectVerminous
Just to keep it up two date: size method name was changed to getSizeTrack
thank you @KrzysztofGrzybek, I've updated the code.Bullion
but if the box(for example a text mesh) is rotated, the size is no more the exact width/height of that text.Pyromorphite
Seems like Box3().getSize() has updated usage. A Vector3 needs to be passed into it as an argument. See here for implementation https://mcmap.net/q/354115/-what-is-the-proper-use-of-threejs-box3-getsize-methodSpinel
Thank you @Spinel I've updated the code.Bullion
Doesn't work for me, throws this error: this.objects[0].getSize is not a functionChadwickchae

© 2022 - 2024 — McMap. All rights reserved.