Пример #1
0
        public SCBFile(string Location)
        {
            using (BinaryReader br = new BinaryReader(File.OpenRead(Location)))
            {
                string Magic = Encoding.ASCII.GetString(br.ReadBytes(8));
                if (Magic != "r3d2Mesh")
                {
                    throw new InvalidFileMagicException();
                }

                UInt16 Major = br.ReadUInt16();
                UInt16 Minor = br.ReadUInt16();
                if (Major != 3 && Major != 2 && Minor != 1) //There are versions [2][1] and [1][1] aswell
                {
                    throw new UnsupportedFileVersionException();
                }

                this.Name = Encoding.ASCII.GetString(br.ReadBytes(128)).Replace("\0", "");
                UInt32   VertexCount = br.ReadUInt32();
                UInt32   FaceCount   = br.ReadUInt32();
                SCBFlags Flags       = (SCBFlags)br.ReadUInt32();
                this.BoundingBox = new R3DBoundingBox(br);

                bool HasTangents = false;
                if (Major == 3 && Minor == 2)
                {
                    HasTangents = br.ReadUInt32() == 1;
                }

                for (int i = 0; i < VertexCount; i++)
                {
                    this.Vertices.Add(new Vector3(br));
                }

                if (Major == 3 && Minor == 2 && Flags.HasFlag(SCBFlags.Tangents) && HasTangents)
                {
                    for (int i = 0; i < VertexCount; i++)
                    {
                        this.Tangents.Add(new Vector4Byte(br));
                    }
                }

                this.CentralPoint = new Vector3(br);

                for (int i = 0; i < FaceCount; i++)
                {
                    this.Faces.Add(new SCBFace(br));
                }

                if (Flags.HasFlag(SCBFlags.VertexColors))
                {
                    this.VertexColors.Add(new Vector3Byte(br));
                }
            }
        }
Пример #2
0
        public void Write(Stream stream)
        {
            using (BinaryWriter bw = new BinaryWriter(stream))
            {
                bw.Write("r3d2Mesh".ToCharArray());
                bw.Write((ushort)3);
                bw.Write((ushort)2);
                bw.Write(this.Name.PadRight(128, '\u0000').ToCharArray());
                bw.Write((uint)this.Vertices.Count);

                uint faceCount = 0;
                foreach (KeyValuePair <string, List <SCBFace> > material in this.Materials)
                {
                    faceCount += (uint)material.Value.Count;
                }
                bw.Write(faceCount);

                SCBFlags flags      = 0;
                uint     hasTangent = 0;
                if (this.Tangents.Count != 0)
                {
                    flags     |= SCBFlags.TANGENTS;
                    hasTangent = 1;
                }
                if (this.VertexColors.Count != 0)
                {
                    flags |= SCBFlags.VERTEX_COLORS;
                }
                bw.Write((uint)flags);
                this.CalculateBoundingBox().Write(bw);
                bw.Write(hasTangent);

                foreach (Vector3 vertex in this.Vertices)
                {
                    vertex.Write(bw);
                }
                foreach (Vector4Byte tangent in this.Tangents)
                {
                    tangent.Write(bw);
                }
                this.CalculateCentralPoint().Write(bw);
                foreach (KeyValuePair <string, List <SCBFace> > material in this.Materials)
                {
                    foreach (SCBFace face in material.Value)
                    {
                        face.Write(bw);
                    }
                }
                foreach (Vector3Byte color in this.VertexColors)
                {
                    color.Write(bw);
                }
            }
        }
Пример #3
0
        public SCBFile(Stream stream)
        {
            using (BinaryReader br = new BinaryReader(stream))
            {
                string magic = Encoding.ASCII.GetString(br.ReadBytes(8));
                if (magic != "r3d2Mesh")
                {
                    throw new Exception("This is not a valid SCB file");
                }

                ushort major = br.ReadUInt16();
                ushort minor = br.ReadUInt16();
                if (major != 3 && major != 2 && minor != 1)
                {
                    throw new Exception(string.Format("The Version: {0}.{1} is not supported", major, minor));
                }

                string   Name        = Encoding.ASCII.GetString(br.ReadBytes(128)).Replace("\0", "");
                uint     vertexCount = br.ReadUInt32();
                uint     faceCount   = br.ReadUInt32();
                SCBFlags flags       = (SCBFlags)br.ReadUInt32();
                Vector3  Org         = new Vector3(br);
                Vector3  Size        = new Vector3(br);

                bool hasTangents = false;
                if (major == 3 && minor == 2)
                {
                    hasTangents = br.ReadUInt32() == 1;
                }

                for (int i = 0; i < vertexCount; i++)
                {
                    Vertices.Add(new Vector3(br));
                }

                if (major == 3 && minor == 2 && flags.HasFlag(SCBFlags.TANGENTS) && hasTangents)
                {
                    for (int i = 0; i < vertexCount; i++)
                    {
                        byte o = br.ReadByte();
                        byte k = br.ReadByte();
                        byte m = br.ReadByte();
                        byte n = br.ReadByte();
                    }
                }

                float u = br.ReadSingle();
                float j = br.ReadSingle();
                float b = br.ReadSingle();

                for (int i = 0; i < faceCount; i++)
                {
                    SCBFace face = new SCBFace(br);

                    if (!Materials.ContainsKey(face.Material))
                    {
                        Materials.Add(face.Material, new List <SCBFace>());
                    }

                    Materials[face.Material].Add(face);
                }
            }
        }
Пример #4
0
        public SCBFile(Stream stream)
        {
            using (BinaryReader br = new BinaryReader(stream))
            {
                string magic = Encoding.ASCII.GetString(br.ReadBytes(8));
                if (magic != "r3d2Mesh")
                {
                    throw new Exception("This is not a valid SCB file");
                }

                ushort major = br.ReadUInt16();
                ushort minor = br.ReadUInt16();
                if (major != 3 && major != 2 && minor != 1) //There are versions [2][1] and [1][1] aswell
                {
                    throw new Exception(string.Format("The Version: {0}.{1} is not supported", major, minor));
                }

                this.Name = Encoding.ASCII.GetString(br.ReadBytes(128)).Replace("\0", "");
                uint     vertexCount = br.ReadUInt32();
                uint     faceCount   = br.ReadUInt32();
                SCBFlags flags       = (SCBFlags)br.ReadUInt32();
                this.BoundingBox = new R3DBoundingBox(br);

                bool hasTangents = false;
                if (major == 3 && minor == 2)
                {
                    hasTangents = br.ReadUInt32() == 1;
                }

                for (int i = 0; i < vertexCount; i++)
                {
                    this.Vertices.Add(new Vector3(br));
                }

                if (major == 3 && minor == 2 && flags.HasFlag(SCBFlags.TANGENTS) && hasTangents)
                {
                    for (int i = 0; i < vertexCount; i++)
                    {
                        this.Tangents.Add(new Vector4Byte(br));
                    }
                }

                this.CentralPoint = new Vector3(br);

                for (int i = 0; i < faceCount; i++)
                {
                    SCBFace face = new SCBFace(br);

                    if (!this.Materials.ContainsKey(face.Material))
                    {
                        this.Materials.Add(face.Material, new List <SCBFace>());
                    }

                    this.Materials[face.Material].Add(face);
                }

                if (flags.HasFlag(SCBFlags.VERTEX_COLORS))
                {
                    this.VertexColors.Add(new Vector3Byte(br));
                }
            }
        }