SkeletonUtils.js 11 KB

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