/// <summary> /// Builds a grid around center with given settings /// </summary> /// <param name="_center">Grid center</param> /// <param name="settings">Grid settings (size / step etc)</param> /// <param name="GetIsoValue">Function that nodes use to determine their iso value</param> public Grid(GridGenerationSettings settings, Func <Vector3, float> GetIsoValue, Chunk _owner, bool onlyBlocked = false) { owner = _owner; settings.step.x = Mathf.Max(settings.step.x, 0.5f); settings.step.y = Mathf.Max(settings.step.y, 0.5f); settings.step.z = Mathf.Max(settings.step.z, 0.5f); var center = owner.bounds.center; var extents = settings.chunkSize; step = settings.step; xSize = (int)(extents.x / step.x); ySize = (int)(extents.y / step.y); zSize = (int)(extents.z / step.z); nodes = new Node[xSize, ySize, zSize]; Vector3 min = owner.bounds.min; Vector3 pos = min; for (int x = 0; x < xSize; x++) { for (int y = 0; y < ySize; y++) { for (int z = 0; z < zSize; z++) { var isoValue = GetIsoValue(pos); if (!onlyBlocked || isoValue > 0) { nodes[x, y, z] = new Node(pos, isoValue); } pos.z += step.z; } pos.z = min.z; pos.y += step.y; } pos.y = min.y; pos.x += step.x; } if (onlyBlocked) { return; } for (int x = 0; x < xSize; x++) { for (int y = 0; y < ySize; y++) { for (int z = 0; z < zSize; z++) { FindNeighbours(x, y, z, settings.allowDiagonalNeighbours); } } } }
/// <summary> /// Sets local position / name according to index, calculates bounds and node array dimensions /// </summary> /// <param name="settings">GridGenerationSettings (grid size / step etc)</param> /// <param name="_x">X index</param> /// <param name="_y">Y index</param> /// <param name="_z">Z index</param> public void Initialize(GridGenerationSettings settings, int _x, int _y, int _z) { gridSettings = settings; x = _x; y = _y; z = _z; var maxX = (int)(gridSettings.chunkSize.x / gridSettings.step.x); var maxY = (int)(gridSettings.chunkSize.y / gridSettings.step.y); var maxZ = (int)(gridSettings.chunkSize.z / gridSettings.step.z); transform.localPosition = new Vector3(x * maxX * gridSettings.step.x, y * maxY * gridSettings.step.y, z * maxZ * gridSettings.step.z) + gridSettings.chunkSize / 2; bounds = new Bounds(transform.position, gridSettings.chunkSize); gameObject.name = "Chunk " + new Vector3Int(x, y, z); }