1 line
14 KiB
Plaintext
1 line
14 KiB
Plaintext
|
|
{"version":3,"file":"SkeletonUtils.cjs","sources":["../../src/utils/SkeletonUtils.js"],"sourcesContent":["import {\n AnimationClip,\n AnimationMixer,\n Matrix4,\n Quaternion,\n QuaternionKeyframeTrack,\n SkeletonHelper,\n Vector3,\n VectorKeyframeTrack,\n} from 'three'\n\nfunction retarget(target, source, options = {}) {\n const pos = new Vector3(),\n quat = new Quaternion(),\n scale = new Vector3(),\n bindBoneMatrix = new Matrix4(),\n relativeMatrix = new Matrix4(),\n globalMatrix = new Matrix4()\n\n options.preserveMatrix = options.preserveMatrix !== undefined ? options.preserveMatrix : true\n options.preservePosition = options.preservePosition !== undefined ? options.preservePosition : true\n options.preserveHipPosition = options.preserveHipPosition !== undefined ? options.preserveHipPosition : false\n options.useTargetMatrix = options.useTargetMatrix !== undefined ? options.useTargetMatrix : false\n options.hip = options.hip !== undefined ? options.hip : 'hip'\n options.names = options.names || {}\n\n const sourceBones = source.isObject3D ? source.skeleton.bones : getBones(source),\n bones = target.isObject3D ? target.skeleton.bones : getBones(target)\n\n let bindBones, bone, name, boneTo, bonesPosition\n\n // reset bones\n\n if (target.isObject3D) {\n target.skeleton.pose()\n } else {\n options.useTargetMatrix = true\n options.preserveMatrix = false\n }\n\n if (options.preservePosition) {\n bonesPosition = []\n\n for (let i = 0; i < bones.length; i++) {\n bonesPosition.push(bones[i].position.clone())\n }\n }\n\n if (options.preserveMatrix) {\n // reset matrix\n\n target.updateMatrixWorld()\n\n target.matrixWorld.identity()\n\n // reset children matrix\n\n for (let i = 0; i < target.children.length; ++i) {\n target.children[i].updateMatrixWorld(true)\n }\n }\n\n if (options.offsets) {\n bindBones = []\n\n for (let i = 0; i < bones.length; ++i) {\n bone = bones[i]\n name = options.names[bone.name] || bone.name\n\n if (options.offsets[name]) {\n bone.matrix.multiply(options.offsets[name])\n\n bone.matrix.decompose(bone.position, bone.quaternion, bone.scale)\n\n bone.updateMatrixWorld()\n }\n\n bindBones.push(bone.matrixWorld.clone())\n }\n }\n\n for (let i = 0; i < bones.length; ++i) {\n bone = bones[i]\n name = options.names[bone.name] || bone.name\n\n boneTo = getBoneByName(name, sourceBones)\n\n globalMatrix.copy(bone.matrixWorld)\n\n if (boneTo) {\n boneTo.updateMatrixWorld()\n\n if (options.useTargetMatrix) {\n relativeMatrix.copy(boneTo.matrixWorld)\n } else {\n relativeMatrix.copy(target.matrixWorld).invert()\n relativeMatrix.multiply(boneTo.matrixWorld)\n }\n\n // ignore scale to extract rotation\n\n scale.setFromMatrixScale(relativeMatrix)\n relativeMatrix.scale(scale.set(1 / scale.x, 1 / scale.y, 1 / scale.z))\n\n // apply to global matrix\n\n globalMatrix.makeRotationFromQuaternion(quat.setFromRotationMatrix(relativeMatrix))\n\n if (target.isObject3D) {\n const boneIndex = bones.indexOf(bone),\n wBindMatrix = bindBones\n ? bindBones[boneIndex]\n : bindBoneMatrix.copy(target.skeleton.boneInverses[boneIndex]).invert()\n\n globalMatrix.multiply(wBindMatrix)\n }\n\n globalMatrix.copyPosition(relativeMatrix)\n }\n\n if (bone.parent && bone.parent.isBone) {\n bone.matrix.copy(bone.parent.matrixWorld).invert()\n bone.matrix.multiply(globalMatrix)\n } else {\n bone.matrix.copy(globalMatrix)\n }\n\n if (options.preserveHipPosition && name === options.hip) {\n bone.matrix.setPosition(pos.set(0, bone.position.y, 0))\n }\n\n bone.matrix.decompose(bone.position, bone.quaternion, bone.scale)\n\n bone.updateMatrixWorld()\n }\n\n if (options.preservePosition) {\n for (let i = 0; i < bones.length; ++i) {\n bone = bones[i]\n name = options.names[bone.name] || bone.name\n\n if (na
|