Jun
29

A quick code drop here to fill in a minor missing piece of functionality in away3d lite- a function to compute an axis-aligned bounding box on a Mesh. The code, which I’ve included as a member function of Mesh.as, simply runs through all the vertices to find the bounding box. This operates on the raw geometry, and does not apply to transformed objects.


/*
 * Returns an axis-aligned bounding box
 *
 * Added by richpixel, 6/27/11, code is from Papervision, modified for away3d vertex format
 */
public function get AABB():Object
{
	var minX :Number = Number.MAX_VALUE;
	var minY :Number = Number.MAX_VALUE;
	var minZ :Number = Number.MAX_VALUE;
	var maxX :Number = -minX;
	var maxY :Number = -minY;
	var maxZ :Number = -minZ;

	var k:int = _vertices.length/3;
	while (k--)
	{
		var vx:Number = _vertices[k*3];
		var vy:Number = _vertices[k*3+1];
		var vz:Number = _vertices[k*3+2];

		minX = Math.min(minX, vx);
		minY = Math.min(minY, vy);
		minZ = Math.min(minZ, vz);
		maxX = Math.max(maxX, vx);
		maxY = Math.max(maxY, vy);
		maxZ = Math.max(maxZ, vz);
	}

	return {minX: minX, minY: minY, minZ: minZ, maxX: maxX, maxY: maxY, maxZ: maxZ};
}