Skip to content

Commit

Permalink
Mesh: Added support for morph targets with BufferGeometry in raycast().
Browse files Browse the repository at this point in the history
  • Loading branch information
Mugen87 committed Mar 15, 2019
1 parent 82974f2 commit 7c27e47
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 9 deletions.
32 changes: 28 additions & 4 deletions examples/webgl_morphtargets.html
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@

var container;

var camera, scene, renderer;
var camera, scene, renderer, raycaster;

var mesh;
var mesh, mouse = new THREE.Vector2();

init();
animate();
Expand All @@ -69,15 +69,16 @@
scene.background = new THREE.Color( 0x222222 );
scene.fog = new THREE.Fog( 0x000000, 1, 15000 );

var light = new THREE.PointLight( 0xff2200 );
var light = new THREE.PointLight( 0xffffff );
light.position.z = 500;
camera.add( light );
scene.add( camera );

var light = new THREE.AmbientLight( 0x111111 );
scene.add( light );

var geometry = new THREE.BoxGeometry( 100, 100, 100 );
var material = new THREE.MeshLambertMaterial( { color: 0xffffff, morphTargets: true } );
var material = new THREE.MeshLambertMaterial( { color: 0xff0000, morphTargets: true } );

// construct 8 blend shapes

Expand Down Expand Up @@ -169,6 +170,8 @@

//

raycaster = new THREE.Raycaster();

renderer = new THREE.WebGLRenderer();
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
Expand All @@ -184,6 +187,27 @@

window.addEventListener( 'resize', onWindowResize, false );

document.addEventListener( 'click', onClick, false );

}

function onClick( event ) {

event.preventDefault();

mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;

raycaster.setFromCamera( mouse, camera );

var intersects = raycaster.intersectObject( mesh );

if ( intersects.length > 0 ) {

mesh.material.color.set( Math.random() * 0xffffff );

}

}

function onWindowResize() {
Expand Down
46 changes: 41 additions & 5 deletions src/objects/Mesh.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ Mesh.prototype = Object.assign( Object.create( Object3D.prototype ), {
var tempB = new Vector3();
var tempC = new Vector3();

var morphA = new Vector3();
var morphB = new Vector3();
var morphC = new Vector3();

var uvA = new Vector2();
var uvB = new Vector2();
var uvC = new Vector2();
Expand Down Expand Up @@ -164,12 +168,43 @@ Mesh.prototype = Object.assign( Object.create( Object3D.prototype ), {

}

function checkBufferGeometryIntersection( object, material, raycaster, ray, position, uv, a, b, c ) {
function checkBufferGeometryIntersection( object, material, raycaster, ray, position, morphPosition, uv, a, b, c ) {

vA.fromBufferAttribute( position, a );
vB.fromBufferAttribute( position, b );
vC.fromBufferAttribute( position, c );

var morphInfluences = object.morphTargetInfluences;

if ( material.morphTargets && morphPosition && morphInfluences ) {

morphA.set( 0, 0, 0 );
morphB.set( 0, 0, 0 );
morphC.set( 0, 0, 0 );

for ( var i = 0, il = morphPosition.length; i < il; i ++ ) {

var influence = morphInfluences[ i ];
var morphAttribute = morphPosition[ i ];

if ( influence === 0 ) continue;

tempA.fromBufferAttribute( morphAttribute, a );
tempB.fromBufferAttribute( morphAttribute, b );
tempC.fromBufferAttribute( morphAttribute, c );

morphA.addScaledVector( tempA.sub( vA ), influence );
morphB.addScaledVector( tempB.sub( vB ), influence );
morphC.addScaledVector( tempC.sub( vC ), influence );

}

vA.add( morphA );
vB.add( morphB );
vC.add( morphC );

}

var intersection = checkIntersection( object, material, raycaster, ray, vA, vB, vC, intersectionPoint );

if ( intersection ) {
Expand Down Expand Up @@ -232,6 +267,7 @@ Mesh.prototype = Object.assign( Object.create( Object3D.prototype ), {
var a, b, c;
var index = geometry.index;
var position = geometry.attributes.position;
var morphPosition = geometry.morphAttributes.position;
var uv = geometry.attributes.uv;
var groups = geometry.groups;
var drawRange = geometry.drawRange;
Expand Down Expand Up @@ -259,7 +295,7 @@ Mesh.prototype = Object.assign( Object.create( Object3D.prototype ), {
b = index.getX( j + 1 );
c = index.getX( j + 2 );

intersection = checkBufferGeometryIntersection( this, groupMaterial, raycaster, ray, position, uv, a, b, c );
intersection = checkBufferGeometryIntersection( this, groupMaterial, raycaster, ray, position, morphPosition, uv, a, b, c );

if ( intersection ) {

Expand All @@ -284,7 +320,7 @@ Mesh.prototype = Object.assign( Object.create( Object3D.prototype ), {
b = index.getX( i + 1 );
c = index.getX( i + 2 );

intersection = checkBufferGeometryIntersection( this, material, raycaster, ray, position, uv, a, b, c );
intersection = checkBufferGeometryIntersection( this, material, raycaster, ray, position, morphPosition, uv, a, b, c );

if ( intersection ) {

Expand Down Expand Up @@ -317,7 +353,7 @@ Mesh.prototype = Object.assign( Object.create( Object3D.prototype ), {
b = j + 1;
c = j + 2;

intersection = checkBufferGeometryIntersection( this, groupMaterial, raycaster, ray, position, uv, a, b, c );
intersection = checkBufferGeometryIntersection( this, groupMaterial, raycaster, ray, position, morphPosition, uv, a, b, c );

if ( intersection ) {

Expand All @@ -342,7 +378,7 @@ Mesh.prototype = Object.assign( Object.create( Object3D.prototype ), {
b = i + 1;
c = i + 2;

intersection = checkBufferGeometryIntersection( this, material, raycaster, ray, position, uv, a, b, c );
intersection = checkBufferGeometryIntersection( this, material, raycaster, ray, position, morphPosition, uv, a, b, c );

if ( intersection ) {

Expand Down

0 comments on commit 7c27e47

Please sign in to comment.