Vig
Vig

Reputation: 143

How can I get 3D object dimensions in ThreeJS?

I'm developing an AR application using WebRTC (webcam access), JSARToolKit (marker detection) and threeJS (3D library).

I want to place 3D objects (exported from Maya using threejs maya exporter) in the center of the detected marker.

This is the code where I load the 3D object using JSONLoader:

// load the model
var loader = new THREE.JSONLoader;
var object;
//var geometry = new THREE.BoxGeometry(1, 1, 1);
loader.load('js/cube.js', function(geometry, materials){
var material = new THREE.MeshFaceMaterial(materials);
object = new THREE.Mesh(geometry, material);

object.position.x -= ***3DobjectWidth/2***;
object.position.y -= ***3DobjectHeight/2***;
object.position.z -= ***3DobjectDepth/2***;

scene.add(object);
});

I need to get width, height and depth of the object to change his position (see 3DobjectWidth ecc).

Any suggestions?

Upvotes: 3

Views: 3189

Answers (2)

emh
emh

Reputation: 309

Geometry has a .center() method. That might be more efficient and simpler if you need to center many meshes based on same geometry. It also calls computeBoundingBox for you.

Upvotes: 0

yomotsu
yomotsu

Reputation: 1462

The object size will be placed at geometry.boundingBox. But it has to be generated once.

try this.

geometry.computeBoundingBox();
var bb = geometry.boundingBox;
var object3DWidth  = bb.max.x - bb.min.x;
var object3DHeight = bb.max.y - bb.min.y;
var object3DDepth  = bb.max.z - bb.min.z;

Upvotes: 5

Related Questions