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);

            GPUSkinningBone bone0 = GetBoneByTransform(smrBones[weights[0].index]);
            GPUSkinningBone bone1 = GetBoneByTransform(smrBones[weights[1].index]);
            GPUSkinningBone bone2 = GetBoneByTransform(smrBones[weights[2].index]);
            GPUSkinningBone 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);
    }
Exemplo n.º 2
0
    public List <BakedData> Bake(GameObject go, GameObject fbx, GameObject rootBone, string path, Dictionary <string, string> dic, ref Mesh tarMesh)
    {
        if (this.animData == null)
        {
            Debug.LogError("bake data is null!!");
            return(this.bakedDataList);
        }


        List <GPUSkinningBone> bones_result = new List <GPUSkinningBone>();
        SkinnedMeshRenderer    skin         = animData.skin;

        CollectBones(bones_result, skin.bones, skin.sharedMesh.bindposes, null, rootBone.transform, 0);
        GPUSkinningBone[] bones = bones_result.ToArray();
        Mesh mesh = new Mesh();

        mesh.name     = skin.sharedMesh.name;
        mesh.vertices = skin.sharedMesh.vertices;
        mesh.normals  = skin.sharedMesh.normals;
        mesh.tangents = skin.sharedMesh.tangents;
        mesh.uv       = skin.sharedMesh.uv;
        BoneWeight[] boneWeights = skin.sharedMesh.boneWeights;
        int          numVertices = mesh.vertices.Length;

        Vector4[]   uv2      = new Vector4[numVertices];
        Vector4[]   uv3      = new Vector4[numVertices];
        Transform[] smrBones = skin.bones;

        for (int i = 0; i < mesh.vertices.Length; i++)
        {
            BoneWeight           weight  = boneWeights[i];
            BoneWeightSortData[] weights = new BoneWeightSortData[4];
            weights[0] = new BoneWeightSortData()
            {
                index = weight.boneIndex0, weight = weight.weight0
            };
            weights[1] = new BoneWeightSortData()
            {
                index = weight.boneIndex1, weight = weight.weight1
            };
            weights[2] = new BoneWeightSortData()
            {
                index = weight.boneIndex2, weight = weight.weight2
            };
            weights[3] = new BoneWeightSortData()
            {
                index = weight.boneIndex3, weight = weight.weight3
            };
            System.Array.Sort(weights);
            //Debug.Log(weights[0].index + ":" + weights[0].weight + ":" + weights[1].index + ":" + weights[1].weight + ":" + weights[2].index + ":" + weights[2].weight + ":" + weights[3].index + ":" + weights[3].weight);
            GPUSkinningBone bone0 = GetBoneByTransform(smrBones[weights[0].index], bones);
            GPUSkinningBone bone1 = GetBoneByTransform(smrBones[weights[1].index], bones);
            GPUSkinningBone bone2 = GetBoneByTransform(smrBones[weights[2].index], bones);
            GPUSkinningBone bone3 = GetBoneByTransform(smrBones[weights[3].index], bones);

            Vector4 skinData_01 = new Vector4();
            skinData_01.x = GetBoneIndex(bone0, bones);
            skinData_01.y = weights[0].weight;
            skinData_01.z = GetBoneIndex(bone1, bones);
            skinData_01.w = weights[1].weight;
            uv2[i]        = skinData_01;
        }

        mesh.SetUVs(1, new List <Vector4>(uv2));
        mesh.triangles = skin.sharedMesh.triangles;

        AssetDatabase.CreateAsset(mesh, "Assets/" + Path.Combine(path, mesh.name + "mesh.asset"));

        tarMesh = mesh;
        BakeAllAnimClip(this.animData.animClips, path, go, 30f, rootBone, bones);

        Object target  = PrefabUtility.GetPrefabParent(fbx);
        string fbxName = AssetDatabase.GetAssetPath(target);

        if (target == null)
        {
            fbxName = AssetDatabase.GetAssetPath(fbx);
        }
        else
        {
            fbxName = "Assets/" + path + "/" + fbx.name + ".fbx";
        }
        Debug.Log(fbxName);
        ModelImporter modelImporter = (ModelImporter)AssetImporter.GetAtPath(fbxName);

        GenAnimationDataFile(modelImporter.clipAnimations, this.animData.animClips, 30f, go, path, dic);

        return(this.bakedDataList);
    }