浏览代码

AnimationUtils: Add clone() helper for skinned meshes.

AnimationUtils: Clean up lint errors.

AnimationUtils: Ensure .clone() does not depend on node names.

GLTFLoader: Revert automatic node naming.
Don McCurdy 7 年之前
父节点
当前提交
b5a16a5a47
共有 1 个文件被更改,包括 50 次插入0 次删除
  1. 50 0
      src/animation/AnimationUtils.js

+ 50 - 0
src/animation/AnimationUtils.js

@@ -158,9 +158,59 @@ var AnimationUtils = {
 
 		}
 
+	},
+
+	clone: function ( source ) {
+
+		var sourceLookup = new Map();
+		var cloneLookup = new Map();
+
+		var clone = source.clone();
+
+		parallelTraverse( source, clone, function ( sourceNode, clonedNode ) {
+
+			sourceLookup.set( clonedNode, sourceNode );
+			cloneLookup.set( sourceNode, clonedNode );
+
+		} );
+
+		clone.traverse( function ( node ) {
+
+			if ( ! node.isSkinnedMesh ) return;
+
+			var clonedMesh = node;
+			var sourceMesh = sourceLookup.get( node );
+			var sourceBones = sourceMesh.skeleton.bones;
+
+			clonedMesh.skeleton = sourceMesh.skeleton.clone();
+			clonedMesh.bindMatrix.copy( sourceMesh.bindMatrix );
+
+			clonedMesh.skeleton.bones = sourceBones.map( function ( bone ) {
+
+				return cloneLookup.get( bone );
+
+			} );
+
+			clonedMesh.bind( clonedMesh.skeleton, clonedMesh.bindMatrix );
+
+		} );
+
+		return clone;
+
 	}
 
 };
 
+function parallelTraverse ( a, b, callback ) {
+
+	callback( a, b );
+
+	for ( var i = 0; i < a.children.length; i ++ ) {
+
+		parallelTraverse( a.children[ i ], b.children[ i ], callback );
+
+	}
+
+}
 
 export { AnimationUtils };