private static Task BuildRect(World world, Cuboid c, BiomeConfiguration configuration = null, LoadedVoxelMaterial mat = null) { return(Task.Run(() => { for (var x = c.Pos.x; x < c.Pos.x + c.Size.x; x++) { for (var z = c.Pos.z; z < c.Pos.z + c.Size.z; z++) { var i = 0; for (var y = c.Pos.y + c.Size.y - 1; y >= c.Pos.y; y--) { if (configuration != null) { mat = configuration.GetLayer(i); } world.SetVoxel(mat, new Vector3Int(x, y, z)); i++; } } } })); }
public static void GenerateWorld(out World world, MaterialCollection collection, BiomeConfiguration configuration) { Random.InitState(WorldSeed); world = new World(collection); var mapSize = 300; var mapHeight = 100; var centerSize = 100; var centerHeight = 30; configuration.Init(collection); world.StartBatch(); var stopwatch = new Stopwatch(); stopwatch.Start(); var cuboidCount = 100; var cuboids = new List <Cuboid>(); var tasks = new List <Task>(); cuboids.AddRange(SplitCenterCube(centerSize, centerHeight)); for (var c = 0; c < cuboidCount || cuboids.Count < cuboidCount / 2; c++) { var posx = Random.Range(-mapSize / 2, mapSize / 2); var posy = Random.Range(-mapHeight / 2, mapHeight / 2); var posz = Random.Range(-mapSize / 2, mapSize / 2); if (posx > -centerSize && posy > -centerHeight && posz > -centerSize && posx < centerSize && posy < centerHeight && posz < centerSize) { continue; } var posCentered = (mapSize - Mathf.Abs(posx)) * (mapHeight - Mathf.Abs(posy)) * (mapSize - Mathf.Abs(posz)) / (float)(mapSize * mapSize * mapHeight); var cubeSize = mapSize * posCentered; var width = Random.Range(cubeSize / 3, cubeSize) * 0.75f; var height = Random.Range(cubeSize / 5, cubeSize / 2.5f) * 0.75f; var depth = Random.Range(cubeSize / 3, cubeSize) * 0.75f; posx -= (int)width / 2; posy -= (int)width / 2; posz -= (int)width / 2; cuboids.Add(new Cuboid() { Pos = new Vector3Int(posx, posy, posz), Size = new Vector3Int((int)width, (int)height, (int)depth) }); } foreach (var cuboid in cuboids) { tasks.Add(BuildRect(world, cuboid, configuration)); } Task.WaitAll(tasks.ToArray()); stopwatch.Stop(); Debug.Log("finished Stone " + stopwatch.ElapsedMilliseconds); /*var waterCube = new Cuboid() * { * Pos = new Vector3Int(0, 50, 0), * Size = new Vector3Int(100, 3, 100) * }; * BuildRect(world, waterCube, null, configuration.GetFluid()).Wait();*/ stopwatch.Reset(); stopwatch.Restart(); world.FinishBatch(); stopwatch.Stop(); Debug.Log("finished Rendering " + stopwatch.ElapsedMilliseconds); }