Skip to content

Commit b5a16a5

Browse files
Don McCurdydonmccurdy
authored andcommitted
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.
1 parent e25ab6b commit b5a16a5

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

src/animation/AnimationUtils.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,59 @@ var AnimationUtils = {
158158

159159
}
160160

161+
},
162+
163+
clone: function ( source ) {
164+
165+
var sourceLookup = new Map();
166+
var cloneLookup = new Map();
167+
168+
var clone = source.clone();
169+
170+
parallelTraverse( source, clone, function ( sourceNode, clonedNode ) {
171+
172+
sourceLookup.set( clonedNode, sourceNode );
173+
cloneLookup.set( sourceNode, clonedNode );
174+
175+
} );
176+
177+
clone.traverse( function ( node ) {
178+
179+
if ( ! node.isSkinnedMesh ) return;
180+
181+
var clonedMesh = node;
182+
var sourceMesh = sourceLookup.get( node );
183+
var sourceBones = sourceMesh.skeleton.bones;
184+
185+
clonedMesh.skeleton = sourceMesh.skeleton.clone();
186+
clonedMesh.bindMatrix.copy( sourceMesh.bindMatrix );
187+
188+
clonedMesh.skeleton.bones = sourceBones.map( function ( bone ) {
189+
190+
return cloneLookup.get( bone );
191+
192+
} );
193+
194+
clonedMesh.bind( clonedMesh.skeleton, clonedMesh.bindMatrix );
195+
196+
} );
197+
198+
return clone;
199+
161200
}
162201

163202
};
164203

204+
function parallelTraverse ( a, b, callback ) {
205+
206+
callback( a, b );
207+
208+
for ( var i = 0; i < a.children.length; i ++ ) {
209+
210+
parallelTraverse( a.children[ i ], b.children[ i ], callback );
211+
212+
}
213+
214+
}
165215

166216
export { AnimationUtils };

0 commit comments

Comments
 (0)