Exemplo n.º 1
0
        private static int[] BuildEmptyMap(List <Transform> markers, VoxelMaterial grass)
        {
            var size = 129;

            while (Map.Instance.CreateMap(null, null).MoveNext())
            {
                ;
            }
            var minX = (int)markers.Min(m => m.position.x) - 10;
            var minY = (int)markers.Min(m => m.position.z) - 10;
            var maxX = (int)markers.Max(m => m.position.x) + 10;
            var maxY = (int)markers.Max(m => m.position.z) + 10;

            var ds     = new DiamondSquare(0.01f, size, size);
            var height = ds.Generate(new System.Random());


            for (var x = Mathf.Max(minX, 0); x < Mathf.Min(maxX, size); x++)
            {
                for (var y = Mathf.Max(minY, 0); y < Mathf.Min(maxY, size); y++)
                {
                    for (var h = 0; h <= (int)((height[x, y] * 3) - 0.01f); h++)
                    {
                        World.At(x, h, y).SetVoxel(grass);
                    }
                }
            }
            return(new[] { maxX - minX, maxY - minY });
        }
Exemplo n.º 2
0
        public void DiamondSquare()
        {
            var diamondSquare = new DiamondSquare(TerrainData.heightmapResolution - 1);
            var results       = diamondSquare.Generate(0.23f, 0.43f);

            TerrainData.SetHeights(0, 0, results);
        }
Exemplo n.º 3
0
        public void Button_GenerateHeightmap_DiamondSquare()
        {
            ParseInputFields();
            var heightmap = DiamondSquare.Generate(_diamondSquareSize, _diamondSquareNoiseFactor, _diamondSquareNoiseFalloff);

            CreateNewMenuItem(heightmap, "DiamondSquare_" + ++_diamondMapsGenerated);
        }
Exemplo n.º 4
0
 public void Trigger()
 {
     diamondSquare.Generate();
     diamondSquare.Create(values =>
     {
         td.SetHeights(0, 0, values);
         return(0); //Create needs a return type
     });
 }
Exemplo n.º 5
0
        public void TestRandomSeed()
        {
            var configEq1   = new DiamondSquareConfig(3, 1, new System.Random(5));
            var configEq2   = new DiamondSquareConfig(3, 1, new System.Random(5));
            var configNotEq = new DiamondSquareConfig(3, 1, new System.Random(10));

            var generatorEq1   = new DiamondSquare(configEq1);
            var generatorEq2   = new DiamondSquare(configEq2);
            var generatorNotEq = new DiamondSquare(configNotEq);

            var mapEq1   = generatorEq1.GenerateAsync();
            var mapEq2   = generatorEq2.Generate();
            var mapnotEq = generatorNotEq.GenerateAsync();

            Assert.Equal(mapEq2[1, 2],
                         mapEq1.GetAwaiter().GetResult()[1, 2]);

            Assert.NotEqual(mapnotEq.GetAwaiter().GetResult()[1, 2],
                            mapEq1.GetAwaiter().GetResult()[1, 2]);
        }
Exemplo n.º 6
0
        public PlasmaTexture(GraphicsDevice device, DiamondSquareSeed seed)
            : base(device, seed.size, seed.size)
        {
            var generator = new DiamondSquare();
            var values = generator.Generate(seed);
            Color[] colorData = new Color[seed.size * seed.size];

            var range = generator.MaxValue - generator.MinValue;
            var min = generator.MinValue;

            for (int x = 0; x < seed.size; ++x)
            {
                for (int y = 0; y < seed.size; ++y)
                {
                    var value = Math.Min(Math.Max(values.Get(seed.size + 1, x, y), 0.0f), 1.0f);
                    //int value = (int)(((values.Get(size + 1, x, y) - min) / range) * 255);
                    colorData[y * seed.size + x] = new Color(value, value, value, 1);
                }
            }

            this.SetData<Color>(colorData);
        }
Exemplo n.º 7
0
        public PlasmaTexture(GraphicsDevice device, DiamondSquareSeed seed)
            : base(device, seed.size, seed.size)
        {
            var generator = new DiamondSquare();
            var values    = generator.Generate(seed);

            Color[] colorData = new Color[seed.size * seed.size];

            var range = generator.MaxValue - generator.MinValue;
            var min   = generator.MinValue;

            for (int x = 0; x < seed.size; ++x)
            {
                for (int y = 0; y < seed.size; ++y)
                {
                    var value = Math.Min(Math.Max(values.Get(seed.size + 1, x, y), 0.0f), 1.0f);
                    //int value = (int)(((values.Get(size + 1, x, y) - min) / range) * 255);
                    colorData[y * seed.size + x] = new Color(value, value, value, 1);
                }
            }

            this.SetData <Color>(colorData);
        }
Exemplo n.º 8
0
 public void Trigger()
 {
     diamondSquare.Generate();
     rend.material.mainTexture = diamondSquare.Create(CreateTexture);
 }
Exemplo n.º 9
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);
            }
        }
    }