SkeletonUtils.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. import {
  2. AnimationClip,
  3. AnimationMixer,
  4. Matrix4,
  5. Quaternion,
  6. QuaternionKeyframeTrack,
  7. SkeletonHelper,
  8. Vector3,
  9. VectorKeyframeTrack
  10. } from 'three';
  11. function retarget( target, source, options = {} ) {
  12. const pos = new Vector3(),
  13. quat = new Quaternion(),
  14. scale = new Vector3(),
  15. bindBoneMatrix = new Matrix4(),
  16. relativeMatrix = new Matrix4(),
  17. globalMatrix = new Matrix4();
  18. options.preserveMatrix = options.preserveMatrix !== undefined ? options.preserveMatrix : true;
  19. options.preservePosition = options.preservePosition !== undefined ? options.preservePosition : true;
  20. options.preserveHipPosition = options.preserveHipPosition !== undefined ? options.preserveHipPosition : false;
  21. options.useTargetMatrix = options.useTargetMatrix !== undefined ? options.useTargetMatrix : false;
  22. options.hip = options.hip !== undefined ? options.hip : 'hip';
  23. options.names = options.names || {};
  24. const sourceBones = source.isObject3D ? source.skeleton.bones : getBones( source ),
  25. bones = target.isObject3D ? target.skeleton.bones : getBones( target );
  26. let bindBones,
  27. bone, name, boneTo,
  28. bonesPosition;
  29. // reset bones
  30. if ( target.isObject3D ) {
  31. target.skeleton.pose();
  32. } else {
  33. options.useTargetMatrix = true;
  34. options.preserveMatrix = false;
  35. }
  36. if ( options.preservePosition ) {
  37. bonesPosition = [];
  38. for ( let i = 0; i < bones.length; i ++ ) {
  39. bonesPosition.push( bones[ i ].position.clone() );
  40. }
  41. }
  42. if ( options.preserveMatrix ) {
  43. // reset matrix
  44. target.updateMatrixWorld();
  45. target.matrixWorld.identity();
  46. // reset children matrix
  47. for ( let i = 0; i < target.children.length; ++ i ) {
  48. target.children[ i ].updateMatrixWorld( true );
  49. }
  50. }
  51. if ( options.offsets ) {
  52. bindBones = [];
  53. for ( let i = 0; i < bones.length; ++ i ) {
  54. bone = bones[ i ];
  55. name = options.names[ bone.name ] || bone.name;
  56. if ( options.offsets[ name ] ) {
  57. bone.matrix.multiply( options.offsets[ name ] );
  58. bone.matrix.decompose( bone.position, bone.quaternion, bone.scale );
  59. bone.updateMatrixWorld();
  60. }
  61. bindBones.push( bone.matrixWorld.clone() );
  62. }
  63. }
  64. for ( let i = 0; i < bones.length; ++ i ) {
  65. bone = bones[ i ];
  66. name = options.names[ bone.name ] || bone.name;
  67. boneTo = getBoneByName( name, sourceBones );
  68. globalMatrix.copy( bone.matrixWorld );
  69. if ( boneTo ) {
  70. boneTo.updateMatrixWorld();
  71. if ( options.useTargetMatrix ) {
  72. relativeMatrix.copy( boneTo.matrixWorld );
  73. } else {
  74. relativeMatrix.copy( target.matrixWorld ).invert();
  75. relativeMatrix.multiply( boneTo.matrixWorld );
  76. }
  77. // ignore scale to extract rotation
  78. scale.setFromMatrixScale( relativeMatrix );
  79. relativeMatrix.scale( scale.set( 1 / scale.x, 1 / scale.y, 1 / scale.z ) );
  80. // apply to global matrix
  81. globalMatrix.makeRotationFromQuaternion( quat.setFromRotationMatrix( relativeMatrix ) );
  82. if ( target.isObject3D ) {
  83. const boneIndex = bones.indexOf( bone ),
  84. wBindMatrix = bindBones ? bindBones[ boneIndex ] : bindBoneMatrix.copy( target.skeleton.boneInverses[ boneIndex ] ).invert();
  85. globalMatrix.multiply( wBindMatrix );
  86. }
  87. globalMatrix.copyPosition( relativeMatrix );
  88. }
  89. if ( bone.parent && bone.parent.isBone ) {
  90. bone.matrix.copy( bone.parent.matrixWorld ).invert();
  91. bone.matrix.multiply( globalMatrix );
  92. } else {
  93. bone.matrix.copy( globalMatrix );
  94. }
  95. if ( options.preserveHipPosition && name === options.hip ) {
  96. bone.matrix.setPosition( pos.set( 0, bone.position.y, 0 ) );
  97. }
  98. bone.matrix.decompose( bone.position, bone.quaternion, bone.scale );
  99. bone.updateMatrixWorld();
  100. }
  101. if ( options.preservePosition ) {
  102. for ( let i = 0; i < bones.length; ++ i ) {
  103. bone = bones[ i ];
  104. name = options.names[ bone.name ] || bone.name;
  105. if ( name !== options.hip ) {
  106. bone.position.copy( bonesPosition[ i ] );
  107. }
  108. }
  109. }
  110. if ( options.preserveMatrix ) {
  111. // restore matrix
  112. target.updateMatrixWorld( true );
  113. }
  114. }
  115. function retargetClip( target, source, clip, options = {} ) {
  116. options.useFirstFramePosition = options.useFirstFramePosition !== undefined ? options.useFirstFramePosition : false;
  117. // Calculate the fps from the source clip based on the track with the most frames, unless fps is already provided.
  118. options.fps = options.fps !== undefined ? options.fps : ( Math.max( ...clip.tracks.map( track => track.times.length ) ) / clip.duration );
  119. options.names = options.names || [];
  120. if ( ! source.isObject3D ) {
  121. source = getHelperFromSkeleton( source );
  122. }
  123. const numFrames = Math.round( clip.duration * ( options.fps / 1000 ) * 1000 ),
  124. delta = clip.duration / ( numFrames - 1 ),
  125. convertedTracks = [],
  126. mixer = new AnimationMixer( source ),
  127. bones = getBones( target.skeleton ),
  128. boneDatas = [];
  129. let positionOffset,
  130. bone, boneTo, boneData,
  131. name;
  132. mixer.clipAction( clip ).play();
  133. mixer.update( 0 );
  134. source.updateMatrixWorld();
  135. for ( let i = 0; i < numFrames; ++ i ) {
  136. const time = i * delta;
  137. retarget( target, source, options );
  138. for ( let j = 0; j < bones.length; ++ j ) {
  139. name = options.names[ bones[ j ].name ] || bones[ j ].name;
  140. boneTo = getBoneByName( name, source.skeleton );
  141. if ( boneTo ) {
  142. bone = bones[ j ];
  143. boneData = boneDatas[ j ] = boneDatas[ j ] || { bone: bone };
  144. if ( options.hip === name ) {
  145. if ( ! boneData.pos ) {
  146. boneData.pos = {
  147. times: new Float32Array( numFrames ),
  148. values: new Float32Array( numFrames * 3 )
  149. };
  150. }
  151. if ( options.useFirstFramePosition ) {
  152. if ( i === 0 ) {
  153. positionOffset = bone.position.clone();
  154. }
  155. bone.position.sub( positionOffset );
  156. }
  157. boneData.pos.times[ i ] = time;
  158. bone.position.toArray( boneData.pos.values, i * 3 );
  159. }
  160. if ( ! boneData.quat ) {
  161. boneData.quat = {
  162. times: new Float32Array( numFrames ),
  163. values: new Float32Array( numFrames * 4 )
  164. };
  165. }
  166. boneData.quat.times[ i ] = time;
  167. bone.quaternion.toArray( boneData.quat.values, i * 4 );
  168. }
  169. }
  170. if ( i === numFrames - 2 ) {
  171. // last mixer update before final loop iteration
  172. // make sure we do not go over or equal to clip duration
  173. mixer.update( delta - 0.0000001 );
  174. } else {
  175. mixer.update( delta );
  176. }
  177. source.updateMatrixWorld();
  178. }
  179. for ( let i = 0; i < boneDatas.length; ++ i ) {
  180. boneData = boneDatas[ i ];
  181. if ( boneData ) {
  182. if ( boneData.pos ) {
  183. convertedTracks.push( new VectorKeyframeTrack(
  184. '.bones[' + boneData.bone.name + '].position',
  185. boneData.pos.times,
  186. boneData.pos.values
  187. ) );
  188. }
  189. convertedTracks.push( new QuaternionKeyframeTrack(
  190. '.bones[' + boneData.bone.name + '].quaternion',
  191. boneData.quat.times,
  192. boneData.quat.values
  193. ) );
  194. }
  195. }
  196. mixer.uncacheAction( clip );
  197. return new AnimationClip( clip.name, - 1, convertedTracks );
  198. }
  199. function clone( source ) {
  200. const sourceLookup = new Map();
  201. const cloneLookup = new Map();
  202. const clone = source.clone();
  203. parallelTraverse( source, clone, function ( sourceNode, clonedNode ) {
  204. sourceLookup.set( clonedNode, sourceNode );
  205. cloneLookup.set( sourceNode, clonedNode );
  206. } );
  207. clone.traverse( function ( node ) {
  208. if ( ! node.isSkinnedMesh ) return;
  209. const clonedMesh = node;
  210. const sourceMesh = sourceLookup.get( node );
  211. const sourceBones = sourceMesh.skeleton.bones;
  212. clonedMesh.skeleton = sourceMesh.skeleton.clone();
  213. clonedMesh.bindMatrix.copy( sourceMesh.bindMatrix );
  214. clonedMesh.skeleton.bones = sourceBones.map( function ( bone ) {
  215. return cloneLookup.get( bone );
  216. } );
  217. clonedMesh.bind( clonedMesh.skeleton, clonedMesh.bindMatrix );
  218. } );
  219. return clone;
  220. }
  221. // internal helper
  222. function getBoneByName( name, skeleton ) {
  223. for ( let i = 0, bones = getBones( skeleton ); i < bones.length; i ++ ) {
  224. if ( name === bones[ i ].name )
  225. return bones[ i ];
  226. }
  227. }
  228. function getBones( skeleton ) {
  229. return Array.isArray( skeleton ) ? skeleton : skeleton.bones;
  230. }
  231. function getHelperFromSkeleton( skeleton ) {
  232. const source = new SkeletonHelper( skeleton.bones[ 0 ] );
  233. source.skeleton = skeleton;
  234. return source;
  235. }
  236. function parallelTraverse( a, b, callback ) {
  237. callback( a, b );
  238. for ( let i = 0; i < a.children.length; i ++ ) {
  239. parallelTraverse( a.children[ i ], b.children[ i ], callback );
  240. }
  241. }
  242. export {
  243. retarget,
  244. retargetClip,
  245. clone,
  246. };