Generating Perlin Noise in C#


Noise textures are often very important in lots of algorithms used in computer graphics.

This post will show a simple implementation of the classic Perlin Noise technique.

The code below shows a class responsible for generating each point of the noise texture. The implementation is simple, we used the perlin formula to generate the point, and after we make a bilinear interpolation to smooth a little the sample:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PloobsEngine.Utils
{
    /// Perlin Noise
    public class PerlinNoise
    {
        /// Perlin Noise Constructot
        public PerlinNoise(int width, int height)
        {
            this.MAX_WIDTH = width;
            this.MAX_HEIGHT = height;                
        }

        public int MAX_WIDTH = 256;
        public int MAX_HEIGHT = 256;
                
        /// Gets the value for a specific X and Y coordinate
        /// results in range [-1, 1] * maxHeight
        public float GetRandomHeight(float X, float Y, float MaxHeight,
            float Frequency, float Amplitude, float Persistance,
            int Octaves)
        {
            GenerateNoise();
            float FinalValue = 0.0f;
            for (int i = 0; i < Octaves; ++i)
            {
                FinalValue += GetSmoothNoise(X * Frequency, Y * Frequency) * Amplitude;
                Frequency *= 2.0f;
                Amplitude *= Persistance;
            }
            if (FinalValue < -1.0f)
            {
                FinalValue = -1.0f;
            }
            else if (FinalValue > 1.0f)
            {
                FinalValue = 1.0f;
            }
            return FinalValue * MaxHeight;
        }

        //This function is a simple bilinear filtering function which is good (and easy) enough.        
        private float GetSmoothNoise(float X, float Y)
        {
            float FractionX = X - (int)X;
            float FractionY = Y - (int)Y;
            int X1 = ((int)X + MAX_WIDTH) % MAX_WIDTH;
            int Y1 = ((int)Y + MAX_HEIGHT) % MAX_HEIGHT;
            //for cool art deco looking images, do +1 for X2 and Y2 instead of -1...
            int X2 = ((int)X + MAX_WIDTH - 1) % MAX_WIDTH;
            int Y2 = ((int)Y + MAX_HEIGHT - 1) % MAX_HEIGHT;
            float FinalValue = 0.0f;
            FinalValue += FractionX * FractionY * Noise[X1, Y1];
            FinalValue += FractionX * (1 - FractionY) * Noise[X1, Y2];
            FinalValue += (1 - FractionX) * FractionY * Noise[X2, Y1];
            FinalValue += (1 - FractionX) * (1 - FractionY) * Noise[X2, Y2];
            return FinalValue;
        }

        float[,] Noise;
        bool NoiseInitialized = false;
        /// create a array of randoms
        private void GenerateNoise()
        {
            if (NoiseInitialized)                //A boolean variable in the class to make sure we only do this once
                return;
            Noise = new float[MAX_WIDTH, MAX_HEIGHT];    //Create the noise table where MAX_WIDTH and MAX_HEIGHT are set to some value>0            
            for (int x = 0; x < MAX_WIDTH; ++x)
            {
                for (int y = 0; y < MAX_HEIGHT; ++y)
                {
                    Noise[x, y] = ((float)(StaticRandom.Random()) - 0.5f) * 2.0f;  //Generate noise between -1 and 1
                }
            }
            NoiseInitialized = true;
        }

    }
}

To transform the sampled points in a texture you could use the following code:

public Texture2D CreatePerlinNoiseTexture(int sizex, int sizey,float frequencia, float amplitude, float persistence, int octave,bool mipmap = false)
        {
            PerlinNoise pn = new PerlinNoise(sizex, sizey);
            Texture2D t = factory.CreateTexture2D(sizex, sizey,mipmap); ///helper that creates a simple empty texture
            Color[] cor = new Color[sizex * sizey];
            for (int i = 0; i < sizex; i++)
            {
                for (int j = 0; j < sizey; j++)
                {
                    float value = pn.GetRandomHeight(i, j, 1, frequencia, amplitude, persistence, octave);
                    value =  0.5f * (1 + value);
                    cor[i + j * sizex] = new Color(value,value,value);
                }
            }

            t.SetData(cor);
            return t;            
        }

  1. #1 by how to get free coins in madden mobile 16 on 27 de fevereiro de 2017 - 7:50 pm

    Healthy Mix foundation Kryolan Magic Circle concealer MAC Mineralize Skinfinish Natural powder Tarte blush Rimmel lipstick Max Factor pencil Catrice Eye

  2. #2 by http://free-coins.club/csgo-free-coins-roulette-sites.html on 27 de fevereiro de 2017 - 8:05 pm

    1000 takk for en koselig tur gjennom vinterferien din ;o)Herlige bilder som alltid, og jeg er sÃ¥ enig i at NÃ… mÃ¥ da vÃ¥ren begynne Ã¥ komme. Sola i dag gjorde fantastisk godt, sÃ¥ det kan godt bli sÃ¥nn en stund – De hvite stakene og vasene ble kjempefint, lurer pÃ¥ om jeg mÃ¥ gjøre noe lignenede i morgen ;o))Ha en fortsatt flott helg ;o))

  3. #3 by AaronVok on 27 de fevereiro de 2017 - 8:59 pm

    wh0cd610173 Generic Artane

  4. #4 by http://coins4games.us/wsop-2017-game-hack.html on 27 de fevereiro de 2017 - 9:37 pm

    A minute saved is a minute earned, and this saved hours!

  5. #5 by tinder dating site on 27 de fevereiro de 2017 - 9:38 pm

    Hi there, I found your website by way of Google while searching for a similar topic, your website
    came up, it seems to be good. I’ve bookmarked it
    in my google bookmarks.
    Hello there, just was aware of your weblog thru Google, and located that it’s really informative.
    I’m gonna watch out for brussels. I will appreciate if you continue this in future.
    A lot of other people will be benefited out of your writing.
    Cheers!

  6. #6 by fifa 17 coins free on 27 de fevereiro de 2017 - 11:22 pm

    Lately, I didn’t give plenty of consideration to leaving comments on weblog Ogrodzenia web page posts and have placed feedback even considerably much less.

  7. #7 by amada edm filter on 27 de fevereiro de 2017 - 11:40 pm

    check below, are some completely unrelated web sites to ours, on the other hand, they are most trustworthy sources that we use

1 262 263 264
(não será publicado)