コード例 #1
0
        public IBlockSection ReadBlock(FileReader reader)
        {
            string sectionMagic = reader.ReadString(4, Encoding.ASCII);
            uint   sectionSize  = reader.ReadUInt32();
            long   pos          = reader.Position;

            IBlockSection block = null;

            switch (sectionMagic)
            {
            case "MDLF":     //Model
                block = new ModelBlock(); break;

            case "OBJO":     //Object block
                block = new ObjectBlock(); break;

            case "MESH":     //Mesh
                block = new MeshBlock(); break;

            case "SKIN":     //Mesh skinning
                block = new SkinningBlock(); break;

            case "ENVS":     //Skinning Envelope section
                block = new EnvelopeBlock(); break;

            case "STRB":     //String Table
                block = new StringTable(); break;

            case "TEXS":     //Texture section
                block = new TextureBlock(); break;

            case "NAME":     //Name
                block = new NameBlock(); break;

            case "TEXO":     //Texture block to map palettes to images by name
                block = new TextureMapperBlock(); break;

            case "IMGO":     //Image block
                block = new ImageBlock(); break;

            case "PLTO":     //Palette block
                block = new PaletteDataBlock(); break;

            case "ANMF":     //Animation block
                block = new AnimationBlock(); break;

            default:
                Console.WriteLine($"Unknown section! {sectionMagic}");
                break;
            }

            if (block != null)
            {
                block.Read(this, reader);
            }

            //Keep track of the model sub sections like textures
            if (sectionMagic != "MDLF")
            {
                reader.SeekBegin(pos + sectionSize);
            }
            return(block);
        }
コード例 #2
0
        private STGenericMesh LoadMesh(ModelBlock mdl, ObjectBlock obj, MeshBlock mesh)
        {
            STGenericMesh genericMesh = new STGenericMesh();

            genericMesh.Name = obj.Name;

            var transform = obj.GetTransform();
            var meshInfo  = obj.MeshData;
            var ctx       = NitroGX.ReadCmds(meshInfo.Data);

            for (int i = 0; i < ctx.vertices.Count; i++)
            {
                STVertex vertex = new STVertex();
                vertex.Position  = ctx.vertices[i].Position;
                vertex.Normal    = ctx.vertices[i].Normal;
                vertex.TexCoords = new Vector2[1]
                {
                    ctx.vertices[i].TexCoord
                };
                vertex.Colors = new Vector4[1]
                {
                    new Vector4(ctx.vertices[i].Color / 255f,
                                ctx.vertices[i].Alpha / 255f)
                };

                vertex.Position = Vector3.TransformPosition(vertex.Position, transform);
                genericMesh.Vertices.Add(vertex);
            }

            uint[] faces = new uint[ctx.indices.Count];
            for (int i = 0; i < ctx.indices.Count; i++)
            {
                faces[i] = ctx.indices[i];
            }

            foreach (var poly in mesh.PolyGroups)
            {
                var material = mdl.Materials[poly.MaterialIndex];

                /*    uint[] polyFaces = new uint[poly.FaceCount];
                 *  for (int i = 0; i < poly.FaceCount; i++) {
                 *      polyFaces[i] = ctx.indices[poly.FaceStart + i];
                 *  }*/

                STGenericMaterial genericMaterial = new STGenericMaterial();
                genericMaterial.Name = material.Name;

                if (material.MaterialBlock.AttributeIndex != -1)
                {
                    var attribute = mdl.Attributes[material.MaterialBlock.AttributeIndex];
                    genericMaterial.TextureMaps.Add(new STGenericTextureMap()
                    {
                        Name = attribute.TextureName,
                        Type = STTextureType.Diffuse,
                    });
                }

                genericMesh.PolygonGroups.Add(new STPolygonGroup()
                {
                    PrimitiveType = STPrimitiveType.Triangles,
                    Material      = genericMaterial,
                    Faces         = faces.ToList(),
                });

                break;
            }

            return(genericMesh);
        }