@Override public int get(int x, int z) { float noiseScale = 100f; float noiseValue = noise.noise(x / noiseScale, z / noiseScale); //Convert noise value to range [0..1] noiseValue = (noiseValue + 1) / 2; return Math.round(min + (noiseValue * (max - min))); } }
@Override public float noise(float x, float y) { float xMod = TeraMath.modulus(x, sampleRate); float yMod = TeraMath.modulus(y, sampleRate); float x0 = x - xMod; float x1 = x0 + sampleRate; float y0 = y - yMod; float y1 = y0 + sampleRate; float q00 = source.noise(x0 * zoom.x, y0 * zoom.y); float q10 = source.noise(x1 * zoom.x, y0 * zoom.y); float q01 = source.noise(x0 * zoom.x, y1 * zoom.y); float q11 = source.noise(x1 * zoom.x, y1 * zoom.y); return TeraMath.biLerp(q00, q10, q01, q11, xMod / sampleRate, yMod / sampleRate); }
@Override public float noise(float x, float y, float z) { float xMod = TeraMath.modulus(x, sampleRate); float yMod = TeraMath.modulus(y, sampleRate); float zMod = TeraMath.modulus(z, sampleRate); float x0 = x - xMod; float x1 = x0 + sampleRate; float y0 = y - yMod; float y1 = y0 + sampleRate; float z0 = z - zMod; float z1 = z0 + sampleRate; float q000 = source.noise(x0 * zoom.x, y0 * zoom.y, z0 * zoom.z); float q100 = source.noise(x1 * zoom.x, y0 * zoom.y, z0 * zoom.z); float q010 = source.noise(x0 * zoom.x, y1 * zoom.y, z0 * zoom.z); float q110 = source.noise(x1 * zoom.x, y1 * zoom.y, z0 * zoom.z); float q001 = source.noise(x0 * zoom.x, y0 * zoom.y, z1 * zoom.z); float q101 = source.noise(x1 * zoom.x, y0 * zoom.y, z1 * zoom.z); float q011 = source.noise(x0 * zoom.x, y1 * zoom.y, z1 * zoom.z); float q111 = source.noise(x1 * zoom.x, y1 * zoom.y, z1 * zoom.z); return TeraMath.triLerp(q000, q100, q010, q110, q001, q101, q011, q111, xMod / sampleRate, yMod / sampleRate, zMod / sampleRate); }
/** * @param x the x coordinate * @param z the z coordinate * @param objs a map (objType to probability) * @return a random pick from the map or <code>null</code> */ protected T getType(int x, int z, Map<T, Float> objs) { float random = Math.abs(typeNoiseGen.noise(x, z)); for (T generator : objs.keySet()) { Float threshold = objs.get(generator); if (threshold != null) { if (random < threshold) { return generator; } else { random -= threshold; } } } return null; }
/** * Filters based on a random noise * * @param noiseGen the noise generator that produces noise in [0..1] * @param density the threshold in [0..1] * @return true if the noise value is <b>below</b> the threshold */ public static Predicate<Vector3i> probability(final Noise noiseGen, final float density) { return input -> Math.abs(noiseGen.noise(input.getX(), input.getY(), input.getZ())) < density; }
/** * 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 *= (float) getLacunarity(); workingY *= (float) getLacunarity(); } return result * scale; }
private float[] getKeyValues(Rect2i fullRegion) { int xDim = fullRegion.sizeX() / sampleRate + 1; int yDim = fullRegion.sizeY() / sampleRate + 1; float[] fullData = new float[xDim * yDim]; for (int y = 0; y < yDim; y++) { for (int x = 0; x < xDim; x++) { int actualX = x * sampleRate + fullRegion.minX(); int actualY = y * sampleRate + fullRegion.minY(); fullData[x + y * xDim] = source.noise(zoom.x * actualX, zoom.y * actualY); } } return fullData; }
/** * Returns Fractional Brownian Motion at the given position. * * @param x Position on the x-axis * @param y Position on the y-axis * @param z Position on the z-axis * @return The noise value in the range of the base noise function */ @Override public float noise(float x, float y, float z) { float result = 0.0f; float workingX = x; float workingY = y; float workingZ = z; for (int i = 0; i < getOctaves(); i++) { result += other.noise(workingX, workingY, workingZ) * spectralWeights[i]; workingX *= (float) getLacunarity(); workingY *= (float) getLacunarity(); workingZ *= (float) getLacunarity(); } return result * scale; }
private float[] getKeyValues(Region3i fullRegion) { int xDim = fullRegion.sizeX() / sampleRate + 1; int yDim = fullRegion.sizeY() / sampleRate + 1; int zDim = fullRegion.sizeZ() / sampleRate + 1; float[] fullData = new float[xDim * yDim * zDim]; for (int z = 0; z < zDim; z++) { for (int y = 0; y < yDim; y++) { for (int x = 0; x < xDim; x++) { int actualX = x * sampleRate + fullRegion.minX(); int actualY = y * sampleRate + fullRegion.minY(); int actualZ = z * sampleRate + fullRegion.minZ(); fullData[x + xDim * (y + yDim * z)] = source.noise(zoom.x * actualX, zoom.y * actualY, zoom.z * actualZ); } } } return fullData; }
@Override public void process(GeneratingRegion region) { // Create our surface height facet (we will get into borders later) Border3D border = region.getBorderForFacet(SurfaceHeightFacet.class); SurfaceHeightFacet facet = new SurfaceHeightFacet(region.getRegion(), border); // loop through every position on our 2d array Rect2i processRegion = facet.getWorldRegion(); for (BaseVector2i position : processRegion.contents()) { facet.setWorld(position, surfaceNoise.noise(position.x(), position.y()) * 20); } // give our newly created and populated facet to the region region.setRegionFacet(SurfaceHeightFacet.class, facet); } }
@Test public void testResolution() { for (int i = 0; i < 1000000; i++) { float posX = rng.nextFloat() * 100f; float posY = rng.nextFloat() * 100f; float posZ = rng.nextFloat() * 100f; float noise = noiseGen.noise(posX, posY, posZ); if (noise > 0 && noise < 0.00005) { return; } } Assert.fail(); } }
@Test public void testMinMax() { float min = Float.POSITIVE_INFINITY; float max = Float.NEGATIVE_INFINITY; for (int i = 0; i < 5000000; i++) { float posX = rng.nextFloat() * 100f; float posY = rng.nextFloat() * 100f; float posZ = rng.nextFloat() * 100f; float noise = noiseGen.noise(posX, posY, posZ); if (noise < min) { min = noise; } if (noise > max) { max = noise; } } Assert.assertTrue(min >= -1); Assert.assertTrue(max <= 1); Assert.assertEquals(-1, min, 0.05); Assert.assertEquals(1, max, 0.05); }