/// <summary> Populate voxels using perlin noise terrain at given position and scale. </summary> public static void FillPerlin(Voxel[] voxels, Vector2Int pos, Vector3Int size, Vector3 scale, int noiseLayers, float noiseScale, AnimationCurve noiseCurve, BlockLayer[] layers) { const int mirrOff = 1024; int pX = pos.x - mirrOff, pY = pos.y - mirrOff; int sx = size.x, sy = size.y, sz = size.z, sxz = sx * size.z; float scx = 1f / sx * scale.x; float scz = 1f / sz * scale.z; float scy = sy * scale.y; float hbais = (sy - scy) / 2 - 1; for (int z = 0; z < sz; z++) { for (int x = 0; x < sx; x++) { float noise = FractalPerlin((pX + x) * scx, (pY + z) * scz, noiseLayers, noiseScale); int h = Mathf.Clamp(Mathf.RoundToInt(noiseCurve.Evaluate(noise) * scy + hbais), 0, sy - 1); /*for (int y = 0; y < sy; y++) { * BlockType ty; * if (y == 0) ty = BlockType.Bedrock; * else if (y < h * 0.66f) ty = BlockType.Stone; * else if (y < h) ty = BlockType.Dirt; * else if (y == h) ty = BlockType.Grass; * else ty = BlockType.Air; * * voxels[XYZtoIndex(x, y, z, sx, sxz)].ty = ty; * }*/ int y = 0; /*voxels[XYZtoIndex(x, y++, z, sx, sxz)].ty = BlockType.Bedrock; * * for (; y < h * 0.66f;) * voxels[XYZtoIndex(x, y++, z, sx, sxz)].ty = BlockType.Stone; * for (; y < h;) * voxels[XYZtoIndex(x, y++, z, sx, sxz)].ty = BlockType.Dirt; * for (; y == h;) * voxels[XYZtoIndex(x, y++, z, sx, sxz)].ty = BlockType.Grass; * for (; y < sy;) * voxels[XYZtoIndex(x, y++, z, sx, sxz)].ty = BlockType.Air;*/ for (int l = 0; l < layers.Length; l++) { BlockLayer lay = layers[l]; float lh = (lay.height == 0 ? h * lay.percentage : y + lay.height); for (; y < lh;) { voxels[XYZtoIndex(x, y++, z, sx, sxz)].ty = lay.type; } } } } }
/// <summary> Populate voxels with flat terrain using given layers. </summary> public static void FillFlat(Voxel[] voxels, Vector3Int size, BlockLayer[] layers) { int wh = size.x * size.z; for (int i_v = 0, i_lay = 0; i_lay < layers.Length; i_lay++) { BlockLayer lay = layers[i_lay]; for (int j = 0; j < wh * lay.height; j++) { voxels[i_v++] = new Voxel(lay.type); //if (lay.type != BlockType.Air) solids++; } } }