public void InitializeTriangleGroup(TriangleGroup group)
            {
                Contracts.Requires.That(this.IsDisposed);

                this.Groups.Add(group);
                this.Required   = group;
                this.Current    = TriangleGroup.Empty;
                this.IsDisposed = false;
            }
Beispiel #2
0
            public bool TryAdd(TriangleGroup group)
            {
                int newVertexCount = this.vertexCount + group.Vertices;

                if (newVertexCount <= this.remainingVertices)
                {
                    this.groupCount++;
                    this.offsetCount += group.Offsets;
                    this.vertexCount  = newVertexCount;
                    return(true);
                }

                return(false);
            }
Beispiel #3
0
            public void Reset(TriangleGroup group, int maxVerticesPerMesh)
            {
                Contracts.Requires.That(maxVerticesPerMesh >= MeshConstants.VerticesPerTriangle);

                this.groupStart  += this.groupCount;
                this.offsetStart += this.offsetCount;
                this.vertexStart += this.vertexCount;

                // last group was already iterated but couldn't fit so start the next mesh with it
                this.groupCount  = 1;
                this.offsetCount = group.Offsets;
                this.vertexCount = group.Vertices;

                // combiner is cleared by creating mesh so remainingVertices is just MaxVerticesPerMesh
                this.remainingVertices = maxVerticesPerMesh;
            }
Beispiel #4
0
        public SUR(byte[] data)
        {
            HardpointSections = new List <HardpointSection>();
            int pos = 0;

            VersionString = Utilities.GetString(data, ref pos, 4);
            VersionNumber = Utilities.GetFloat(data, ref pos);

            if (VersionNumber != 2.0f || VersionString != "vers")
            {
                throw new FormatException();
            }

            MeshId      = Utilities.GetDWord(data, ref pos);
            SurfaceType = Utilities.GetDWord(data, ref pos);

            SurfaceSection ss = new SurfaceSection();

            while (pos < data.Length)
            {
                string nextType = Utilities.GetString(data, ref pos, 4);
                if (nextType == "hpid")
                {
                    HardpointSection hps = new HardpointSection();
                    hps.MeshIdCount = Utilities.GetDWord(data, ref pos);
                    hps.MeshIds     = new uint[hps.MeshIdCount];
                    for (int a = 0; a < hps.MeshIdCount; a++)
                    {
                        hps.MeshIds[a] = Utilities.GetDWord(data, ref pos);
                    }
                    HardpointSections.Add(hps);
                }
                else if (nextType == "!fxd")
                {
                    FixedFlag = nextType;
                }
                else if (nextType == "exts")
                {
                    ss.BoundingBox = new SharpDX.Vector3[2];
                    for (int a = 0; a < 2; a++)
                    {
                        ss.BoundingBox[a] = new SharpDX.Vector3(
                            Utilities.GetFloat(data, ref pos),
                            Utilities.GetFloat(data, ref pos),
                            Utilities.GetFloat(data, ref pos));
                    }
                }
                else if (nextType == "surf")
                {
                    uint unk = Utilities.GetDWord(data, ref pos);
                    ss.Center = new SharpDX.Vector3(
                        Utilities.GetFloat(data, ref pos),
                        Utilities.GetFloat(data, ref pos),
                        Utilities.GetFloat(data, ref pos));
                    ss.Inertia = new SharpDX.Vector3(
                        Utilities.GetFloat(data, ref pos),
                        Utilities.GetFloat(data, ref pos),
                        Utilities.GetFloat(data, ref pos));
                    ss.Radius            = Utilities.GetFloat(data, ref pos);
                    ss.Scale             = data[pos++];
                    ss.Size              = Utilities.GetDWord(data, ref pos) & 0x00FFFFFF; pos--;
                    ss.BitsSectionOffset = Utilities.GetDWord(data, ref pos);

                    // padding
                    pos += 3 * sizeof(uint);

                    int maxPos = data.Length;
                    while (pos < maxPos)
                    {
                        TriangleGroup tgh = new TriangleGroup();

                        tgh.VertexOffset   = Utilities.GetDWord(data, ref pos);
                        tgh.MeshId         = Utilities.GetDWord(data, ref pos);
                        tgh.Type           = data[pos++];
                        tgh.RefVertexCount = Utilities.GetDWord(data, ref pos) & 0x00FFFFFF; pos--;
                        tgh.TriangleCount  = Utilities.GetShort(data, ref pos);
                        tgh.Triangles      = new Triangle[tgh.TriangleCount];

                        maxPos = pos + (int)tgh.VertexOffset;

                        for (int a = 0; a < tgh.TriangleCount; a++)
                        {
                            Triangle t        = new Triangle();
                            byte[]   tempData = new byte[20];
                            Array.Copy(data, pos, tempData, 0, 20);
                            BitArray br     = new BitArray(tempData);
                            int      bitpos = 0;
                            bitpos          += 13;
                            t.TriangleNumber = (ushort)Advance(br, 12, ref bitpos);
                            t.TriangleOpp    = (ushort)Advance(br, 12, ref bitpos);
                            bitpos          += 7;
                            t.Flag           = (byte)Advance(br, 1, ref bitpos);
                            t.Indices        = new Index[3];
                            for (int b = 0; b < 3; b++)
                            {
                                Index i = new Index();
                                i.VertexId   = (ushort)Advance(br, sizeof(ushort) * 8, ref bitpos);
                                i.Offset     = (short)Advance(br, 15, ref bitpos);
                                i.Flag       = (byte)Advance(br, 1, ref bitpos);
                                t.Indices[b] = i;
                            }
                            tgh.Triangles[a] = t;

                            PrintBits(br);

                            pos += 20;
                        }
                    }
                }
                else
                {
                    throw new FormatException();
                }
            }
        }
Beispiel #5
0
    public Mesh CreateMesh()
    {
        foreach (Transform child in transform.Cast <Transform>().OrderBy(item => item.name))
        {
            TriangleGroup g = new TriangleGroup();
            //P1点
            g.Point = child.localPosition;
            //方位角a [-PI, PI]
            float a = Mathf.Atan2(g.Point.x, g.Point.z) / Mathf.PI * 180;
            //弧度 [-180, 180]
            float radian = Mathf.PI / 180 * (a - OffsetAngle);
            //左边的顶点
            g.LeftSidePoint = new Vector3(g.Point.x + Thickness * Mathf.Sin(radian), g.Point.y, g.Point.z + Thickness * Mathf.Cos(radian));
            radian          = Mathf.PI / 180 * (a + OffsetAngle);
            //右边的顶点
            g.RightSidePoint = new Vector3(g.Point.x + Thickness * Mathf.Sin(radian), g.Point.y, g.Point.z + Thickness * Mathf.Cos(radian));

            groups.Add(g);
        }

        Mesh           mesh      = new Mesh();
        List <Vector3> vertices  = new List <Vector3>();
        List <Vector2> uv        = new List <Vector2>();
        List <int>     triangles = new List <int>();

        TriangleGroup curGroup;
        TriangleGroup nextGroup;

        for (int i = 0, len = groups.Count; i < len; i++)
        {
            // (v0,v1, v2)
            // (v2, v3, v4)
            // (v4, v5, v6)
            curGroup  = groups[i];
            nextGroup = groups[(i + 1) % len];
            //v0
            vertices.Add(curGroup.Point);
            uv.Add(new Vector2(1, 1));
            //v1
            vertices.Add(nextGroup.Point);
            uv.Add(new Vector2(0, 1));
            //v2
            vertices.Add(curGroup.RightSidePoint);
            uv.Add(new Vector2(1, 0.05f));
            //v3
            vertices.Add(nextGroup.LeftSidePoint);
            uv.Add(new Vector2(0, 0.05f));
            //v4
            vertices.Add(nextGroup.Point);
            uv.Add(new Vector2(0, 1));
            //v5
            vertices.Add(nextGroup.LeftSidePoint);
            uv.Add(new Vector2(0, 0.05f));
            //v6
            vertices.Add(nextGroup.RightSidePoint);
            uv.Add(new Vector2(0, 0.05f));
        }

        int index = 0;

        for (int i = 0, len = vertices.Count; i < len; i += 7)
        {
            index = i;

            // v0, v3, v1
            triangles.Add(index);
            triangles.Add(index + 3);
            triangles.Add(index + 1);

            // v2, v3, v0
            triangles.Add(index + 2);
            triangles.Add(index + 3);
            triangles.Add(index);

            // v4, v5, v6
            triangles.Add(index + 4);
            triangles.Add(index + 5);
            triangles.Add(index + 6);
        }

        mesh.vertices  = vertices.ToArray();
        mesh.uv        = uv.ToArray();
        mesh.triangles = triangles.ToArray();

        return(mesh);
    }
        /// <summary>
        /// WARNING THIS IS NOT FINISHED AND IS DEFINITELY BROKEN
        /// </summary>
        /// <param name="filePath"></param>
        public SurFile(string filePath)
        {
            this.FilePath = filePath;
            byte[] data;

            using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            {
                data = new byte[fs.Length];
                fs.Read(data, 0, (int)fs.Length);
                fs.Close();
            }

            int pos = 0;

            VersionString = Utilities.GetString(data, ref pos, 4);
            VersionNumber = Utilities.GetFloat(data, ref pos);

            if (VersionNumber != 2.0f || VersionString != "vers")
            {
                throw new FormatException();
            }

            while (pos < data.Length)
            {
                Mesh m = new Mesh();
                m.HardpointSections = new List <HardpointSection>();
                m.SurfaceSections   = new List <SurfaceSection>();
                m.MeshId            = Utilities.GetDWord(data, ref pos);
                uint blockCount = Utilities.GetDWord(data, ref pos);

                while (blockCount-- > 0)
                {
                    string nextType = Utilities.GetString(data, ref pos, 4);
                    if (nextType == "hpid")
                    {
                        HardpointSection hps = new HardpointSection();
                        hps.MeshIdCount = Utilities.GetDWord(data, ref pos);
                        hps.MeshIds     = new uint[hps.MeshIdCount];
                        for (int a = 0; a < hps.MeshIdCount; a++)
                        {
                            hps.MeshIds[a] = Utilities.GetDWord(data, ref pos);
                        }
                        m.HardpointSections.Add(hps);
                    }
                    else if (nextType == "!fxd")
                    {
                        FixedFlag = nextType;
                    }
                    else if (nextType == "exts")
                    {
                        m.BoundingBox = new SharpDX.Vector3[2];
                        for (int a = 0; a < 2; a++)
                        {
                            m.BoundingBox[a] = new SharpDX.Vector3(
                                Utilities.GetFloat(data, ref pos),
                                Utilities.GetFloat(data, ref pos),
                                Utilities.GetFloat(data, ref pos));
                        }
                    }
                    else if (nextType == "surf")
                    {
                        SurfaceSection ss = new SurfaceSection();

                        uint size       = Utilities.GetDWord(data, ref pos);
                        uint header_pos = (uint)pos;
                        ss.Center = new SharpDX.Vector3(
                            Utilities.GetFloat(data, ref pos),
                            Utilities.GetFloat(data, ref pos),
                            Utilities.GetFloat(data, ref pos));
                        ss.Inertia = new SharpDX.Vector3(
                            Utilities.GetFloat(data, ref pos),
                            Utilities.GetFloat(data, ref pos),
                            Utilities.GetFloat(data, ref pos));
                        ss.Radius            = Utilities.GetFloat(data, ref pos);
                        ss.Scale             = data[pos] / 250.0f;
                        ss.Size              = Utilities.GetDWord(data, ref pos) >> 8;
                        ss.BitsSectionOffset = Utilities.GetDWord(data, ref pos);
                        ss.TriangleGroups    = new List <TriangleGroup>();
                        ss.Vertices          = new List <Vertex>();

                        uint bits_beg = header_pos + ss.BitsSectionOffset;
                        uint bits_end = header_pos + ss.Size;

                        // padding
                        pos += 3 * sizeof(uint);

                        int vertBufStart = data.Length;
                        while (pos < vertBufStart)
                        {
                            TriangleGroup tgh = new TriangleGroup();

                            int group_pos = pos;
                            tgh.VertexOffset   = Utilities.GetDWord(data, ref pos);
                            tgh.MeshId         = Utilities.GetDWord(data, ref pos);
                            tgh.Type           = data[pos];
                            tgh.RefVertexCount = Utilities.GetDWord(data, ref pos) >> 8;
                            tgh.TriangleCount  = Utilities.GetShort(data, ref pos); pos += 2;
                            tgh.Triangles      = new Triangle[tgh.TriangleCount];

                            vertBufStart = group_pos + (int)tgh.VertexOffset;

                            for (int a = 0; a < tgh.TriangleCount; a++)
                            {
                                Triangle t        = new Triangle();
                                byte[]   tempData = new byte[(12 + 12 + 7 + 1 + 3 * (16 + 15 + 1)) / 8];
                                Array.Copy(data, pos, tempData, 0, tempData.Length);
                                pos += tempData.Length;
                                BitArray br = new BitArray(tempData);

                                int bitpos = 0;
                                t.TriangleNumber = (ushort)Advance(br, 12, ref bitpos);
                                t.TriangleOpp    = (ushort)Advance(br, 12, ref bitpos);
                                bitpos          += 7;
                                t.Flag           = (byte)Advance(br, 1, ref bitpos);
                                t.Indices        = new Index[3];
                                for (int b = 0; b < 3; b++)
                                {
                                    Index i = new Index();
                                    i.VertexId   = (ushort)Advance(br, sizeof(ushort) * 8, ref bitpos);
                                    i.Offset     = (short)Advance(br, 15, ref bitpos);
                                    i.Flag       = (byte)Advance(br, 1, ref bitpos);
                                    t.Indices[b] = i;
                                }
                                tgh.Triangles[a] = t;
                            }

                            ss.TriangleGroups.Add(tgh);
                        }

                        // Vertices
                        while (pos < bits_beg)
                        {
                            Vertex vertex = new Vertex();
                            vertex.X      = Utilities.GetFloat(data, ref pos);
                            vertex.Y      = Utilities.GetFloat(data, ref pos);
                            vertex.Z      = Utilities.GetFloat(data, ref pos);
                            vertex.MeshId = Utilities.GetDWord(data, ref pos);
                            ss.Vertices.Add(vertex);
                        }

                        m.SurfaceSections.Add(ss);

                        pos = (int)bits_end;
                    }
                    else
                    {
                        throw new FormatException();
                    }
                }

                Meshes.Add(m);
            }
        }