SkeletonUtils.js 12 KB

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