コード例 #1
0
 private static void Flatten(Noise2D noise, int factor)
 {
     float[,] original = noise.GetNormalizedData();
     for (int i = 0; i < noise.Width; i++)
     {
         for (int j = 0; j < noise.Height; j++)
         {
             float total    = 0;
             int   elements = 0;
             for (int x = i + (factor * -1); x <= i + factor; x++)
             {
                 for (int y = j + (factor * -1); y <= j + factor; y++)
                 {
                     if (x >= 0 && x < noise.Width && y >= 0 && y < noise.Height && (x != 0 && y != 0))
                     {
                         total += original[x, y];
                         elements++;
                     }
                 }
             }
             float average = original[i, j];
             if (elements > 0)
             {
                 average = total / (float)elements;
             }
             noise[i, j] = average;
         }
     }
 }
コード例 #2
0
ファイル: MapGenerator.cs プロジェクト: zladovan/unity-world
        public Map GenerateMap(Vector2 offset)
        {
            Debug.Log(string.Format("Generating map with offset {0}:{1}, seed: {2}", offset.x, offset.y, seed));
            var perlin = new Perlin();

            perlin.OctaveCount = octaves;
            perlin.Frequency   = frequency;
            perlin.Lacunarity  = lacunarity;
            perlin.Persistence = persistence;
            perlin.Seed        = seed;

            var heightMapBuilder = new Noise2D(ChunkSize, ChunkSize, perlin);
            var halfShift        = new Vector2(0.5f / noiseScale.x, 0.5f / noiseScale.y);

            heightMapBuilder.GeneratePlanar(
                (offset.x + noiseShift.x) / noiseScale.x - halfShift.x,
                (offset.x + noiseShift.x) / noiseScale.x + halfShift.x,
                (-offset.y + noiseShift.y) / noiseScale.y - halfShift.y,
                (-offset.y + noiseShift.y) / noiseScale.y + halfShift.y
                );

            // prepare texture
            var colorMap = heightMapBuilder.GetTexturePixels(terrainColors);

            Debug.Log("Texture created");
            // preare height map
            var heightMap = new HeightMap(
                data: heightMapBuilder.GetNormalizedData(),
                heightMultiplier: meshHeightMultiplier,
                heightCurve: new AnimationCurve(meshHeightCurve.keys)
                );


            return(new Map(heightMap, colorMap));
        }
コード例 #3
0
    public void generate()
    {
        // Get the terrain data of the currently active terrain
        var terrainData = Terrain.activeTerrain.terrainData;

        // A new ridged multifractal generator
        var generator = new RidgedMultifractal(frequency, lacu, octaves, (int)(Random.value * 0xffffff), QualityMode.High);

        // The thresholded output -- choose either 0.0 or 1.0, based on the output
        var clamped = new LibNoise.Operator.Select(new Const(0.0f), new Const(1.0f), generator);

        // Set the threshold and falloff rate
        clamped.SetBounds(0f, threshold);
        clamped.FallOff = falloff;

        // Create a 2D noise generator for the terrain heightmap, using the generator we just created
        var noise = new Noise2D(terrainData.heightmapResolution, clamped);

        // Generate a plane from [0, 1] on x, [0, 1] on y
        noise.GeneratePlanar(0, 1, 0, 1);

        // Get the data in an array so we can use it to set the heights
        // var data = noise.GetData(true, 0, 0, true);
        var data = noise.GetNormalizedData();

        // .. and actually set the heights
        terrainData.SetHeights(0, 0, data);
    }
コード例 #4
0
        void DrawSelectedNodeDetails(NodeBase node)
        {
            if (previewNeedsUpdate || lastSelected != node)
            {
                preview = new Texture2D(230, 230);
                if (node.Module != null && previewCalculation == null)
                {
                    previewCalculation = new NoiseCalculation(node.Module, 230, 230);
                }
                previewNeedsUpdate = false;
                lastSelected       = node;
            }

            if (previewCalculation != null && previewCalculation.Done)
            {
                preview            = previewCalculation.Noise.GetTexture();
                previewCalculation = null;
            }

            var state = mainEditorState as NoiseDesignerState;

            EditorGUILayout.Space();
            EditorGUILayout.LabelField("Selected Node:");
            GUILayout.Box(preview);
            EditorGUILayout.Space();
            EditorGUILayout.Separator();
            EditorGUILayout.LabelField("Apply to terrain:");
            terrain = EditorGUILayout.ObjectField("TerrainData", terrain, typeof(TerrainData), false) as TerrainData;
            if (GUILayout.Button("Apply"))
            {
                Noise2D noise = new Noise2D(terrain.heightmapWidth, terrain.heightmapHeight, node.Module);
                noise.GeneratePlanar(4.0f, 10.0f, 1.0f, 5.0f);
                terrain.SetHeights(0, 0, noise.GetNormalizedData());
            }
            EditorGUILayout.Space();
            EditorGUILayout.Separator();
            EditorGUILayout.LabelField("Export as texture:");
            state.textureSize = EditorGUILayout.Vector2Field("Texture Size", state.textureSize);
            if (GUILayout.Button("Save as PNG"))
            {
                var path = EditorUtility.SaveFilePanelInProject("Save as PNG", "noise", "png", "");
                if (!string.IsNullOrEmpty(path))
                {
                    Noise2D noise = new Noise2D((int)state.textureSize.x, (int)state.textureSize.y, node.Module);
                    noise.GeneratePlanar(4.0f, 10.0f, 1.0f, 5.0f);
                    var texture = noise.GetTexture();
                    File.WriteAllBytes(path, texture.EncodeToPNG());
                    AssetDatabase.Refresh();
                }
            }
        }
コード例 #5
0
        public SS_Background(int seed, int size, SS_NoiseGenerator backgroundNoise, double frequency, double lacunarity, double persistence, int octaves, Color tint, float brightness)
        {
            // Create sprite texture
            Sprite = new SS_Texture(size, size, Color.white);

            // Initialize noise with parameters
            ModuleBase myModule;

            if (backgroundNoise == SS_NoiseGenerator.Perlin)
            {
                Perlin perlin = new Perlin(frequency, lacunarity, persistence, octaves, seed, QualityMode.Low);
                myModule = perlin;
            }
            else
            {
                RidgedMultifractal ridgedMultifractal = new RidgedMultifractal(frequency, lacunarity, octaves, seed, QualityMode.Low);
                myModule = ridgedMultifractal;
            }

            // Create seemless tiling noise
            Noise2D noise = new Noise2D(size, size, myModule);

            noise.GeneratePlanar(0, size, 0, size, false);

            // Get noise data
            float[,] noiseData = noise.GetNormalizedData(false, 0, 0);

            // Create cloud
            for (int y = 0; y < size; y++)
            {
                for (int x = 0; x < size; x++)
                {
                    float n          = noiseData[y, x];;
                    Color pixelColor = tint * n * brightness;

                    pixelColor.a = ((pixelColor.r + pixelColor.g + pixelColor.b) / 3.0f);

                    Sprite.SetPixel(x, y, pixelColor);
                }
            }
        }
コード例 #6
0
    public void Generate()

    {
        TerrainData terrainData = terrain.terrainData;

        Debug.Log("Working");
        var generator = new RidgedMultifractal(sampleSizeX, sampleSizeY, octaves, (int)(Random.value * 0xffffff), QualityMode.High);

        var clamped = new Select(new Const(0.0f), new Const(1.0f), generator);

        clamped.SetBounds(0f, threshold);
        clamped.FallOff = falloff;

        terrainData.size = new Vector3(length, height, width);

        var noise = new Noise2D(terrainData.heightmapResolution, clamped);


        noise.GeneratePlanar(sampleOffsetX, sampleOffsetX + sampleSizeX, sampleOffsetY, sampleOffsetY + sampleSizeY);

        float[,] heights = noise.GetNormalizedData(true, 0, 0);

        terrainData.SetHeights(0, 0, heights);
    }