SkeletonUtils.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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.offset ) {
  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.offset[ name ] ) {
  52. bone.matrix.multiply( options.offset[ 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.fps = options.fps !== undefined ? options.fps : 30;
  111. options.names = options.names || [];
  112. if ( ! source.isObject3D ) {
  113. var skeleton = source;
  114. source = new THREE.SkeletonHelper( skeleton.bones[ 0 ] );
  115. source.skeleton = skeleton;
  116. }
  117. var numFrames = Math.round( clip.duration * ( options.fps / 1000 ) * 1000 ),
  118. delta = 1 / options.fps,
  119. convertedTracks = [],
  120. mixer = new THREE.AnimationMixer( source ),
  121. bones = this.getBones( target.skeleton ),
  122. boneDatas = [],
  123. bone, boneTo, boneData, i, j;
  124. mixer.clipAction( clip ).play();
  125. mixer.update( 0 );
  126. //source.updateMatrixWorld();
  127. for ( i = 0; i < numFrames; ++ i ) {
  128. var time = i * delta;
  129. this.retarget( target, source, options );
  130. for ( j = 0; j < bones.length; ++ j ) {
  131. boneTo = this.getBoneByName( source.skeleton, options.names[ bones[ j ].name ] || bones[ j ].name );
  132. if ( boneTo ) {
  133. bone = bones[ j ];
  134. boneData = boneDatas[ j ] = boneDatas[ j ] || { bone: bone };
  135. if ( options.hip === boneData.bone.name ) {
  136. if ( ! boneData.pos ) {
  137. boneData.pos = {
  138. times: new Float32Array( numFrames ),
  139. values: new Float32Array( numFrames * 3 )
  140. };
  141. }
  142. boneData.pos.times[ i ] = time;
  143. bone.position.toArray( boneData.pos.values, i * 3 );
  144. }
  145. if ( ! boneData.quat ) {
  146. boneData.quat = {
  147. times: new Float32Array( numFrames ),
  148. values: new Float32Array( numFrames * 4 )
  149. };
  150. }
  151. boneData.quat.times[ i ] = time;
  152. bone.quaternion.toArray( boneData.quat.values, i * 4 );
  153. }
  154. }
  155. mixer.update( delta );
  156. //source.updateMatrixWorld();
  157. }
  158. for ( i = 0; i < boneDatas.length; ++ i ) {
  159. boneData = boneDatas[ i ];
  160. if ( boneData ) {
  161. if ( boneData.pos ) {
  162. convertedTracks.push( new THREE.VectorKeyframeTrack(
  163. ".bones[" + boneData.bone.name + "].position",
  164. boneData.pos.times,
  165. boneData.pos.values
  166. ) );
  167. }
  168. convertedTracks.push( new THREE.QuaternionKeyframeTrack(
  169. ".bones[" + boneData.bone.name + "].quaternion",
  170. boneData.quat.times,
  171. boneData.quat.values
  172. ) );
  173. }
  174. }
  175. mixer.uncacheAction( clip );
  176. return new THREE.AnimationClip( clip.name, - 1, convertedTracks );
  177. },
  178. rename: function ( skeleton, names ) {
  179. var bones = this.getBones( skeleton );
  180. for ( var i = 0; i < bones.length; ++ i ) {
  181. var bone = bones[ i ];
  182. if ( names[ bone.name ] ) {
  183. bone.name = names[ bone.name ];
  184. }
  185. }
  186. return this;
  187. },
  188. getBones: function ( skeleton ) {
  189. return Array.isArray( skeleton ) ? skeleton : skeleton.bones;
  190. },
  191. getBoneByName: function ( skeleton, name ) {
  192. for ( var i = 0, bones = this.getBones( skeleton ); i < bones.length; i ++ ) {
  193. if ( name === bones[ i ].name )
  194. return bones[ i ];
  195. }
  196. },
  197. findBoneTrackData: function ( name, tracks ) {
  198. var regexp = /\[(.*)\]\.(.*)/,
  199. result = { name: name };
  200. for ( var i = 0; i < tracks.length; ++ i ) {
  201. // 1 is track name
  202. // 2 is track type
  203. var trackData = regexp.exec( tracks[ i ].name );
  204. if ( trackData && name === trackData[ 1 ] ) {
  205. result[ trackData[ 2 ] ] = i;
  206. }
  207. }
  208. return result;
  209. },
  210. getEqualsBonesNames: function ( skeleton, targetSkeleton ) {
  211. var sourceBones = this.getBones( skeleton ),
  212. targetBones = this.getBones( targetSkeleton ),
  213. bones = [];
  214. search : for ( var i = 0; i < sourceBones.length; i ++ ) {
  215. var boneName = sourceBones[ i ].name;
  216. for ( var j = 0; j < targetBones.length; j ++ ) {
  217. if ( boneName === targetBones[ j ].name ) {
  218. bones.push( boneName );
  219. continue search;
  220. }
  221. }
  222. }
  223. return bones;
  224. }
  225. };