Jump to content

[1.7.10 | SOLVED-ish] Noise/Terrain Generation


TLHPoE

Recommended Posts

Ok, I've been messing around with the perlin noise class and was able to create this beautiful piece of art:

rd1jKXT.png

 

Here's the code I used to generate it:

package terrarium.util;

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;

import javax.imageio.ImageIO;

import net.minecraft.world.gen.NoiseGeneratorOctaves;
import net.minecraft.world.gen.NoiseGeneratorPerlin;

public class NoiseTesting {
public static void init() {
	NoiseGeneratorPerlin noise = new NoiseGeneratorPerlin(new Random(1L), 1);

	double[][] noiseArray = new double[100][100];

	for(int i = 0; i < 100; i++) {
		for(int j = 0; j < 100; j++) {
			noiseArray[i][j] = noise.func_151601_a(i, j);
		}
	}

	BufferedImage img = new BufferedImage(100, 100, BufferedImage.TYPE_INT_ARGB);

	for(int i = 0; i < 100; i++) {
		for(int j = 0; j < 100; j++) {
			int n2 = (int) (255 * ((1D + noiseArray[i][j]) / 2));

			img.setRGB(i, j, new Color(n2, n2, n2).getRGB());
		}
	}

	File file = new File("./image.png");

	try {
		ImageIO.write(img, "png", file);
	} catch(IOException e) {
		e.printStackTrace();
	}
}
}

 

 

One problem though, you probably notice that it doesn't seem as smooth. My question is, how does Minecraft interpolate the original noise to a state that is satisfactory? I know how to interpolate a 1 dimensional array, but not a 2 dimensional array.

Kain

Link to comment
Share on other sites

Since I'm all alone in my world of noise, I might as well keep this thread as a journal to help other people.

 

I have generated this beautiful image here:

sNASQZm.png

 

Using this code here:

package terrarium.util;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;

import javax.imageio.ImageIO;

import net.minecraft.world.gen.NoiseGeneratorPerlin;

public class NoiseTesting {
public static void init() {
	Random random = new Random(1L);

	double[][] noise1 = generateNoise(random, 1, 0, 0, 25, 25);
	double[][] noise2 = generateNoise(random, 1, 0, 0, 50, 50);
	double[][] noise3 = generateNoise(random, 1, 0, 0, 100, 100);
	double[][] noise4 = generateNoise(random, 1, 0, 0, 200, 200);

	BufferedImage img1 = noiseToImage(noise1, 25, 25);
	BufferedImage img2 = noiseToImage(noise2, 50, 50);
	BufferedImage img3 = noiseToImage(noise3, 100, 100);
	BufferedImage img4 = noiseToImage(noise4, 200, 200);

	setAlpha(img4, 64);
	setAlpha(img3, 64);
	setAlpha(img2, 64);
	setAlpha(img1, 64);

	Graphics2D g = (Graphics2D) img4.getGraphics();

	g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
	g.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
	g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);

	g.drawImage(img3, 0, 0, 200, 200, null);
	g.drawImage(img2, 0, 0, 200, 200, null);
	g.drawImage(img1, 0, 0, 200, 200, null);

	g.dispose();

	setAlpha(img4, 255);

	try {
		saveImage(img1, "./", "img1");
		saveImage(img2, "./", "img2");
		saveImage(img3, "./", "img3");
		saveImage(img4, "./", "img4");
	} catch(IOException e) {
		e.printStackTrace();
	}
}

public static double[][] generateNoise(Random random, int idk, int x, int y, int width, int height) {
	NoiseGeneratorPerlin noiseGenerator = new NoiseGeneratorPerlin(random, idk);
	double[][] noiseArray = new double[width][height];

	for(int i = 0; i < width; i++) {
		for(int j = 0; j < height; j++) {
			noiseArray[i][j] = noiseGenerator.func_151601_a(x + i, y + j);
		}
	}

	return noiseArray;
}

public static BufferedImage noiseToImage(double[][] noise, int width, int height) {
	BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

	for(int i = 0; i < width; i++) {
		for(int j = 0; j < height; j++) {
			int hue = (int) (255 * ((1D + noise[i][j]) / 2));

			img.setRGB(i, j, new Color(hue, hue, hue).getRGB());
		}
	}

	return img;
}

public static void saveImage(BufferedImage img, String directory, String name) throws IOException {
	ImageIO.write(img, "png", new File(directory + name + ".png"));
}

public static void setAlpha(BufferedImage img, int alpha) {
	for(int i = 0; i < img.getWidth(); i++) {
		for(int j = 0; j < img.getHeight(); j++) {
			Color currentColor = new Color(img.getRGB(i, j));
			Color newColor = new Color(currentColor.getRed(), currentColor.getGreen(), currentColor.getBlue(), alpha);

			img.setRGB(i, j, newColor.getRGB());
		}
	}
}
}

 

Please don't assassinate me because I used Graphics2D and BufferedImages to mix the noises, I have no clue on how to interpolate a 2 dimensional array.

Kain

Link to comment
Share on other sites

Ok, I attempted an interpolation method, and this what I got:

package terrarium.util;

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;

import javax.imageio.ImageIO;

import net.minecraft.world.gen.NoiseGeneratorPerlin;

public class NoiseTesting {
public static void init() {
	Random random = new Random(1L);

	double[][] noise1 = generateNoise(random, 1, 0, 0, 25, 25);
	double[][] noise2 = generateNoise(random, 1, 0, 0, 50, 50);
	double[][] noise3 = generateNoise(random, 1, 0, 0, 100, 100);
	double[][] noise4 = generateNoise(random, 1, 0, 0, 200, 200);
	double[][] noise5 = generateNoise(random, 1, 0, 0, 400, 400);

	noise4 = interpolateBilinear(noise4, 200, 200);

	noise3 = interpolateBilinear(noise3, 100, 100);
	noise3 = interpolateBilinear(noise3, 200, 200);

	noise2 = interpolateBilinear(noise2, 50, 50);
	noise2 = interpolateBilinear(noise2, 100, 100);
	noise2 = interpolateBilinear(noise2, 200, 200);

	noise1 = interpolateBilinear(noise1, 25, 25);
	noise1 = interpolateBilinear(noise1, 50, 50);
	noise1 = interpolateBilinear(noise1, 100, 100);
	noise1 = interpolateBilinear(noise1, 200, 200);

	try {
		saveImage(noiseToImage(noise5, 400, 400), "./", "noise5");
	} catch(IOException e) {
		e.printStackTrace();
	}

	noise5 = combineArrays(noise5, noise4, 400, 400);

	try {
		saveImage(noiseToImage(noise5, 400, 400), "./", "noise5+4");
	} catch(IOException e) {
		e.printStackTrace();
	}

	noise5 = combineArrays(noise5, noise3, 400, 400);

	try {
		saveImage(noiseToImage(noise5, 400, 400), "./", "noise5+4+3");
	} catch(IOException e) {
		e.printStackTrace();
	}

	noise5 = combineArrays(noise5, noise2, 400, 400);

	try {
		saveImage(noiseToImage(noise5, 400, 400), "./", "noise5+4+3+2");
	} catch(IOException e) {
		e.printStackTrace();
	}

	noise5 = combineArrays(noise5, noise1, 400, 400);

	try {
		saveImage(noiseToImage(noise5, 400, 400), "./", "noise5+4+3+2+1");
	} catch(IOException e) {
		e.printStackTrace();
	}
}

public static double[][] generateNoise(Random random, int octaves, int x, int y, int width, int height) {
	NoiseGeneratorPerlin noiseGenerator = new NoiseGeneratorPerlin(random, octaves);
	double[][] noiseArray = new double[width][height];

	for(int i = 0; i < width; i++) {
		for(int j = 0; j < height; j++) {
			noiseArray[i][j] = noiseGenerator.func_151601_a(x + i, y + j);
		}
	}

	return noiseArray;
}

public static BufferedImage noiseToImage(double[][] noise, int width, int height) {
	BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

	for(int i = 0; i < width; i++) {
		for(int j = 0; j < height; j++) {
			int hue = (int) (255 * ((1D + noise[i][j]) / 2));

			img.setRGB(i, j, new Color(hue, hue, hue).getRGB());
		}
	}

	return img;
}

public static void saveImage(BufferedImage img, String directory, String name) throws IOException {
	ImageIO.write(img, "png", new File(directory + name + ".png"));
}

public static void setAlpha(BufferedImage img, int alpha) {
	for(int i = 0; i < img.getWidth(); i++) {
		for(int j = 0; j < img.getHeight(); j++) {
			Color currentColor = new Color(img.getRGB(i, j));
			Color newColor = new Color(currentColor.getRed(), currentColor.getGreen(), currentColor.getBlue(), alpha);

			img.setRGB(i, j, newColor.getRGB());
		}
	}
}

public static double[][] interpolateBilinear(double[][] array, int width, int height) {
	double[][] res = new double[width * 2][height * 2];

	for(int i = 0; i < width; i++) {
		for(int j = 0; j < height; j++) {
			res[i * 2][j * 2] = array[i][j];
		}
	}

	for(int i = 0; i < width * 2; i++) {
		for(int j = 0; j < height * 2; j++) {
			if(i == 0 || i % 2 == 0) {
				if((j + 1) % 2 == 0) {
					if(j != (width * 2) - 1) {
						res[i][j] = bilinearInterpolatePoints(res[i][j - 1], res[i][j + 1]);
					} else {
						res[i][j] = res[i][j - 2];
					}
				}
			} else {
				if(i != (height * 2) - 1) {
					res[i][j] = bilinearInterpolatePoints(res[i - 1][j], res[i + 1][j]);
				} else {
					res[i][j] = res[i - 2][j];
				}
			}
		}
	}

	return res;
}

public static double[][] combineArrays(double[][] array1, double[][] array2, int width, int height) {
	double[][] res = new double[width][height];

	for(int i = 0; i < width; i++) {
		for(int j = 0; j < height; j++) {
			res[i][j] = bilinearInterpolatePoints(array1[i][j], array2[i][j]);
		}
	}

	return res;
}

public static double cosineInterpolatePoints(double p1, double p2) {
	double mu2;

	mu2 = (1 - Math.cos(0.5 * Math.PI)) / 2; //0.5 is mu

	return(p1 * (1 - mu2) + p2 * mu2);
}

public static double bilinearInterpolatePoints(double p1, double p2) {
	return (p1 + p2) / 2;
}
}

 

As you can see in the code, I generate the file every time I'm about to stack another layer of noise on top.

 

I guess my attempt was semi-fruitful, as you can see in the pictures:

 

 

noise5:

PEv6qnN.png

 

noise5+4:

AJ9yvB7.png

 

noise5+4+3:

APBMDZy.png

 

noise5+4+3+2:

Pk9YC1a.png

 

noise5+4+3+2+1:

PtBpWgb.png

 

 

Kain

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.