2

loading the .obj:

        loader.load( 'test.obj', function ( objMesh ) {
                objMesh.traverse( function ( child ) {
                if ( child instanceof THREE.Mesh ) {
                       child.material = mat2;
                }
            } );

I tried to find the position with mrdoobs code:

objMesh.geometry.computeBoundingBox();

var boundingBox = objMesh.geometry.boundingBox;

var position = new THREE.Vector3();
position.subVectors( boundingBox.max, boundingBox.min );
position.multiplyScalar( 0.5 );
position.add( boundingBox.min );

position.applyMatrix4( objMesh.matrixWorld );

alert(position.x + ',' + position.y + ',' + position.z);

however this fails with

objMesh.geometry is undefined

Is this not possible with loaded meshes?

1 Answers1

1

It's possible, but in your case seems that the objMesh is a local variable in the scope of function ( objMesh ) {...}.

So you can declare a global variable, let's say mesh and then set its value inside the onLoad callback function

var mesh;
...
loader.load( 'test.obj', function ( objMesh ) {
            objMesh.traverse( function ( child ) {
            if ( child instanceof THREE.Mesh ) {
                   child.material = mat2;
                   mesh = child; // set value to the global variable, applicable, if the objMesh has one child of THREE.Mesh()
            }

        } );

and then apply mrdoob's code to the mesh variable, not the objMesh.

Or, you can wrap mrdoob's code in a function and then call this function in the callback onLoad function with a parameter of your mesh:

function absPos( myMesh ){
    myMesh.geometry.computeBoundingBox();

    var boundingBox = myMesh.geometry.boundingBox;

    var position = new THREE.Vector3();
    position.subVectors( boundingBox.max, boundingBox.min );
    position.multiplyScalar( 0.5 );
    position.add( boundingBox.min );

    position.applyMatrix4( myMesh.matrixWorld );

    alert(position.x + ',' + position.y + ',' + position.z);
}

calling it in the callback function

loader.load( 'test.obj', function ( objMesh ) {
            objMesh.traverse( function ( child ) {
            if ( child instanceof THREE.Mesh ) {
                   child.material = mat2;
                   absPos( child ); // call our function
            }

        } );
prisoner849
  • 16,894
  • 4
  • 34
  • 68
  • Hmm doesn´t seem to be the solution. To avoid own mistakes I just took the example from three.js https://github.com/mrdoob/three.js/blob/master/examples/webgl_loader_obj.html , added the function and absPos(object) below scene.add( object ); result: TypeError: myMesh.geometry is undefined – Gemelo Molinero Nov 29 '16 at 11:52
  • yeah, my fault. I've updated the answer. `objMesh` is an `object`, whose children are of `THREE.Mesh()`, and of course the `object` doesn't have the `geometry` property. – prisoner849 Nov 29 '16 at 12:16
  • uhm, nope not yet, remains myMesh.geometry is undefined pointing at myMesh.geometry.computeBoundingBox(); I assume the children don´t have a three.js typical geometry if imported from an .obj but thats just a guess – Gemelo Molinero Nov 29 '16 at 12:25
  • 1
    as an option, you can `console.log(myMesh)` and see in the browser console what your object consists of. – prisoner849 Nov 29 '16 at 12:32
  • as I expected there is no geometry in the object so this BoundingBox thing won´t work here, I guess have to go deeper into the objloader – Gemelo Molinero Nov 29 '16 at 13:04