3

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?

Vig
  • 143
  • 6
  • 13

2 Answers2

5

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;
yomotsu
  • 1,412
  • 1
  • 12
  • 17
  • I actually need to translate only y position using bb.min.y for my purpouse. Thanks! – Vig May 13 '16 at 18:27
0

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.

emh
  • 199
  • 10