示例#1
0
        public static List <SubMesh> CreateWalls(SECTORS sector, WAD wad, GeneratingGo generating)
        {
            List <SubMesh> sMeshs = new List <SubMesh>();

            foreach (LINEDEFS line in sector.lines)
            {
                SECTORS fSector = line.getFrontSector();
                SECTORS bSector = line.getBackSector();

                // check to see if this line is totally broken
                if (fSector == null && bSector == null)
                {
                    continue;
                }

                // figure out which sector this wall actually belongs to

                bool hasBothSectors = (fSector != null && bSector != null);
                bool genMidWalls    = (generating == GeneratingGo.Sector);
                bool genLowerWalls  = hasBothSectors;
                bool genUpperWalls  = hasBothSectors;

                if (hasBothSectors)
                {
                    if (sector.isMovingCeiling)
                    {
                        genLowerWalls = genLowerWalls ? (generating != GeneratingGo.Ceiling) : false;
                        genUpperWalls = genUpperWalls ? (generating == GeneratingGo.Ceiling) : false;
                    }

                    if (sector.isMovingFloor)
                    {
                        genLowerWalls = genLowerWalls ? (generating == GeneratingGo.Floor) : false;
                        genUpperWalls = genUpperWalls ? (generating != GeneratingGo.Floor) : false;
                    }
                }

                // create all walls
                if (genMidWalls)
                {
                    sMeshs.AddRange(CreateMidWalls(sector, line, wad));
                }

                if (genLowerWalls)
                {
                    sMeshs.AddRange(CreateLowerWalls(sector, line, wad));
                }

                if (genUpperWalls)
                {
                    sMeshs.AddRange(CreateUpperWalls(sector, line, wad));
                }
            }

            return(sMeshs);
        }
示例#2
0
        public static SubMesh CreateCeiling(SubMesh floor, SECTORS sector, Material mat, GeneratingGo generating)
        {
            if (sector.isMovingFloor && generating == GeneratingGo.Floor)
            {
                return(null);
            }

            if (sector.isMovingFloor && generating != GeneratingGo.Floor)
            {
                floor = CreateFloor(sector, mat, GeneratingGo.Floor);
            }

            if (sector.isMovingCeiling && generating != GeneratingGo.Ceiling)
            {
                return(null);
            }

            if (sector.isMovingCeiling && generating == GeneratingGo.Ceiling)
            {
                floor = CreateFloor(sector, mat, sector.isMovingFloor ? GeneratingGo.Floor : GeneratingGo.Sector);
            }

            //reverse
            Mesh ceiling = new Mesh();

            List <Vector3> tmpVerts = new List <Vector3>(floor.mesh.vertices);
            List <Vector2> tmpUvs   = new List <Vector2>(floor.mesh.uv);
            List <int>     tmpTris  = new List <int>(floor.mesh.triangles);
            List <Vector3> tmpNrm   = new List <Vector3>(floor.mesh.normals);


            for (int a = 0; a < tmpVerts.Count; a++)
            {
                tmpVerts[a] = new Vector3(tmpVerts[a].x, sector.ceilingHeight, tmpVerts[a].z); //change height for ceiling
                tmpNrm[a]   = Vector3.down;
            }


            for (int i = 0; i < tmpTris.Count; i += 3)
            {
                int tri = tmpTris[i];

                tmpTris[i]     = tmpTris[i + 2];
                tmpTris[i + 2] = tri;
            }


            ceiling.vertices  = tmpVerts.ToArray();
            ceiling.uv        = tmpUvs.ToArray();
            ceiling.triangles = tmpTris.ToArray();
            ceiling.normals   = tmpNrm.ToArray();



            SubMesh newSubMesh = new SubMesh();

            newSubMesh.mesh     = ceiling;
            newSubMesh.material = mat;


            return(newSubMesh);
        }
示例#3
0
        // This class is the actual triangulator, you feed it a list of linedefs and it spits out triangles

        public static SubMesh CreateFloor(SECTORS sector, Material mat, GeneratingGo generating)
        {
            if (sector.isMovingCeiling && generating == GeneratingGo.Ceiling)
            {
                return(null);
            }

            if (sector.isMovingFloor && generating != GeneratingGo.Floor)
            {
                return(null);
            }

            Mesh              mesh       = new Mesh();
            VertexPool        vertexPool = new VertexPool();
            List <Triangle2D> triangles  = new List <Triangle2D>();
            List <Line>       lines      = new List <Line>();

            // convert linedefs to lines
            foreach (LINEDEFS linedef in sector.lines)
            {
                Vertex v1 = vertexPool.Get(linedef.firstVert.x, linedef.firstVert.z);
                Vertex v2 = vertexPool.Get(linedef.lastVert.x, linedef.lastVert.z);

                if (linedef.getFrontSector() == sector)
                {
                    lines.Add(new Line(v1, v2, true, false));
                }
                else
                {
                    lines.Add(new Line(v1, v2, true, true));
                }
            }

            // walk lines to make sure the sector isn't broken
            RepairSector(sector, lines);

            // go through every line and attach it to every other vertex.
            // reject the triangle if it isn't valid
            List <Line> unmarked = new List <Line>(lines);

            // ignore all initial lines that do not come from a linedef
            for (int i = 0; i < unmarked.Count; i++)
            {
                Line line = unmarked[i];
                if (!line.fromLinedef)
                {
                    unmarked.RemoveAt(i);
                    i--;
                }
            }

            while (unmarked.Count > 0)
            {
                Line line = unmarked.Last();
                unmarked.Remove(line);
                CreateTriangle(line, lines, unmarked, triangles, vertexPool.vertices);
            }

            List <Vector3> vertices = new List <Vector3>();
            List <Vector2> uvs      = new List <Vector2>();
            List <Vector3> normals  = new List <Vector3>();

            foreach (Vertex vertex in vertexPool.vertices)
            {
                vertices.Add(new Vector3((float)vertex.x, sector.floorHeight, (float)vertex.y));
                uvs.Add(new Vector2((float)vertex.x / 64f, (float)vertex.y / 64f));

                normals.Add(Vector3.up);
            }

            List <int> triangleIndices = new List <int>();

            foreach (Triangle2D triangle in triangles)
            {
                double mx = (triangle.vertices[0].x + triangle.vertices[1].x + triangle.vertices[2].x) / 3f;
                double my = (triangle.vertices[0].y + triangle.vertices[1].y + triangle.vertices[2].y) / 3f;
                SortedList <double, Vertex> sorted = new SortedList <double, Vertex>();
                foreach (Vertex vertex in triangle.vertices)
                {
                    double theta = Math.Atan2(my - vertex.y, mx - vertex.x);
                    //while (sorted.ContainsKey(theta)) { theta += 0.1; } // maybe needed?
                    sorted.Add(theta, vertex);
                }
                triangleIndices.Add(vertexPool.vertices.IndexOf(sorted.Values[2]));
                triangleIndices.Add(vertexPool.vertices.IndexOf(sorted.Values[1]));
                triangleIndices.Add(vertexPool.vertices.IndexOf(sorted.Values[0]));
            }

            mesh.Clear();
            mesh.vertices  = vertices.ToArray();
            mesh.uv        = uvs.ToArray();
            mesh.triangles = triangleIndices.ToArray();
            mesh.normals   = normals.ToArray();

            SubMesh newSubMesh = new SubMesh();

            newSubMesh.mesh     = mesh;
            newSubMesh.material = mat;

            return(newSubMesh);
        }