SkeletonUtils.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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. options.fps = options.fps !== undefined ? options.fps : 30;
  118. options.names = options.names || [];
  119. if ( ! source.isObject3D ) {
  120. source = getHelperFromSkeleton( source );
  121. }
  122. const numFrames = Math.round( clip.duration * ( options.fps / 1000 ) * 1000 ),
  123. delta = 1 / options.fps,
  124. convertedTracks = [],
  125. mixer = new AnimationMixer( source ),
  126. bones = getBones( target.skeleton ),
  127. boneDatas = [];
  128. let positionOffset,
  129. bone, boneTo, boneData,
  130. name;
  131. mixer.clipAction( clip ).play();
  132. mixer.update( 0 );
  133. source.updateMatrixWorld();
  134. for ( let i = 0; i < numFrames; ++ i ) {
  135. const time = i * delta;
  136. retarget( target, source, options );
  137. for ( let j = 0; j < bones.length; ++ j ) {
  138. name = options.names[ bones[ j ].name ] || bones[ j ].name;
  139. boneTo = getBoneByName( name, source.skeleton );
  140. if ( boneTo ) {
  141. bone = bones[ j ];
  142. boneData = boneDatas[ j ] = boneDatas[ j ] || { bone: bone };
  143. if ( options.hip === name ) {
  144. if ( ! boneData.pos ) {
  145. boneData.pos = {
  146. times: new Float32Array( numFrames ),
  147. values: new Float32Array( numFrames * 3 )
  148. };
  149. }
  150. if ( options.useFirstFramePosition ) {
  151. if ( i === 0 ) {
  152. positionOffset = bone.position.clone();
  153. }
  154. bone.position.sub( positionOffset );
  155. }
  156. boneData.pos.times[ i ] = time;
  157. bone.position.toArray( boneData.pos.values, i * 3 );
  158. }
  159. if ( ! boneData.quat ) {
  160. boneData.quat = {
  161. times: new Float32Array( numFrames ),
  162. values: new Float32Array( numFrames * 4 )
  163. };
  164. }
  165. boneData.quat.times[ i ] = time;
  166. bone.quaternion.toArray( boneData.quat.values, i * 4 );
  167. }
  168. }
  169. mixer.update( delta );
  170. source.updateMatrixWorld();
  171. }
  172. for ( let i = 0; i < boneDatas.length; ++ i ) {
  173. boneData = boneDatas[ i ];
  174. if ( boneData ) {
  175. if ( boneData.pos ) {
  176. convertedTracks.push( new VectorKeyframeTrack(
  177. '.bones[' + boneData.bone.name + '].position',
  178. boneData.pos.times,
  179. boneData.pos.values
  180. ) );
  181. }
  182. convertedTracks.push( new QuaternionKeyframeTrack(
  183. '.bones[' + boneData.bone.name + '].quaternion',
  184. boneData.quat.times,
  185. boneData.quat.values
  186. ) );
  187. }
  188. }
  189. mixer.uncacheAction( clip );
  190. return new AnimationClip( clip.name, - 1, convertedTracks );
  191. }
  192. function clone( source ) {
  193. const sourceLookup = new Map();
  194. const cloneLookup = new Map();
  195. const clone = source.clone();
  196. parallelTraverse( source, clone, function ( sourceNode, clonedNode ) {
  197. sourceLookup.set( clonedNode, sourceNode );
  198. cloneLookup.set( sourceNode, clonedNode );
  199. } );
  200. clone.traverse( function ( node ) {
  201. if ( ! node.isSkinnedMesh ) return;
  202. const clonedMesh = node;
  203. const sourceMesh = sourceLookup.get( node );
  204. const sourceBones = sourceMesh.skeleton.bones;
  205. clonedMesh.skeleton = sourceMesh.skeleton.clone();
  206. clonedMesh.bindMatrix.copy( sourceMesh.bindMatrix );
  207. clonedMesh.skeleton.bones = sourceBones.map( function ( bone ) {
  208. return cloneLookup.get( bone );
  209. } );
  210. clonedMesh.bind( clonedMesh.skeleton, clonedMesh.bindMatrix );
  211. } );
  212. return clone;
  213. }
  214. // internal helper
  215. function getBoneByName( name, skeleton ) {
  216. for ( let i = 0, bones = getBones( skeleton ); i < bones.length; i ++ ) {
  217. if ( name === bones[ i ].name )
  218. return bones[ i ];
  219. }
  220. }
  221. function getBones( skeleton ) {
  222. return Array.isArray( skeleton ) ? skeleton : skeleton.bones;
  223. }
  224. function getHelperFromSkeleton( skeleton ) {
  225. const source = new SkeletonHelper( skeleton.bones[ 0 ] );
  226. source.skeleton = skeleton;
  227. return source;
  228. }
  229. function parallelTraverse( a, b, callback ) {
  230. callback( a, b );
  231. for ( let i = 0; i < a.children.length; i ++ ) {
  232. parallelTraverse( a.children[ i ], b.children[ i ], callback );
  233. }
  234. }
  235. export {
  236. retarget,
  237. retargetClip,
  238. clone,
  239. };