SkeletonUtils.js 12 KB

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