Skip to content

Commit a9cde37

Browse files
author
Don McCurdy
committed
AnimationUtils: Add clone() helper for skinned meshes.
1 parent f7d292d commit a9cde37

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

src/animation/AnimationUtils.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,58 @@ 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+
187+
clonedMesh.skeleton.bones = sourceBones.map( function ( bone ) {
188+
189+
return cloneLookup.get( bone );
190+
191+
} );
192+
193+
clonedMesh.bind( clonedMesh.skeleton, clonedMesh.bindMatrix );
194+
195+
} );
196+
197+
return clone;
198+
161199
}
162200

163201
};
164202

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

166215
export { AnimationUtils };

0 commit comments

Comments
 (0)