SkeletonUtils.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. /**
  2. * @author sunag / http://www.sunag.com.br
  3. */
  4. 'use strict';
  5. THREE.SkeletonUtils = {
  6. retarget: function () {
  7. var quat = new THREE.Quaternion(),
  8. scale = new THREE.Vector3(),
  9. bindBoneMatrix = new THREE.Matrix4(),
  10. relativeMatrix = new THREE.Matrix4(),
  11. globalMatrix = new THREE.Matrix4();
  12. return function ( target, source, options ) {
  13. options = options || {};
  14. options.preserveMatrix = options.preserveMatrix !== undefined ? options.preserveMatrix : true;
  15. options.preservePosition = options.preservePosition !== undefined ? options.preservePosition : true;
  16. options.useTargetMatrix = options.useTargetMatrix !== undefined ? options.useTargetMatrix : false;
  17. options.hip = options.hip !== undefined ? options.hip : "hip";
  18. options.names = options.names || {};
  19. var sourceBones = source.isObject3D ? source.skeleton.bones : this.getBones( source ),
  20. bones = target.isObject3D ? target.skeleton.bones : this.getBones( target ),
  21. bindBones,
  22. bone, name, boneTo,
  23. bonesPosition, i;
  24. // reset bones
  25. if ( target.isObject3D ) {
  26. target.skeleton.pose();
  27. } else {
  28. options.useTargetMatrix = true;
  29. options.preserveMatrix = false;
  30. }
  31. if ( options.preservePosition ) {
  32. bonesPosition = [];
  33. for ( i = 0; i < bones.length; i ++ ) {
  34. bonesPosition.push( bones[ i ].position.clone() );
  35. }
  36. }
  37. if ( options.preserveMatrix ) {
  38. // reset matrix
  39. target.updateMatrixWorld();
  40. target.matrixWorld.identity();
  41. // reset children matrix
  42. for ( i = 0; i < target.children.length; ++ i ) {
  43. target.children[ i ].updateMatrixWorld( true );
  44. }
  45. }
  46. if ( options.offsets ) {
  47. bindBones = [];
  48. for ( i = 0; i < bones.length; ++ i ) {
  49. bone = bones[ i ];
  50. name = options.names[ bone.name ] || bone.name;
  51. if ( options.offsets[ name ] ) {
  52. bone.matrix.multiply( options.offsets[ name ] );
  53. bone.matrix.decompose( bone.position, bone.quaternion, bone.scale );
  54. bone.updateMatrixWorld();
  55. }
  56. bindBones.push( bone.matrixWorld.clone() );
  57. }
  58. }
  59. for ( i = 0; i < bones.length; ++ i ) {
  60. bone = bones[ i ];
  61. name = options.names[ bone.name ] || bone.name;
  62. boneTo = this.getBoneByName( sourceBones, name );
  63. globalMatrix.copy( bone.matrixWorld );
  64. if ( boneTo ) {
  65. boneTo.updateMatrixWorld();
  66. if ( options.useTargetMatrix ) {
  67. relativeMatrix.copy( boneTo.matrixWorld );
  68. } else {
  69. relativeMatrix.getInverse( target.matrixWorld );
  70. relativeMatrix.multiply( boneTo.matrixWorld );
  71. }
  72. // ignore scale to extract rotation
  73. scale.setFromMatrixScale( relativeMatrix );
  74. relativeMatrix.scale( scale.set( 1 / scale.x, 1 / scale.y, 1 / scale.z ) );
  75. // apply to global matrix
  76. globalMatrix.makeRotationFromQuaternion( quat.setFromRotationMatrix( relativeMatrix ) );
  77. if ( target.isObject3D ) {
  78. var boneIndex = bones.indexOf( bone ),
  79. wBindMatrix = bindBones ? bindBones[ boneIndex ] : bindBoneMatrix.getInverse( target.skeleton.boneInverses[ boneIndex ] );
  80. globalMatrix.multiply( wBindMatrix );
  81. }
  82. globalMatrix.copyPosition( relativeMatrix );
  83. }
  84. if ( bone.parent && bone.parent.isBone ) {
  85. bone.matrix.getInverse( bone.parent.matrixWorld );
  86. bone.matrix.multiply( globalMatrix );
  87. } else {
  88. bone.matrix.copy( globalMatrix );
  89. }
  90. bone.matrix.decompose( bone.position, bone.quaternion, bone.scale );
  91. bone.updateMatrixWorld();
  92. }
  93. if ( options.preservePosition ) {
  94. for ( i = 0; i < bones.length; ++ i ) {
  95. bone = bones[ i ];
  96. name = options.names[ bone.name ] || bone.name;
  97. if ( name !== options.hip ) {
  98. bone.position.copy( bonesPosition[ i ] );
  99. }
  100. }
  101. }
  102. if ( options.preserveMatrix ) {
  103. // restore matrix
  104. target.updateMatrixWorld( true );
  105. }
  106. };
  107. }(),
  108. retargetClip: function ( target, source, clip, options ) {
  109. options = options || {};
  110. options.useFirstFramePosition = options.useFirstFramePosition !== undefined ? options.useFirstFramePosition : false;
  111. options.fps = options.fps !== undefined ? options.fps : 30;
  112. options.names = options.names || [];
  113. if ( ! source.isObject3D ) {
  114. var skeleton = source;
  115. source = new THREE.SkeletonHelper( skeleton.bones[ 0 ] );
  116. source.skeleton = skeleton;
  117. }
  118. var numFrames = Math.round( clip.duration * ( options.fps / 1000 ) * 1000 ),
  119. delta = 1 / options.fps,
  120. convertedTracks = [],
  121. mixer = new THREE.AnimationMixer( source ),
  122. bones = this.getBones( target.skeleton ),
  123. boneDatas = [],
  124. positionOffset,
  125. bone, boneTo, boneData, i, j;
  126. mixer.clipAction( clip ).play();
  127. mixer.update( 0 );
  128. source.updateMatrixWorld();
  129. for ( i = 0; i < numFrames; ++ i ) {
  130. var time = i * delta;
  131. this.retarget( target, source, options );
  132. for ( j = 0; j < bones.length; ++ j ) {
  133. boneTo = this.getBoneByName( source.skeleton, options.names[ bones[ j ].name ] || bones[ j ].name );
  134. if ( boneTo ) {
  135. bone = bones[ j ];
  136. boneData = boneDatas[ j ] = boneDatas[ j ] || { bone: bone };
  137. if ( options.hip === boneData.bone.name ) {
  138. if ( ! boneData.pos ) {
  139. boneData.pos = {
  140. times: new Float32Array( numFrames ),
  141. values: new Float32Array( numFrames * 3 )
  142. };
  143. }
  144. if ( options.useFirstFramePosition ) {
  145. if ( i === 0 ) {
  146. positionOffset = bone.position.clone();
  147. }
  148. bone.position.sub( positionOffset );
  149. }
  150. boneData.pos.times[ i ] = time;
  151. bone.position.toArray( boneData.pos.values, i * 3 );
  152. }
  153. if ( ! boneData.quat ) {
  154. boneData.quat = {
  155. times: new Float32Array( numFrames ),
  156. values: new Float32Array( numFrames * 4 )
  157. };
  158. }
  159. boneData.quat.times[ i ] = time;
  160. bone.quaternion.toArray( boneData.quat.values, i * 4 );
  161. }
  162. }
  163. mixer.update( delta );
  164. source.updateMatrixWorld();
  165. }
  166. for ( i = 0; i < boneDatas.length; ++ i ) {
  167. boneData = boneDatas[ i ];
  168. if ( boneData ) {
  169. if ( boneData.pos ) {
  170. convertedTracks.push( new THREE.VectorKeyframeTrack(
  171. ".bones[" + boneData.bone.name + "].position",
  172. boneData.pos.times,
  173. boneData.pos.values
  174. ) );
  175. }
  176. convertedTracks.push( new THREE.QuaternionKeyframeTrack(
  177. ".bones[" + boneData.bone.name + "].quaternion",
  178. boneData.quat.times,
  179. boneData.quat.values
  180. ) );
  181. }
  182. }
  183. mixer.uncacheAction( clip );
  184. return new THREE.AnimationClip( clip.name, - 1, convertedTracks );
  185. },
  186. rename: function ( skeleton, names ) {
  187. var bones = this.getBones( skeleton );
  188. for ( var i = 0; i < bones.length; ++ i ) {
  189. var bone = bones[ i ];
  190. if ( names[ bone.name ] ) {
  191. bone.name = names[ bone.name ];
  192. }
  193. }
  194. return this;
  195. },
  196. getBones: function ( skeleton ) {
  197. return Array.isArray( skeleton ) ? skeleton : skeleton.bones;
  198. },
  199. getBoneByName: function ( skeleton, name ) {
  200. for ( var i = 0, bones = this.getBones( skeleton ); i < bones.length; i ++ ) {
  201. if ( name === bones[ i ].name )
  202. return bones[ i ];
  203. }
  204. },
  205. findBoneTrackData: function ( name, tracks ) {
  206. var regexp = /\[(.*)\]\.(.*)/,
  207. result = { name: name };
  208. for ( var i = 0; i < tracks.length; ++ i ) {
  209. // 1 is track name
  210. // 2 is track type
  211. var trackData = regexp.exec( tracks[ i ].name );
  212. if ( trackData && name === trackData[ 1 ] ) {
  213. result[ trackData[ 2 ] ] = i;
  214. }
  215. }
  216. return result;
  217. },
  218. getEqualsBonesNames: function ( skeleton, targetSkeleton ) {
  219. var sourceBones = this.getBones( skeleton ),
  220. targetBones = this.getBones( targetSkeleton ),
  221. bones = [];
  222. search : for ( var i = 0; i < sourceBones.length; i ++ ) {
  223. var boneName = sourceBones[ i ].name;
  224. for ( var j = 0; j < targetBones.length; j ++ ) {
  225. if ( boneName === targetBones[ j ].name ) {
  226. bones.push( boneName );
  227. continue search;
  228. }
  229. }
  230. }
  231. return bones;
  232. }
  233. };