public static void generatePerlinImages() { Random random = new Random(); // Create a seeded noise generator Noise n = new Noise(random.nextInt()); // Prepare two arrays to fill with ARGB values // The images' size will be 800 by 600 pixels int[][] argbColor = new int[800][600]; int[][] argbGreyscale = new int[800][600]; for (int y = 0; y < argbColor.length; y++) { for (int x = 0; x < argbColor[0].length; x++) { // Generate a noise value for the coordinate (x,y) float noiseValue = 0; noiseValue += scale256(n.interpolatedNoise(x * 0.01f, y * 0.01f)); noiseValue += scale256(n.interpolatedNoise(x * 0.02f, y * 0.02f)); noiseValue += scale256(n.interpolatedNoise(x * 0.04f, y * 0.04f)); noiseValue += scale256(n.interpolatedNoise(x * 0.08f, y * 0.08f)); int roundedValue = Math.round(noiseValue / 4f); // Generate a color and a greyscale ARGB value based on that noise value int colorValue = getARGBColor(roundedValue); int greyscaleValue = getARGBGreyscale(roundedValue); argbColor[y][x] = colorValue; argbGreyscale[y][x] = greyscaleValue; } } // Convert the arrays to buffered PNG-Images BufferedImage colorImage = ImageExporter.convertRGBArray(argbColor); BufferedImage greyscaleImage = ImageExporter.convertRGBArray(argbGreyscale); // Save those PNG-Images to the file system ImageExporter.saveAsFile("perlinColor.png", colorImage); ImageExporter.saveAsFile("perlinGreyscale.png", greyscaleImage); }
protected void transformInverse(int x, int y, float[] out) { float nx = m00 * x + m01 * y; float ny = m10 * x + m11 * y; nx /= scale; ny /= scale * stretch; if (turbulence == 1.0f) { out[0] = x + amount * Noise.noise3(nx + 0.5f, ny, time); out[1] = y + amount * Noise.noise3(nx, ny + 0.5f, time); } else { out[0] = x + amount * Noise.turbulence3(nx + 0.5f, ny, turbulence, time); out[1] = y + amount * Noise.turbulence3(nx, ny + 0.5f, turbulence, time); } }
/** * Returns Fractional Brownian Motion at the given position. * * @param x Position on the x-axis * @param y Position on the y-axis * @return The noise value in the range of the base noise function */ @Override public float noise(float x, float y) { float result = 0.0f; float workingX = x; float workingY = y; for (int i = 0; i < getOctaves(); i++) { result += other.noise(workingX, workingY) * spectralWeights[i]; workingX *= getLacunarity(); workingY *= getLacunarity(); } return result * scale; }
@Override public double compute(double x, double z) { return noise1.compute(x + noise2.compute(x, z), z); }
private float checkCube(float x, float y, int cubeX, int cubeY, Point[] results) { int numPoints; random.setSeed(571 * cubeX + 23 * cubeY); switch (gridType) { case RANDOM: default: numPoints = probabilities[random.nextInt() & 0x1fff]; break; case SQUARE: numPoints = 1; break; case HEXAGONAL: numPoints = 1; break; case OCTAGONAL: numPoints = 2; break; case TRIANGULAR: numPoints = 2; break; } for (int i = 0; i < numPoints; i++) { float px = 0, py = 0; float weight = 1.0f; switch (gridType) { case RANDOM: px = random.nextFloat(); py = random.nextFloat(); break; case SQUARE: px = py = 0.5f; if (randomness != 0) { px += randomness * (random.nextFloat() - 0.5); py += randomness * (random.nextFloat() - 0.5); } break; case HEXAGONAL: if ((cubeX & 1) == 0) { px = 0.75f; py = 0; } else { px = 0.75f; py = 0.5f; } if (randomness != 0) { px += randomness * Noise.noise2(271 * (cubeX + px), 271 * (cubeY + py)); py += randomness * Noise.noise2(271 * (cubeX + px) + 89, 271 * (cubeY + py) + 137); } break; case OCTAGONAL: switch (i) { case 0: px = 0.207f; py = 0.207f; break; case 1: px = 0.707f; py = 0.707f; weight = 1.6f; break; } if (randomness != 0) { px += randomness * Noise.noise2(271 * (cubeX + px), 271 * (cubeY + py)); py += randomness * Noise.noise2(271 * (cubeX + px) + 89, 271 * (cubeY + py) + 137); } break; case TRIANGULAR: if ((cubeY & 1) == 0) { if (i == 0) { px = 0.25f; py = 0.35f; } else { px = 0.75f; py = 0.65f; } } else { if (i == 0) { px = 0.75f; py = 0.35f; } else { px = 0.25f; py = 0.65f; } } if (randomness != 0) { px += randomness * Noise.noise2(271 * (cubeX + px), 271 * (cubeY + py)); py += randomness * Noise.noise2(271 * (cubeX + px) + 89, 271 * (cubeY + py) + 137); } break; } float dx = (float) Math.abs(x - px); float dy = (float) Math.abs(y - py); float d; dx *= weight; dy *= weight; if (distancePower == 1.0f) d = dx + dy; else if (distancePower == 2.0f) d = (float) Math.sqrt(dx * dx + dy * dy); else d = (float) Math.pow( (float) Math.pow(dx, distancePower) + (float) Math.pow(dy, distancePower), 1 / distancePower); // Insertion sort the long way round to speed it up a bit if (d < results[0].distance) { Point p = results[2]; results[2] = results[1]; results[1] = results[0]; results[0] = p; p.distance = d; p.dx = dx; p.dy = dy; p.x = cubeX + px; p.y = cubeY + py; } else if (d < results[1].distance) { Point p = results[2]; results[2] = results[1]; results[1] = p; p.distance = d; p.dx = dx; p.dy = dy; p.x = cubeX + px; p.y = cubeY + py; } else if (d < results[2].distance) { Point p = results[2]; p.distance = d; p.dx = dx; p.dy = dy; p.x = cubeX + px; p.y = cubeY + py; } } return results[2].distance; }
private int displacementMap(int x, int y) { return PixelUtils.clamp((int) (127 * (1 + Noise.noise2(x / xScale, y / xScale)))); }