Beispiel #1
0
    IEnumerator Coroutine()
    {
        Utils.ClearMemory();
        if (terrainData == null || terrain == null)
        {
            CreateTerrain();
            yield return(null);
        }

        heights = terrainData.GetHeights(0, 0, terrainData.heightmapWidth, terrainData.heightmapHeight);
        alphas  = terrainData.GetAlphamaps(0, 0, terrainData.alphamapWidth, terrainData.alphamapHeight);
        Vector3 position            = terrain.transform.position;
        float   waterRelativeHeight = water.position.y / size.y;

        //Generate heights
        terrainGenerator.SetupConfig();
        bool terrainHeights1 = true;

        ThreadPool.QueueUserWorkItem((o) => {
            terrainGenerator.GenerateHeights(heights, 0, 4);
            terrainHeights1 = false;
        });
        bool terrainHeights2 = true;

        ThreadPool.QueueUserWorkItem((o) => {
            terrainGenerator.GenerateHeights(heights, 1, 4);
            terrainHeights2 = false;
        });
        bool terrainHeights3 = true;

        ThreadPool.QueueUserWorkItem((o) => {
            terrainGenerator.GenerateHeights(heights, 2, 4);
            terrainHeights3 = false;
        });
        bool terrainHeights4 = true;

        ThreadPool.QueueUserWorkItem((o) => {
            terrainGenerator.GenerateHeights(heights, 3, 4);
            terrainHeights4 = false;
        });

        while (terrainHeights1 || terrainHeights2 || terrainHeights3 || terrainHeights4)
        {
            yield return(null);
        }
        //create roads
        bool road = true;

        ThreadPool.QueueUserWorkItem((o) => {
            roadGenerator.Generate(heights, size, position, waterRelativeHeight);
            road = false;
        });

        //paint textures
        bool terrainTextures = true;

        ThreadPool.QueueUserWorkItem((o) => {
            terrainGenerator.PrepareTextures(heights, waterRelativeHeight);
            terrainTextures = false;
        });
        while (terrainTextures)
        {
            yield return(null);
        }
        bool terrainTextures1 = true;

        ThreadPool.QueueUserWorkItem((o) => {
            terrainGenerator.GenerateTextures(heights, alphas, 0, 4);
            terrainTextures1 = false;
        });
        bool terrainTextures2 = true;

        ThreadPool.QueueUserWorkItem((o) => {
            terrainGenerator.GenerateTextures(heights, alphas, 1, 4);
            terrainTextures2 = false;
        });
        bool terrainTextures3 = true;

        ThreadPool.QueueUserWorkItem((o) => {
            terrainGenerator.GenerateTextures(heights, alphas, 2, 4);
            terrainTextures3 = false;
        });
        bool terrainTextures4 = true;

        ThreadPool.QueueUserWorkItem((o) => {
            terrainGenerator.GenerateTextures(heights, alphas, 3, 4);
            terrainTextures4 = false;
        });

        //level roads
        while (road)
        {
            yield return(null);
        }
        bool roadHeights = true;

        ThreadPool.QueueUserWorkItem((o) => {
            roadGenerator.DrawRoadHeights(heights, size, position, alphas.GetLength(0));
            roadHeights = false;
        });

        //paint roads
        while (road || terrainTextures1 || terrainTextures2 || terrainTextures3 || terrainTextures4)
        {
            yield return(null);
        }
        bool roadTextures = true;

        ThreadPool.QueueUserWorkItem((o) => {
            roadGenerator.DrawRoadTextures(alphas, size, position);
            roadTextures = false;
        });

        while (roadTextures || roadHeights)
        {
            yield return(null);
        }
        terrainData.SetHeights(0, 0, heights);
        terrainData.SetAlphamaps(0, 0, alphas);
        terrain.Flush();

        yield return(null);

        reflection.RenderProbe();
        Utils.ClearMemory();
        if (onGenerated != null)
        {
            onGenerated();
        }
    }