CCDIKSolver.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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. if ( angle < 1e-5 ) continue;
  96. if ( ik.minAngle !== undefined && angle < ik.minAngle ) {
  97. angle = ik.minAngle;
  98. }
  99. if ( ik.maxAngle !== undefined && angle > ik.maxAngle ) {
  100. angle = ik.maxAngle;
  101. }
  102. _axis.crossVectors( _effectorVec, _targetVec );
  103. _axis.normalize();
  104. _q.setFromAxisAngle( _axis, angle );
  105. link.quaternion.multiply( _q ); // TODO: re-consider the limitation specification
  106. if ( limitation !== undefined ) {
  107. let c = link.quaternion.w;
  108. if ( c > 1.0 ) c = 1.0;
  109. const c2 = math.sqrt( 1 - c * c );
  110. link.quaternion.set( limitation.x * c2, limitation.y * c2, limitation.z * c2, c );
  111. }
  112. if ( rotationMin !== undefined ) {
  113. link.rotation.setFromVector3( link.rotation.toVector3( _vector ).max( rotationMin ) );
  114. }
  115. if ( rotationMax !== undefined ) {
  116. link.rotation.setFromVector3( link.rotation.toVector3( _vector ).min( rotationMax ) );
  117. }
  118. link.updateMatrixWorld( true );
  119. rotated = true;
  120. }
  121. if ( ! rotated ) break;
  122. }
  123. return this;
  124. }
  125. /**
  126. * Creates Helper
  127. *
  128. * @return {CCDIKHelper}
  129. */
  130. createHelper() {
  131. return new CCDIKHelper( this.mesh, this.mesh.geometry.userData.MMD.iks );
  132. } // private methods
  133. _valid() {
  134. const iks = this.iks;
  135. const bones = this.mesh.skeleton.bones;
  136. for ( let i = 0, il = iks.length; i < il; i ++ ) {
  137. const ik = iks[ i ];
  138. const effector = bones[ ik.effector ];
  139. const links = ik.links;
  140. let link0, link1;
  141. link0 = effector;
  142. for ( let j = 0, jl = links.length; j < jl; j ++ ) {
  143. link1 = bones[ links[ j ].index ];
  144. if ( link0.parent !== link1 ) {
  145. console.warn( 'THREE.CCDIKSolver: bone ' + link0.name + ' is not the child of bone ' + link1.name );
  146. }
  147. link0 = link1;
  148. }
  149. }
  150. }
  151. }
  152. function getPosition( bone, matrixWorldInv ) {
  153. return _vector.setFromMatrixPosition( bone.matrixWorld ).applyMatrix4( matrixWorldInv );
  154. }
  155. function setPositionOfBoneToAttributeArray( array, index, bone, matrixWorldInv ) {
  156. const v = getPosition( bone, matrixWorldInv );
  157. array[ index * 3 + 0 ] = v.x;
  158. array[ index * 3 + 1 ] = v.y;
  159. array[ index * 3 + 2 ] = v.z;
  160. }
  161. /**
  162. * Visualize IK bones
  163. *
  164. * @param {SkinnedMesh} mesh
  165. * @param {Array<Object>} iks
  166. */
  167. class CCDIKHelper extends THREE.Object3D {
  168. constructor( mesh, iks = [] ) {
  169. super();
  170. this.root = mesh;
  171. this.iks = iks;
  172. this.matrix.copy( mesh.matrixWorld );
  173. this.matrixAutoUpdate = false;
  174. this.sphereGeometry = new THREE.SphereGeometry( 0.25, 16, 8 );
  175. this.targetSphereMaterial = new THREE.MeshBasicMaterial( {
  176. color: new THREE.Color( 0xff8888 ),
  177. depthTest: false,
  178. depthWrite: false,
  179. transparent: true
  180. } );
  181. this.effectorSphereMaterial = new THREE.MeshBasicMaterial( {
  182. color: new THREE.Color( 0x88ff88 ),
  183. depthTest: false,
  184. depthWrite: false,
  185. transparent: true
  186. } );
  187. this.linkSphereMaterial = new THREE.MeshBasicMaterial( {
  188. color: new THREE.Color( 0x8888ff ),
  189. depthTest: false,
  190. depthWrite: false,
  191. transparent: true
  192. } );
  193. this.lineMaterial = new THREE.LineBasicMaterial( {
  194. color: new THREE.Color( 0xff0000 ),
  195. depthTest: false,
  196. depthWrite: false,
  197. transparent: true
  198. } );
  199. this._init();
  200. }
  201. /**
  202. * Updates IK bones visualization.
  203. */
  204. updateMatrixWorld( force ) {
  205. const mesh = this.root;
  206. if ( this.visible ) {
  207. let offset = 0;
  208. const iks = this.iks;
  209. const bones = mesh.skeleton.bones;
  210. _matrix.copy( mesh.matrixWorld ).invert();
  211. for ( let i = 0, il = iks.length; i < il; i ++ ) {
  212. const ik = iks[ i ];
  213. const targetBone = bones[ ik.target ];
  214. const effectorBone = bones[ ik.effector ];
  215. const targetMesh = this.children[ offset ++ ];
  216. const effectorMesh = this.children[ offset ++ ];
  217. targetMesh.position.copy( getPosition( targetBone, _matrix ) );
  218. effectorMesh.position.copy( getPosition( effectorBone, _matrix ) );
  219. for ( let j = 0, jl = ik.links.length; j < jl; j ++ ) {
  220. const link = ik.links[ j ];
  221. const linkBone = bones[ link.index ];
  222. const linkMesh = this.children[ offset ++ ];
  223. linkMesh.position.copy( getPosition( linkBone, _matrix ) );
  224. }
  225. const line = this.children[ offset ++ ];
  226. const array = line.geometry.attributes.position.array;
  227. setPositionOfBoneToAttributeArray( array, 0, targetBone, _matrix );
  228. setPositionOfBoneToAttributeArray( array, 1, effectorBone, _matrix );
  229. for ( let j = 0, jl = ik.links.length; j < jl; j ++ ) {
  230. const link = ik.links[ j ];
  231. const linkBone = bones[ link.index ];
  232. setPositionOfBoneToAttributeArray( array, j + 2, linkBone, _matrix );
  233. }
  234. line.geometry.attributes.position.needsUpdate = true;
  235. }
  236. }
  237. this.matrix.copy( mesh.matrixWorld );
  238. super.updateMatrixWorld( force );
  239. } // private method
  240. _init() {
  241. const scope = this;
  242. const iks = this.iks;
  243. function createLineGeometry( ik ) {
  244. const geometry = new THREE.BufferGeometry();
  245. const vertices = new Float32Array( ( 2 + ik.links.length ) * 3 );
  246. geometry.setAttribute( 'position', new THREE.BufferAttribute( vertices, 3 ) );
  247. return geometry;
  248. }
  249. function createTargetMesh() {
  250. return new THREE.Mesh( scope.sphereGeometry, scope.targetSphereMaterial );
  251. }
  252. function createEffectorMesh() {
  253. return new THREE.Mesh( scope.sphereGeometry, scope.effectorSphereMaterial );
  254. }
  255. function createLinkMesh() {
  256. return new THREE.Mesh( scope.sphereGeometry, scope.linkSphereMaterial );
  257. }
  258. function createLine( ik ) {
  259. return new THREE.Line( createLineGeometry( ik ), scope.lineMaterial );
  260. }
  261. for ( let i = 0, il = iks.length; i < il; i ++ ) {
  262. const ik = iks[ i ];
  263. this.add( createTargetMesh() );
  264. this.add( createEffectorMesh() );
  265. for ( let j = 0, jl = ik.links.length; j < jl; j ++ ) {
  266. this.add( createLinkMesh() );
  267. }
  268. this.add( createLine( ik ) );
  269. }
  270. }
  271. }
  272. THREE.CCDIKSolver = CCDIKSolver;
  273. } )();