Created
February 6, 2017 20:32
-
-
Save Limeth/dd8c94eb7e6778b7123d13d64ad2d937 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
public static final int IMAGE_ZOOM = 8; | |
public static final int WIDTH = 64; | |
public static final int HEIGHT = WIDTH; | |
public static final double FREQUENCY = 1.0 / 4; | |
public static final int OCTAVES = 8; | |
public static void main(String[] args) throws IOException { | |
Path path = Paths.get("perlin.png"); | |
BufferedImage image = new BufferedImage(WIDTH * IMAGE_ZOOM, HEIGHT * IMAGE_ZOOM, BufferedImage.TYPE_INT_ARGB); | |
Graphics2D graphics = image.createGraphics(); | |
Const zero = new Const(); | |
Const one = new Const(); | |
Const negativeOne = new Const(); | |
zero.setValue(0); | |
one.setValue(1); | |
negativeOne.setValue(-1); | |
Perlin perlin = new BalancedPerlin(); | |
perlin.setSeed((int) System.currentTimeMillis()); | |
perlin.setFrequency(FREQUENCY); | |
perlin.setNoiseQuality(NoiseQuality.FAST); | |
perlin.setOctaveCount(OCTAVES); | |
Add unitPerlinPartial = new Add(); | |
unitPerlinPartial.setSourceModule(0, perlin); | |
unitPerlinPartial.setSourceModule(1, one); | |
Multiply unitPerlin = new Multiply(); | |
Const half = new Const(); | |
half.setValue(0.5); | |
unitPerlin.setSourceModule(0, unitPerlinPartial); | |
unitPerlin.setSourceModule(1, half); | |
Cache unitPerlinCache = new Cache(); | |
unitPerlinCache.setSourceModule(0, unitPerlin); | |
Const threshold = new Const(); | |
Const negativeThreshold = new Const(); | |
Const leftover = new Const(); | |
threshold.setValue(0.52); | |
negativeThreshold.setValue(-threshold.getValue()); | |
leftover.setValue(1.0 / (1 - threshold.getValue())); | |
Add selectThreshold = new Add(); | |
selectThreshold.setSourceModule(0, unitPerlinCache); | |
selectThreshold.setSourceModule(1, negativeThreshold); | |
Multiply normalizedSelectThreshold = new Multiply(); | |
normalizedSelectThreshold.setSourceModule(0, selectThreshold); | |
normalizedSelectThreshold.setSourceModule(1, leftover); | |
Clamp clamp = new Clamp(); | |
clamp.setLowerBound(0); | |
clamp.setUpperBound(1); | |
clamp.setSourceModule(0, normalizedSelectThreshold); | |
Module module = clamp; | |
for (int y = 0; y < HEIGHT; y++) { | |
for (int x = 0; x < WIDTH; x++) { | |
double value = module.getValue(x, y, 0); | |
System.out.println(value); | |
graphics.setColor(Color.getHSBColor(0, 0, (float) Math.max(0.0, Math.min(1.0, value)))); | |
graphics.fillRect(x * IMAGE_ZOOM, y * IMAGE_ZOOM, IMAGE_ZOOM, IMAGE_ZOOM); | |
} | |
} | |
ImageIO.write(image, "PNG", path.toFile()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment