SkeletonUtils.js 11 KB

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