Beispiel #1
0
    public void LoadChunk(int seed)
    {
        Stopwatch swTotal = new Stopwatch();

        swTotal.Start();

        transform.position = new Vector3(coordinates.x, coordinates.y, coordinates.z) * chunkSize;
        Vector3 offset = new Vector3(coordinates.x, coordinates.y, coordinates.z) * chunkSize;

        blocks      = new int[chunkSize, chunkSize, chunkSize];
        blocksAbove = new int[chunkSize, chunkSize];

        Vector3 chunkPosition = new Vector3(coordinates.x, coordinates.y, coordinates.z) * chunkSize;

        Stopwatch swTerrainA = new Stopwatch();

        swTerrainA.Start();

        //float[,,] noiseMapMountains = Noise.GenerateNoiseMap3D(chunkSize, chunkSizeP1, chunkSize, seed, worldGenerator.noiseSettings_Mountains, offset);
        float[,,] noiseMapMountains = Noise.GenerateNoiseMap3D(chunkSize, chunkSizeP1, chunkSize, worldGenerator.seed, worldGenerator.noiseSettings_Mountains, offset);

        swTerrainA.Stop();

        Stopwatch swTerrainB = new Stopwatch();

        swTerrainB.Start();

        float[,,] noiseMapElevation = Noise.Convert2DTo3D(Noise.GenerateNoiseMap2D(chunkSize, chunkSize, seed, worldGenerator.noiseSettings_Elevation, new Vector3(offset.x, offset.z)), Mathf.Clamp((worldGenerator.elevationHeight - worldGenerator.elevationBottom) + 1, 0, 2048), worldGenerator.curve_Elevation);

        swTerrainB.Stop();

        Stopwatch swOres = new Stopwatch();

        swOres.Start();

        float[,,] noiseMapGravel = Noise.GenerateNoiseMap3D(chunkSize, chunkSizeP1, chunkSize, seed + 1, worldGenerator.noiseSettings_Gravel, offset);
        float[,,] noiseMapCoal   = Noise.GenerateNoiseMap3D(chunkSize, chunkSizeP1, chunkSize, seed + 2, worldGenerator.noiseSettings_Coal, offset);
        float[,,] noiseMapIron   = Noise.GenerateNoiseMap3D(chunkSize, chunkSizeP1, chunkSize, seed + 3, worldGenerator.noiseSettings_Iron, offset);

        swOres.Stop();



        Stopwatch swBlockChange = new Stopwatch();

        swBlockChange.Start();

        float[,,] noiseMapElevationSlice = new float[chunkSize, chunkSizeP1, chunkSize];

        // Generate NoiseMapElevationSlice
        for (int x = 0; x < chunkSize; x++)
        {
            for (int z = 0; z < chunkSize; z++)
            {
                for (int y = 0; y < chunkSizeP1; y++)
                {
                    if (chunkPosition.y < worldGenerator.elevationBottom)
                    {
                        noiseMapElevationSlice[x, y, z] = 1.5f;
                    }
                    else if (chunkPosition.y > worldGenerator.elevationBottom + worldGenerator.elevationHeight)
                    {
                        noiseMapElevationSlice[x, y, z] = -0.8f;
                    }
                    else
                    {
                        noiseMapElevationSlice[x, y, z] = noiseMapElevation[x, (int)(chunkPosition.y - worldGenerator.elevationBottom + y), z];
                    }
                }
            }
        }

        // Combine NoiseMaps
        float[,,] noiseMapFinal = Noise.CombineNoiseMaps(noiseMapMountains, noiseMapElevationSlice);
        //float[,,] noiseMapFinal = noiseMapElevationSlice;

        // Generate Blocks
        for (int x = 0; x < chunkSize; x++)
        {
            for (int z = 0; z < chunkSize; z++)
            {
                for (int y = 0; y < chunkSizeP1; y++)
                {
                    if (y == chunkSize)
                    {
                        if (noiseMapFinal[x, y, z] >= 0)
                        {
                            blocksAbove[x, z] = 1;
                        }
                        else
                        {
                            blocksAbove[x, z] = 0;
                        }
                    }
                    else
                    {
                        if (noiseMapFinal[x, y, z] >= 0)
                        {
                            blocks[x, y, z] = 1;
                        }
                        else
                        {
                            blocks[x, y, z] = 0;
                        }
                    }
                }
            }
        }

        // Generate Grass / Dirt
        for (int y = 0; y < chunkSize; y++)
        {
            int verticalPos = (int)chunkPosition.y + y;

            for (int x = 0; x < chunkSize; x++)
            {
                for (int z = 0; z < chunkSize; z++)
                {
                    if (verticalPos >= worldGenerator.elevationBottom && (verticalPos < worldGenerator.elevationBottom + worldGenerator.elevationHeight - 1) && noiseMapElevationSlice[x, y + 1, z] < 1f)
                    {
                        if (blocks[x, y, z] == 1 && ((y < chunkSizeM1 && blocks[x, y + 1, z] == 0) || (y == chunkSizeM1 && blocksAbove[x, z] == 0)))
                        {
                            blocks[x, y, z] = 3;                                    // Set Grass
                            if (y > 0 && blocks[x, y - 1, z] == 1)
                            {
                                blocks[x, y - 1, z] = 2;                                    // Set Dirt
                                if (y > 2 && blocks[x, y - 2, z] == 1)
                                {
                                    blocks[x, y - 2, z] = 2;                                        // Set Dirt
                                }
                            }
                        }
                    }
                }
            }
        }


        // Generate Ores
        for (int x = 0; x < chunkSize; x++)
        {
            for (int z = 0; z < chunkSize; z++)
            {
                for (int y = 0; y < chunkSize; y++)
                {
                    if (blocks[x, y, z] == 1)
                    {
                        if (noiseMapGravel[x, y, z] > 0.75f)                                         // Gravel
                        {
                            blocks[x, y, z] = 4;
                        }
                        else if (noiseMapCoal[x, y, z] > 2.0f)                                      // Coal
                        {
                            blocks[x, y, z] = 5;
                        }
                        else if (noiseMapIron[x, y, z] > 2.5f)                                      // Iron
                        {
                            blocks[x, y, z] = 6;
                        }
                    }
                }
            }
        }

        swBlockChange.Stop();

        Stopwatch swMesh = new Stopwatch();

        swMesh.Start();

        GenerateMesh();

        swMesh.Stop();

        swTotal.Stop();

        // Add up averages
        worldGenerator.averageTimeTotal       *= worldGenerator.averageTimeCount;
        worldGenerator.averageTimeTotal       += swTotal.ElapsedMilliseconds;
        worldGenerator.averageTimeTerrainA    *= worldGenerator.averageTimeCount;
        worldGenerator.averageTimeTerrainA    += swTerrainA.ElapsedMilliseconds;
        worldGenerator.averageTimeTerrainB    *= worldGenerator.averageTimeCount;
        worldGenerator.averageTimeTerrainB    += swTerrainB.ElapsedMilliseconds;
        worldGenerator.averageTimeOres        *= worldGenerator.averageTimeCount;
        worldGenerator.averageTimeOres        += swOres.ElapsedMilliseconds;
        worldGenerator.averageTimeBlockChange *= worldGenerator.averageTimeCount;
        worldGenerator.averageTimeBlockChange += swBlockChange.ElapsedMilliseconds;
        worldGenerator.averageTimeMesh        *= worldGenerator.averageTimeCount;
        worldGenerator.averageTimeMesh        += swMesh.ElapsedMilliseconds;

        worldGenerator.averageTimeCount++;

        worldGenerator.averageTimeTotal       /= worldGenerator.averageTimeCount;
        worldGenerator.averageTimeTerrainA    /= worldGenerator.averageTimeCount;
        worldGenerator.averageTimeTerrainB    /= worldGenerator.averageTimeCount;
        worldGenerator.averageTimeOres        /= worldGenerator.averageTimeCount;
        worldGenerator.averageTimeBlockChange /= worldGenerator.averageTimeCount;
        worldGenerator.averageTimeMesh        /= worldGenerator.averageTimeCount;

        //UnityEngine.Debug.Log("(Load Time: " + swTotal.ElapsedMilliseconds + ") (Average Total: " + worldGenerator.averageTimeTotal + ")" + ") (TerrainA: " + worldGenerator.averageTimeTerrainA + ")" + ") (TerrainB: " + worldGenerator.averageTimeTerrainB + ")" + ") (Ores: " + worldGenerator.averageTimeOres + ")" + ") (Block Change: " + worldGenerator.averageTimeBlockChange + ")" + ") (Mesh: " + worldGenerator.averageTimeMesh + ")");
    }