Created
June 1, 2014 14:48
-
-
Save banthar/e494273d6174d0c5f3be to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.Graphics; | |
import java.awt.image.BufferedImage; | |
import javax.swing.JFrame; | |
public class PerlinChunks { | |
private static int hash32shift(int key) { | |
key = ~key + (key << 15); | |
key = key ^ (key >>> 12); | |
key = key + (key << 2); | |
key = key ^ (key >>> 4); | |
key = key * 2057; | |
key = key ^ (key >>> 16); | |
return key; | |
} | |
public static float generateWhiteNoise(int x, int y) { | |
return Math.abs(hash32shift(x ^ hash32shift(y))) / (float) Integer.MAX_VALUE; | |
} | |
static double interpolate(double x0, double x1, double alpha) { | |
return x0 * (1 - alpha) + alpha * x1; | |
} | |
static double generateSmoothNoise(double x, double y) { | |
int intX = (int) x; | |
double horizontal_blend = x - intX; | |
int intY = (int) y; | |
double vertical_blend = y - intY; | |
double top = interpolate(generateWhiteNoise(intX, intY), generateWhiteNoise(intX + 1, intY), horizontal_blend); | |
double bottom = interpolate(generateWhiteNoise(intX, intY + 1), generateWhiteNoise(intX + 1, intY + 1), horizontal_blend); | |
return interpolate(top, bottom, vertical_blend); | |
} | |
static double generatePerlinNoise(double x, double y, int octaveCount) { | |
double persistance = 0.5f; | |
double amplitude = 1.0f; | |
double totalAmplitude = 0.0f; | |
double perlinNoise = 0.0; | |
for (int i = 0; i < octaveCount; i++) { | |
amplitude *= persistance; | |
totalAmplitude += amplitude; | |
double m = Math.pow(2, i - 4); | |
perlinNoise += generateSmoothNoise(x * m, y * m) * amplitude; | |
} | |
return perlinNoise / totalAmplitude; | |
} | |
private static int CHUNK_SIZE = 256; | |
static double[][] generateChunk(int chunkX, int chunkY) { | |
double[][] chunk = new double[CHUNK_SIZE][CHUNK_SIZE]; | |
for (int y = 0; y < CHUNK_SIZE; y++) { | |
for (int x = 0; x < CHUNK_SIZE; x++) { | |
chunk[y][x] = generatePerlinNoise(chunkX * CHUNK_SIZE + x, chunkY * CHUNK_SIZE + y, 16); | |
} | |
} | |
return chunk; | |
} | |
private static BufferedImage createChunkImage(int chunkX, int chunkY) { | |
double[][] generateChunk = generateChunk(chunkX, chunkY); | |
BufferedImage image = new BufferedImage(generateChunk.length, generateChunk[0].length, BufferedImage.TYPE_BYTE_GRAY); | |
for (int y = 0; y < image.getHeight(); y++) { | |
for (int x = 0; x < image.getWidth(); x++) { | |
int c = (int) (generateChunk[y][x] * 255); | |
image.setRGB(x, y, 0x010101 * c); | |
} | |
} | |
return image; | |
} | |
public static void main(String[] args) throws Exception { | |
JFrame frame = new JFrame() { | |
@Override | |
public void paint(Graphics g) { | |
super.paint(g); | |
for (int y = 0; y <= getHeight() / CHUNK_SIZE; y++) { | |
for (int x = 0; x <= getWidth() / CHUNK_SIZE; x++) { | |
g.drawImage(createChunkImage(x, y), x * CHUNK_SIZE, y * CHUNK_SIZE, null); | |
} | |
} | |
} | |
}; | |
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
frame.setVisible(true); | |
frame.setSize(800, 600); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment