Exemplo n.º 1
0
        public void ReadPrerequisites(string name)
        {
            using (BinaryReader reader = new BinaryReader(File.Open(name, FileMode.Open)))
            {
                ushort MeshFlags = reader.ReadUInt16();
                VertexDecleration = (VertexFlags)reader.ReadUInt32();

                int IndexSize = reader.ReadInt32();
                IndexBuffer = reader.ReadBytes(IndexSize);

                VertexCount = reader.ReadInt32();
                int VertexSize = reader.ReadInt32();
                VertexBuffer = reader.ReadBytes(VertexSize);
            }

            //VertexDecleration = (VertexFlags)134933;
            //Offset = new Vector3(-233.86592f, -642.7829f, -1.5614158f);
            //Scale = 0.013024263f;

            int vertexSize;
            Dictionary <VertexFlags, FrameLOD.VertexOffset> vertexOffsets = GetVertexOffsets(out vertexSize);

            int tempVertexSize = VertexBuffer.Length / VertexCount;

            Vertex[] Vertices = new Vertex[VertexCount];

            for (int x = 0; x != VertexCount; x++)
            {
                //declare data required and send to decompresser
                byte[] data = new byte[tempVertexSize];
                Array.Copy(VertexBuffer, (x * tempVertexSize), data, 0, tempVertexSize);
                Vertex decompressed = VertexTranslator.DecompressVertex(data, VertexDecleration, Vector3.Zero, 1.525879E-05f, vertexOffsets);
                Vertices[x] = decompressed;
            }

            Int3[] Triangles = new Int3[IndexBuffer.Length / 3];
            int    index     = 0;

            for (int y = 0; y != IndexBuffer.Length; y += 6)
            {
                int  X        = BitConverter.ToUInt16(IndexBuffer, y + 0);
                int  Y        = BitConverter.ToUInt16(IndexBuffer, y + 2);
                int  Z        = BitConverter.ToUInt16(IndexBuffer, y + 4);
                Int3 triangle = new Int3(X, Y, Z);
                Triangles[index] = triangle;
                index++;
            }

            File.WriteAllLines(name + ".obj", BuildMesh(Vertices, Triangles));
        }
Exemplo n.º 2
0
        public void ReadPrerequisites()
        {
            VertexDecleration = (VertexFlags)134933;
            Offset            = new Vector3(-233.86592f, -642.7829f, -1.5614158f);
            Scale             = 0.013024263f;

            int vertexSize;
            Dictionary <VertexFlags, FrameLOD.VertexOffset> vertexOffsets = GetVertexOffsets(out vertexSize);

            VertexBuffer = File.ReadAllBytes("M3/VertexBuffer.bin");
            IndexBuffer  = File.ReadAllBytes("M3/IndexBuffer.bin");

            int tempVertexSize = VertexBuffer.Length / VertexCount;

            Vertex[] Vertices = new Vertex[VertexCount];

            for (int x = 0; x != VertexCount; x++)
            {
                //declare data required and send to decompresser
                byte[] data = new byte[tempVertexSize];
                Array.Copy(VertexBuffer, (x * tempVertexSize), data, 0, tempVertexSize);
                Vertex decompressed = VertexTranslator.DecompressVertex(data, VertexDecleration, Vector3.Zero, 1.525879E-05f, vertexOffsets);
                Vertices[x] = decompressed;
            }

            Int3[] Triangles = new Int3[IndexBuffer.Length / 3];
            int    index     = 0;

            for (int y = 0; y != IndexBuffer.Length; y += 6)
            {
                Int3 triangle = new Int3();
                triangle.X       = BitConverter.ToUInt16(IndexBuffer, y + 0);
                triangle.Y       = BitConverter.ToUInt16(IndexBuffer, y + 2);
                triangle.Z       = BitConverter.ToUInt16(IndexBuffer, y + 4);
                Triangles[index] = triangle;
                index++;
            }

            File.WriteAllLines("Test.obj", BuildMesh(Vertices, Triangles));
        }
Exemplo n.º 3
0
        public bool ConvertFrameToRenderModel(FrameObjectSingleMesh mesh, FrameGeometry geom, FrameMaterial mats, IndexBuffer[] indexBuffers, VertexBuffer[] vertexBuffers)
        {
            if (mesh == null || geom == null || mats == null || indexBuffers[0] == null || vertexBuffers[0] == null)
            {
                return(false);
            }

            aoHash = mesh.OMTextureHash;
            SetTransform(mesh.WorldTransform);
            //DoRender = (mesh.SecondaryFlags == 4097 ? true : false);
            BoundingBox = mesh.Boundings;
            LODs        = new LOD[geom.NumLods];

            for (int i = 0; i != geom.NumLods; i++)
            {
                LOD lod = new LOD();
                lod.Indices    = indexBuffers[i].GetData();
                lod.ModelParts = new ModelPart[mats.LodMatCount[i]];

                for (int z = 0; z != mats.Materials[i].Length; z++)
                {
                    lod.ModelParts[z]              = new ModelPart();
                    lod.ModelParts[z].NumFaces     = (uint)mats.Materials[i][z].NumFaces;
                    lod.ModelParts[z].StartIndex   = (uint)mats.Materials[i][z].StartIndex;
                    lod.ModelParts[z].MaterialHash = mats.Materials[i][z].MaterialHash;
                    lod.ModelParts[z].Material     = MaterialsManager.LookupMaterialByHash(lod.ModelParts[z].MaterialHash);
                }

                lod.Vertices = new VertexLayouts.NormalLayout.Vertex[geom.LOD[i].NumVerts];
                int vertexSize;
                Dictionary <VertexFlags, FrameLOD.VertexOffset> vertexOffsets = geom.LOD[i].GetVertexOffsets(out vertexSize);
                try
                {
                    for (int x = 0; x != lod.Vertices.Length; x++)
                    {
                        VertexLayouts.NormalLayout.Vertex vertex = new VertexLayouts.NormalLayout.Vertex();

                        //declare data required and send to decompresser
                        byte[] data = new byte[vertexSize];
                        Array.Copy(vertexBuffers[i].Data, (x * vertexSize), data, 0, vertexSize);
                        Vertex decompressed = VertexTranslator.DecompressVertex(data, geom.LOD[i].VertexDeclaration, geom.DecompressionOffset, geom.DecompressionFactor, vertexOffsets);

                        //retrieve the data we require
                        vertex.Position  = decompressed.Position;
                        vertex.Normal    = decompressed.Normal;
                        vertex.Tangent   = decompressed.Tangent;
                        vertex.Binormal  = decompressed.Binormal;
                        vertex.TexCoord0 = decompressed.UVs[0];
                        vertex.TexCoord7 = decompressed.UVs[3];

                        lod.Vertices[x] = vertex;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(string.Format("Error when creating renderable {1}!: \n{0}", ex.Message, mesh.Name.ToString()), "Toolkit");
                    return(false);
                }
                LODs[i] = lod;
            }

            SetupShaders();
            return(true);
        }
Exemplo n.º 4
0
        public bool ConvertFrameToRenderModel(FrameObjectSingleMesh mesh, FrameGeometry geom, FrameMaterial mats, IndexBuffer[] indexBuffers, VertexBuffer[] vertexBuffers)
        {
            if (mesh == null || geom == null || mats == null || indexBuffers == null || vertexBuffers == null)
            {
                return(false);
            }

            aoHash = mesh.OMTextureHash;
            SetTransform(mesh.Matrix.Position, mesh.Matrix.Matrix);
            //DoRender = (mesh.SecondaryFlags == 4097 ? true : false);
            BoundingBox = new RenderBoundingBox();
            BoundingBox.Init(mesh.Boundings);
            BoundingBox.SetTransform(Transform);
            BoundingBox.DoRender = false;
            LODs = new LOD[geom.NumLods];

            for (int i = 0; i != geom.NumLods; i++)
            {
                LOD lod = new LOD();
                lod.Indices    = indexBuffers[i].Data;
                lod.ModelParts = new ModelPart[mats.LodMatCount[i]];

                for (int z = 0; z != mats.Materials[i].Length; z++)
                {
                    lod.ModelParts[z]              = new ModelPart();
                    lod.ModelParts[z].NumFaces     = (uint)mats.Materials[i][z].NumFaces;
                    lod.ModelParts[z].StartIndex   = (uint)mats.Materials[i][z].StartIndex;
                    lod.ModelParts[z].MaterialHash = mats.Materials[i][z].MaterialHash;
                    lod.ModelParts[z].Material     = MaterialsManager.LookupMaterialByHash(lod.ModelParts[z].MaterialHash);
                }

                lod.Vertices = new VertexLayouts.NormalLayout.Vertex[geom.LOD[i].NumVertsPr];
                int vertexSize;
                Dictionary <VertexFlags, FrameLOD.VertexOffset> vertexOffsets = geom.LOD[i].GetVertexOffsets(out vertexSize);

                for (int x = 0; x != lod.Vertices.Length; x++)
                {
                    VertexLayouts.NormalLayout.Vertex vertex = new VertexLayouts.NormalLayout.Vertex();

                    if (geom.LOD[i].VertexDeclaration.HasFlag(VertexFlags.Position))
                    {
                        int startIndex = x * vertexSize + vertexOffsets[VertexFlags.Position].Offset;
                        vertex.Position = VertexTranslator.ReadPositionDataFromVB(vertexBuffers[i].Data, startIndex, geom.DecompressionFactor, geom.DecompressionOffset);
                    }

                    if (geom.LOD[i].VertexDeclaration.HasFlag(VertexFlags.Tangent))
                    {
                        int startIndex = x * vertexSize + vertexOffsets[VertexFlags.Position].Offset;
                        vertex.Tangent = VertexTranslator.ReadTangentDataFromVB(vertexBuffers[i].Data, startIndex);
                    }

                    if (geom.LOD[i].VertexDeclaration.HasFlag(VertexFlags.Normals))
                    {
                        int startIndex = x * vertexSize + vertexOffsets[VertexFlags.Normals].Offset;
                        vertex.Normal = VertexTranslator.ReadNormalDataFromVB(vertexBuffers[i].Data, startIndex);
                    }

                    if (geom.LOD[i].VertexDeclaration.HasFlag(VertexFlags.Skin))
                    {
                        //int startIndex = v * vertexSize + vertexOffsets[VertexFlags.BlendData].Offset;
                        // vertex.BlendWeight = VertexTranslator.ReadBlendWeightFromVB(vertexBuffer.Data, startIndex);
                        // vertex.BoneID = VertexTranslator.ReadBlendIDFromVB(vertexBuffer.Data, startIndex);
                    }

                    if (geom.LOD[i].VertexDeclaration.HasFlag(VertexFlags.Color))
                    {
                        //unknown
                    }

                    if (geom.LOD[i].VertexDeclaration.HasFlag(VertexFlags.TexCoords0))
                    {
                        int startIndex = x * vertexSize + vertexOffsets[VertexFlags.TexCoords0].Offset;
                        vertex.TexCoord0 = VertexTranslator.ReadTexcoordFromVB(vertexBuffers[i].Data, startIndex);
                    }

                    if (geom.LOD[i].VertexDeclaration.HasFlag(VertexFlags.TexCoords1))
                    {
                        //int startIndex = v * vertexSize + vertexOffsets[VertexFlags.TexCoords1].Offset;
                        //vertex.UVs[1] = VertexTranslator.ReadTexcoordFromVB(vertexBuffer.Data, startIndex);
                    }

                    if (geom.LOD[i].VertexDeclaration.HasFlag(VertexFlags.TexCoords2))
                    {
                        //int startIndex = v * vertexSize + vertexOffsets[VertexFlags.TexCoords2].Offset;
                        //vertex.UVs[2] = VertexTranslator.ReadTexcoordFromVB(vertexBuffer.Data, startIndex);
                    }

                    if (geom.LOD[i].VertexDeclaration.HasFlag(VertexFlags.ShadowTexture))
                    {
                        int startIndex = x * vertexSize + vertexOffsets[VertexFlags.ShadowTexture].Offset;
                        vertex.TexCoord7 = VertexTranslator.ReadTexcoordFromVB(vertexBuffers[i].Data, startIndex);
                    }

                    if (geom.LOD[i].VertexDeclaration.HasFlag(VertexFlags.Color1))
                    {
                        //unknown
                    }

                    if (geom.LOD[i].VertexDeclaration.HasFlag(VertexFlags.BBCoeffs))
                    {
                        //unknown
                    }

                    if (geom.LOD[i].VertexDeclaration.HasFlag(VertexFlags.DamageGroup))
                    {
                        //int startIndex = v * vertexSize + vertexOffsets[VertexFlags.DamageGroup].Offset;
                        //vertex.DamageGroup = VertexTranslator.ReadDamageGroupFromVB(vertexBuffer.Data, startIndex);
                    }

                    lod.Vertices[x] = vertex;
                }
                LODs[i] = lod;
            }

            SetupShaders();
            return(true);
        }