public void RasterizeBox(Square[,,,] squares, RasterizationGrid rast, Vector3 tl, Vector3 tr, Vector3 bl, Vector3 br) { float left = Mathf.Min(tl.x, tr.x, bl.x, br.x); float right = Mathf.Max(tl.x, tr.x, bl.x, br.x); float up = Mathf.Max(tl.y, tr.y, bl.y, br.y); float down = Mathf.Min(tl.y, tr.y, bl.y, br.y); Vector2 topLeft = new Vector2(left, up); Vector2 bottomRight = new Vector2(right, down); int sectorTop = rast.SectorColumn(topLeft); int sectorBottom = rast.SectorColumn(bottomRight); int sectorLeft = rast.SectorRow(topLeft); int sectorRight = rast.SectorRow(bottomRight); for (int sr = sectorLeft; sr <= sectorRight; sr++) { for (int sc = sectorTop; sc <= sectorBottom; sc++) { Vector2Int sector = new Vector2Int(sr, sc); int gridTop = rast.GridColumn(sector, topLeft); int gridBottom = rast.GridColumn(sector, bottomRight); int gridLeft = rast.GridRow(sector, topLeft); int gridRight = rast.GridRow(sector, bottomRight); for (int gr = gridLeft; gr <= gridRight; gr++) { for (int gc = gridTop; gc <= gridBottom; gc++) { squares[sr, sc, gr, gc].FillBox(tl, tr, bl, br); } } } } }
public void RasterizeCircle(Square[,,,] squares, RasterizationGrid rast, Vector3 center, float radius) { Vector2 topLeft = new Vector2(center.x - radius, center.y + radius); Vector2 bottomRight = new Vector2(center.x + radius, center.y - radius); int sectorTop = rast.SectorColumn(topLeft); int sectorBottom = rast.SectorColumn(bottomRight); int sectorLeft = rast.SectorRow(topLeft); int sectorRight = rast.SectorRow(bottomRight); for (int sr = sectorLeft; sr <= sectorRight; sr++) { for (int sc = sectorTop; sc <= sectorBottom; sc++) { Vector2Int sector = new Vector2Int(sr, sc); int gridTop = rast.GridColumn(sector, topLeft); int gridBottom = rast.GridColumn(sector, bottomRight); int gridLeft = rast.GridRow(sector, topLeft); int gridRight = rast.GridRow(sector, bottomRight); for (int gr = gridLeft; gr <= gridRight; gr++) { for (int gc = gridTop; gc <= gridBottom; gc++) { squares[sr, sc, gr, gc].FillCircle(center, radius); } } } } }
private void AddWalls( Vector2Int sector, Vector2Int grid, Square[,,,] squares, RasterizationGrid rastGrid, List <Vector3> vert, List <int> tri, int oldVerts) { bool left = grid.x == 0 || !squares[sector.x, sector.y, grid.x - 1, grid.y].Filled; bool right = grid.x == rastGrid.GridDimension.x - 1 || !squares[sector.x, sector.y, grid.x + 1, grid.y].Filled; bool up = grid.y == 0 || !squares[sector.x, sector.y, grid.x, grid.y - 1].Filled; bool down = grid.y == rastGrid.GridDimension.y - 1 || !squares[sector.x, sector.y, grid.x, grid.y + 1].Filled; if (grid.x == 0 && sector.x != 0) { left = !squares[sector.x - 1, sector.y, rastGrid.GridDimension.x - 1, grid.y].Filled; } if (grid.x == rastGrid.GridDimension.x - 1 && sector.x != rastGrid.SectorDimension.x - 1) { right = !squares[sector.x + 1, sector.y, 0, grid.y].Filled; } if (grid.y == 0 && sector.y != 0) { up = !squares[sector.x, sector.y - 1, grid.x, rastGrid.GridDimension.y - 1].Filled; } if (grid.y == rastGrid.GridDimension.y - 1 && sector.y != rastGrid.SectorDimension.y - 1) { down = !squares[sector.x, sector.y + 1, grid.x, 0].Filled; } List <int> walls = squares[sector.x, sector.y, grid.x, grid.y].GetWallPoints(up, down, left, right); for (int i = 0; i < walls.Count - 1; i += 2) { Vector3 pos1 = vert[walls[i] + oldVerts]; int a = vert.Count; vert.Add(pos1); Vector3 pos2 = vert[walls[i + 1] + oldVerts]; int b = vert.Count; vert.Add(pos2); Vector3 pos3 = vert[walls[i + 1]]; int c = vert.Count; vert.Add(pos3); Vector3 pos4 = vert[walls[i]]; int d = vert.Count; vert.Add(pos4); MeshUtility.AddSquare(tri, a, b, c, d, this.InvertTriangles); } }
public Square[,,,] InitialzeGrid(RasterizationGrid rast) { Vector2Int sectors = rast.SectorDimension; Vector2Int grids = rast.GridDimension; Square[,,,] squares = new Square[sectors.x, sectors.y, grids.x, grids.y]; for (int sr = 0; sr < sectors.x; sr++) { for (int sc = 0; sc < sectors.y; sc++) { Vector2Int sector = new Vector2Int(sr, sc); for (int gr = 0; gr < grids.x; gr++) { for (int gc = 0; gc < grids.y; gc++) { Vector3 pos = rast.GridCenter(sector, new Vector2Int(gr, gc)); Vector2 size = rast.GridSize; Square square = new Square(pos, size); squares[sr, sc, gr, gc] = square; if (gc == 0) { square.TopLeft = new Corner(new Vector3(pos.x - size.x / 2f, pos.y + size.y / 2f)); square.TopRight = new Corner(new Vector3(pos.x + size.x / 2f, pos.y + size.y / 2f)); square.Top = new Corner(Vector3.zero); } else { square.TopLeft = squares[sr, sc, gr, gc - 1].BottomLeft; square.TopRight = squares[sr, sc, gr, gc - 1].BottomRight; square.Top = squares[sr, sc, gr, gc - 1].Bottom; } if (gr == 0) { square.BottomLeft = new Corner(new Vector3(pos.x - size.x / 2f, pos.y - size.y / 2f)); square.Left = new Corner(Vector3.zero); } else { square.BottomLeft = squares[sr, sc, gr - 1, gc].BottomRight; square.Left = squares[sr, sc, gr - 1, gc].Right; } square.BottomRight = new Corner(new Vector3(pos.x + size.x / 2f, pos.y - size.y / 2f)); square.Right = new Corner(Vector3.zero); square.Bottom = new Corner(Vector3.zero); } } } } return(squares); }
private void EvaluateGrid( Vector2Int sector, Square[,,,] squares, RasterizationGrid grid, List <Vector3> vert, List <int> tri, bool onlyMarked) { for (int r = 0; r < grid.GridDimension.x; r++) { for (int c = 0; c < grid.GridDimension.y; c++) { if (squares[sector.x, sector.y, r, c].Filled && (!onlyMarked || squares[sector.x, sector.y, r, c].Marked)) { List <int> triangles = squares[sector.x, sector.y, r, c].GetTriangles(vert, this.InvertTriangles); foreach (int i in triangles) { tri.Add(i); } } } } if (this.extrudeMesh) { // Duplicate and offset mesh mesh int oldVerts = vert.Count; for (int i = 0; i < oldVerts; i++) { Vector3 v = new Vector3(vert[i].x, vert[i].y, vert[i].z + this.zOffset); vert.Add(v); } // Duplicate triangles in reverse order to invert int oldTris = tri.Count; for (int i = 0; i < oldTris - 2; i += 3) { int a = tri[i] + oldVerts; int b = tri[i + 1] + oldVerts; int c = tri[i + 2] + oldVerts; tri.Add(c); tri.Add(b); tri.Add(a); } for (int r = 0; r < grid.GridDimension.x; r++) { for (int c = 0; c < grid.GridDimension.y; c++) { if (squares[sector.x, sector.y, r, c].Filled && (!onlyMarked || squares[sector.x, sector.y, r, c].Marked)) { AddWalls(sector, new Vector2Int(r, c), squares, grid, vert, tri, oldVerts); } } } } }
private void ReserveGridSquares(Floor floor, Vector3 start, Vector3 end, float width) { RasterizationGrid rast = floor.rasterizationGrid; Square[,,,] squares = floor.Squares; Vector3 tl, tr, bl, br; GenerationUtility.BoxBounds(start, end, width, out tl, out tr, out bl, out br); float left = Mathf.Min(tl.x, tr.x, bl.x, br.x); float right = Mathf.Max(tl.x, tr.x, bl.x, br.x); float up = Mathf.Max(tl.y, tr.y, bl.y, br.y); float down = Mathf.Min(tl.y, tr.y, bl.y, br.y); Vector2 topLeft = new Vector2(left, up); Vector2 bottomRight = new Vector2(right, down); int sectorTop = rast.SectorColumn(topLeft); int sectorBottom = rast.SectorColumn(bottomRight); int sectorLeft = rast.SectorRow(topLeft); int sectorRight = rast.SectorRow(bottomRight); for (int sr = sectorLeft; sr <= sectorRight; sr++) { for (int sc = sectorTop; sc <= sectorBottom; sc++) { Vector2Int sector = new Vector2Int(sr, sc); int gridTop = rast.GridColumn(sector, topLeft); int gridBottom = rast.GridColumn(sector, bottomRight); int gridLeft = rast.GridRow(sector, topLeft); int gridRight = rast.GridRow(sector, bottomRight); for (int gr = gridLeft; gr <= gridRight; gr++) { for (int gc = gridTop; gc <= gridBottom; gc++) { bool a = GenerationUtility.PointInBox(squares[sr, sc, gr, gc].TopLeft.Position, tl, tr, bl, br); bool b = GenerationUtility.PointInBox(squares[sr, sc, gr, gc].TopRight.Position, tl, tr, bl, br); bool c = GenerationUtility.PointInBox(squares[sr, sc, gr, gc].BottomLeft.Position, tl, tr, bl, br); bool d = GenerationUtility.PointInBox(squares[sr, sc, gr, gc].BottomRight.Position, tl, tr, bl, br); if (a || b || c || d) { squares[sr, sc, gr, gc].Reserved = true; } } } } } }
private void RaiseMesh(Square[,,,] squares, RasterizationGrid rast, Vector3 start, Vector3 end, float width, Vector3 startHeight, Vector3 endHeight) { Vector3 tl, tr, bl, br; GenerationUtility.BoxBounds(start, end, width, out tl, out tr, out bl, out br); float left = Mathf.Min(tl.x, tr.x, bl.x, br.x); float right = Mathf.Max(tl.x, tr.x, bl.x, br.x); float up = Mathf.Max(tl.y, tr.y, bl.y, br.y); float down = Mathf.Min(tl.y, tr.y, bl.y, br.y); Vector2 topLeft = new Vector2(left, up); Vector2 bottomRight = new Vector2(right, down); int sectorTop = rast.SectorColumn(topLeft); int sectorBottom = rast.SectorColumn(bottomRight); int sectorLeft = rast.SectorRow(topLeft); int sectorRight = rast.SectorRow(bottomRight); for (int sr = sectorLeft; sr <= sectorRight; sr++) { for (int sc = sectorTop; sc <= sectorBottom; sc++) { Vector2Int sector = new Vector2Int(sr, sc); int gridTop = rast.GridColumn(sector, topLeft); int gridBottom = rast.GridColumn(sector, bottomRight); int gridLeft = rast.GridRow(sector, topLeft); int gridRight = rast.GridRow(sector, bottomRight); for (int gr = gridLeft; gr <= gridRight; gr++) { for (int gc = gridTop; gc <= gridBottom; gc++) { if (squares[sr, sc, gr, gc].Marked) { squares[sr, sc, gr, gc].SetZHeight(startHeight, endHeight); } } } } } }