• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Luthon · Sep 27, 2015 at 05:11 PM · c#terraingenerationperlin noise

Can anyone explain this code?

 using UnityEngine;
 using System.Collections;
 
 public class PerlinNoise
 {
     const int B = 256;
     int[] m_perm = new int[B+B];
 
     public PerlinNoise(int seed)
     {
         UnityEngine.Random.seed = seed;
 
         int i, j, k;
         for (i = 0 ; i < B ; i++) 
         {
             m_perm[i] = i;
         }
 
         while (--i != 0) 
         {
             k = m_perm[i];
             j = UnityEngine.Random.Range(0, B);
             m_perm[i] = m_perm[j];
             m_perm[j] = k;
         }
     
         for (i = 0 ; i < B; i++) 
         {
             m_perm[B + i] = m_perm[i];
         }
         
     }
     
     float FADE(float t) { return t * t * t * ( t * ( t * 6.0f - 15.0f ) + 10.0f ); }
     
     float LERP(float t, float a, float b) { return (a) + (t)*((b)-(a)); }
     
     float GRAD1(int hash, float x ) 
     {
         //This method uses the mod operator which is slower 
         //than bitwise operations but is included out of interest
 //        int h = hash % 16;
 //        float grad = 1.0f + (h % 8);
 //        if((h%8) < 4) grad = -grad;
 //        return ( grad * x );
         
         int h = hash & 15;
         float grad = 1.0f + (h & 7);
         if ((h&8) != 0) grad = -grad;
         return ( grad * x );
     }
     
     float GRAD2(int hash, float x, float y)
     {
         //This method uses the mod operator which is slower 
         //than bitwise operations but is included out of interest
 //        int h = hash % 16;
 //        float u = h<4 ? x : y;
 //        float v = h<4 ? y : x;
 //        int hn = h%2;
 //        int hm = (h/2)%2;
 //        return ((hn != 0) ? -u : u) + ((hm != 0) ? -2.0f*v : 2.0f*v);
         
         int h = hash & 7;
         float u = h<4 ? x : y;
         float v = h<4 ? y : x;
         return (((h&1) != 0)? -u : u) + (((h&2) != 0) ? -2.0f*v : 2.0f*v);
     }
     
     
     float GRAD3(int hash, float x, float y , float z)
     {
         //This method uses the mod operator which is slower 
         //than bitwise operations but is included out of interest
 //        int h = hash % 16;
 //        float u = (h<8) ? x : y;
 //        float v = (h<4) ? y : (h==12||h==14) ? x : z;
 //        int hn = h%2;
 //        int hm = (h/2)%2;
 //        return ((hn != 0) ? -u : u) + ((hm != 0) ? -v : v);
         
         int h = hash & 15;
         float u = h<8 ? x : y;
         float v = (h<4) ? y : (h==12 || h==14) ? x : z;
         return (((h&1) != 0)? -u : u) + (((h&2) != 0)? -v : v);
     }
     
     float Noise1D( float x )
     {
         //returns a noise value between -0.5 and 0.5
         int ix0, ix1;
         float fx0, fx1;
         float s, n0, n1;
     
         ix0 = (int)Mathf.Floor(x);     // Integer part of x
         fx0 = x - ix0;           // Fractional part of x
         fx1 = fx0 - 1.0f;
         ix1 = ( ix0+1 ) & 0xff;
         ix0 = ix0 & 0xff;        // Wrap to 0..255
         
         s = FADE(fx0);
     
         n0 = GRAD1(m_perm[ix0], fx0);
         n1 = GRAD1(m_perm[ix1], fx1);
         return 0.188f * LERP( s, n0, n1);
     }
     
     float Noise2D( float x, float y )
     {
         //returns a noise value between -0.75 and 0.75
         int ix0, iy0, ix1, iy1;
         float fx0, fy0, fx1, fy1, s, t, nx0, nx1, n0, n1;
     
         ix0 = (int)Mathf.Floor(x);     // Integer part of x
         iy0 = (int)Mathf.Floor(y);     // Integer part of y
         fx0 = x - ix0;            // Fractional part of x
         fy0 = y - iy0;            // Fractional part of y
         fx1 = fx0 - 1.0f;
         fy1 = fy0 - 1.0f;
         ix1 = (ix0 + 1) & 0xff; // Wrap to 0..255
         iy1 = (iy0 + 1) & 0xff;
         ix0 = ix0 & 0xff;
         iy0 = iy0 & 0xff;
         
         t = FADE( fy0 );
         s = FADE( fx0 );
     
         nx0 = GRAD2(m_perm[ix0 + m_perm[iy0]], fx0, fy0);
         nx1 = GRAD2(m_perm[ix0 + m_perm[iy1]], fx0, fy1);
         
         n0 = LERP( t, nx0, nx1 );
     
         nx0 = GRAD2(m_perm[ix1 + m_perm[iy0]], fx1, fy0);
         nx1 = GRAD2(m_perm[ix1 + m_perm[iy1]], fx1, fy1);
         
         n1 = LERP(t, nx0, nx1);
     
         return 0.507f * LERP( s, n0, n1 );
     }
     
     float Noise3D( float x, float y, float z )
     {
         //returns a noise value between -1.5 and 1.5
         int ix0, iy0, ix1, iy1, iz0, iz1;
         float fx0, fy0, fz0, fx1, fy1, fz1;
         float s, t, r;
         float nxy0, nxy1, nx0, nx1, n0, n1;
     
         ix0 = (int)Mathf.Floor( x ); // Integer part of x
         iy0 = (int)Mathf.Floor( y ); // Integer part of y
         iz0 = (int)Mathf.Floor( z ); // Integer part of z
         fx0 = x - ix0;        // Fractional part of x
         fy0 = y - iy0;        // Fractional part of y
         fz0 = z - iz0;        // Fractional part of z
         fx1 = fx0 - 1.0f;
         fy1 = fy0 - 1.0f;
         fz1 = fz0 - 1.0f;
         ix1 = ( ix0 + 1 ) & 0xff; // Wrap to 0..255
         iy1 = ( iy0 + 1 ) & 0xff;
         iz1 = ( iz0 + 1 ) & 0xff;
         ix0 = ix0 & 0xff;
         iy0 = iy0 & 0xff;
         iz0 = iz0 & 0xff;
         
         r = FADE( fz0 );
         t = FADE( fy0 );
         s = FADE( fx0 );
     
         nxy0 = GRAD3(m_perm[ix0 + m_perm[iy0 + m_perm[iz0]]], fx0, fy0, fz0);
         nxy1 = GRAD3(m_perm[ix0 + m_perm[iy0 + m_perm[iz1]]], fx0, fy0, fz1);
         nx0 = LERP( r, nxy0, nxy1 );
     
         nxy0 = GRAD3(m_perm[ix0 + m_perm[iy1 + m_perm[iz0]]], fx0, fy1, fz0);
         nxy1 = GRAD3(m_perm[ix0 + m_perm[iy1 + m_perm[iz1]]], fx0, fy1, fz1);
         nx1 = LERP( r, nxy0, nxy1 );
     
         n0 = LERP( t, nx0, nx1 );
     
         nxy0 = GRAD3(m_perm[ix1 + m_perm[iy0 + m_perm[iz0]]], fx1, fy0, fz0);
         nxy1 = GRAD3(m_perm[ix1 + m_perm[iy0 + m_perm[iz1]]], fx1, fy0, fz1);
         nx0 = LERP( r, nxy0, nxy1 );
     
         nxy0 = GRAD3(m_perm[ix1 + m_perm[iy1 + m_perm[iz0]]], fx1, fy1, fz0);
            nxy1 = GRAD3(m_perm[ix1 + m_perm[iy1 + m_perm[iz1]]], fx1, fy1, fz1);
         nx1 = LERP( r, nxy0, nxy1 );
     
         n1 = LERP( t, nx0, nx1 );
         
         return 0.936f * LERP( s, n0, n1 );
     }
     
     public float FractalNoise1D(float x, int octNum, float frq, float amp)
     {
         float gain = 1.0f;
         float sum = 0.0f;
     
         for(int i = 0; i < octNum; i++)
         {
             sum +=  Noise1D(x*gain/frq) * amp/gain;
             gain *= 2.0f;
         }
         return sum;
     }
     
     public float FractalNoise2D(float x, float y, int octNum, float frq, float amp)
     {
         float gain = 1.0f;
         float sum = 0.0f;
         
         for(int i = 0; i < octNum; i++)
         {
             sum += Noise2D(x*gain/frq, y*gain/frq) * amp/gain;
             gain *= 2.0f;
         }
         return sum;
     }
     
     public float FractalNoise3D(float x, float y, float z, int octNum, float frq, float amp)
     {
         float gain = 1.0f;
         float sum = 0.0f;
     
         for(int i = 0; i < octNum; i++)
         {
             sum +=  Noise3D(x*gain/frq, y*gain/frq, z*gain/frq) * amp/gain;
             gain *= 2.0f;
         }
         return sum;
     }
 
 }

I found this while looking up tutorials for procedural terrain generation, but it comes with no explanation . Can someone please explain how this script works?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Graham-Dunnett · Sep 27, 2015 at 05:12 PM

It's Perlin noise. See:

https://en.wikipedia.org/wiki/Perlin_noise

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Distribute terrain in zones 3 Answers

2D Efficient Realtime Terrain Generation 0 Answers

Random Island generation using Perlin Noise ? 1 Answer

Multiple Cars not working 1 Answer

Procedural Island Terrain Generation 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges