Skip to content

Commit

Permalink
Merge pull request #14954 from looeee/fbxloader_morph_multimat
Browse files Browse the repository at this point in the history
FBXLoader: set morphTargets=true on multi-materials
  • Loading branch information
mrdoob committed Sep 22, 2018
2 parents 1e9e2b0 + b4c8522 commit a6db06c
Showing 1 changed file with 48 additions and 15 deletions.
63 changes: 48 additions & 15 deletions examples/js/loaders/FBXLoader.js
Expand Up @@ -1424,31 +1424,26 @@ THREE.FBXLoader = ( function () {

setupMorphMaterials: function () {

var self = this;
sceneGraph.traverse( function ( child ) {

if ( child.isMesh ) {

if ( child.geometry.morphAttributes.position || child.geometry.morphAttributes.normal ) {

var uuid = child.uuid;
var matUuid = child.material.uuid;
if ( child.geometry.morphAttributes.position && child.geometry.morphAttributes.position.length ) {

// if a geometry has morph targets, it cannot share the material with other geometries
var sharedMat = false;
if ( Array.isArray( child.material ) ) {

sceneGraph.traverse( function ( child ) {
child.material.forEach( function ( material, i ) {

if ( child.isMesh ) {
self.setupMorphMaterial( child, material, i );

if ( child.material.uuid === matUuid && child.uuid !== uuid ) sharedMat = true;

}
} );

} );
} else {

if ( sharedMat === true ) child.material = child.material.clone();
self.setupMorphMaterial( child, child.material );

child.material.morphTargets = true;
}

}

Expand All @@ -1458,6 +1453,44 @@ THREE.FBXLoader = ( function () {

},

setupMorphMaterial: function ( child, material, index ) {

var uuid = child.uuid;
var matUuid = material.uuid;

// if a geometry has morph targets, it cannot share the material with other geometries
var sharedMat = false;

sceneGraph.traverse( function ( node ) {

if ( node.isMesh ) {

if ( Array.isArray( node.material ) ) {

node.material.forEach( function ( mat ) {

if ( mat.uuid === matUuid && node.uuid !== uuid ) sharedMat = true;

} );

} else if ( node.material.uuid === matUuid && node.uuid !== uuid ) sharedMat = true;

}

} );

if ( sharedMat === true ) {

var clonedMat = material.clone();
clonedMat.morphTargets = true;

if ( index === undefined ) child.material = clonedMat;
else child.material[ index ] = clonedMat;

} else material.morphTargets = true;

}

};

// parse Geometry data from FBXTree and return map of BufferGeometries
Expand Down Expand Up @@ -2608,7 +2641,7 @@ THREE.FBXLoader = ( function () {
var initialRotation = new THREE.Quaternion();
var initialScale = new THREE.Vector3();

rawTracks.transform.decompose( initialPosition, initialRotation, initialScale );
if ( rawTracks.transform ) rawTracks.transform.decompose( initialPosition, initialRotation, initialScale );

initialPosition = initialPosition.toArray();
initialRotation = new THREE.Euler().setFromQuaternion( initialRotation, rawTracks.eulerOrder ).toArray();
Expand Down

0 comments on commit a6db06c

Please sign in to comment.