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)
            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
            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);

            return t;            

  1. #1 by Serena Yan on 9 de dezembro de 2016 - 4:37 pm

    Ultimately, a problem that I’m passionate about. I have looked for data of this caliber for the previous various hours. Your site is greatly appreciated.

  2. #2 by Allyson Hertz on 9 de dezembro de 2016 - 4:40 pm

    I am usually to blogging and i genuinely appreciate your content regularly. This content has truly peaks my interest. I will bookmark your web site and maintain checking achievable details.

  3. #3 by girokonto kredit saldo quotas on 9 de dezembro de 2016 - 4:41 pm

    That saves me. Thanks for being so sensible!

  4. #4 by lewisville dentist on 9 de dezembro de 2016 - 5:19 pm

    Amazing! Its genuinely remarkable paragraph, I have got much clear idea regarding from this paragraph.|

  5. #5 by schnellkredit ohne einkommen wien öffnungszeiten on 9 de dezembro de 2016 - 6:36 pm

    That position certainly suits Landon. In reality the budget is not that hard to understand. What other districts spend is largely irrelevant. The BOE estimates that there will be FEWER students on opening day 2011 than there were on opening day 2010.

  6. #6 by geld kredite von privat on 9 de dezembro de 2016 - 8:14 pm

    Sharon! This is so great! I love marble :) I've NEVER been to NY.. how embarrassing is that?? When we go to Paris at the end of June we will stop there to change airplanes. I told my husband I wish we could spend time there however our schedule is very busy and there is no time :( leslie

  7. #7 by die ursprüngliche Quelle on 9 de dezembro de 2016 - 8:39 pm

    Dennoch wollte ich mich heimlich privat erotikchat daran berauschen.

  8. #8 by Arnoldo Modin on 9 de dezembro de 2016 - 9:06 pm

    Blogs ou should be reading… […]Here is a Great Blog You Might Find Interesting that we Encourage You[…]……

  9. #9 by on 9 de dezembro de 2016 - 9:34 pm

    Heckuva good job. I sure appreciate it.

  10. #10 by Google on 9 de dezembro de 2016 - 10:04 pm

    Very couple of internet sites that take place to become in depth below, from our point of view are undoubtedly nicely worth checking out.

  11. #11 by Black Friday Nike Flyknit Lunar 3 on 9 de dezembro de 2016 - 10:43 pm

    Nike Free 4.0 V3 And Best Christmas Deals
    Black Friday Nike Flyknit Lunar 3

  12. #12 by Nike Air Jordan CP3 VI Cheap Sale on 9 de dezembro de 2016 - 10:43 pm

    Nike Air Lunar Hyperdunk Shoes Online
    Nike Air Jordan CP3 VI Cheap Sale

  13. #13 by sofortkredit online student on 9 de dezembro de 2016 - 10:56 pm

    I will be putting this dazzling insight to good use in no time.

  14. #14 by auto insurance quote on 9 de dezembro de 2016 - 11:42 pm

    Great video!!! Very inspirational. If you are as motivated to lose weight as I am, I recommend this free ebook on weight-loss secrets that you can download at dietwithoutpain. com

  15. #15 by on 10 de dezembro de 2016 - 12:31 am

    That’s way the bestest answer so far!

  16. #16 by Nike Air Presto Leopard Shoes Online on 10 de dezembro de 2016 - 12:59 am

    Nike Football Shoes Deals & Sales
    Nike Air Presto Leopard Shoes Online

  17. #17 by Black Friday Nike Air Max 90 Huarache on 10 de dezembro de 2016 - 12:59 am

    Nike Slippers Shoes Online
    Black Friday Nike Air Max 90 Huarache

  18. #18 by Nike Air Max LTD 4 New Discount on 10 de dezembro de 2016 - 1:00 am

    Black Friday Nike Air Max 2014
    Nike Air Max LTD 4 New Discount

  19. #19 by Nike Air Max Sakura Cheap Sale on 10 de dezembro de 2016 - 1:01 am

    Black Friday Nike Mag Glowing
    Nike Air Max Sakura Cheap Sale

  20. #20 by on 10 de dezembro de 2016 - 1:12 am

    Nummer 7; de fans die gekocht zijnNummer 8; de fans die je krijgt via een wederzijdse like (denk aan de talloze LinkedIn groepen, “Like mijn pagina en je krijgt een like terug”

  21. #21 by on 10 de dezembro de 2016 - 2:02 am

    I think you have a great attitude towards training and you will definitely be ready when race day comes! I actually love training plans…cuz I’m a nerd like that! I like logging my progress, but I definitely think all training needs some flexibility.Ice cream remains on the menu throughout training…and extra sleep is a must! A scoop of the good stuff, some extra beauty sleep and I’m a happy lady!

  22. #22 by on 10 de dezembro de 2016 - 3:18 am

    Great thinking! That really breaks the mold!

  23. #23 by kredit wohnwagen on 10 de dezembro de 2016 - 4:03 am

    Thanks for introducing a little rationality into this debate.

  24. #24 by on 10 de dezembro de 2016 - 4:13 am

    It’s great to read something that’s both enjoyable and provides pragmatisdc solutions.

  25. #25 by r movers ltd on 10 de dezembro de 2016 - 4:28 am

    Wonderful story, reckoned we could combine a couple of unrelated data, nonetheless truly worth taking a search, whoa did 1 study about Mid East has got extra problerms at the same time

  26. #26 byühren.html on 10 de dezembro de 2016 - 4:37 am

    I have observed that intelligent real estate agents everywhere you go are starting to warm up to FSBO Promoting. They are seeing that it’s not just placing a sign post in the front property. It’s really about building associations with these dealers who one of these days will become customers. So, whenever you give your time and effort to assisting these dealers go it alone : the “Law connected with Reciprocity” kicks in. Thanks for your blog post.

  27. #27 by bankkredit oder bauspardarlehen on 10 de dezembro de 2016 - 4:48 am

    Hi Trish:I thought I had responded to this. What a troubling nightmare. Robert is such a sweet person. He is so at peace with himself and his faith. You are a great caregiver so how can he not be a great care receiver. Hugs:o)Jane ~ mom to Nicole, 17 yo, VSD, PAH, Eisenmengers, BHJS"You're braver than you believe, and stronger than you seem and smarter than you think."

  28. #28 by privatkredit vertrag vordruck download kostenlos on 10 de dezembro de 2016 - 6:11 am

    I really needed to find this info, thank God!

  29. #29 by Nike Kobe 10 High Deals & Sales on 10 de dezembro de 2016 - 6:38 am

    Nike Kobe 10 Mid EXT 2017
    Nike Kobe 10 High Deals & Sales

  30. #30 by saarland kredit ohne schufaauskunft vermieter on 10 de dezembro de 2016 - 8:01 am

    Most of the things you say is supprisingly accurate and that makes me wonder the reason why I hadn’t looked at this in this light before. Your piece really did switch the light on for me as far as this particular subject goes. Nonetheless there is actually one particular issue I am not really too comfy with and whilst I make an effort to reconcile that with the central theme of your issue, allow me see just what all the rest of the readers have to point out.Nicely done.

  31. #31 by Google on 10 de dezembro de 2016 - 8:31 am

    Here are some of the web-sites we suggest for our visitors.

  32. #32 by on 10 de dezembro de 2016 - 8:41 am

    I didn’t know where to find this info then kaboom it was here.

1 206 207 208
(não será publicado)