예제 #1
0
        public static remMesh CreateMesh(WorkspaceMesh mesh, out string[] materialNames, 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;
            materialNames = new string[numSubmeshes];
            indices = new int[numSubmeshes];
            worldCoords = new bool[numSubmeshes];
            replaceSubmeshesOption = new bool[numSubmeshes];

            remMesh newMesh = new remMesh(numSubmeshes);
            newMesh.name = new remId(mesh.Name);

            List<remVertex> newVertices = new List<remVertex>();
            List<int> newFaces = new List<int>();
            List<int> newFaceMarks = new List<int>();
            for (int i = 0, submeshIdx = 0; i < numSubmeshes; i++, submeshIdx++)
            {
                while (!mesh.isSubmeshEnabled(mesh.SubmeshList[submeshIdx]))
                    submeshIdx++;

                ImportedSubmesh submesh = mesh.SubmeshList[submeshIdx];

                newMesh.AddMaterial(new remId(submesh.Material));
                materialNames[i] = submesh.Material;
                indices[i] = submesh.Index;
                worldCoords[i] = submesh.WorldCoords;
                replaceSubmeshesOption[i] = mesh.isSubmeshReplacingOriginal(submesh);

                List<ImportedFace> faceList = submesh.FaceList;
                newFaces.Capacity += faceList.Count * 3;
                int[] faceMarks = new int[faceList.Count];
                for (int j = 0; j < faceList.Count; j++)
                {
                    ImportedFace face = faceList[j];
                    for (int k = 0; k < 3; k++)
                    {
                        newFaces.Add(face.VertexIndices[k] + newVertices.Count);
                    }
                    faceMarks[j] = i;
                }
                newFaceMarks.AddRange(faceMarks);

                List<ImportedVertex> vertexList = submesh.VertexList;
                newVertices.Capacity += vertexList.Count;
                for (int j = 0; j < vertexList.Count; j++)
                {
                    ImportedVertex vert = vertexList[j];
                    remVertex newVertex = new remVertex();

                    if (submesh.WorldCoords)
                    {
                        newVertex.Position = vert.Position;
                        newVertex.Normal = vert.Normal;
                    }
                    else
                    {
                        newVertex.Position = new Vector3(vert.Position.X, -vert.Position.Z, vert.Position.Y);
                        newVertex.Normal = new Vector3(vert.Normal.X, -vert.Normal.Z, vert.Normal.Y);
                    }
                    newVertex.UV = new Vector2(vert.UV[0], vert.UV[1]);
                    newVertices.Add(newVertex);
                }
            }
            newMesh.vertices = newVertices.ToArray();
            newMesh.faces = newFaces.ToArray();
            newMesh.faceMarks = newFaceMarks.ToArray();

            return newMesh;
        }
예제 #2
0
        private static remMESCsection ReadMeshes(string sectionName, int sectionLength, int numMeshes, byte[] sectionBuffer)
        {
            remMESCsection meshSec = new remMESCsection(numMeshes);
            int secBufIdx = 0;
            for (int subSection = 0; subSection < numMeshes; subSection++)
            {
                byte[] type = new byte[4] { sectionBuffer[secBufIdx+0], sectionBuffer[secBufIdx+1], sectionBuffer[secBufIdx+2], sectionBuffer[secBufIdx+3] };
                int length = BitConverter.ToInt32(sectionBuffer, secBufIdx+4);

                remMesh mesh = new remMesh(5);
                Trace.Assert(TypeCheck(remMesh.ClassType, type));
                mesh.frame = GetIdentifier(sectionBuffer, secBufIdx+8);

                int numMats = BitConverter.ToInt32(sectionBuffer, secBufIdx+8+256);
                mesh.name = GetIdentifier(sectionBuffer, secBufIdx+8+256+4);
                int numFaces = BitConverter.ToInt32(sectionBuffer, secBufIdx+8+256+4+256);
                int numVertices = BitConverter.ToInt32(sectionBuffer, secBufIdx+8+256+4+256 + 4);
                for (int i = 0; i < mesh.unknown.Length; i++)
                    mesh.unknown[i] = BitConverter.ToInt32(sectionBuffer, secBufIdx+8+256+4+256+8 + i*4);
                for (int i = 0; i < numMats; i++)
                {
                    remId mat = GetIdentifier(sectionBuffer, secBufIdx+8+256+4+256 + 4*4 + i*256);
                    mesh.AddMaterial(mat);
                }

                mesh.vertices = new remVertex[numVertices];
                int vertBufIdx = secBufIdx+8+256+4+256 + 4*4 + mesh.numMats*256;
                for (int i = 0; i < numVertices; i++)
                {
                    remVertex vertex = new remVertex();
                    vertex.Position = new Vector3();
                    vertex.Position[0] = BitConverter.ToSingle(sectionBuffer, vertBufIdx + 0);
                    vertex.Position[1] = BitConverter.ToSingle(sectionBuffer, vertBufIdx + 4);
                    vertex.Position[2] = BitConverter.ToSingle(sectionBuffer, vertBufIdx + 8);

                    vertex.UV = new Vector2();
                    vertex.UV[0]  = BitConverter.ToSingle(sectionBuffer, vertBufIdx + 12);
                    vertex.UV[1]  = BitConverter.ToSingle(sectionBuffer, vertBufIdx + 16);

                    vertex.Normal = new Vector3();
                    vertex.Normal[0] = BitConverter.ToSingle(sectionBuffer, vertBufIdx + 20);
                    vertex.Normal[1] = BitConverter.ToSingle(sectionBuffer, vertBufIdx + 24);
                    vertex.Normal[2] = BitConverter.ToSingle(sectionBuffer, vertBufIdx + 28);

                    vertex.RGBA = new Color4(BitConverter.ToInt32(sectionBuffer, vertBufIdx + 32));

                    mesh.vertices[i] = vertex;
                    vertBufIdx += 36;
                }

                mesh.faces = new int[numFaces * 3];
                int faceBufIdx = vertBufIdx;
                for (int i = 0; i < numFaces; i++)
                {
                    mesh.faces[i*3+0] = BitConverter.ToInt32(sectionBuffer, faceBufIdx + 0);
                    mesh.faces[i*3+1] = BitConverter.ToInt32(sectionBuffer, faceBufIdx + 4);
                    mesh.faces[i*3+2] = BitConverter.ToInt32(sectionBuffer, faceBufIdx + 8);
                    faceBufIdx += 12;
                }

                mesh.faceMarks = new int[numFaces];
                int faceExtraIdx = faceBufIdx;
                for (int i = 0; i < numFaces; i++)
                {
                    mesh.faceMarks[i] = BitConverter.ToInt32(sectionBuffer, faceExtraIdx);
                    faceExtraIdx += 4;
                }

                meshSec.AddChild(mesh);

                secBufIdx += length;
            }
            if (secBufIdx != sectionLength)
                Report.ReportLog("Warning! MESC section has wrong length.");
            return meshSec;
        }