2
0

SkeletonUtils.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. THREE.SkeletonUtils = {
  2. retarget: function () {
  3. var pos = new THREE.Vector3(),
  4. quat = new THREE.Quaternion(),
  5. scale = new THREE.Vector3(),
  6. bindBoneMatrix = new THREE.Matrix4(),
  7. relativeMatrix = new THREE.Matrix4(),
  8. globalMatrix = new THREE.Matrix4();
  9. return function ( target, source, options ) {
  10. options = options || {};
  11. options.preserveMatrix = options.preserveMatrix !== undefined ? options.preserveMatrix : true;
  12. options.preservePosition = options.preservePosition !== undefined ? options.preservePosition : true;
  13. options.preserveHipPosition = options.preserveHipPosition !== undefined ? options.preserveHipPosition : false;
  14. options.useTargetMatrix = options.useTargetMatrix !== undefined ? options.useTargetMatrix : false;
  15. options.hip = options.hip !== undefined ? options.hip : 'hip';
  16. options.names = options.names || {};
  17. var sourceBones = source.isObject3D ? source.skeleton.bones : this.getBones( source ),
  18. bones = target.isObject3D ? target.skeleton.bones : this.getBones( target ),
  19. bindBones,
  20. bone, name, boneTo,
  21. bonesPosition, i;
  22. // reset bones
  23. if ( target.isObject3D ) {
  24. target.skeleton.pose();
  25. } else {
  26. options.useTargetMatrix = true;
  27. options.preserveMatrix = false;
  28. }
  29. if ( options.preservePosition ) {
  30. bonesPosition = [];
  31. for ( i = 0; i < bones.length; i ++ ) {
  32. bonesPosition.push( bones[ i ].position.clone() );
  33. }
  34. }
  35. if ( options.preserveMatrix ) {
  36. // reset matrix
  37. target.updateMatrixWorld();
  38. target.matrixWorld.identity();
  39. // reset children matrix
  40. for ( i = 0; i < target.children.length; ++ i ) {
  41. target.children[ i ].updateMatrixWorld( true );
  42. }
  43. }
  44. if ( options.offsets ) {
  45. bindBones = [];
  46. for ( i = 0; i < bones.length; ++ i ) {
  47. bone = bones[ i ];
  48. name = options.names[ bone.name ] || bone.name;
  49. if ( options.offsets && options.offsets[ name ] ) {
  50. bone.matrix.multiply( options.offsets[ name ] );
  51. bone.matrix.decompose( bone.position, bone.quaternion, bone.scale );
  52. bone.updateMatrixWorld();
  53. }
  54. bindBones.push( bone.matrixWorld.clone() );
  55. }
  56. }
  57. for ( i = 0; i < bones.length; ++ i ) {
  58. bone = bones[ i ];
  59. name = options.names[ bone.name ] || bone.name;
  60. boneTo = this.getBoneByName( name, sourceBones );
  61. globalMatrix.copy( bone.matrixWorld );
  62. if ( boneTo ) {
  63. boneTo.updateMatrixWorld();
  64. if ( options.useTargetMatrix ) {
  65. relativeMatrix.copy( boneTo.matrixWorld );
  66. } else {
  67. relativeMatrix.copy( target.matrixWorld ).invert();
  68. relativeMatrix.multiply( boneTo.matrixWorld );
  69. }
  70. // ignore scale to extract rotation
  71. scale.setFromMatrixScale( relativeMatrix );
  72. relativeMatrix.scale( scale.set( 1 / scale.x, 1 / scale.y, 1 / scale.z ) );
  73. // 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, boneTo, boneData,
  125. name, i, j;
  126. mixer.clipAction( clip ).play();
  127. mixer.update( 0 );
  128. source.updateMatrixWorld();
  129. for ( i = 0; i < numFrames; ++ i ) {
  130. var time = i * delta;
  131. this.retarget( target, source, options );
  132. for ( j = 0; j < bones.length; ++ j ) {
  133. name = options.names[ bones[ j ].name ] || bones[ j ].name;
  134. boneTo = this.getBoneByName( name, source.skeleton );
  135. if ( boneTo ) {
  136. bone = bones[ j ];
  137. boneData = boneDatas[ j ] = boneDatas[ j ] || { bone: bone };
  138. if ( options.hip === name ) {
  139. if ( ! boneData.pos ) {
  140. boneData.pos = {
  141. times: new Float32Array( numFrames ),
  142. values: new Float32Array( numFrames * 3 )
  143. };
  144. }
  145. if ( options.useFirstFramePosition ) {
  146. if ( i === 0 ) {
  147. positionOffset = bone.position.clone();
  148. }
  149. bone.position.sub( positionOffset );
  150. }
  151. boneData.pos.times[ i ] = time;
  152. bone.position.toArray( boneData.pos.values, i * 3 );
  153. }
  154. if ( ! boneData.quat ) {
  155. boneData.quat = {
  156. times: new Float32Array( numFrames ),
  157. values: new Float32Array( numFrames * 4 )
  158. };
  159. }
  160. boneData.quat.times[ i ] = time;
  161. bone.quaternion.toArray( boneData.quat.values, i * 4 );
  162. }
  163. }
  164. mixer.update( delta );
  165. source.updateMatrixWorld();
  166. }
  167. for ( i = 0; i < boneDatas.length; ++ i ) {
  168. boneData = boneDatas[ i ];
  169. if ( boneData ) {
  170. if ( boneData.pos ) {
  171. convertedTracks.push( new THREE.VectorKeyframeTrack(
  172. '.bones[' + boneData.bone.name + '].position',
  173. boneData.pos.times,
  174. boneData.pos.values
  175. ) );
  176. }
  177. convertedTracks.push( new THREE.QuaternionKeyframeTrack(
  178. '.bones[' + boneData.bone.name + '].quaternion',
  179. boneData.quat.times,
  180. boneData.quat.values
  181. ) );
  182. }
  183. }
  184. mixer.uncacheAction( clip );
  185. return new THREE.AnimationClip( clip.name, - 1, convertedTracks );
  186. },
  187. getHelperFromSkeleton: function ( skeleton ) {
  188. var source = new THREE.SkeletonHelper( skeleton.bones[ 0 ] );
  189. source.skeleton = skeleton;
  190. return source;
  191. },
  192. getSkeletonOffsets: function () {
  193. var targetParentPos = new THREE.Vector3(),
  194. targetPos = new THREE.Vector3(),
  195. sourceParentPos = new THREE.Vector3(),
  196. sourcePos = new THREE.Vector3(),
  197. targetDir = new THREE.Vector2(),
  198. sourceDir = new THREE.Vector2();
  199. return function ( target, source, options ) {
  200. options = options || {};
  201. options.hip = options.hip !== undefined ? options.hip : 'hip';
  202. options.names = options.names || {};
  203. if ( ! source.isObject3D ) {
  204. source = this.getHelperFromSkeleton( source );
  205. }
  206. var nameKeys = Object.keys( options.names ),
  207. nameValues = Object.values( options.names ),
  208. sourceBones = source.isObject3D ? source.skeleton.bones : this.getBones( source ),
  209. bones = target.isObject3D ? target.skeleton.bones : this.getBones( target ),
  210. offsets = [],
  211. bone, boneTo,
  212. name, 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(
  228. new THREE.Vector2( targetPos.x, targetPos.y ),
  229. new THREE.Vector2( targetParentPos.x, targetParentPos.y )
  230. ).normalize();
  231. sourceDir.subVectors(
  232. new THREE.Vector2( sourcePos.x, sourcePos.y ),
  233. new THREE.Vector2( sourceParentPos.x, sourceParentPos.y )
  234. ).normalize();
  235. var laterialAngle = targetDir.angle() - sourceDir.angle();
  236. var offset = new THREE.Matrix4().makeRotationFromEuler(
  237. new THREE.Euler(
  238. 0,
  239. 0,
  240. laterialAngle
  241. )
  242. );
  243. bone.matrix.multiply( offset );
  244. bone.matrix.decompose( bone.position, bone.quaternion, bone.scale );
  245. bone.updateMatrixWorld();
  246. offsets[ name ] = offset;
  247. }
  248. }
  249. return offsets;
  250. };
  251. }(),
  252. renameBones: function ( skeleton, names ) {
  253. var bones = this.getBones( skeleton );
  254. for ( var i = 0; i < bones.length; ++ i ) {
  255. var bone = bones[ i ];
  256. if ( names[ bone.name ] ) {
  257. bone.name = names[ bone.name ];
  258. }
  259. }
  260. return this;
  261. },
  262. getBones: function ( skeleton ) {
  263. return Array.isArray( skeleton ) ? skeleton : skeleton.bones;
  264. },
  265. getBoneByName: function ( name, skeleton ) {
  266. for ( var i = 0, bones = this.getBones( skeleton ); i < bones.length; i ++ ) {
  267. if ( name === bones[ i ].name )
  268. return bones[ i ];
  269. }
  270. },
  271. getNearestBone: function ( bone, names ) {
  272. while ( bone.isBone ) {
  273. if ( names.indexOf( bone.name ) !== - 1 ) {
  274. return bone;
  275. }
  276. bone = bone.parent;
  277. }
  278. },
  279. findBoneTrackData: function ( name, tracks ) {
  280. var regexp = /\[(.*)\]\.(.*)/,
  281. result = { name: name };
  282. for ( var i = 0; i < tracks.length; ++ i ) {
  283. // 1 is track name
  284. // 2 is track type
  285. var trackData = regexp.exec( tracks[ i ].name );
  286. if ( trackData && name === trackData[ 1 ] ) {
  287. result[ trackData[ 2 ] ] = i;
  288. }
  289. }
  290. return result;
  291. },
  292. getEqualsBonesNames: function ( skeleton, targetSkeleton ) {
  293. var sourceBones = this.getBones( skeleton ),
  294. targetBones = this.getBones( targetSkeleton ),
  295. bones = [];
  296. search : for ( var i = 0; i < sourceBones.length; i ++ ) {
  297. var boneName = sourceBones[ i ].name;
  298. for ( var j = 0; j < targetBones.length; j ++ ) {
  299. if ( boneName === targetBones[ j ].name ) {
  300. bones.push( boneName );
  301. continue search;
  302. }
  303. }
  304. }
  305. return bones;
  306. },
  307. clone: function ( source ) {
  308. var sourceLookup = new Map();
  309. var cloneLookup = new Map();
  310. var clone = source.clone();
  311. parallelTraverse( source, clone, function ( sourceNode, clonedNode ) {
  312. sourceLookup.set( clonedNode, sourceNode );
  313. cloneLookup.set( sourceNode, clonedNode );
  314. } );
  315. clone.traverse( function ( node ) {
  316. if ( ! node.isSkinnedMesh ) return;
  317. var clonedMesh = node;
  318. var sourceMesh = sourceLookup.get( node );
  319. var sourceBones = sourceMesh.skeleton.bones;
  320. clonedMesh.skeleton = sourceMesh.skeleton.clone();
  321. clonedMesh.bindMatrix.copy( sourceMesh.bindMatrix );
  322. clonedMesh.skeleton.bones = sourceBones.map( function ( bone ) {
  323. return cloneLookup.get( bone );
  324. } );
  325. clonedMesh.bind( clonedMesh.skeleton, clonedMesh.bindMatrix );
  326. } );
  327. return clone;
  328. }
  329. };
  330. function parallelTraverse( a, b, callback ) {
  331. callback( a, b );
  332. for ( var i = 0; i < a.children.length; i ++ ) {
  333. parallelTraverse( a.children[ i ], b.children[ i ], callback );
  334. }
  335. }