SkeletonUtils.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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 + 1 ),
  147. values: new Float32Array( ( numFrames + 1 ) * 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. // `numFrames + 1` accomodate final keyframe
  161. boneData.quat = {
  162. times: new Float32Array( numFrames + 1 ),
  163. values: new Float32Array( ( numFrames + 1 ) * 4 )
  164. };
  165. }
  166. boneData.quat.times[ i ] = time;
  167. bone.quaternion.toArray( boneData.quat.values, i * 4 );
  168. }
  169. }
  170. // check for final keyframe
  171. if ( i === numFrames - 1 ) {
  172. mixer.update( Math.max( clip.duration - mixer.time, 0 ) );
  173. } else {
  174. mixer.update( delta );
  175. }
  176. source.updateMatrixWorld();
  177. }
  178. retarget( target, source, options );
  179. // add boneData at final keyframe
  180. for ( let j = 0; j < boneDatas.length; ++ j ) {
  181. boneData = boneDatas[ j ];
  182. if ( boneData ) {
  183. if ( boneData.pos ) {
  184. boneData.pos.times[ numFrames ] = clip.duration;
  185. bone.position.toArray( boneData.pos.values, numFrames * 3 );
  186. }
  187. if ( boneData.quat ) {
  188. boneData.quat.times[ numFrames ] = clip.duration;
  189. bone.position.toArray( boneData.quat.values, numFrames * 4 );
  190. }
  191. }
  192. }
  193. for ( let i = 0; i < boneDatas.length; ++ i ) {
  194. boneData = boneDatas[ i ];
  195. if ( boneData ) {
  196. if ( boneData.pos ) {
  197. convertedTracks.push( new VectorKeyframeTrack(
  198. '.bones[' + boneData.bone.name + '].position',
  199. boneData.pos.times,
  200. boneData.pos.values
  201. ) );
  202. }
  203. convertedTracks.push( new QuaternionKeyframeTrack(
  204. '.bones[' + boneData.bone.name + '].quaternion',
  205. boneData.quat.times,
  206. boneData.quat.values
  207. ) );
  208. }
  209. }
  210. mixer.uncacheAction( clip );
  211. return new AnimationClip( clip.name, - 1, convertedTracks );
  212. }
  213. function clone( source ) {
  214. const sourceLookup = new Map();
  215. const cloneLookup = new Map();
  216. const clone = source.clone();
  217. parallelTraverse( source, clone, function ( sourceNode, clonedNode ) {
  218. sourceLookup.set( clonedNode, sourceNode );
  219. cloneLookup.set( sourceNode, clonedNode );
  220. } );
  221. clone.traverse( function ( node ) {
  222. if ( ! node.isSkinnedMesh ) return;
  223. const clonedMesh = node;
  224. const sourceMesh = sourceLookup.get( node );
  225. const sourceBones = sourceMesh.skeleton.bones;
  226. clonedMesh.skeleton = sourceMesh.skeleton.clone();
  227. clonedMesh.bindMatrix.copy( sourceMesh.bindMatrix );
  228. clonedMesh.skeleton.bones = sourceBones.map( function ( bone ) {
  229. return cloneLookup.get( bone );
  230. } );
  231. clonedMesh.bind( clonedMesh.skeleton, clonedMesh.bindMatrix );
  232. } );
  233. return clone;
  234. }
  235. // internal helper
  236. function getBoneByName( name, skeleton ) {
  237. for ( let i = 0, bones = getBones( skeleton ); i < bones.length; i ++ ) {
  238. if ( name === bones[ i ].name )
  239. return bones[ i ];
  240. }
  241. }
  242. function getBones( skeleton ) {
  243. return Array.isArray( skeleton ) ? skeleton : skeleton.bones;
  244. }
  245. function getHelperFromSkeleton( skeleton ) {
  246. const source = new SkeletonHelper( skeleton.bones[ 0 ] );
  247. source.skeleton = skeleton;
  248. return source;
  249. }
  250. function parallelTraverse( a, b, callback ) {
  251. callback( a, b );
  252. for ( let i = 0; i < a.children.length; i ++ ) {
  253. parallelTraverse( a.children[ i ], b.children[ i ], callback );
  254. }
  255. }
  256. export {
  257. retarget,
  258. retargetClip,
  259. clone,
  260. };