private SafeMesh GenerateCaveMap(Vector3 offsets) { int size = WorldBuilder.CHUNK_SIZE + 1; Tile currentTile = WorldBuilder.GetTile(offsets); // Gets added to coordinates, is a decimal to make sure it does not end up at an integer float addendum = 1000.17777f; // Coordinates are divided by scale, larger scale = larger/more spread out caves float scale = 20f; float[,,] caveMap = new float[size, CAVE_DEPTH * 2, size]; for (int x = 0; x < size; x++) { for (int z = 0; z < size; z++) { // -5 to make the amount of rocks sticking out of the terrain lower int coordinateHeight = Mathf.FloorToInt(currentTile.HeightMap[x, z]) - 5; int caveHeight = coordinateHeight + CAVE_DEPTH; // Cave height make height dynamic based on heightmap[x,z] for (int y = 0; y < caveHeight; y++) { if (y == 0) { caveMap[x, y, z] = 1; } else if (y <= 2) { double tempVal = ridgedMultifractal.GetValue((z + offsets.z + addendum) / scale, (y + addendum) / scale, (x + offsets.x + addendum) / scale); int isCave = tempVal < 0 ? 0 : 1; caveMap[x, y, z] = isCave; } else { double tempVal = ridgedMultifractal.GetValue((x + offsets.x + addendum) / scale, (y + addendum) / scale, (z + offsets.z + addendum) / scale); int isCave = tempVal < 0.35 ? 0 : 1; caveMap[x, y, z] = isCave; } } } } SafeMesh safeMesh = MarchingCubes.BuildMesh(caveMap); safeMesh.position = offsets; return(safeMesh); }