Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- mport java.awt.Color;
- import java.awt.Graphics;
- import java.awt.event.KeyEvent;
- import java.awt.event.KeyListener;
- import java.util.Random;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- /* Project Name: Perlin Noise Demo
- * Author: Nick Peluso
- * Last Modified: 8/17/2012 11:59PM
- *
- * Description: Perlin Noise is used in procedural generation. Followed a tutorial on gamedev.tutsplus.com, but adapted it to my liking and display method.
- * Although the perlin noise function is still unclear to me, I am starting to get a better understanding. Fun to play around with, w/h/k control pretty much
- * everything except the color choice - that can be found in genColor. Enjoy.
- */
- public class PerlinNoiseDemo extends JPanel implements KeyListener{
- int w = 1920, h = 1080, k = 7;//Dimensions of arrays/# of octaves("zoom"/"scale")
- Color[][] map;
- public static void main(String[]args){
- PerlinNoiseDemo pnd = new PerlinNoiseDemo();
- pnd.execute();
- }
- public void execute(){
- float[][] whiteNoise = genWhiteNoise(w, h);
- float[][] perlinNoise = genPerlinNoise(whiteNoise, k);
- map = genMap(perlinNoise);
- addKeyListener(this);
- JFrame f = new JFrame();
- f.add(this);
- f.setSize(w,h);
- f.setTitle("Perlin Noise Demo by Nick Peluso");
- f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- f.setVisible(true);
- setFocusable(true);
- }
- public void paint(Graphics g){
- for(int i = 0; i < map.length; i++){
- for(int j = 0; j < map[0].length; j++){
- g.setColor(map[i][j]);
- g.fillRect(i, j, 1, 1);
- }
- }
- g.setColor(Color.white);
- g.drawString("k:"+k, 100, 100);
- }
- public float[][] genWhiteNoise(int width, int height){
- Random random = new Random();
- float[][] noise = new float[width][height];
- for(int i = 0; i < width; i++){
- for(int j = 0; j < height; j++){
- noise[i][j] = random.nextFloat();
- }
- }
- return noise;
- }
- public float[][] genSmoothNoise(float[][] whiteNoise, int octave){
- int width = whiteNoise.length;
- int height = whiteNoise[0].length;
- float[][] smoothNoise = new float[width][height];
- int samplePeriod = (int) Math.pow(2, octave);
- float sampleFrequency = 1.0f / samplePeriod;
- for(int i = 0; i < width; i++){
- //blend the horizontal sampling indices
- int sample_i0 = (i / samplePeriod) * samplePeriod;
- int sample_i1 = (sample_i0 + samplePeriod) % width; //wrap around
- float horizontal_blend = (i - sample_i0) * sampleFrequency;
- for(int j = 0; j < height; j++){
- //blend the vertical sampling indices
- int sample_j0 = (j / samplePeriod) * samplePeriod;
- int sample_j1 = (sample_j0 + samplePeriod) % height; //wrap around
- float vertical_blend = (j - sample_j0) * sampleFrequency;
- //blend the top two corners
- float top = Interpolate(whiteNoise[sample_i0][sample_j0], whiteNoise[sample_i1][sample_j0], horizontal_blend);
- //blend the bottom two corners
- float bottom = Interpolate(whiteNoise[sample_i0][sample_j1], whiteNoise[sample_i1][sample_j1], horizontal_blend);
- //final blend
- smoothNoise[i][j] = Interpolate(top, bottom, vertical_blend);
- }
- }
- return smoothNoise;
- }
- public float[][] genPerlinNoise(float[][] whiteNoise, int octaveCount){
- int width = whiteNoise.length;
- int height = whiteNoise[0].length;
- float smoothNoise[][][] = new float[octaveCount][][];
- float persistance = 0.5f;
- //generate smooth noise
- for(int k = 0; k < octaveCount; k++){
- smoothNoise[k] = genSmoothNoise(whiteNoise, k);
- }
- float[][] perlinNoise = new float[width][height];
- float amplitude = 1.0f;
- float totalAmplitude = 0.0f;
- //blend noise together
- for(int octave = octaveCount-1; octave >= 0; octave--){
- amplitude *= persistance;
- totalAmplitude += amplitude;
- for(int i = 0; i < width; i++){
- for(int j = 0; j < height; j++){
- perlinNoise[i][j] += smoothNoise[octave][i][j] * amplitude;
- }
- }
- }
- //normalization
- for(int i = 0; i < width; i++){
- for(int j = 0; j < height; j++){
- perlinNoise[i][j] /= totalAmplitude;
- }
- }
- return perlinNoise;
- }
- public Color[][] genMap(float[][] perlinNoise){
- int width = perlinNoise.length;
- int height = perlinNoise[0].length;
- Color[][] colorMap = new Color[width][height];
- for(int i = 0; i < width; i++){
- for(int j = 0; j < height; j++){
- float x = perlinNoise[i][j];
- if(x > .9){ colorMap[i][j] = new Color(255,255,255);}
- else if(x > .85){colorMap[i][j] = new Color(200,200,200);}
- else if(x > .8){ colorMap[i][j] = new Color(173,173,173);}
- else if(x > .75){colorMap[i][j] = new Color(130,130,130);}
- else if(x > .7){ colorMap[i][j] = new Color(191,124,47);}
- else if(x > .6){ colorMap[i][j] = new Color(44,84,0);}
- else if(x > .5){ colorMap[i][j] = new Color(17,130,0);}
- else if(x > .4){ colorMap[i][j] = new Color(0,188,88);}
- else if(x > .3){ colorMap[i][j] = new Color(191,124,47);}
- else if(x > .2){ colorMap[i][j] = new Color(229,202,0);}
- else if(x > .1){ colorMap[i][j] = new Color(0,135,204);}
- else{ colorMap[i][j] = new Color(0,78,142);}
- }
- }
- return colorMap;
- }
- float Interpolate(float x0, float x1, float alpha){
- return x0 * (1 - alpha) + alpha * x1;
- }
- public void keyPressed(KeyEvent e){
- if(e.getKeyCode() == KeyEvent.VK_UP){
- k++;
- map = genMap(genPerlinNoise(genWhiteNoise(w, h), k));
- repaint();
- }
- if(e.getKeyCode() == KeyEvent.VK_DOWN){
- k--;
- map = genMap(genPerlinNoise(genWhiteNoise(w, h), k));
- repaint();
- }
- System.out.println(""+k);
- }
- public void keyReleased(KeyEvent e){}
- public void keyTyped(KeyEvent e){}
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement