Getting meshes in size order

I have a number of meshes in my scene, anywhere from 2 - 500.

Could anyone offer guidance on the based way to get an array of the objects in order of size (largest to smallest or vice versa);

Is calculating the bounding box, then getting it’s volume, then somehow comparing them best way? If so could anyone suggest the most performant way or comparing? Ive found myself creating objects which store the mesh against its volume then trying to order that into a new array which all seems very messy.

Assuming the boundingSphere is available, list is the array of meshes. It uses the bounding sphere radius to keep it simple, as you said you can use the bounding box volume too. (code not tested)

let list = [];

function sortSize(a, b) {
	return b.size - a.size;
}

for ( let i = 0, l = list.length; i < l; i ++ ) {

	const mesh = list[i];

	mesh.size = mesh.geometry.boundingSphere.radius;
	
}

list.sort(sortSize);
1 Like

Best practice is to put custom properties in the userData:

mesh.userData.size

1 Like

Yes, code is only for simplicity.

1 Like

tricky to decide whats biggest. The bounding sphere check can yield no volume if done on a say plane, but could be a very large radius.

Bounding sphere radius was just for simplicity, bounding box will be quite more reliable, computing volume based on triangles even more, but it depends on the usecase.