Beispiel #1
0
    override public void Erode(GameObject inTerrain)
    {
        Terrain     ter         = inTerrain.GetComponent <Terrain>();
        TerrainData terrainData = ter.terrainData;

        w       = terrainData.heightmapWidth;
        h       = terrainData.heightmapWidth;
        heights = terrainData.GetHeights(0, 0, w, h);

        float talus = smoothness / (float)w;

        for (int iterCount = 0; iterCount < iterations; iterCount++)
        {
            for (int x = 1; x < (w - 1); x++)
            {
                for (int y = 1; y < (h - 1); y++)
                {
                    int   lowestX = 0;
                    int   lowestY = 0;
                    float newHeight;
                    float currentHeight = heights[x, y];
                    float maxDifference = float.MinValue;

                    for (int i = -1; i <= 1; i += 1)
                    {
                        for (int j = -1; j <= 1; j += 1)
                        {
                            float currentDifference = currentHeight - heights[x + i, y + j];

                            if (currentDifference > maxDifference)
                            {
                                maxDifference = currentDifference;

                                lowestX = i;
                                lowestY = j;
                            }
                        }
                    }

                    if (maxDifference > talus)
                    {
                        newHeight = maxDifference / 2.0f;

                        heights[x, y] -= newHeight;
                        heights[x + lowestX, y + lowestY] += newHeight;
                    }
                }
            }
        }

        terrainData.SetHeights(0, 0, heights);

        if (doSmooth)
        {
            Smoother.Smoothen(inTerrain, 1);
        }
    }
Beispiel #2
0
    // Redraws the menu on each GUI draw
    void OnGUI()
    {
        // Field to attach the terrain to generate too.
        GUILayout.Label("Terrain Generator", EditorStyles.boldLabel);
        GUILayout.Space(16);
        terrain = (GameObject)EditorGUILayout.ObjectField("Terrain", terrain, typeof(UnityEngine.Object), true);

        //================= Terrain Generation =================\\
        GUILayout.Space(16);
        GUILayout.Label("Generation:", EditorStyles.boldLabel);

        // Select Algorithm and show options
        type = (AlgorithmType)EditorGUILayout.EnumPopup("Algorithm to Use:", type);
        switch (type)
        {
        case AlgorithmType.Random:
            ShowRandomGuiOptions();
            break;

        case AlgorithmType.Perlin:
            ShowPerlinGuiOptions();
            break;

        case AlgorithmType.DiamondSquare:
            ShowDiamondSquareGuiOptions();
            break;
        }

        // Run selected algorithm if possible
        if (GUILayout.Button("Generate!"))
        {
            if (terrain == null || terrain.GetComponent <Terrain>() == null)
            {
                ShowNotification(new GUIContent("Terrain Field is empty or not a valid Terrain!"));
            }
            else
            {
                // Set the undo array
                Terrain     ter         = terrain.GetComponent <Terrain>();
                TerrainData terrainData = ter.terrainData;
                int         w           = terrainData.heightmapWidth;
                int         h           = terrainData.heightmapWidth;

                undoArray = terrainData.GetHeights(0, 0, w, h);

                switch (type)
                {
                case AlgorithmType.Random:
                    randomGen.Generate(terrain);
                    break;

                case AlgorithmType.Perlin:
                    perlinGen.Generate(terrain);
                    break;

                case AlgorithmType.DiamondSquare:
                    diamondSquareGen.Generate(terrain);
                    break;
                }
            }
        }

        //================= Erosion Generation =================\\
        GUILayout.Space(16);
        GUILayout.Label("Erosion:", EditorStyles.boldLabel);
        erodeType = (ErosionType)EditorGUILayout.EnumPopup("Algorithm to Use:", erodeType);

        // Select erosion type
        switch (erodeType)
        {
        case ErosionType.Thermal:
            ShowThermalGuiOptions();
            break;

        case ErosionType.ImprovedThermal:
            ShowImprovedThermalGuiOptions();
            break;
        }

        // Erode if possible
        if (GUILayout.Button("Erode!"))
        {
            if (terrain == null || terrain.GetComponent <Terrain>() == null)
            {
                ShowNotification(new GUIContent("Terrain Field is empty or not a valid Terrain!"));
            }
            else
            {
                // Set the undo array
                Terrain     ter         = terrain.GetComponent <Terrain>();
                TerrainData terrainData = ter.terrainData;
                int         w           = terrainData.heightmapWidth;
                int         h           = terrainData.heightmapWidth;

                undoArray = terrainData.GetHeights(0, 0, w, h);
                switch (erodeType)
                {
                case ErosionType.Thermal:
                    thermalGen.Erode(terrain);
                    break;

                case ErosionType.ImprovedThermal:
                    improvedThermalGen.Erode(terrain);
                    break;
                }
            }
        }

        //================= Smoothing =================\\
        GUILayout.Space(16);
        GUILayout.Label("Smooth:", EditorStyles.boldLabel);
        smoothenIterations = EditorGUILayout.IntField("Iterations: ", smoothenIterations);

        if (GUILayout.Button("Smooth!"))
        {
            // Set the undo array
            Terrain     ter         = terrain.GetComponent <Terrain>();
            TerrainData terrainData = ter.terrainData;
            int         w           = terrainData.heightmapWidth;
            int         h           = terrainData.heightmapWidth;

            undoArray = terrainData.GetHeights(0, 0, w, h);

            Smoother.Smoothen(terrain, smoothenIterations);
        }


        //================= Undo =================\\
        GUILayout.Space(16);
        if (GUILayout.Button("Undo Last Action!"))
        {
            if (terrain == null || terrain.GetComponent <Terrain>() == null || undoArray == null)
            {
                ShowNotification(new GUIContent("Can't undo!"));
            }
            else
            {
                Terrain     ter         = terrain.GetComponent <Terrain>();
                TerrainData terrainData = ter.terrainData;
                terrainData.SetHeights(0, 0, undoArray);
            }
        }
    }