SkeletonUtils.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. import {
  2. AnimationClip,
  3. AnimationMixer,
  4. Matrix4,
  5. Quaternion,
  6. QuaternionKeyframeTrack,
  7. SkeletonHelper,
  8. Vector3,
  9. VectorKeyframeTrack
  10. } from 'three';
  11. /**
  12. * @module SkeletonUtils
  13. * @three_import import * as SkeletonUtils from 'three/addons/utils/SkeletonUtils.js';
  14. */
  15. function getBoneName( bone, options ) {
  16. if ( options.getBoneName !== undefined ) {
  17. return options.getBoneName( bone );
  18. }
  19. return options.names[ bone.name ];
  20. }
  21. /**
  22. * Retargets the skeleton from the given source 3D object to the
  23. * target 3D object.
  24. *
  25. * @param {Object3D} target - The target 3D object.
  26. * @param {Object3D} source - The source 3D object.
  27. * @param {module:SkeletonUtils~RetargetOptions} options - The options.
  28. */
  29. function retarget( target, source, options = {} ) {
  30. const quat = new Quaternion(),
  31. scale = new Vector3(),
  32. relativeMatrix = new Matrix4(),
  33. globalMatrix = new Matrix4();
  34. options.preserveBoneMatrix = options.preserveBoneMatrix !== undefined ? options.preserveBoneMatrix : true;
  35. options.preserveBonePositions = options.preserveBonePositions !== undefined ? options.preserveBonePositions : true;
  36. options.useTargetMatrix = options.useTargetMatrix !== undefined ? options.useTargetMatrix : false;
  37. options.hip = options.hip !== undefined ? options.hip : 'hip';
  38. options.hipInfluence = options.hipInfluence !== undefined ? options.hipInfluence : new Vector3( 1, 1, 1 );
  39. options.scale = options.scale !== undefined ? options.scale : 1;
  40. options.names = options.names || {};
  41. const sourceBones = source.isObject3D ? source.skeleton.bones : getBones( source ),
  42. bones = target.isObject3D ? target.skeleton.bones : getBones( target );
  43. let bone, name, boneTo,
  44. bonesPosition;
  45. // reset bones
  46. if ( target.isObject3D ) {
  47. target.skeleton.pose();
  48. } else {
  49. options.useTargetMatrix = true;
  50. options.preserveBoneMatrix = false;
  51. }
  52. if ( options.preserveBonePositions ) {
  53. bonesPosition = [];
  54. for ( let i = 0; i < bones.length; i ++ ) {
  55. bonesPosition.push( bones[ i ].position.clone() );
  56. }
  57. }
  58. if ( options.preserveBoneMatrix ) {
  59. // reset matrix
  60. target.updateMatrixWorld();
  61. target.matrixWorld.identity();
  62. // reset children matrix
  63. for ( let i = 0; i < target.children.length; ++ i ) {
  64. target.children[ i ].updateMatrixWorld( true );
  65. }
  66. }
  67. for ( let i = 0; i < bones.length; ++ i ) {
  68. bone = bones[ i ];
  69. name = getBoneName( bone, options );
  70. boneTo = getBoneByName( name, sourceBones );
  71. globalMatrix.copy( bone.matrixWorld );
  72. if ( boneTo ) {
  73. boneTo.updateMatrixWorld();
  74. if ( options.useTargetMatrix ) {
  75. relativeMatrix.copy( boneTo.matrixWorld );
  76. } else {
  77. relativeMatrix.copy( target.matrixWorld ).invert();
  78. relativeMatrix.multiply( boneTo.matrixWorld );
  79. }
  80. // ignore scale to extract rotation
  81. scale.setFromMatrixScale( relativeMatrix );
  82. relativeMatrix.scale( scale.set( 1 / scale.x, 1 / scale.y, 1 / scale.z ) );
  83. // apply to global matrix
  84. globalMatrix.makeRotationFromQuaternion( quat.setFromRotationMatrix( relativeMatrix ) );
  85. if ( target.isObject3D ) {
  86. if ( options.localOffsets ) {
  87. if ( options.localOffsets[ bone.name ] ) {
  88. globalMatrix.multiply( options.localOffsets[ bone.name ] );
  89. }
  90. }
  91. }
  92. globalMatrix.copyPosition( relativeMatrix );
  93. }
  94. if ( name === options.hip ) {
  95. globalMatrix.elements[ 12 ] *= options.scale * options.hipInfluence.x;
  96. globalMatrix.elements[ 13 ] *= options.scale * options.hipInfluence.y;
  97. globalMatrix.elements[ 14 ] *= options.scale * options.hipInfluence.z;
  98. if ( options.hipPosition !== undefined ) {
  99. globalMatrix.elements[ 12 ] += options.hipPosition.x * options.scale;
  100. globalMatrix.elements[ 13 ] += options.hipPosition.y * options.scale;
  101. globalMatrix.elements[ 14 ] += options.hipPosition.z * options.scale;
  102. }
  103. }
  104. if ( bone.parent ) {
  105. bone.matrix.copy( bone.parent.matrixWorld ).invert();
  106. bone.matrix.multiply( globalMatrix );
  107. } else {
  108. bone.matrix.copy( globalMatrix );
  109. }
  110. bone.matrix.decompose( bone.position, bone.quaternion, bone.scale );
  111. bone.updateMatrixWorld();
  112. }
  113. if ( options.preserveBonePositions ) {
  114. for ( let i = 0; i < bones.length; ++ i ) {
  115. bone = bones[ i ];
  116. name = getBoneName( bone, options ) || bone.name;
  117. if ( name !== options.hip ) {
  118. bone.position.copy( bonesPosition[ i ] );
  119. }
  120. }
  121. }
  122. if ( options.preserveBoneMatrix ) {
  123. // restore matrix
  124. target.updateMatrixWorld( true );
  125. }
  126. }
  127. /**
  128. * Retargets the animation clip of the source object to the
  129. * target 3D object.
  130. *
  131. * @param {Object3D} target - The target 3D object.
  132. * @param {Object3D} source - The source 3D object.
  133. * @param {AnimationClip} clip - The animation clip.
  134. * @param {module:SkeletonUtils~RetargetOptions} options - The options.
  135. * @return {AnimationClip} The retargeted animation clip.
  136. */
  137. function retargetClip( target, source, clip, options = {} ) {
  138. options.useFirstFramePosition = options.useFirstFramePosition !== undefined ? options.useFirstFramePosition : false;
  139. // Calculate the fps from the source clip based on the track with the most frames, unless fps is already provided.
  140. options.fps = options.fps !== undefined ? options.fps : ( Math.max( ...clip.tracks.map( track => track.times.length ) ) / clip.duration );
  141. options.names = options.names || [];
  142. if ( ! source.isObject3D ) {
  143. source = getHelperFromSkeleton( source );
  144. }
  145. const numFrames = Math.round( clip.duration * ( options.fps / 1000 ) * 1000 ),
  146. delta = clip.duration / ( numFrames - 1 ),
  147. convertedTracks = [],
  148. mixer = new AnimationMixer( source ),
  149. bones = getBones( target.skeleton ),
  150. boneDatas = [];
  151. let positionOffset,
  152. bone, boneTo, boneData,
  153. name;
  154. mixer.clipAction( clip ).play();
  155. // trim
  156. let start = 0, end = numFrames;
  157. if ( options.trim !== undefined ) {
  158. start = Math.round( options.trim[ 0 ] * options.fps );
  159. end = Math.min( Math.round( options.trim[ 1 ] * options.fps ), numFrames ) - start;
  160. mixer.update( options.trim[ 0 ] );
  161. } else {
  162. mixer.update( 0 );
  163. }
  164. source.updateMatrixWorld();
  165. //
  166. for ( let frame = 0; frame < end; ++ frame ) {
  167. const time = frame * delta;
  168. retarget( target, source, options );
  169. for ( let j = 0; j < bones.length; ++ j ) {
  170. bone = bones[ j ];
  171. name = getBoneName( bone, options ) || bone.name;
  172. boneTo = getBoneByName( name, source.skeleton );
  173. if ( boneTo ) {
  174. boneData = boneDatas[ j ] = boneDatas[ j ] || { bone: bone };
  175. if ( options.hip === name ) {
  176. if ( ! boneData.pos ) {
  177. boneData.pos = {
  178. times: new Float32Array( end ),
  179. values: new Float32Array( end * 3 )
  180. };
  181. }
  182. if ( options.useFirstFramePosition ) {
  183. if ( frame === 0 ) {
  184. positionOffset = bone.position.clone();
  185. }
  186. bone.position.sub( positionOffset );
  187. }
  188. boneData.pos.times[ frame ] = time;
  189. bone.position.toArray( boneData.pos.values, frame * 3 );
  190. }
  191. if ( ! boneData.quat ) {
  192. boneData.quat = {
  193. times: new Float32Array( end ),
  194. values: new Float32Array( end * 4 )
  195. };
  196. }
  197. boneData.quat.times[ frame ] = time;
  198. bone.quaternion.toArray( boneData.quat.values, frame * 4 );
  199. }
  200. }
  201. if ( frame === end - 2 ) {
  202. // last mixer update before final loop iteration
  203. // make sure we do not go over or equal to clip duration
  204. mixer.update( delta - 0.0000001 );
  205. } else {
  206. mixer.update( delta );
  207. }
  208. source.updateMatrixWorld();
  209. }
  210. for ( let i = 0; i < boneDatas.length; ++ i ) {
  211. boneData = boneDatas[ i ];
  212. if ( boneData ) {
  213. if ( boneData.pos ) {
  214. convertedTracks.push( new VectorKeyframeTrack(
  215. '.bones[' + boneData.bone.name + '].position',
  216. boneData.pos.times,
  217. boneData.pos.values
  218. ) );
  219. }
  220. convertedTracks.push( new QuaternionKeyframeTrack(
  221. '.bones[' + boneData.bone.name + '].quaternion',
  222. boneData.quat.times,
  223. boneData.quat.values
  224. ) );
  225. }
  226. }
  227. mixer.uncacheAction( clip );
  228. return new AnimationClip( clip.name, - 1, convertedTracks );
  229. }
  230. /**
  231. * Clones the given 3D object and its descendants, ensuring that any `SkinnedMesh` instances are
  232. * correctly associated with their bones. Bones are also cloned, and must be descendants of the
  233. * object passed to this method. Other data, like geometries and materials, are reused by reference.
  234. *
  235. * @param {Object3D} source - The 3D object to clone.
  236. * @return {Object3D} The cloned 3D object.
  237. */
  238. function clone( source ) {
  239. const sourceLookup = new Map();
  240. const cloneLookup = new Map();
  241. const clone = source.clone();
  242. parallelTraverse( source, clone, function ( sourceNode, clonedNode ) {
  243. sourceLookup.set( clonedNode, sourceNode );
  244. cloneLookup.set( sourceNode, clonedNode );
  245. } );
  246. clone.traverse( function ( node ) {
  247. if ( ! node.isSkinnedMesh ) return;
  248. const clonedMesh = node;
  249. const sourceMesh = sourceLookup.get( node );
  250. const sourceBones = sourceMesh.skeleton.bones;
  251. clonedMesh.skeleton = sourceMesh.skeleton.clone();
  252. clonedMesh.bindMatrix.copy( sourceMesh.bindMatrix );
  253. clonedMesh.skeleton.bones = sourceBones.map( function ( bone ) {
  254. return cloneLookup.get( bone );
  255. } );
  256. clonedMesh.bind( clonedMesh.skeleton, clonedMesh.bindMatrix );
  257. } );
  258. return clone;
  259. }
  260. // internal helper
  261. function getBoneByName( name, skeleton ) {
  262. for ( let i = 0, bones = getBones( skeleton ); i < bones.length; i ++ ) {
  263. if ( name === bones[ i ].name )
  264. return bones[ i ];
  265. }
  266. }
  267. function getBones( skeleton ) {
  268. return Array.isArray( skeleton ) ? skeleton : skeleton.bones;
  269. }
  270. function getHelperFromSkeleton( skeleton ) {
  271. const source = new SkeletonHelper( skeleton.bones[ 0 ] );
  272. source.skeleton = skeleton;
  273. return source;
  274. }
  275. function parallelTraverse( a, b, callback ) {
  276. callback( a, b );
  277. for ( let i = 0; i < a.children.length; i ++ ) {
  278. parallelTraverse( a.children[ i ], b.children[ i ], callback );
  279. }
  280. }
  281. /**
  282. * Retarget options of `SkeletonUtils`.
  283. *
  284. * @typedef {Object} module:SkeletonUtils~RetargetOptions
  285. * @property {boolean} [useFirstFramePosition=false] - Whether to use the position of the first frame or not.
  286. * @property {number} [fps] - The FPS of the clip.
  287. * @property {Object<string,string>} [names] - A dictionary for mapping target to source bone names.
  288. * @property {function(string):string} [getBoneName] - A function for mapping bone names. Alternative to `names`.
  289. * @property {Array<number>} [trim] - Whether to trim the clip or not. If set the array should hold two values for the start and end.
  290. * @property {boolean} [preserveBoneMatrix=true] - Whether to preserve bone matrices or not.
  291. * @property {boolean} [preserveBonePositions=true] - Whether to preserve bone positions or not.
  292. * @property {boolean} [useTargetMatrix=false] - Whether to use the target matrix or not.
  293. * @property {string} [hip='hip'] - The name of the source's hip bone.
  294. * @property {Vector3} [hipInfluence=(1,1,1)] - The hip influence.
  295. * @property {number} [scale=1] - The scale.
  296. **/
  297. export {
  298. retarget,
  299. retargetClip,
  300. clone,
  301. };