I've almost optimized everything I can imagine, but being more of a novice there's probably some tricks that I don't yet know of. Here's some pieces of code I believe could be faster, and I'll explain what I've done for each one.
Generation:
Starting Generation:
// Use this for initialization
public IEnumerator Generate()
{
blocks = new Block[16, 16, 16];
posx = (int)Position.x;
posy = (int)Position.y;
posz = (int)Position.z;
loadthread = UnityThreadHelper.TaskDistributor.Dispatch(() => blocks = GeneratePlanet.Generate("rock", planet.planetSize, planet.planetSeed, planet.cave, planet.cracked, posx, posy, posz));
while (loadthread.IsSucceeded == false)
{
yield return new WaitForSeconds(0.1f);
}
loadthread = UnityThreadHelper.TaskDistributor.Dispatch(() => blocks = GeneratePlanet.GenerateOres(blocks, planet.planetSize, planet.planetSeed, posx, posy, posz));
while (loadthread.IsSucceeded == false)
{
yield return new WaitForSeconds(0.1f);
}
Generated = true;
UpdatePlanetChunk();
}
- Used threading to increase performance
- Used Coroutines to spread out processing
Actual Generation (Kinda Complicated! ...And the most intensive code):
public static Block[,,] Generate(string PlanetType, int planetSize, int seed, bool cave, bool cracked, int posx, int posy, int posz)
{
Block[,,] blocks = new Block[16, 16, 16];
int PlanetSize = planetSize;
int r = PlanetSize / 2;
int offsetx = PlanetSize / 2;
int offsety = PlanetSize / 2;
int offsetz = PlanetSize / 2;
for (int x = 0; x < 16; x++)
{
for (int y = 0; y < 16; y++)
{
for (int z = 0; z < 16; z++)
{
if (blocks[x, y, z] == null)
{
blocks[x, y, z] = new BlockEmpty();
}
}
}
}
for (int tx = 0; tx < 16; tx++)
{
for (int ty = 0; ty < 16; ty++)
{
for (int tz = 0; tz < 16; tz++)
{
if (Mathf.Sqrt(Mathf.Pow(tx + (posx * 16) - r, 2) + Mathf.Pow(ty + (posy * 16) - r, 2) + Mathf.Pow(tz + (posz * 16) - r, 2)) <= r - PlanetSize / 2.5)
{
blocks[tx, ty, tz] = new BlockCore();
}
else if (Mathf.Sqrt(Mathf.Pow(tx + (posx * 16) - r, 2) + Mathf.Pow(ty + (posy * 16) - r, 2) + Mathf.Pow(tz + (posz * 16) - r, 2)) <= r - PlanetSize / 3.5)
{
blocks[tx, ty, tz] = new BlockBedrock();
}
else if (Mathf.Sqrt(Mathf.Pow(tx + (posx * 16) - r, 2) + Mathf.Pow(ty + (posy * 16) - r, 2) + Mathf.Pow(tz + (posz * 16) - r, 2)) <= r - PlanetSize / 5)
{
blocks[tx, ty, tz] = new BlockSubstone();
}
else if (Mathf.Sqrt(Mathf.Pow(tx + (posx * 16) - r, 2) + Mathf.Pow(ty + (posy * 16) - r, 2) + Mathf.Pow(tz + (posz * 16) - r, 2)) <= r - PlanetSize / 6)
{
blocks[tx, ty, tz] = new BlockStone();
}
}
}
}
if (cracked == true)
{
var CrackGen = new RidgeNoise(seed)
{
Frequency = (float)0.03,
Lacunarity = 2,
OctaveCount = 2,
Exponent = 2,
Offset = 1,
Gain = (float)1,
} * 1;
for (int x = 0; x < 16; x++)
{
for (int y = 0; y < 16; y++)
{
for (int z = 0; z < 16; z++)
{
float value = CrackGen.GetValue(new Vector3(x + (posx * 16), y + (posy * 16), z + (posz * 16)));
if (value > 1.00)
{
blocks[x, y, z] = new BlockEmpty();
}
}
}
}
}
if (cave == true)
{
var CaveGen = new BillowNoise(seed);
CaveGen.Frequency = 0.08f;
CaveGen.Persistence = 0.4f;
for (int x = 0; x < 16; x++)
{
for (int y = 0; y < 16; y++)
{
for (int z = 0; z < 16; z++)
{
float value = CaveGen.GetValue(new Vector3(x + (posx * 16), y + (posy * 16), z + (posz * 16)));
if (value > 0)
{
blocks[x, y, z] = new BlockEmpty();
}
}
}
}
}
return blocks;
}
public static Block[,,] GenerateOres(Block[,,] blocks, int planetSize, int seed, int posx, int posy, int posz)
{
var OreGen = new BillowNoise(seed);
OreGen.Frequency = 0.16f;
OreGen.Persistence = 0.4f;
List<Block> OreTypes = new List<Block>();
OreTypes.Add(new BlockOreUranium());
foreach (Block ore in OreTypes)
{
for (int x = 0; x < 16; x++)
{
for (int y = 0; y < 16; y++)
{
for (int z = 0; z < 16; z++)
{
float value = OreGen.GetValue(new Vector3(x + (posx * 16), y + (posy * 16), z + (posz * 16)));
if (value > ore.Rarity() && blocks[x, y, z].BlockName() == ore.BaseBlock())
{
blocks[x, y, z] = ore;
}
}
}
}
}
return blocks;
}
- Used else-ifs to reduce testing of variables
- Changed to a chunk system to generate one bit at a time
If anyone sees any tips to improve this, thanks!