6
votes

Slightly complex, so bear with me:

  • Ray intersect works perfectly when an object has no morphTargets.

  • When an object has morphTargets only the original position can be intersected, that is to say, if I morph a model from 0,0,0 to 50,50,50 the ray will not intersect with the object at 50,50,50, instead, when I mouse over 0,0,0 I get an intersection (even though the object is no longer there!?).

Is there some sort of flag I need to turn on to make three.js aware that the verts have moved?

Edit, code added.

This makes my mesh and adds it to the objects array (which ray intersect uses):

function createDeer( deerGeometry, materials ) {
    mesh = new THREE.MorphAnimMesh( deerGeometry, new THREE.MeshLambertMaterial( { color: 0xE8E8E8, ambient: 0xE8E8E8, morphTargets: true, vertexColors: THREE.FaceColors } ) );
    mesh.scale.set( 3, 3, 3 );
    mesh.position.set( 0, -3, 0 );
    mesh.rotation.set( 0, 0, 0 );
    mesh.castShadow = true;
    mesh.receiveShadow  = true;
    mesh.geometry.dynamic = true;
    scene.add( mesh );
    objects.push( mesh );
}

Ray intersection happens on mouseDown (there's a mouseOver as well, same thing), like I said, the code works fine, it's just intersecting with the original unmorphed mesh:

function onDocumentMouseDown( event ) {

    event.preventDefault();

    var vector = new THREE.Vector3( mouse.x, mouse.y, 0.5 );
    projector.unprojectVector( vector, camera );

    var ray = new THREE.Ray( camera.position, vector.subSelf( camera.position ).normalize() );

    var intersects = ray.intersectObjects( objects );

    if ( intersects.length > 0 ) {

        SELECTED = intersects[ 0 ].object;

        for(var i=0; i<objects.length; i++)
            { 
                if(SELECTED.position.x == objects[0].position.x) {
                    thisObject = i; 
                    }
                }

            }

        var intersects = ray.intersectObject( plane );

        container.style.cursor = 'pointer';
    }

}

I've decided the problem must be related to the fact that the position of the deer (as in the mesh transform) never changes, however the vertices do move away, and as the ray intersect is comparing object positions perhaps the problem is here?

2

2 Answers

1
votes

I've made a pull request that has been merged and fixes this.

Note that for it to work, the boundingSphere of the object needs to contain the full extent of the morphing

0
votes

The MorphTarget animation takes place entirely on GPU (in the shaders code) while the ray intersection is always computed on CPU. So in fact, there's no easy way to achieve what you're describing here.