CCDIKSolver.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. ( function () {
  2. const _q = new THREE.Quaternion();
  3. const _targetPos = new THREE.Vector3();
  4. const _targetVec = new THREE.Vector3();
  5. const _effectorPos = new THREE.Vector3();
  6. const _effectorVec = new THREE.Vector3();
  7. const _linkPos = new THREE.Vector3();
  8. const _invLinkQ = new THREE.Quaternion();
  9. const _linkScale = new THREE.Vector3();
  10. const _axis = new THREE.Vector3();
  11. const _vector = new THREE.Vector3();
  12. const _matrix = new THREE.Matrix4();
  13. /**
  14. * CCD Algorithm
  15. * - https://sites.google.com/site/auraliusproject/ccd-algorithm
  16. *
  17. * // ik parameter example
  18. * //
  19. * // target, effector, index in links are bone index in skeleton.bones.
  20. * // the bones relation should be
  21. * // <-- parent child -->
  22. * // links[ n ], links[ n - 1 ], ..., links[ 0 ], effector
  23. * iks = [ {
  24. * target: 1,
  25. * effector: 2,
  26. * links: [ { index: 5, limitation: new THREE.Vector3( 1, 0, 0 ) }, { index: 4, enabled: false }, { index : 3 } ],
  27. * iteration: 10,
  28. * minAngle: 0.0,
  29. * maxAngle: 1.0,
  30. * } ];
  31. */
  32. class CCDIKSolver {
  33. /**
  34. * @param {THREE.SkinnedMesh} mesh
  35. * @param {Array<Object>} iks
  36. */
  37. constructor( mesh, iks = [] ) {
  38. this.mesh = mesh;
  39. this.iks = iks;
  40. this._valid();
  41. }
  42. /**
  43. * Update all IK bones.
  44. *
  45. * @return {CCDIKSolver}
  46. */
  47. update() {
  48. const iks = this.iks;
  49. for ( let i = 0, il = iks.length; i < il; i ++ ) {
  50. this.updateOne( iks[ i ] );
  51. }
  52. return this;
  53. }
  54. /**
  55. * Update one IK bone
  56. *
  57. * @param {Object} ik parameter
  58. * @return {CCDIKSolver}
  59. */
  60. updateOne( ik ) {
  61. const bones = this.mesh.skeleton.bones; // for reference overhead reduction in loop
  62. const math = Math;
  63. const effector = bones[ ik.effector ];
  64. const target = bones[ ik.target ]; // don't use getWorldPosition() here for the performance
  65. // because it calls updateMatrixWorld( true ) inside.
  66. _targetPos.setFromMatrixPosition( target.matrixWorld );
  67. const links = ik.links;
  68. const iteration = ik.iteration !== undefined ? ik.iteration : 1;
  69. for ( let i = 0; i < iteration; i ++ ) {
  70. let rotated = false;
  71. for ( let j = 0, jl = links.length; j < jl; j ++ ) {
  72. const link = bones[ links[ j ].index ]; // skip this link and following links.
  73. // this skip is used for MMD performance optimization.
  74. if ( links[ j ].enabled === false ) break;
  75. const limitation = links[ j ].limitation;
  76. const rotationMin = links[ j ].rotationMin;
  77. const rotationMax = links[ j ].rotationMax; // don't use getWorldPosition/Quaternion() here for the performance
  78. // because they call updateMatrixWorld( true ) inside.
  79. link.matrixWorld.decompose( _linkPos, _invLinkQ, _linkScale );
  80. _invLinkQ.invert();
  81. _effectorPos.setFromMatrixPosition( effector.matrixWorld ); // work in link world
  82. _effectorVec.subVectors( _effectorPos, _linkPos );
  83. _effectorVec.applyQuaternion( _invLinkQ );
  84. _effectorVec.normalize();
  85. _targetVec.subVectors( _targetPos, _linkPos );
  86. _targetVec.applyQuaternion( _invLinkQ );
  87. _targetVec.normalize();
  88. let angle = _targetVec.dot( _effectorVec );
  89. if ( angle > 1.0 ) {
  90. angle = 1.0;
  91. } else if ( angle < - 1.0 ) {
  92. angle = - 1.0;
  93. }
  94. angle = math.acos( angle ); // skip if changing angle is too small to prevent vibration of bone
  95. // Refer to http://www20.atpages.jp/katwat/three.js_r58/examples/mytest37/mmd.three.js
  96. if ( angle < 1e-5 ) continue;
  97. if ( ik.minAngle !== undefined && angle < ik.minAngle ) {
  98. angle = ik.minAngle;
  99. }
  100. if ( ik.maxAngle !== undefined && angle > ik.maxAngle ) {
  101. angle = ik.maxAngle;
  102. }
  103. _axis.crossVectors( _effectorVec, _targetVec );
  104. _axis.normalize();
  105. _q.setFromAxisAngle( _axis, angle );
  106. link.quaternion.multiply( _q ); // TODO: re-consider the limitation specification
  107. if ( limitation !== undefined ) {
  108. let c = link.quaternion.w;
  109. if ( c > 1.0 ) c = 1.0;
  110. const c2 = math.sqrt( 1 - c * c );
  111. link.quaternion.set( limitation.x * c2, limitation.y * c2, limitation.z * c2, c );
  112. }
  113. if ( rotationMin !== undefined ) {
  114. link.rotation.setFromVector3( link.rotation.toVector3( _vector ).max( rotationMin ) );
  115. }
  116. if ( rotationMax !== undefined ) {
  117. link.rotation.setFromVector3( link.rotation.toVector3( _vector ).min( rotationMax ) );
  118. }
  119. link.updateMatrixWorld( true );
  120. rotated = true;
  121. }
  122. if ( ! rotated ) break;
  123. }
  124. return this;
  125. }
  126. /**
  127. * Creates Helper
  128. *
  129. * @return {CCDIKHelper}
  130. */
  131. createHelper() {
  132. return new CCDIKHelper( this.mesh, this.mesh.geometry.userData.MMD.iks );
  133. } // private methods
  134. _valid() {
  135. const iks = this.iks;
  136. const bones = this.mesh.skeleton.bones;
  137. for ( let i = 0, il = iks.length; i < il; i ++ ) {
  138. const ik = iks[ i ];
  139. const effector = bones[ ik.effector ];
  140. const links = ik.links;
  141. let link0, link1;
  142. link0 = effector;
  143. for ( let j = 0, jl = links.length; j < jl; j ++ ) {
  144. link1 = bones[ links[ j ].index ];
  145. if ( link0.parent !== link1 ) {
  146. console.warn( 'THREE.CCDIKSolver: bone ' + link0.name + ' is not the child of bone ' + link1.name );
  147. }
  148. link0 = link1;
  149. }
  150. }
  151. }
  152. }
  153. function getPosition( bone, matrixWorldInv ) {
  154. return _vector.setFromMatrixPosition( bone.matrixWorld ).applyMatrix4( matrixWorldInv );
  155. }
  156. function setPositionOfBoneToAttributeArray( array, index, bone, matrixWorldInv ) {
  157. const v = getPosition( bone, matrixWorldInv );
  158. array[ index * 3 + 0 ] = v.x;
  159. array[ index * 3 + 1 ] = v.y;
  160. array[ index * 3 + 2 ] = v.z;
  161. }
  162. /**
  163. * Visualize IK bones
  164. *
  165. * @param {SkinnedMesh} mesh
  166. * @param {Array<Object>} iks
  167. */
  168. class CCDIKHelper extends THREE.Object3D {
  169. constructor( mesh, iks = [] ) {
  170. super();
  171. this.root = mesh;
  172. this.iks = iks;
  173. this.matrix.copy( mesh.matrixWorld );
  174. this.matrixAutoUpdate = false;
  175. this.sphereGeometry = new THREE.SphereGeometry( 0.25, 16, 8 );
  176. this.targetSphereMaterial = new THREE.MeshBasicMaterial( {
  177. color: new THREE.Color( 0xff8888 ),
  178. depthTest: false,
  179. depthWrite: false,
  180. transparent: true
  181. } );
  182. this.effectorSphereMaterial = new THREE.MeshBasicMaterial( {
  183. color: new THREE.Color( 0x88ff88 ),
  184. depthTest: false,
  185. depthWrite: false,
  186. transparent: true
  187. } );
  188. this.linkSphereMaterial = new THREE.MeshBasicMaterial( {
  189. color: new THREE.Color( 0x8888ff ),
  190. depthTest: false,
  191. depthWrite: false,
  192. transparent: true
  193. } );
  194. this.lineMaterial = new THREE.LineBasicMaterial( {
  195. color: new THREE.Color( 0xff0000 ),
  196. depthTest: false,
  197. depthWrite: false,
  198. transparent: true
  199. } );
  200. this._init();
  201. }
  202. /**
  203. * Updates IK bones visualization.
  204. */
  205. updateMatrixWorld( force ) {
  206. const mesh = this.root;
  207. if ( this.visible ) {
  208. let offset = 0;
  209. const iks = this.iks;
  210. const bones = mesh.skeleton.bones;
  211. _matrix.copy( mesh.matrixWorld ).invert();
  212. for ( let i = 0, il = iks.length; i < il; i ++ ) {
  213. const ik = iks[ i ];
  214. const targetBone = bones[ ik.target ];
  215. const effectorBone = bones[ ik.effector ];
  216. const targetMesh = this.children[ offset ++ ];
  217. const effectorMesh = this.children[ offset ++ ];
  218. targetMesh.position.copy( getPosition( targetBone, _matrix ) );
  219. effectorMesh.position.copy( getPosition( effectorBone, _matrix ) );
  220. for ( let j = 0, jl = ik.links.length; j < jl; j ++ ) {
  221. const link = ik.links[ j ];
  222. const linkBone = bones[ link.index ];
  223. const linkMesh = this.children[ offset ++ ];
  224. linkMesh.position.copy( getPosition( linkBone, _matrix ) );
  225. }
  226. const line = this.children[ offset ++ ];
  227. const array = line.geometry.attributes.position.array;
  228. setPositionOfBoneToAttributeArray( array, 0, targetBone, _matrix );
  229. setPositionOfBoneToAttributeArray( array, 1, effectorBone, _matrix );
  230. for ( let j = 0, jl = ik.links.length; j < jl; j ++ ) {
  231. const link = ik.links[ j ];
  232. const linkBone = bones[ link.index ];
  233. setPositionOfBoneToAttributeArray( array, j + 2, linkBone, _matrix );
  234. }
  235. line.geometry.attributes.position.needsUpdate = true;
  236. }
  237. }
  238. this.matrix.copy( mesh.matrixWorld );
  239. super.updateMatrixWorld( force );
  240. } // private method
  241. _init() {
  242. const scope = this;
  243. const iks = this.iks;
  244. function createLineGeometry( ik ) {
  245. const geometry = new THREE.BufferGeometry();
  246. const vertices = new Float32Array( ( 2 + ik.links.length ) * 3 );
  247. geometry.setAttribute( 'position', new THREE.BufferAttribute( vertices, 3 ) );
  248. return geometry;
  249. }
  250. function createTargetMesh() {
  251. return new THREE.Mesh( scope.sphereGeometry, scope.targetSphereMaterial );
  252. }
  253. function createEffectorMesh() {
  254. return new THREE.Mesh( scope.sphereGeometry, scope.effectorSphereMaterial );
  255. }
  256. function createLinkMesh() {
  257. return new THREE.Mesh( scope.sphereGeometry, scope.linkSphereMaterial );
  258. }
  259. function createLine( ik ) {
  260. return new THREE.Line( createLineGeometry( ik ), scope.lineMaterial );
  261. }
  262. for ( let i = 0, il = iks.length; i < il; i ++ ) {
  263. const ik = iks[ i ];
  264. this.add( createTargetMesh() );
  265. this.add( createEffectorMesh() );
  266. for ( let j = 0, jl = ik.links.length; j < jl; j ++ ) {
  267. this.add( createLinkMesh() );
  268. }
  269. this.add( createLine( ik ) );
  270. }
  271. }
  272. }
  273. THREE.CCDIKSolver = CCDIKSolver;
  274. } )();