Exemplo n.º 1
0
        public static SkinnedMeshRenderer CreateSkinnedMeshRenderer(Animator parser, List <Material> materials, WorkspaceMesh mesh, out int[] indices, out bool[] worldCoords, out bool[] replaceSubmeshesOption)
        {
            int numUncheckedSubmeshes = 0;

            foreach (ImportedSubmesh submesh in mesh.SubmeshList)
            {
                if (!mesh.isSubmeshEnabled(submesh))
                {
                    numUncheckedSubmeshes++;
                }
            }
            int numSubmeshes = mesh.SubmeshList.Count - numUncheckedSubmeshes;

            indices                = new int[numSubmeshes];
            worldCoords            = new bool[numSubmeshes];
            replaceSubmeshesOption = new bool[numSubmeshes];

            List <Matrix>            poseMatrices = new List <Matrix>(mesh.BoneList.Count);
            List <PPtr <Transform> > bones        = CreateBoneList(parser.m_GameObject.instance.FindLinkedComponent(UnityClassID.Transform), mesh.BoneList, poseMatrices);

            SkinnedMeshRenderer sMesh = new SkinnedMeshRenderer(parser.file);

            int totVerts = 0, totFaces = 0;

            sMesh.m_Materials.Capacity = numSubmeshes;
            foreach (ImportedSubmesh submesh in mesh.SubmeshList)
            {
                if (!mesh.isSubmeshEnabled(submesh))
                {
                    continue;
                }

                Material matFound = materials.Find
                                    (
                    delegate(Material mat)
                {
                    return(mat.m_Name == submesh.Material);
                }
                                    );
                sMesh.m_Materials.Add(new PPtr <Material>(matFound));

                totVerts += submesh.VertexList.Count;
                totFaces += submesh.FaceList.Count;
            }
            Mesh uMesh = new Mesh(parser.file);

            uMesh.m_Name = mesh.Name;
            sMesh.m_Mesh = new PPtr <Mesh>(uMesh);

            sMesh.m_Bones          = bones;
            uMesh.m_BindPose       = poseMatrices;
            uMesh.m_BoneNameHashes = new List <uint>(poseMatrices.Count);
            for (int i = 0; i < mesh.BoneList.Count; i++)
            {
                string bone = mesh.BoneList[i].Name;
                uint   hash = parser.m_Avatar.instance.BoneHash(bone);
                uMesh.m_BoneNameHashes.Add(hash);
            }

            uMesh.m_VertexData  = new VertexData((uint)totVerts);
            uMesh.m_Skin        = new List <BoneInfluence>(totVerts);
            uMesh.m_IndexBuffer = new byte[totFaces * 3 * sizeof(ushort)];
            using (BinaryWriter vertWriter = new BinaryWriter(new MemoryStream(uMesh.m_VertexData.m_DataSize)),
                   indexWriter = new BinaryWriter(new MemoryStream(uMesh.m_IndexBuffer)))
            {
                uMesh.m_LocalAABB.m_Center = new Vector3(Single.MaxValue, Single.MaxValue, Single.MaxValue);
                uMesh.m_LocalAABB.m_Extend = new Vector3(Single.MinValue, Single.MinValue, Single.MinValue);
                uMesh.m_SubMeshes          = new List <SubMesh>(numSubmeshes);
                int vertIndex = 0;
                for (int i = 0, submeshIdx = 0; i < numSubmeshes; i++, submeshIdx++)
                {
                    while (!mesh.isSubmeshEnabled(mesh.SubmeshList[submeshIdx]))
                    {
                        submeshIdx++;
                    }

                    SubMesh submesh = new SubMesh();
                    submesh.indexCount  = (uint)mesh.SubmeshList[submeshIdx].FaceList.Count * 3;
                    submesh.vertexCount = (uint)mesh.SubmeshList[submeshIdx].VertexList.Count;
                    submesh.firstVertex = (uint)vertIndex;
                    uMesh.m_SubMeshes.Add(submesh);

                    indices[i]                = mesh.SubmeshList[submeshIdx].Index;
                    worldCoords[i]            = mesh.SubmeshList[submeshIdx].WorldCoords;
                    replaceSubmeshesOption[i] = mesh.isSubmeshReplacingOriginal(mesh.SubmeshList[submeshIdx]);

                    List <ImportedVertex> vertexList = mesh.SubmeshList[submeshIdx].VertexList;
                    Vector3 min = new Vector3(Single.MaxValue, Single.MaxValue, Single.MaxValue);
                    Vector3 max = new Vector3(Single.MinValue, Single.MinValue, Single.MinValue);
                    for (int str = 0; str < uMesh.m_VertexData.m_Streams.Count; str++)
                    {
                        StreamInfo sInfo = uMesh.m_VertexData.m_Streams[str];
                        if (sInfo.channelMask == 0)
                        {
                            continue;
                        }

                        for (int j = 0; j < vertexList.Count; j++)
                        {
                            ImportedVertex vert = vertexList[j];
                            for (int chn = 0; chn < uMesh.m_VertexData.m_Channels.Count; chn++)
                            {
                                ChannelInfo cInfo = uMesh.m_VertexData.m_Channels[chn];
                                if ((sInfo.channelMask & (1 << chn)) == 0)
                                {
                                    continue;
                                }

                                vertWriter.BaseStream.Position = sInfo.offset + (j + submesh.firstVertex) * sInfo.stride + cInfo.offset;
                                switch (chn)
                                {
                                case 0:
                                    Vector3 pos = vert.Position;
                                    pos.X *= -1;
                                    vertWriter.Write(pos);
                                    min = Vector3.Minimize(min, pos);
                                    max = Vector3.Maximize(max, pos);
                                    break;

                                case 1:
                                    Vector3 normal = vert.Normal;
                                    normal.X *= -1;
                                    vertWriter.Write(normal);
                                    break;

                                case 3:
                                    vertWriter.Write(vert.UV);
                                    break;

                                case 5:
                                    Vector4 tangent = vert.Tangent;
                                    tangent.X *= -1;
                                    tangent.W *= -1;
                                    vertWriter.Write(vert.Tangent);
                                    break;
                                }
                            }

                            if (sMesh.m_Bones.Count > 0 && sInfo.offset == 0 && uMesh.m_Skin.Count < totVerts)
                            {
                                BoneInfluence item = new BoneInfluence();
                                for (int k = 0; k < 4; k++)
                                {
                                    item.boneIndex[k] = vert.BoneIndices[k] != 0xFF ? vert.BoneIndices[k] : 0;
                                }
                                vert.Weights.CopyTo(item.weight, 0);
                                uMesh.m_Skin.Add(item);
                            }
                        }
                    }
                    vertIndex += (int)submesh.vertexCount;

                    submesh.localAABB.m_Extend = max - min;
                    submesh.localAABB.m_Center = min + submesh.localAABB.m_Extend / 2;
                    uMesh.m_LocalAABB.m_Extend = Vector3.Maximize(uMesh.m_LocalAABB.m_Extend, max);
                    uMesh.m_LocalAABB.m_Center = Vector3.Minimize(uMesh.m_LocalAABB.m_Center, min);

                    List <ImportedFace> faceList = mesh.SubmeshList[submeshIdx].FaceList;
                    submesh.firstByte = (uint)indexWriter.BaseStream.Position;
                    for (int j = 0; j < faceList.Count; j++)
                    {
                        int[] vertexIndices = faceList[j].VertexIndices;
                        indexWriter.Write((ushort)(vertexIndices[0] + submesh.firstVertex));
                        indexWriter.Write((ushort)(vertexIndices[2] + submesh.firstVertex));
                        indexWriter.Write((ushort)(vertexIndices[1] + submesh.firstVertex));
                    }
                }
                uMesh.m_LocalAABB.m_Extend -= uMesh.m_LocalAABB.m_Center;
                uMesh.m_LocalAABB.m_Center += uMesh.m_LocalAABB.m_Extend / 2;
            }

            return(sMesh);
        }
Exemplo n.º 2
0
        public static SkinnedMeshRenderer CreateSkinnedMeshRenderer(Animator parser, List<Material> materials, WorkspaceMesh mesh, Matrix meshMatrix, out int[] indices, out bool[] worldCoords, out bool[] replaceSubmeshesOption)
        {
            int numUncheckedSubmeshes = 0;
            foreach (ImportedSubmesh submesh in mesh.SubmeshList)
            {
                if (!mesh.isSubmeshEnabled(submesh))
                {
                    numUncheckedSubmeshes++;
                }
            }
            int numSubmeshes = mesh.SubmeshList.Count - numUncheckedSubmeshes;
            indices = new int[numSubmeshes];
            worldCoords = new bool[numSubmeshes];
            replaceSubmeshesOption = new bool[numSubmeshes];

            List<Matrix> poseMatrices = new List<Matrix>(mesh.BoneList.Count);
            Transform animatorTransform = parser.m_GameObject.instance.FindLinkedComponent(UnityClassID.Transform);
            List<PPtr<Transform>> bones = CreateBoneList(animatorTransform, meshMatrix, mesh.BoneList, poseMatrices);

            SkinnedMeshRenderer sMesh = new SkinnedMeshRenderer(parser.file);

            int totVerts = 0, totFaces = 0;
            sMesh.m_Materials.Capacity = numSubmeshes;
            foreach (ImportedSubmesh submesh in mesh.SubmeshList)
            {
                if (!mesh.isSubmeshEnabled(submesh))
                {
                    continue;
                }

                Material matFound = materials.Find
                (
                    delegate(Material mat)
                    {
                        return mat.m_Name == submesh.Material;
                    }
                );
                sMesh.m_Materials.Add(new PPtr<Material>(matFound));

                totVerts += submesh.VertexList.Count;
                totFaces += submesh.FaceList.Count;
            }
            Mesh uMesh = new Mesh(parser.file);
            uMesh.m_Name = mesh.Name;
            sMesh.m_Mesh = new PPtr<Mesh>(uMesh);

            sMesh.m_Bones = bones;
            uMesh.m_BindPose = poseMatrices;
            uMesh.m_BoneNameHashes = new List<uint>(poseMatrices.Count);
            for (int i = 0; i < mesh.BoneList.Count; i++)
            {
                string bone = mesh.BoneList[i].Name;
                uint hash = parser.m_Avatar.instance.BoneHash(bone);
                uMesh.m_BoneNameHashes.Add(hash);
            }

            uMesh.m_VertexData = new VertexData((uint)totVerts);
            uMesh.m_Skin = new List<BoneInfluence>(totVerts);
            uMesh.m_IndexBuffer = new byte[totFaces * 3 * sizeof(ushort)];
            using (BinaryWriter vertWriter = new BinaryWriter(new MemoryStream(uMesh.m_VertexData.m_DataSize)),
                    indexWriter = new BinaryWriter(new MemoryStream(uMesh.m_IndexBuffer)))
            {
                uMesh.m_LocalAABB.m_Center = new Vector3(Single.MaxValue, Single.MaxValue, Single.MaxValue);
                uMesh.m_LocalAABB.m_Extend = new Vector3(Single.MinValue, Single.MinValue, Single.MinValue);
                uMesh.m_SubMeshes = new List<SubMesh>(numSubmeshes);
                int vertIndex = 0;
                for (int i = 0, submeshIdx = 0; i < numSubmeshes; i++, submeshIdx++)
                {
                    while (!mesh.isSubmeshEnabled(mesh.SubmeshList[submeshIdx]))
                    {
                        submeshIdx++;
                    }

                    SubMesh submesh = new SubMesh();
                    submesh.indexCount = (uint)mesh.SubmeshList[submeshIdx].FaceList.Count * 3;
                    submesh.vertexCount = (uint)mesh.SubmeshList[submeshIdx].VertexList.Count;
                    submesh.firstVertex = (uint)vertIndex;
                    uMesh.m_SubMeshes.Add(submesh);

                    indices[i] = mesh.SubmeshList[submeshIdx].Index;
                    worldCoords[i] = mesh.SubmeshList[submeshIdx].WorldCoords;
                    replaceSubmeshesOption[i] = mesh.isSubmeshReplacingOriginal(mesh.SubmeshList[submeshIdx]);

                    List<ImportedVertex> vertexList = mesh.SubmeshList[submeshIdx].VertexList;
                    Vector3 min = new Vector3(Single.MaxValue, Single.MaxValue, Single.MaxValue);
                    Vector3 max = new Vector3(Single.MinValue, Single.MinValue, Single.MinValue);
                    for (int str = 0; str < uMesh.m_VertexData.m_Streams.Count; str++)
                    {
                        StreamInfo sInfo = uMesh.m_VertexData.m_Streams[str];
                        if (sInfo.channelMask == 0)
                        {
                            continue;
                        }

                        for (int j = 0; j < vertexList.Count; j++)
                        {
                            ImportedVertex vert = vertexList[j];
                            for (int chn = 0; chn < uMesh.m_VertexData.m_Channels.Count; chn++)
                            {
                                ChannelInfo cInfo = uMesh.m_VertexData.m_Channels[chn];
                                if ((sInfo.channelMask & (1 << chn)) == 0)
                                {
                                    continue;
                                }

                                vertWriter.BaseStream.Position = sInfo.offset + (j + submesh.firstVertex) * sInfo.stride + cInfo.offset;
                                switch (chn)
                                {
                                case 0:
                                    Vector3 pos = vert.Position;
                                    pos.X *= -1;
                                    vertWriter.Write(pos);
                                    min = Vector3.Minimize(min, pos);
                                    max = Vector3.Maximize(max, pos);
                                    break;
                                case 1:
                                    Vector3 normal = vert.Normal;
                                    normal.X *= -1;
                                    vertWriter.Write(normal);
                                    break;
                                case 3:
                                    vertWriter.Write(vert.UV);
                                    break;
                                case 5:
                                    Vector4 tangent = vert.Tangent;
                                    tangent.X *= -1;
                                    tangent.W *= -1;
                                    vertWriter.Write(tangent);
                                    break;
                                }
                            }

                            if (sMesh.m_Bones.Count > 0 && sInfo.offset == 0 && uMesh.m_Skin.Count < totVerts)
                            {
                                BoneInfluence item = new BoneInfluence();
                                for (int k = 0; k < 4; k++)
                                {
                                    item.boneIndex[k] = vert.BoneIndices[k] != 0xFF ? vert.BoneIndices[k] : 0;
                                }
                                vert.Weights.CopyTo(item.weight, 0);
                                uMesh.m_Skin.Add(item);
                            }
                        }
                    }
                    vertIndex += (int)submesh.vertexCount;

                    submesh.localAABB.m_Extend = max - min;
                    submesh.localAABB.m_Center = min + submesh.localAABB.m_Extend / 2;
                    uMesh.m_LocalAABB.m_Extend = Vector3.Maximize(uMesh.m_LocalAABB.m_Extend, max);
                    uMesh.m_LocalAABB.m_Center = Vector3.Minimize(uMesh.m_LocalAABB.m_Center, min);

                    List<ImportedFace> faceList = mesh.SubmeshList[submeshIdx].FaceList;
                    submesh.firstByte = (uint)indexWriter.BaseStream.Position;
                    for (int j = 0; j < faceList.Count; j++)
                    {
                        int[] vertexIndices = faceList[j].VertexIndices;
                        indexWriter.Write((ushort)(vertexIndices[0] + submesh.firstVertex));
                        indexWriter.Write((ushort)(vertexIndices[2] + submesh.firstVertex));
                        indexWriter.Write((ushort)(vertexIndices[1] + submesh.firstVertex));
                    }
                }
                uMesh.m_LocalAABB.m_Extend -= uMesh.m_LocalAABB.m_Center;
                uMesh.m_LocalAABB.m_Center += uMesh.m_LocalAABB.m_Extend / 2;
            }

            return sMesh;
        }
Exemplo n.º 3
0
            private void ConvertMeshRenderers(List <MeshRenderer> meshList, bool skins, bool morphs)
            {
                MeshList     = new List <ImportedMesh>(meshList.Count);
                MaterialList = new List <ImportedMaterial>(meshList.Count);
                TextureList  = new List <ImportedTexture>(meshList.Count);
                MorphList    = new List <ImportedMorph>(meshList.Count);
                foreach (MeshRenderer meshR in meshList)
                {
                    Mesh mesh = Operations.GetMesh(meshR);
                    if (mesh == null)
                    {
                        Report.ReportLog("skipping " + meshR.m_GameObject.instance.m_Name + " - no mesh");
                        continue;
                    }

                    ImportedMesh iMesh         = new ImportedMesh();
                    Transform    meshTransform = meshR.m_GameObject.instance.FindLinkedComponent(UnityClassID.Transform);
                    iMesh.Name        = meshTransform.GetTransformPath();
                    iMesh.SubmeshList = new List <ImportedSubmesh>(mesh.m_SubMeshes.Count);
                    using (BinaryReader vertReader = new BinaryReader(new MemoryStream(mesh.m_VertexData.m_DataSize)),
                           indexReader = new BinaryReader(new MemoryStream(mesh.m_IndexBuffer)))
                    {
                        for (int i = 0; i < mesh.m_SubMeshes.Count; i++)
                        {
                            SubMesh         submesh  = mesh.m_SubMeshes[i];
                            ImportedSubmesh iSubmesh = new ImportedSubmesh();
                            iSubmesh.Index   = i;
                            iSubmesh.Visible = true;

                            Material mat = meshR.m_Materials[i].instance;
                            ConvertMaterial(mat);
                            iSubmesh.Material = mat.m_Name;

                            iSubmesh.VertexList = new List <ImportedVertex>((int)submesh.vertexCount);
                            for (int str = 0; str < mesh.m_VertexData.m_Streams.Count; str++)
                            {
                                StreamInfo sInfo = mesh.m_VertexData.m_Streams[str];
                                if (sInfo.channelMask == 0)
                                {
                                    continue;
                                }

                                for (int j = 0; j < mesh.m_SubMeshes[i].vertexCount; j++)
                                {
                                    ImportedVertex iVertex;
                                    if (iSubmesh.VertexList.Count < mesh.m_SubMeshes[i].vertexCount)
                                    {
                                        iVertex = new ImportedVertex();
                                        iSubmesh.VertexList.Add(iVertex);
                                    }
                                    else
                                    {
                                        iVertex = iSubmesh.VertexList[j];
                                    }

                                    for (int chn = 0; chn < mesh.m_VertexData.m_Channels.Count; chn++)
                                    {
                                        ChannelInfo cInfo = mesh.m_VertexData.m_Channels[chn];
                                        if ((sInfo.channelMask & (1 << chn)) == 0)
                                        {
                                            continue;
                                        }

                                        vertReader.BaseStream.Position = sInfo.offset + (submesh.firstVertex + j) * sInfo.stride + cInfo.offset;
                                        switch (chn)
                                        {
                                        case 0:
                                            iVertex.Position = new SlimDX.Vector3(-vertReader.ReadSingle(), vertReader.ReadSingle(), vertReader.ReadSingle());
                                            break;

                                        case 1:
                                            iVertex.Normal = new SlimDX.Vector3(-vertReader.ReadSingle(), vertReader.ReadSingle(), vertReader.ReadSingle());
                                            break;

                                        case 3:
                                            iVertex.UV = new float[2] {
                                                vertReader.ReadSingle(), vertReader.ReadSingle()
                                            };
                                            break;

                                        case 5:
                                            iVertex.Tangent = new SlimDX.Vector4(-vertReader.ReadSingle(), vertReader.ReadSingle(), vertReader.ReadSingle(), -vertReader.ReadSingle());
                                            break;
                                        }
                                    }

                                    if (skins && iVertex.BoneIndices == null && mesh.m_Skin.Count > 0)
                                    {
                                        BoneInfluence inf = mesh.m_Skin[(int)submesh.firstVertex + j];
                                        iVertex.BoneIndices = new byte[inf.boneIndex.Length];
                                        for (int k = 0; k < iVertex.BoneIndices.Length; k++)
                                        {
                                            iVertex.BoneIndices[k] = (byte)inf.boneIndex[k];
                                        }
                                        iVertex.Weights = (float[])inf.weight.Clone();
                                    }
                                }
                            }

                            int numFaces = (int)(submesh.indexCount / 3);
                            iSubmesh.FaceList = new List <ImportedFace>(numFaces);
                            indexReader.BaseStream.Position = submesh.firstByte;
                            for (int j = 0; j < numFaces; j++)
                            {
                                ImportedFace face = new ImportedFace();
                                face.VertexIndices    = new int[3];
                                face.VertexIndices[0] = indexReader.ReadUInt16() - (int)submesh.firstVertex;
                                face.VertexIndices[2] = indexReader.ReadUInt16() - (int)submesh.firstVertex;
                                face.VertexIndices[1] = indexReader.ReadUInt16() - (int)submesh.firstVertex;
                                iSubmesh.FaceList.Add(face);
                            }

                            iMesh.SubmeshList.Add(iSubmesh);
                        }
                    }

                    if (skins && meshR is SkinnedMeshRenderer)
                    {
                        SkinnedMeshRenderer sMesh = (SkinnedMeshRenderer)meshR;
                        if (sMesh.m_Bones.Count >= 256)
                        {
                            throw new Exception("Too many bones (" + mesh.m_BindPose.Count + ")");
                        }
                        if (sMesh.m_Bones.Count != mesh.m_BindPose.Count || sMesh.m_Bones.Count != mesh.m_BoneNameHashes.Count)
                        {
                            throw new Exception("Mismatching number of bones bind pose=" + mesh.m_BindPose.Count + " hashes=" + mesh.m_BoneNameHashes.Count + " numBones=" + sMesh.m_Bones.Count);
                        }
                        iMesh.BoneList = new List <ImportedBone>(sMesh.m_Bones.Count);
                        for (int i = 0; i < sMesh.m_Bones.Count; i++)
                        {
                            ImportedBone bone     = new ImportedBone();
                            uint         boneHash = mesh.m_BoneNameHashes[i];
                            bone.Name = avatar.FindBoneName(boneHash);

                            Matrix     m = Matrix.Transpose(mesh.m_BindPose[i]);
                            Vector3    s, t;
                            Quaternion q;
                            m.Decompose(out s, out q, out t);
                            t.X *= -1;
                            Vector3 euler = FbxUtility.QuaternionToEuler(q);
                            euler.Y    *= -1;
                            euler.Z    *= -1;
                            q           = FbxUtility.EulerToQuaternion(euler);
                            bone.Matrix = Matrix.Scaling(s) * Matrix.RotationQuaternion(q) * Matrix.Translation(t);

                            iMesh.BoneList.Add(bone);
                        }
                    }

                    if (morphs && mesh.m_Shapes.shapes.Count > 0)
                    {
                        ImportedMorph morph = new ImportedMorph();
                        morph.Name         = iMesh.Name;
                        morph.ClipName     = Operations.BlendShapeName(mesh);
                        morph.KeyframeList = new List <ImportedMorphKeyframe>(mesh.m_Shapes.shapes.Count);
                        for (int i = 0; i < mesh.m_Shapes.shapes.Count; i++)
                        {
                            ImportedMorphKeyframe keyframe = new ImportedMorphKeyframe();
                            keyframe.Name                 = Operations.BlendShapeKeyframeName(mesh, i);
                            keyframe.VertexList           = new List <ImportedVertex>((int)mesh.m_Shapes.shapes[i].vertexCount);
                            keyframe.MorphedVertexIndices = new List <ushort>((int)mesh.m_Shapes.shapes[i].vertexCount);
                            int lastVertIndex = (int)(mesh.m_Shapes.shapes[i].firstVertex + mesh.m_Shapes.shapes[i].vertexCount);
                            for (int j = (int)mesh.m_Shapes.shapes[i].firstVertex; j < lastVertIndex; j++)
                            {
                                BlendShapeVertex morphVert = mesh.m_Shapes.vertices[j];
                                ImportedVertex   vert      = GetSourceVertex(iMesh.SubmeshList, (int)morphVert.index);
                                ImportedVertex   destVert  = new ImportedVertex();
                                Vector3          morphPos  = morphVert.vertex;
                                morphPos.X       *= -1;
                                destVert.Position = vert.Position + morphPos;
                                Vector3 morphNormal = morphVert.normal;
                                morphNormal.X  *= -1;
                                destVert.Normal = morphNormal;
                                Vector4 morphTangent = new Vector4(morphVert.tangent, 0);
                                morphTangent.X  *= -1;
                                destVert.Tangent = morphTangent;
                                keyframe.VertexList.Add(destVert);
                                keyframe.MorphedVertexIndices.Add((ushort)morphVert.index);
                            }
                            morph.KeyframeList.Add(keyframe);
                        }
                        MorphList.Add(morph);
                    }

                    MeshList.Add(iMesh);
                }
            }