SkeletonUtils.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. /**
  2. * @author sunag / http://www.sunag.com.br
  3. */
  4. 'use strict';
  5. THREE.SkeletonUtils = {
  6. retarget: function () {
  7. var pos = new THREE.Vector3(),
  8. quat = new THREE.Quaternion(),
  9. scale = new THREE.Vector3(),
  10. bindBoneMatrix = new THREE.Matrix4(),
  11. relativeMatrix = new THREE.Matrix4(),
  12. globalMatrix = new THREE.Matrix4();
  13. return function ( target, source, options ) {
  14. options = options || {};
  15. options.preserveMatrix = options.preserveMatrix !== undefined ? options.preserveMatrix : true;
  16. options.preservePosition = options.preservePosition !== undefined ? options.preservePosition : true;
  17. options.preserveHipPosition = options.preserveHipPosition !== undefined ? options.preserveHipPosition : false;
  18. options.useTargetMatrix = options.useTargetMatrix !== undefined ? options.useTargetMatrix : false;
  19. options.hip = options.hip !== undefined ? options.hip : "hip";
  20. options.names = options.names || {};
  21. var sourceBones = source.isObject3D ? source.skeleton.bones : this.getBones( source ),
  22. bones = target.isObject3D ? target.skeleton.bones : this.getBones( target ),
  23. bindBones,
  24. bone, name, boneTo,
  25. bonesPosition, i;
  26. // reset bones
  27. if ( target.isObject3D ) {
  28. target.skeleton.pose();
  29. } else {
  30. options.useTargetMatrix = true;
  31. options.preserveMatrix = false;
  32. }
  33. if ( options.preservePosition ) {
  34. bonesPosition = [];
  35. for ( i = 0; i < bones.length; i ++ ) {
  36. bonesPosition.push( bones[ i ].position.clone() );
  37. }
  38. }
  39. if ( options.preserveMatrix ) {
  40. // reset matrix
  41. target.updateMatrixWorld();
  42. target.matrixWorld.identity();
  43. // reset children matrix
  44. for ( i = 0; i < target.children.length; ++ i ) {
  45. target.children[ i ].updateMatrixWorld( true );
  46. }
  47. }
  48. if ( options.offsets ) {
  49. bindBones = [];
  50. for ( i = 0; i < bones.length; ++ i ) {
  51. bone = bones[ i ];
  52. name = options.names[ bone.name ] || bone.name;
  53. if ( options.offsets && options.offsets[ name ] ) {
  54. bone.matrix.multiply( options.offsets[ name ] );
  55. bone.matrix.decompose( bone.position, bone.quaternion, bone.scale );
  56. bone.updateMatrixWorld();
  57. }
  58. bindBones.push( bone.matrixWorld.clone() );
  59. }
  60. }
  61. for ( i = 0; i < bones.length; ++ i ) {
  62. bone = bones[ i ];
  63. name = options.names[ bone.name ] || bone.name;
  64. boneTo = this.getBoneByName( name, sourceBones );
  65. globalMatrix.copy( bone.matrixWorld );
  66. if ( boneTo ) {
  67. boneTo.updateMatrixWorld();
  68. if ( options.useTargetMatrix ) {
  69. relativeMatrix.copy( boneTo.matrixWorld );
  70. } else {
  71. relativeMatrix.getInverse( target.matrixWorld );
  72. relativeMatrix.multiply( boneTo.matrixWorld );
  73. }
  74. // ignore scale to extract rotation
  75. scale.setFromMatrixScale( relativeMatrix );
  76. relativeMatrix.scale( scale.set( 1 / scale.x, 1 / scale.y, 1 / scale.z ) );
  77. // apply to global matrix
  78. globalMatrix.makeRotationFromQuaternion( quat.setFromRotationMatrix( relativeMatrix ) );
  79. if ( target.isObject3D ) {
  80. var boneIndex = bones.indexOf( bone ),
  81. wBindMatrix = bindBones ? bindBones[ boneIndex ] : bindBoneMatrix.getInverse( target.skeleton.boneInverses[ boneIndex ] );
  82. globalMatrix.multiply( wBindMatrix );
  83. }
  84. globalMatrix.copyPosition( relativeMatrix );
  85. }
  86. if ( bone.parent && bone.parent.isBone ) {
  87. bone.matrix.getInverse( bone.parent.matrixWorld );
  88. bone.matrix.multiply( globalMatrix );
  89. } else {
  90. bone.matrix.copy( globalMatrix );
  91. }
  92. if ( options.preserveHipPosition && name === options.hip ) {
  93. bone.matrix.setPosition( pos.set( 0, bone.position.y, 0 ) );
  94. }
  95. bone.matrix.decompose( bone.position, bone.quaternion, bone.scale );
  96. bone.updateMatrixWorld();
  97. }
  98. if ( options.preservePosition ) {
  99. for ( i = 0; i < bones.length; ++ i ) {
  100. bone = bones[ i ];
  101. name = options.names[ bone.name ] || bone.name;
  102. if ( name !== options.hip ) {
  103. bone.position.copy( bonesPosition[ i ] );
  104. }
  105. }
  106. }
  107. if ( options.preserveMatrix ) {
  108. // restore matrix
  109. target.updateMatrixWorld( true );
  110. }
  111. };
  112. }(),
  113. retargetClip: function ( target, source, clip, options ) {
  114. options = options || {};
  115. options.useFirstFramePosition = options.useFirstFramePosition !== undefined ? options.useFirstFramePosition : false;
  116. options.fps = options.fps !== undefined ? options.fps : 30;
  117. options.names = options.names || [];
  118. if ( ! source.isObject3D ) {
  119. source = this.getHelperFromSkeleton( source );
  120. }
  121. var numFrames = Math.round( clip.duration * ( options.fps / 1000 ) * 1000 ),
  122. delta = 1 / options.fps,
  123. convertedTracks = [],
  124. mixer = new THREE.AnimationMixer( source ),
  125. bones = this.getBones( target.skeleton ),
  126. boneDatas = [],
  127. positionOffset,
  128. bone, boneTo, boneData,
  129. name, i, j;
  130. mixer.clipAction( clip ).play();
  131. mixer.update( 0 );
  132. source.updateMatrixWorld();
  133. for ( i = 0; i < numFrames; ++ i ) {
  134. var time = i * delta;
  135. this.retarget( target, source, options );
  136. for ( j = 0; j < bones.length; ++ j ) {
  137. name = options.names[ bones[ j ].name ] || bones[ j ].name;
  138. boneTo = this.getBoneByName( name, source.skeleton );
  139. if ( boneTo ) {
  140. bone = bones[ j ];
  141. boneData = boneDatas[ j ] = boneDatas[ j ] || { bone: bone };
  142. if ( options.hip === name ) {
  143. if ( ! boneData.pos ) {
  144. boneData.pos = {
  145. times: new Float32Array( numFrames ),
  146. values: new Float32Array( numFrames * 3 )
  147. };
  148. }
  149. if ( options.useFirstFramePosition ) {
  150. if ( i === 0 ) {
  151. positionOffset = bone.position.clone();
  152. }
  153. bone.position.sub( positionOffset );
  154. }
  155. boneData.pos.times[ i ] = time;
  156. bone.position.toArray( boneData.pos.values, i * 3 );
  157. }
  158. if ( ! boneData.quat ) {
  159. boneData.quat = {
  160. times: new Float32Array( numFrames ),
  161. values: new Float32Array( numFrames * 4 )
  162. };
  163. }
  164. boneData.quat.times[ i ] = time;
  165. bone.quaternion.toArray( boneData.quat.values, i * 4 );
  166. }
  167. }
  168. mixer.update( delta );
  169. source.updateMatrixWorld();
  170. }
  171. for ( i = 0; i < boneDatas.length; ++ i ) {
  172. boneData = boneDatas[ i ];
  173. if ( boneData ) {
  174. if ( boneData.pos ) {
  175. convertedTracks.push( new THREE.VectorKeyframeTrack(
  176. ".bones[" + boneData.bone.name + "].position",
  177. boneData.pos.times,
  178. boneData.pos.values
  179. ) );
  180. }
  181. convertedTracks.push( new THREE.QuaternionKeyframeTrack(
  182. ".bones[" + boneData.bone.name + "].quaternion",
  183. boneData.quat.times,
  184. boneData.quat.values
  185. ) );
  186. }
  187. }
  188. mixer.uncacheAction( clip );
  189. return new THREE.AnimationClip( clip.name, - 1, convertedTracks );
  190. },
  191. getHelperFromSkeleton: function ( skeleton ) {
  192. var source = new THREE.SkeletonHelper( skeleton.bones[ 0 ] );
  193. source.skeleton = skeleton;
  194. return source;
  195. },
  196. getSkeletonOffsets: function () {
  197. var targetParentPos = new THREE.Vector3(),
  198. targetPos = new THREE.Vector3(),
  199. sourceParentPos = new THREE.Vector3(),
  200. sourcePos = new THREE.Vector3(),
  201. targetDir = new THREE.Vector2(),
  202. sourceDir = new THREE.Vector2();
  203. return function ( target, source, options ) {
  204. options = options || {};
  205. options.hip = options.hip !== undefined ? options.hip : "hip";
  206. options.names = options.names || {};
  207. if ( ! source.isObject3D ) {
  208. source = this.getHelperFromSkeleton( source );
  209. }
  210. var nameKeys = Object.keys( options.names ),
  211. nameValues = Object.values( options.names ),
  212. sourceBones = source.isObject3D ? source.skeleton.bones : this.getBones( source ),
  213. bones = target.isObject3D ? target.skeleton.bones : this.getBones( target ),
  214. offsets = [],
  215. bone, boneTo,
  216. name, i;
  217. target.skeleton.pose();
  218. for ( i = 0; i < bones.length; ++ i ) {
  219. bone = bones[ i ];
  220. name = options.names[ bone.name ] || bone.name;
  221. boneTo = this.getBoneByName( name, sourceBones );
  222. if ( boneTo && name !== options.hip ) {
  223. var boneParent = this.getNearestBone( bone.parent, nameKeys ),
  224. boneToParent = this.getNearestBone( boneTo.parent, nameValues );
  225. boneParent.updateMatrixWorld();
  226. boneToParent.updateMatrixWorld();
  227. targetParentPos.setFromMatrixPosition( boneParent.matrixWorld );
  228. targetPos.setFromMatrixPosition( bone.matrixWorld );
  229. sourceParentPos.setFromMatrixPosition( boneToParent.matrixWorld );
  230. sourcePos.setFromMatrixPosition( boneTo.matrixWorld );
  231. targetDir.subVectors(
  232. new THREE.Vector2( targetPos.x, targetPos.y ),
  233. new THREE.Vector2( targetParentPos.x, targetParentPos.y )
  234. ).normalize();
  235. sourceDir.subVectors(
  236. new THREE.Vector2( sourcePos.x, sourcePos.y ),
  237. new THREE.Vector2( sourceParentPos.x, sourceParentPos.y )
  238. ).normalize();
  239. var laterialAngle = targetDir.angle() - sourceDir.angle();
  240. var offset = new THREE.Matrix4().makeRotationFromEuler(
  241. new THREE.Euler(
  242. 0,
  243. 0,
  244. laterialAngle
  245. )
  246. );
  247. bone.matrix.multiply( offset );
  248. bone.matrix.decompose( bone.position, bone.quaternion, bone.scale );
  249. bone.updateMatrixWorld();
  250. offsets[ name ] = offset;
  251. }
  252. }
  253. return offsets;
  254. };
  255. }(),
  256. renameBones: function ( skeleton, names ) {
  257. var bones = this.getBones( skeleton );
  258. for ( var i = 0; i < bones.length; ++ i ) {
  259. var bone = bones[ i ];
  260. if ( names[ bone.name ] ) {
  261. bone.name = names[ bone.name ];
  262. }
  263. }
  264. return this;
  265. },
  266. getBones: function ( skeleton ) {
  267. return Array.isArray( skeleton ) ? skeleton : skeleton.bones;
  268. },
  269. getBoneByName: function ( name, skeleton ) {
  270. for ( var i = 0, bones = this.getBones( skeleton ); i < bones.length; i ++ ) {
  271. if ( name === bones[ i ].name )
  272. return bones[ i ];
  273. }
  274. },
  275. getNearestBone: function ( bone, names ) {
  276. while ( bone.isBone ) {
  277. if ( names.indexOf( bone.name ) !== - 1 ) {
  278. return bone;
  279. }
  280. bone = bone.parent;
  281. }
  282. },
  283. findBoneTrackData: function ( name, tracks ) {
  284. var regexp = /\[(.*)\]\.(.*)/,
  285. result = { name: name };
  286. for ( var i = 0; i < tracks.length; ++ i ) {
  287. // 1 is track name
  288. // 2 is track type
  289. var trackData = regexp.exec( tracks[ i ].name );
  290. if ( trackData && name === trackData[ 1 ] ) {
  291. result[ trackData[ 2 ] ] = i;
  292. }
  293. }
  294. return result;
  295. },
  296. getEqualsBonesNames: function ( skeleton, targetSkeleton ) {
  297. var sourceBones = this.getBones( skeleton ),
  298. targetBones = this.getBones( targetSkeleton ),
  299. bones = [];
  300. search : for ( var i = 0; i < sourceBones.length; i ++ ) {
  301. var boneName = sourceBones[ i ].name;
  302. for ( var j = 0; j < targetBones.length; j ++ ) {
  303. if ( boneName === targetBones[ j ].name ) {
  304. bones.push( boneName );
  305. continue search;
  306. }
  307. }
  308. }
  309. return bones;
  310. }
  311. };