SHARE
TWEET

Untitled

a guest Jun 16th, 2017 186 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.mygdx.game.Utils;
  2.  
  3. import com.badlogic.gdx.math.Vector2;
  4.  
  5. import com.mygdx.game.Utils.Bezier.*;
  6.  
  7. import java.util.ArrayList;
  8. import java.util.Random;
  9.  
  10. /**
  11.  * Created by Ryuu on 15.06.2017.
  12.  */
  13. public class PerlinNoise {
  14.  
  15.     public static PerlinNoise perlinNoise = new PerlinNoise();
  16.  
  17.     private final int SEED = 1245547;
  18.     private final int OCTAVE = 10;
  19.  
  20.     public ArrayList<Vector2> generateMap(int width, int height) {
  21.  
  22.         float[][] whiteNoise = generateWhiteNoise(width, height);
  23.         float[][] paralaNaiseX = generatePerlinNoise(whiteNoise, OCTAVE);
  24.  
  25.         Random random = new Random();
  26.  
  27.         return convertToVector2(paralaNaiseX, random.nextInt(height));
  28.     }
  29.  
  30.  
  31.     private float interpolate(float x0, float x1, float alpha) {
  32.         return x0 * (1 - alpha) + alpha * x1;
  33.     }
  34.  
  35.     private float[][] generateWhiteNoise(int width, int height) {
  36.         Random random = new Random(SEED);
  37.         float[][] noise = new float[width][height];
  38.  
  39.         for(int i = 0; i < width; i++) {
  40.             for(int j = 0; j < height; j++) {
  41.                 noise[i][j] = (float)random.nextDouble() % 1;
  42.             }
  43.         }
  44.  
  45.         return noise;
  46.     }
  47.  
  48.     private float[][] generateSmoothNoise(float[][] baseNoisem, int octave) {
  49.  
  50.         int width = baseNoisem.length;
  51.         int height = baseNoisem[0].length;
  52.  
  53.         float[][] smoothNoise = new float[width][height];
  54.  
  55.         int samplePeriod = 1 ;
  56.         float sampleFrequency = 1.0f;
  57.  
  58.         for (int i = 0 ; i < width; i++) {
  59.  
  60.             int sample_i0 = (i / samplePeriod) * samplePeriod;
  61.             int sample_i1 = (sample_i0 + samplePeriod) % width;
  62.  
  63.             float horizontal_blend = (i - sample_i0) * sampleFrequency;
  64.  
  65.             for (int j = 0; j < height; j++) {
  66.                 int sample_j0 = (j / samplePeriod) * samplePeriod;
  67.                 int sample_j1 = (sample_j0 + samplePeriod) % height;
  68.                 float vertical_blend = (j - sample_j0) * sampleFrequency;
  69.  
  70.                 float top = interpolate(baseNoisem[sample_i0][sample_j0], baseNoisem[sample_i1][sample_j0], horizontal_blend);
  71.                 float botton = interpolate(baseNoisem[sample_i0][sample_j1], baseNoisem[sample_i1][sample_j1], horizontal_blend);
  72.  
  73.                 smoothNoise[i][j] = interpolate(top, botton, vertical_blend);
  74.             }
  75.         }
  76.  
  77.         return  smoothNoise;
  78.     }
  79.  
  80.     float[][] generatePerlinNoise(float[][] baseNoise, int octaveCount) {
  81.  
  82.         int width = baseNoise.length;
  83.         int height = baseNoise[0].length;
  84.  
  85.         float[][] smoothNoise = new float[octaveCount][]; //an array of 2D arrays containing
  86.  
  87.         float persistance = 0.5f;
  88.  
  89.         for (int i = 0; i < octaveCount; i++) {
  90.             smoothNoise = generateSmoothNoise(baseNoise, i);
  91.         }
  92.  
  93.         float[][] perlinNoise = new float[width][height];
  94.         float aplitude = 1.0f;
  95.         float totalAmplitude = 0.0f;
  96.  
  97.         for(int octave = octaveCount - 1; octave > 0; octave--) {
  98.             aplitude *= persistance;
  99.             totalAmplitude += aplitude;
  100.  
  101.             for (int i = 0; i < width ; i++) {
  102.                 for (int j = 0; j < height; j++) {
  103.                     perlinNoise[i][j] += smoothNoise[i][j] * aplitude;
  104.                 }
  105.             }
  106.         }
  107.  
  108.         for (int i = 0; i < width; i++)
  109.         {
  110.             for (int j = 0; j < height; j++)
  111.             {
  112.                 perlinNoise[i][j] /= totalAmplitude;
  113.             }
  114.         }
  115.  
  116.         return perlinNoise;
  117.     }
  118.  
  119.     private ArrayList<Vector2> convertToVector2(float[][] perlinNoise, int y) {
  120.  
  121.         ArrayList<Vector2> vectorList = new ArrayList<>();
  122.  
  123.         int x = 0;
  124.  
  125.         for(int i = 0 ; i < perlinNoise.length; i++) {
  126.             for(int j = 0; j < perlinNoise.length; j++) {
  127.  
  128. //                float y_ = (float) Math.cos(y * perlinNoise[i][j]) + (float)Math.sin(y * perlinNoise[i][j]);
  129.                 float y_ = y + perlinNoise[i][j];
  130.                 vectorList.add(new Vector2((x++ ) * Constanse.PPM , y_ * Constanse.PPM));
  131.  
  132.             }
  133.         }
  134.  
  135.         System.out.println(vectorList.size());
  136.  
  137.         return vectorList;
  138.     }
  139. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top