public Corner(Corner corner) : this(corner.Index, corner._grid) { _grid.Corners[corner.Index.x, corner.Index.y, corner.Index.z] = this; }
public Grid3d(Bounds bbox, float voxelSize = 1.0f) { var watch = Stopwatch.StartNew(); BBox = bbox; VoxelSize = voxelSize; bbox.min = new Vector3(bbox.min.x, 0, bbox.min.z); var sizef = bbox.size / voxelSize; Size = new Vector3Int((int)sizef.x, (int)sizef.y, (int)sizef.z); sizef = new Vector3(Size.x, Size.y, Size.z); Corner = bbox.min + (bbox.size - sizef * voxelSize) * 0.5f; // make voxels Voxels = new Voxel[Size.x, Size.y, Size.z]; for (int x = 0; x < Size.x; x++) { for (int y = 0; y < Size.y; y++) { for (int z = 0; z < Size.z; z++) { Voxels[x, y, z] = new Voxel(new Vector3Int(x, y, z), this); } } } // make corners Corners = new Corner[Size.x + 1, Size.y + 1, Size.z + 1]; for (int x = 0; x < Size.x + 1; x++) { for (int y = 0; y < Size.y + 1; y++) { for (int z = 0; z < Size.z + 1; z++) { Corners[x, y, z] = new Corner(new Vector3Int(x, y, z), this); } } } // make faces Faces[0] = new Face[Size.x + 1, Size.y, Size.z]; for (int x = 0; x < Size.x + 1; x++) { for (int y = 0; y < Size.y; y++) { for (int z = 0; z < Size.z; z++) { Faces[0][x, y, z] = new Face(x, y, z, Axis.X, this); } } } Faces[1] = new Face[Size.x, Size.y + 1, Size.z]; for (int x = 0; x < Size.x; x++) { for (int y = 0; y < Size.y + 1; y++) { for (int z = 0; z < Size.z; z++) { Faces[1][x, y, z] = new Face(x, y, z, Axis.Y, this); } } } Faces[2] = new Face[Size.x, Size.y, Size.z + 1]; for (int x = 0; x < Size.x; x++) { for (int y = 0; y < Size.y; y++) { for (int z = 0; z < Size.z + 1; z++) { Faces[2][x, y, z] = new Face(x, y, z, Axis.Z, this); } } } // make edges Edges[2] = new Edge[Size.x + 1, Size.y + 1, Size.z]; for (int x = 0; x < Size.x + 1; x++) { for (int y = 0; y < Size.y + 1; y++) { for (int z = 0; z < Size.z; z++) { Edges[2][x, y, z] = new Edge(x, y, z, Axis.Z, this); } } } Edges[0] = new Edge[Size.x, Size.y + 1, Size.z + 1]; for (int x = 0; x < Size.x; x++) { for (int y = 0; y < Size.y + 1; y++) { for (int z = 0; z < Size.z + 1; z++) { Edges[0][x, y, z] = new Edge(x, y, z, Axis.X, this); } } } Edges[1] = new Edge[Size.x + 1, Size.y, Size.z + 1]; for (int x = 0; x < Size.x + 1; x++) { for (int y = 0; y < Size.y; y++) { for (int z = 0; z < Size.z + 1; z++) { Edges[1][x, y, z] = new Edge(x, y, z, Axis.Y, this); } } } Debug.Log($"Grid took: {watch.ElapsedMilliseconds} ms to create.\r\nGrid size: {Size}, {Size.x * Size.y * Size.z} voxels."); }