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 legit work from home jobs no fees on 21 de outubro de 2016 - 11:29 pm

    Here is a great Blog You may Uncover Intriguing that we Encourage You

  2. #2 by cash advance in pa on 22 de outubro de 2016 - 12:18 am

    Really nice design and style and fantastic subject matter, absolutely nothing else we want : D.

  3. #3 by buy paintings on 22 de outubro de 2016 - 4:09 am

    I will gear this review towards Two styles of These: latest Zune home owners who are considering an update, and people in america striving towards come to a decision in between a Zune and an iPod. (There are other players worthy of looking at out there, which include the Sony Walkman X, yet I anticipate this features your self sufficient facts toward make an conscious option of the Zune vs players other than the iPod line as very well.)

  4. #4 by The Ranch at Prescott on 22 de outubro de 2016 - 6:37 am

    I’ve been absent for a while, but now I remember why I used to love this blog. Thank you, I will try and check back more frequently. How frequently you update your website?

  5. #5 by smart balance wheel on 22 de outubro de 2016 - 8:12 am

    Rehab not to mention Damage CompensationFor harmed muscle tissue, it is very important you can keep them accommodating, while not imposing improved deterioration.

  6. #6 by singles chat room on 22 de outubro de 2016 - 12:38 pm

    Among me and my partner we’ve owned even further MP3 gamers over the many years than I can depend, which include Sansas, iRivers, iPods (classic & touch), the Ibiza Rhapsody, and so on. But, the past handful of many years I’ve resolved down toward a person line of avid gamers. Why? Given that I was joyful toward examine how well-designed and entertaining in the direction of retain the services of the underappreciated (and widely mocked) Zunes are.

  7. #7 by latest news on healthcare on 22 de outubro de 2016 - 12:38 pm

    Sorry for the huge critique, still I am seriously loving the contemporary Zune, and count on this, as well as the good quality assessments some other human beings comprise prepared, will aid by yourself choose if it’s the straight choice for on your own.

  8. #8 by Travail a domicile Maroc on 22 de outubro de 2016 - 12:59 pm

    Thank you for your post.Really thank you! Really Great.

  9. #9 by bCyAtZtSprXIBOZiy on 22 de outubro de 2016 - 12:59 pm

    I loved your blog article.Thanks Again. Cool.

  10. #10 by Labouring Jobs Birmingham on 22 de outubro de 2016 - 1:06 pm

    Thanks for helping out, wonderful information.

  11. #11 by chicas paisas on 22 de outubro de 2016 - 1:21 pm

    You have mentioned very interesting points! ps nice site.

  12. #12 by MLM Maroc on 22 de outubro de 2016 - 1:22 pm

    Thank you ever so for you post. Want more.

  13. #13 by on 22 de outubro de 2016 - 1:41 pm

    At last some rationality in our little debate.

  14. #14 by kfz versicherung a klasse on 22 de outubro de 2016 - 1:44 pm

    The next time I read a weblog, I hope that it doesnt disappoint me as a lot as this one. I mean, I do know it was my option to learn, but I actually thought youd have something interesting to say. All I hear is a bunch of whining about one thing that you would fix in case you werent too busy looking for attention.

  15. #15 by delhi call girl service on 22 de outubro de 2016 - 2:11 pm

    hello there and thank you for your info – I have certainly picked up anything new from right here. I did however expertise a few technical issues using this site, as I experienced to reload the website a lot of times previous to I could get it to load correctly. I had been wondering if your web hosting is OK? Not that I am complaining, but sluggish loading instances times will sometimes affect your placement in google and can damage your high-quality score if advertising and marketing with Adwords. Anyway I’m adding this RSS to my email and can look out for a lot more of your respective intriguing content. Ensure that you update this again very soon..

  16. #16 by on 22 de outubro de 2016 - 2:53 pm

    Superb information here, ol’e chap; keep burning the midnight oil.

  17. #17 by on 22 de outubro de 2016 - 3:56 pm

    With the bases loaded you struck us out with that answer!

  18. #18 by on 22 de outubro de 2016 - 4:01 pm

    This posting knocked my socks off

  19. #19 by ratenkredit selbstaendig on 22 de outubro de 2016 - 4:26 pm

    I’m not sure I get your post, Ryan. You seem to be inferring a dichotomy between land development and tree planting. This is false. When urban tree advocates promote increased tree cover, they are almost entirely talking about trees complementing development (e.g. street trees and parks), not supplanting it.

  20. #20 by on 22 de outubro de 2016 - 7:19 pm

    I’m impressed by your writing. Are you a professional or just very knowledgeable?

  21. #21 by pr application on 22 de outubro de 2016 - 7:26 pm

    Thank you!

  22. #22 by anal sex on 22 de outubro de 2016 - 7:27 pm

    we came across a cool website that you simply may well get pleasure from. Take a look in case you want

  23. #23 by fall furnace service on 22 de outubro de 2016 - 7:32 pm

    When I originally commented I clicked the -Notify me when new comments are added- checkbox and now each time a comment is added I get four emails with the same comment. Is there any way you can remove me from that service? Thanks!

  24. #24 by on 22 de outubro de 2016 - 8:02 pm

    some people have accused the pastors of not being orthodox) the sacraments do them little good.Bp. Morlino’s interdict communicates the erroneous notion that the sacraments exist for the clergy and not for all the baptized.What? He a) hasn’t interdicted anyone and b) so you’re opposed to excommunication and (nota bene: personal) interdict generally on principle?

  25. #25 by on 22 de outubro de 2016 - 8:10 pm

    This article is a home run, pure and simple!

  26. #26 byünstigste-kreditkarte-schweiz.html on 22 de outubro de 2016 - 8:10 pm

    I’m more than happy to discover this page. I wanted to thank you for your time for this fantastic read!! I definitely appreciated every part of it and I have you book-marked to see new information in your blog.

  27. #27 by permanent resident on 22 de outubro de 2016 - 8:48 pm

    Thank you!

  28. #28 by porn on 22 de outubro de 2016 - 11:07 pm

    usually posts some quite exciting stuff like this. If you are new to this site

  29. #29 by auto insurance quotes on 22 de outubro de 2016 - 11:12 pm

    You Sir/Madam are the enemy of confusion everywhere!

  30. #30 by on 23 de outubro de 2016 - 1:44 am

    Wow – you are so talented! The coasters are absolutely gorgeous! I have such a hard time picking out presents for my finance since like tons of men – every time he wants something he just goes out and buys it! I think I am going to plan a little weekend getaway and buy little presents that hint toward it….

  31. #31 by on 23 de outubro de 2016 - 2:38 am

    Your post captures the issue perfectly!

  32. #32 by Donnie Mikes on 23 de outubro de 2016 - 5:03 am

    health & beauty

  33. #33 by Labouring Jobs Birmingham on 23 de outubro de 2016 - 5:18 am

    Hello.This post was really motivating, particularly since I was looking for thoughts on this topic last couple of days.

  34. #34 by Tatiana Wene on 23 de outubro de 2016 - 5:26 am

    Buy Facebook likes

  35. #35 by on 23 de outubro de 2016 - 5:43 am

    Just the type of insight we need to fire up the debate.

  36. #36 by student kreditkarte dkb on 23 de outubro de 2016 - 6:57 am

    Sorry, ik weet verder niets af van Braat in Soerabaja, behalve van het bombardement >>Kenpeitai. Braat in Delft is al lang weg, ik weet ook niet waar de archieven zijn, informeren bij de Kamer van Koophandel? Volgens mij had Braat geen hoogovens, nergens in Indië.

  37. #37 by on 23 de outubro de 2016 - 7:00 am

    Perfect answer! That really gets to the heart of it!

1 149 150 151
(não será publicado)