Esempio n. 1
0
        public override void UpdateComponent()
        {
            float[,] heightmap = GetTerrainHeight();

            int   N     = heightmap.GetLength(0);
            float talus = talusFactor / N;

            thermalErosion.Erode(heightmap, talus, factor, iterations);

            UpdateTerrainHeight(heightmap);
        }
Esempio n. 2
0
        public void Erode_ThermalErosion_ShouldBeEqualErodedHeighmap(string originalHeightmap, string erodedHeightmap)
        {
            // Arrange
            float[,] actualHeighmap   = ReadHeightMap(originalHeightmap);
            float[,] expectedHeighmap = ReadHeightMap(erodedHeightmap);

            // Act
            thermalErosion.Erode(actualHeighmap, talus, factor, iterations);

            // Assert
            AreEqual(actualHeighmap, expectedHeighmap).Should().Be(true);
        }
Esempio n. 3
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);
            }
        }
    }