Guest User

Untitled

a guest
May 16th, 2018
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.10 KB | None | 0 0
  1. import java.util.Random;
  2.  
  3. /**
  4.  * A  well documented Java implementation of Perlin Noise.
  5.  *
  6.  * @author Ramon Millsteed
  7.  */
  8. public class PerlinNoise {
  9.  
  10.     /** The frequencies (2^i) for each octave. */
  11.     private static int[] frequencies = new int[32];
  12.  
  13.     /** The permutations for the random gradient generator. */
  14.     private static int[] permutations = new int[512];
  15.  
  16.     /**
  17.      * Initialises the static frequency exponentials.
  18.      */
  19.     static {
  20.         for (int i = 0; i < frequencies.length; i++) {
  21.             frequencies[i] = (int) Math.pow(2, i);
  22.         }
  23.     }
  24.  
  25.     /**
  26.      * Instantiates a new {@code PerlinNoise}.
  27.      *
  28.      * @param seed
  29.      *      The random number seed
  30.      */
  31.     public PerlinNoise(int seed) {
  32.         Random random = new Random(seed);
  33.         for (int i = 0; i < permutations.length / 2; i++) {
  34.             permutations[256 + i] = permutations[i] = random.nextInt(256);
  35.         }
  36.     }
  37.  
  38.     /**
  39.      * Returns a height value in a 3D plane using Perlin Noise with octave repetition.
  40.      *
  41.      * @param x
  42.      *      The x coordinate
  43.      * @param y
  44.      *      The y coordinate
  45.      * @param z
  46.      *      The z coordinate
  47.      * @param octaves
  48.      *      The number of successive octaves
  49.      * @return The height value
  50.      */
  51.     public float noise(float x, float y, float z, int octaves) {
  52.         float height = 0.0f;
  53.         for (int octave = 1; octave <= octaves; octave++) {
  54.             int frequency = frequencies[octave];
  55.             height += noise(x * frequency, y * frequency, z * frequency) / frequency;
  56.         }
  57.         return height;
  58.     }
  59.  
  60.     /**
  61.      * Returns a height value in a 3D plane using Perlin Noise without octave repetition.
  62.      *
  63.      * @param x
  64.      *      The x coordinate
  65.      * @param y
  66.      *      The y coordinate
  67.      * @param z
  68.      *      The z coordinate
  69.      * @return The height value
  70.      */
  71.     public float noise(float x, float y, float z) {
  72.         float fx = (float) Math.floor(x);
  73.         float fy = (float) Math.floor(y);
  74.         float fz = (float) Math.floor(z);
  75.  
  76.         int gx = (int) fx & 0xFF;
  77.         int gy = (int) fy & 0xFF;
  78.         int gz = (int) fz & 0xFF;
  79.  
  80.         float u = fade(x -= fx);
  81.         float v = fade(y -= fy);
  82.         float w = fade(z -= fz);
  83.  
  84.         int a0 = permutations[gx + 0] + gy;
  85.         int b0 = permutations[gx + 1] + gy;
  86.         int aa = permutations[a0 + 0] + gz;
  87.         int ab = permutations[a0 + 1] + gz;
  88.         int ba = permutations[b0 + 0] + gz;
  89.         int bb = permutations[b0 + 1] + gz;
  90.  
  91.         float x2y2z2 = gradient(permutations[bb + 1], x - 1, y - 1, z - 1);
  92.         float x1y2z2 = gradient(permutations[ab + 1], x - 0, y - 1, z - 1);
  93.         float x2y1z2 = gradient(permutations[ba + 1], x - 1, y - 0, z - 1);
  94.         float x1y1z2 = gradient(permutations[aa + 1], x - 0, y - 0, z - 1);
  95.         float x2y2z1 = gradient(permutations[bb + 0], x - 1, y - 1, z - 0);
  96.         float x1y2z1 = gradient(permutations[ab + 0], x - 0, y - 1, z - 0);
  97.         float x2y1z1 = gradient(permutations[ba + 0], x - 1, y - 0, z - 0);
  98.         float x1y1z1 = gradient(permutations[aa + 0], x - 0, y - 0, z - 0);
  99.  
  100.         float uInterpolatedY2Z2 = linearInterpolate(x1y2z2, x2y2z2, u);
  101.         float uInterpolatedY1Z2 = linearInterpolate(x1y1z2, x2y1z2, u);
  102.         float uInterpolatedY2Z1 = linearInterpolate(x1y2z1, x2y2z1, u);
  103.         float uInterpolatedY1Z1 = linearInterpolate(x1y1z1, x2y1z1, u);
  104.  
  105.         float vInterpolatedZ1 = linearInterpolate(uInterpolatedY1Z1, uInterpolatedY2Z1, v);
  106.         float vInterpolatedZ2 = linearInterpolate(uInterpolatedY1Z2, uInterpolatedY2Z2, v);
  107.  
  108.         return linearInterpolate(vInterpolatedZ1, vInterpolatedZ2, w);
  109.     }
  110.  
  111.     /**
  112.      * Finds the fade curve for a specified coordinate.
  113.      *
  114.      * @param coordinate
  115.      *      The decimal component of the coordinate
  116.      * @return The fade curve
  117.      */
  118.     private float fade(float coordinate) {
  119.         return (float) (Math.pow(coordinate, 3) * (coordinate * (coordinate * 6.0f - 15.0f) + 10.0f));
  120.     }
  121.  
  122.     /**
  123.      * Interpolates a value between two functions using a fade curve.
  124.      *
  125.      * @param a
  126.      *      The first function
  127.      * @param b
  128.      *      The second function
  129.      * @param fadeCurve
  130.      *      The coordinate fade curve
  131.      * @return The interpolated value
  132.      */
  133.     private float linearInterpolate(float a, float b, float fadeCurve) {
  134.         return a + fadeCurve * (b - a);
  135.     }
  136.  
  137.     /**
  138.      * Generates a random gradient using coordinates from a 3D plane.
  139.      *
  140.      * @param hash
  141.      *      The random hash permutation generated with a seed
  142.      * @param x
  143.      *      The x coordinate
  144.      * @param y
  145.      *      The y coordinate
  146.      * @param z
  147.      *      The z coordinate
  148.      * @return The random gradient
  149.      */
  150.     private float gradient(int hash, float x, float y, float z) {
  151.         hash &= 15;
  152.         float u = (hash < 8) ? x : y;
  153.         float v = (hash < 4) ? y : ((hash == 12 || hash == 14) ? x : z);
  154.         return ((hash & 1) == 0 ? u : -u) + ((hash & 2) == 0 ? v : -v);
  155.     }
  156.    
  157. }
Add Comment
Please, Sign In to add comment