1 line
16 KiB
Plaintext
1 line
16 KiB
Plaintext
|
|
{"version":3,"file":"CCDIKSolver.cjs","sources":["../../src/animation/CCDIKSolver.js"],"sourcesContent":["import {\n BufferAttribute,\n BufferGeometry,\n Color,\n Line,\n LineBasicMaterial,\n Matrix4,\n Mesh,\n MeshBasicMaterial,\n Object3D,\n Quaternion,\n SphereGeometry,\n Vector3,\n} from 'three'\n\nconst _q = /* @__PURE__ */ new Quaternion()\nconst _targetPos = /* @__PURE__ */ new Vector3()\nconst _targetVec = /* @__PURE__ */ new Vector3()\nconst _effectorPos = /* @__PURE__ */ new Vector3()\nconst _effectorVec = /* @__PURE__ */ new Vector3()\nconst _linkPos = /* @__PURE__ */ new Vector3()\nconst _invLinkQ = /* @__PURE__ */ new Quaternion()\nconst _linkScale = /* @__PURE__ */ new Vector3()\nconst _axis = /* @__PURE__ */ new Vector3()\nconst _vector = /* @__PURE__ */ new Vector3()\nconst _matrix = /* @__PURE__ */ new Matrix4()\n\n/**\n * CCD Algorithm\n * - https://sites.google.com/site/auraliusproject/ccd-algorithm\n *\n * // ik parameter example\n * //\n * // target, effector, index in links are bone index in skeleton.bones.\n * // the bones relation should be\n * // <-- parent child -->\n * // links[ n ], links[ n - 1 ], ..., links[ 0 ], effector\n * iks = [ {\n *\ttarget: 1,\n *\teffector: 2,\n *\tlinks: [ { index: 5, limitation: new Vector3( 1, 0, 0 ) }, { index: 4, enabled: false }, { index : 3 } ],\n *\titeration: 10,\n *\tminAngle: 0.0,\n *\tmaxAngle: 1.0,\n * } ];\n */\n\nclass CCDIKSolver {\n /**\n * @param {THREE.SkinnedMesh} mesh\n * @param {Array<Object>} iks\n */\n constructor(mesh, iks = []) {\n this.mesh = mesh\n this.iks = iks\n\n this._valid()\n }\n\n /**\n * Update all IK bones.\n *\n * @return {CCDIKSolver}\n */\n update() {\n const iks = this.iks\n\n for (let i = 0, il = iks.length; i < il; i++) {\n this.updateOne(iks[i])\n }\n\n return this\n }\n\n /**\n * Update one IK bone\n *\n * @param {Object} ik parameter\n * @return {CCDIKSolver}\n */\n updateOne(ik) {\n const bones = this.mesh.skeleton.bones\n\n // for reference overhead reduction in loop\n const math = Math\n\n const effector = bones[ik.effector]\n const target = bones[ik.target]\n\n // don't use getWorldPosition() here for the performance\n // because it calls updateMatrixWorld( true ) inside.\n _targetPos.setFromMatrixPosition(target.matrixWorld)\n\n const links = ik.links\n const iteration = ik.iteration !== undefined ? ik.iteration : 1\n\n for (let i = 0; i < iteration; i++) {\n let rotated = false\n\n for (let j = 0, jl = links.length; j < jl; j++) {\n const link = bones[links[j].index]\n\n // skip this link and following links.\n // this skip is used for MMD performance optimization.\n if (links[j].enabled === false) break\n\n const limitation = links[j].limitation\n const rotationMin = links[j].rotationMin\n const rotationMax = links[j].rotationMax\n\n // don't use getWorldPosition/Quaternion() here for the performance\n // because they call updateMatrixWorld( true ) inside.\n link.matrixWorld.decompose(_linkPos, _invLinkQ, _linkScale)\n _invLinkQ.invert()\n _effectorPos.setFromMatrixPosition(effector.matrixWorld)\n\n // work in link world\n _effectorVec.subVectors(_effectorPos, _linkPos)\n _effectorVec.applyQuaternion(_invLinkQ)\n _effectorVec.normalize()\n\n _targetVec.subVectors(_targetPos, _linkPos)\n _targetVec.applyQuaternion(_invLinkQ)\n _targetVec.normalize()\n\n let angle = _targetVec.dot(_effectorVec)\n\n if (angle > 1.0) {\n angle = 1.0\n } else if (angle < -1.0) {\n angle = -1.0\n }\n\n angle = math.acos(angle)\n\n // skip if changing angle is too small to prevent vibration of bone\n if (angle < 1e-5) continue\n\n if (ik.minAngle !== undefined && angle < ik.minAngle) {\n angle = ik.minAngle\n }\n\n if (ik.maxAngle !== undefined && angle > ik.maxAngl
|