Example #1
0
        /// <summary>
        /// Read data from stream.
        /// </summary>
        /// <param name="reader"></param>
        public void ReadFromFile(BinaryReader reader)
        {
            numFaces     = reader.ReadInt32();
            startIndex   = reader.ReadInt32();
            materialHash = reader.ReadUInt64();
            unk3         = reader.ReadInt32();

            Material mat = MaterialsManager.LookupMaterialByHash(materialHash);

            if (mat != null)
            {
                materialName = mat.MaterialName;
            }
            else
            {
                materialName = "UNABLE TO GET FROM MTLs";
            }
        }
Example #2
0
        /// <summary>
        /// Build Lods from retrieved data.
        /// </summary>
        public void BuildLods(FrameGeometry frameGeometry, FrameMaterial frameMaterial, VertexBuffer[] vertexBuffers, IndexBuffer[] indexBuffers)
        {
            lods = new Lod[frameGeometry.NumLods];
            for (int i = 0; i != lods.Length; i++)
            {
                FrameLOD frameLod = frameGeometry.LOD[i];
                lods[i] = new Lod
                {
                    VertexDeclaration = frameGeometry.LOD[i].VertexDeclaration
                };
                IndexBuffer  indexBuffer  = indexBuffers[i];
                VertexBuffer vertexBuffer = vertexBuffers[i];

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

                lods[i].NumUVChannels = 4;
                lods[i].Vertices      = new Vertex[frameLod.NumVertsPr];

                for (int v = 0; v != lods[i].Vertices.Length; v++)
                {
                    Vertex vertex = new Vertex();
                    vertex.UVs = new UVVector2[lods[i].NumUVChannels];
                    if (lods[i].VertexDeclaration.HasFlag(VertexFlags.Position))
                    {
                        int startIndex = v * vertexSize + vertexOffsets[VertexFlags.Position].Offset;
                        vertex.ReadPositionData(vertexBuffer.Data, startIndex, frameGeometry.DecompressionFactor, frameGeometry.DecompressionOffset);
                    }

                    if (lods[i].VertexDeclaration.HasFlag(VertexFlags.Tangent))
                    {
                        int startIndex = v * vertexSize + vertexOffsets[VertexFlags.Position].Offset;
                        vertex.ReadTangentData(vertexBuffer.Data, startIndex);
                    }

                    if (lods[i].VertexDeclaration.HasFlag(VertexFlags.Normals))
                    {
                        int startIndex = v * vertexSize + vertexOffsets[VertexFlags.Normals].Offset;
                        vertex.ReadNormalData(vertexBuffer.Data, startIndex);
                    }

                    if (lods[i].VertexDeclaration.HasFlag(VertexFlags.BlendData))
                    {
                        int startIndex = v * vertexSize + vertexOffsets[VertexFlags.BlendData].Offset;
                        vertex.ReadBlendData(vertexBuffer.Data, startIndex);
                    }

                    if (lods[i].VertexDeclaration.HasFlag(VertexFlags.flag_0x80))
                    {
                        Console.WriteLine("Skip vertex with flag_0x80");
                    }

                    if (lods[i].VertexDeclaration.HasFlag(VertexFlags.TexCoords0))
                    {
                        int startIndex = v * vertexSize + vertexOffsets[VertexFlags.TexCoords0].Offset;
                        vertex.ReadUvData(vertexBuffer.Data, startIndex, 0);
                    }

                    if (lods[i].VertexDeclaration.HasFlag(VertexFlags.TexCoords1))
                    {
                        int startIndex = v * vertexSize + vertexOffsets[VertexFlags.TexCoords1].Offset;
                        vertex.ReadUvData(vertexBuffer.Data, startIndex, 1);
                    }

                    if (lods[i].VertexDeclaration.HasFlag(VertexFlags.TexCoords2))
                    {
                        int startIndex = v * vertexSize + vertexOffsets[VertexFlags.TexCoords2].Offset;
                        vertex.ReadUvData(vertexBuffer.Data, startIndex, 2);
                    }

                    if (lods[i].VertexDeclaration.HasFlag(VertexFlags.TexCoords7))
                    {
                        int startIndex = v * vertexSize + vertexOffsets[VertexFlags.TexCoords7].Offset;
                        vertex.ReadUvData(vertexBuffer.Data, startIndex, 3);
                    }

                    if (lods[i].VertexDeclaration.HasFlag(VertexFlags.flag_0x20000))
                    {
                        Console.WriteLine("Skip vertex with flag_0x20000");
                    }

                    if (lods[i].VertexDeclaration.HasFlag(VertexFlags.flag_0x40000))
                    {
                        Console.WriteLine("Skip vertex with flag_0x40000");
                    }

                    if (lods[i].VertexDeclaration.HasFlag(VertexFlags.DamageGroup))
                    {
                        int startIndex = v * vertexSize + vertexOffsets[VertexFlags.DamageGroup].Offset;
                        vertex.ReadDamageGroup(vertexBuffer.Data, startIndex);
                    }

                    if (lods[i].NormalMapInfoPresent)
                    {
                        vertex.BuildBinormals();
                    }

                    lods[i].Vertices[v] = vertex;
                    MaterialStruct[] materials = frameMaterial.Materials[i];
                    lods[i].Parts = new ModelPart[materials.Length];
                    for (int x = 0; x != materials.Length; x++)
                    {
                        ModelPart modelPart = new ModelPart();
                        Material  mat       = MaterialsManager.LookupMaterialByHash(materials[x].MaterialHash);

                        if (mat == null || mat.SPS.Length == 0)
                        {
                            modelPart.Material = "_test_gray";
                        }
                        else
                        {
                            modelPart.Material = (mat == null) ? "null" : mat.SPS[0].File;
                        }

                        int           num        = materials[x].StartIndex + materials[x].NumFaces * 3;
                        List <Short3> intList    = new List <Short3>(materials[x].NumFaces);
                        int           startIndex = materials[x].StartIndex;
                        while (startIndex < num)
                        {
                            Short3 indice = new Short3();
                            indice.S1 = indexBuffer.Data[startIndex + 0];
                            indice.S2 = indexBuffer.Data[startIndex + 1];
                            indice.S3 = indexBuffer.Data[startIndex + 2];
                            intList.Add(indice);
                            startIndex += 3;
                        }

                        modelPart.Indices = intList.ToArray();
                        lods[i].Parts[x]  = modelPart;
                    }
                }
            }
        }