private void CollectBones(List <GPUSkinBone> bones_result, Transform[] bones_smr, Matrix4x4[] bindposes, GPUSkinBone parentBone, Transform currentBoneTransform, int currentBoneIndex) { GPUSkinBone currentBone = new GPUSkinBone(); bones_result.Add(currentBone); int indexOfSmrBones = System.Array.IndexOf(bones_smr, currentBoneTransform); currentBone.transform = currentBoneTransform; currentBone.name = currentBone.transform.gameObject.name; currentBone.bindpose = indexOfSmrBones == -1 ? Matrix4x4.identity : bindposes[indexOfSmrBones]; currentBone.parentBoneIndex = parentBone == null ? -1 : bones_result.IndexOf(parentBone); if (parentBone != null) { parentBone.childrenBonesIndices[currentBoneIndex] = bones_result.IndexOf(currentBone); } int numChildren = currentBone.transform.childCount; if (numChildren > 0) { currentBone.childrenBonesIndices = new int[numChildren]; for (int i = 0; i < numChildren; ++i) { CollectBones(bones_result, bones_smr, bindposes, currentBone, currentBone.transform.GetChild(i), i); } } }
private IEnumerator SamplingCoroutine(GPUSkinFrame frame, int totalFrames) { yield return(new WaitForEndOfFrame()); List <GPUSkinBone> bones = gpuSkinAnimData.bones; int numBones = bones.Count; for (int i = 0; i < numBones; ++i) { Transform boneTransform = bones[i].transform; GPUSkinBone currentBone = GetBoneByTransform(boneTransform); frame.matrices[i] = Matrix4x4.identity;// currentBone.bindpose; do { Matrix4x4 mat = Matrix4x4.TRS(currentBone.transform.localPosition, currentBone.transform.localRotation, currentBone.transform.localScale); frame.matrices[i] = mat * frame.matrices[i]; if (currentBone.parentBoneIndex == -1) { break; } else { currentBone = bones[currentBone.parentBoneIndex]; } }while (true); } int rootBoneIndex = gpuSkinAnimData.rootBoneIndex; if (samplingFrameIndex == 0) { rootMotionPosition = bones[rootBoneIndex].transform.localPosition; rootMotionRotation = bones[rootBoneIndex].transform.localRotation; } else { Vector3 newPosition = bones[rootBoneIndex].transform.localPosition; Quaternion newRotation = bones[rootBoneIndex].transform.localRotation; Vector3 deltaPosition = newPosition - rootMotionPosition; frame.rootMotionDeltaPositionQ = Quaternion.Inverse(Quaternion.Euler(transform.forward.normalized)) * Quaternion.Euler(deltaPosition.normalized); frame.rootMotionDeltaPositionL = deltaPosition.magnitude; frame.rootMotionDeltaRotation = Quaternion.Inverse(rootMotionRotation) * newRotation; if (samplingFrameIndex == 1) { gpuSkinClip.frames[0].rootMotionDeltaPositionQ = gpuSkinClip.frames[1].rootMotionDeltaPositionQ; gpuSkinClip.frames[0].rootMotionDeltaPositionL = gpuSkinClip.frames[1].rootMotionDeltaPositionL; gpuSkinClip.frames[0].rootMotionDeltaRotation = gpuSkinClip.frames[1].rootMotionDeltaRotation; } } ++samplingFrameIndex; }
private int GetBoneIndex(GPUSkinBone bone) { List <GPUSkinBone> bones = gpuSkinAnimData.bones; int numBones = bones.Count; for (int i = 0; i < numBones; ++i) { if (bone == bones[i]) { return(i); } } return(-1); }
private Mesh CreateNewMesh(Mesh mesh, string meshName) { Vector3[] normals = mesh.normals; Vector4[] tangents = mesh.tangents; Color[] colors = mesh.colors; Vector2[] uv = mesh.uv; Mesh newMesh = new Mesh(); newMesh.name = meshName; newMesh.vertices = mesh.vertices; if (normals != null && normals.Length > 0) { newMesh.normals = normals; } if (tangents != null && tangents.Length > 0) { newMesh.tangents = tangents; } if (colors != null && colors.Length > 0) { newMesh.colors = colors; } if (uv != null && uv.Length > 0) { newMesh.uv = uv; } int numVertices = mesh.vertexCount; BoneWeight[] boneWeights = mesh.boneWeights; Vector4[] uv2 = new Vector4[numVertices]; Vector4[] uv3 = new Vector4[numVertices]; Transform[] smrBones = smr.bones; for (int i = 0; i < numVertices; ++i) { BoneWeight boneWeight = boneWeights[i]; BoneWeightSortData[] weights = new BoneWeightSortData[4]; weights[0] = new BoneWeightSortData() { index = boneWeight.boneIndex0, weight = boneWeight.weight0 }; weights[1] = new BoneWeightSortData() { index = boneWeight.boneIndex1, weight = boneWeight.weight1 }; weights[2] = new BoneWeightSortData() { index = boneWeight.boneIndex2, weight = boneWeight.weight2 }; weights[3] = new BoneWeightSortData() { index = boneWeight.boneIndex3, weight = boneWeight.weight3 }; System.Array.Sort(weights); GPUSkinBone bone0 = GetBoneByTransform(smrBones[weights[0].index]); GPUSkinBone bone1 = GetBoneByTransform(smrBones[weights[1].index]); GPUSkinBone bone2 = GetBoneByTransform(smrBones[weights[2].index]); GPUSkinBone bone3 = GetBoneByTransform(smrBones[weights[3].index]); Vector4 skinData_01 = new Vector4(); skinData_01.x = GetBoneIndex(bone0); skinData_01.y = weights[0].weight; skinData_01.z = GetBoneIndex(bone1); skinData_01.w = weights[1].weight; uv2[i] = skinData_01; Vector4 skinData_23 = new Vector4(); skinData_23.x = GetBoneIndex(bone2); skinData_23.y = weights[2].weight; skinData_23.z = GetBoneIndex(bone3); skinData_23.w = weights[3].weight; uv3[i] = skinData_23; } newMesh.SetUVs(1, new List <Vector4>(uv2)); newMesh.SetUVs(2, new List <Vector4>(uv3)); newMesh.triangles = mesh.triangles; return(newMesh); }