PerlinNoise.GeneratePerlinNoise C# (CSharp) Method

GeneratePerlinNoise() public static method

public static GeneratePerlinNoise ( int seed, int octaveCount, float persistence, int noiseWidth, int noiseHeight ) : ].float[
seed int
octaveCount int
persistence float
noiseWidth int
noiseHeight int
return ].float[
    public static float[,] GeneratePerlinNoise(int seed, int octaveCount, float persistence, int noiseWidth, int noiseHeight)
    {
        List<float[,]> smoothNoiseOctaves = new List<float[,]>();

        // Get smooth noise octaves
        for(int i = 0; i < octaveCount; i++) {

            smoothNoiseOctaves.Add(SmoothNoise.GenerateSmoothNoise(WhiteNoise.GenerateWhiteNoise(seed, noiseWidth, noiseHeight), i, noiseWidth, noiseHeight));
        }

        float[,] noise = new float[noiseWidth, noiseHeight];
        float amplitude = 1.0f;
        float totalAmplitude = 0.0f;

        // Blend noise together
        for(int octave = octaveCount - 1; octave >= 0; octave--) {

            amplitude *= persistence;
            totalAmplitude += amplitude;

            for(int i = 0; i < noiseWidth; i++) {

                for(int j = 0; j < noiseHeight; j++) {

                    noise[i, j] += smoothNoiseOctaves[octave][i, j] * amplitude;
                }
            }
        }

        // Normalise
        for(int i = 0; i < noiseWidth; i++) {

            for(int j = 0; j < noiseHeight; j++) {

                noise[i, j] /= totalAmplitude;
            }
        }

        return noise;
    }

Usage Example

Example #1
0
    void GenerateCloudNoise(ref Texture2D tex, int noiseWidth, int noiseHeight, int seed, int scale)
    {
        float[,] perlinNoise = PerlinNoise.GeneratePerlinNoise(seed, octaves, persistence, noiseWidth, noiseHeight);
        float noiseValue;

        for (int y = 0; y < noiseWidth; y++)
        {
            for (int x = 0; x < noiseHeight; x++)
            {
                noiseValue  = perlinNoise[x, y];
                noiseValue *= SimplexNoise.SeamlessNoise((float)x / (float)_texWidth, (float)y / (float)_texHeight, scale, scale, 0f);

                noiseValue = Mathf.Clamp(noiseValue, contrastLow, contrastHigh + contrastLow) - contrastLow;
                noiseValue = Mathf.Clamp(noiseValue, 0f, 1f);

                var   brightOff = Random.Range(-0.01f, 0.01f);
                float r         = Mathf.Clamp(CloudColor.r + brightOff, 0f, 1f);
                float g         = Mathf.Clamp(CloudColor.g + brightOff, 0f, 1f);
                float b         = Mathf.Clamp(CloudColor.b + brightOff, 0f, 1f);

                tex.SetPixel(x, y, new Color(r, g, b, noiseValue));
                tex.SetPixel(511 - x, y, new Color(r, g, b, noiseValue));
                tex.SetPixel(x, 511 - y, new Color(r, g, b, noiseValue));
                tex.SetPixel(511 - x, 511 - y, new Color(r, g, b, noiseValue));
            }
        }

        tex.Apply();
    }
All Usage Examples Of PerlinNoise::GeneratePerlinNoise