diff --git a/src/main/java/ru/betterend/world/generator/IslandLayer.java b/src/main/java/ru/betterend/world/generator/IslandLayer.java index 426c50ca..b840389c 100644 --- a/src/main/java/ru/betterend/world/generator/IslandLayer.java +++ b/src/main/java/ru/betterend/world/generator/IslandLayer.java @@ -8,52 +8,60 @@ import java.util.Random; import com.google.common.collect.Maps; import net.minecraft.util.math.BlockPos; +import ru.betterend.noise.OpenSimplexNoise; import ru.betterend.util.MHelper; import ru.betterend.util.sdf.SDF; +import ru.betterend.util.sdf.operator.SDFScale; +import ru.betterend.util.sdf.operator.SDFSmoothUnion; +import ru.betterend.util.sdf.operator.SDFTranslate; +import ru.betterend.util.sdf.operator.SDFUnion; import ru.betterend.util.sdf.primitive.SDFCapedCone; public class IslandLayer { - private static final List POSITIONS = new ArrayList(9); private static final Random RANDOM = new Random(); + private static final SDF ISLAND; + private final List positions = new ArrayList(9); private final Map islands = Maps.newHashMap(); + private final OpenSimplexNoise density; private final double distance; + private final float scale; private final int seed; private final int minY; private final int maxY; - public IslandLayer(int seed, double distance, int minY, int maxY) { + public IslandLayer(int seed, double distance, float scale, int center, int heightVariation) { this.distance = distance; + this.density = new OpenSimplexNoise(seed); + this.scale = scale; this.seed = seed; - this.minY = minY; - this.maxY = maxY; + this.minY = center - heightVariation; + this.maxY = center + heightVariation; } - private int getSeed(int x, int y, int z) { - int h = seed + x * 374761393 + y * 668265263 + z; + private int getSeed(int x, int z) { + int h = seed + x * 374761393 + z * 668265263; h = (h ^ (h >> 13)) * 1274126177; return h ^ (h >> 16); } - private void updatePositions(double x, double y, double z) { - POSITIONS.clear(); + public void updatePositions(double x, double z) { + positions.clear(); int ix = MHelper.floor(x / distance); - int iy = MHelper.floor(y / distance); int iz = MHelper.floor(z / distance); for (int pox = -1; pox < 2; pox++) { int px = pox + ix; - for (int poy = -1; poy < 2; poy++) { - int py = poy + iy; - for (int poz = -1; poz < 2; poz++) { - int pz = poz + iz; - RANDOM.setSeed(getSeed(px, py, pz)); + for (int poz = -1; poz < 2; poz++) { + int pz = poz + iz; + //if (density.eval(px * distance * 0.002, pz * distance * 0.002) > 0) { + RANDOM.setSeed(getSeed(px, pz)); double posX = (px + RANDOM.nextFloat()) * distance; - double posY = (py + RANDOM.nextFloat()) * distance; + double posY = MHelper.randRange(minY, maxY, RANDOM); double posZ = (pz + RANDOM.nextFloat()) * distance; - POSITIONS.add(new BlockPos(posX, posY, posZ)); - } + positions.add(new BlockPos(posX, posY, posZ)); + //} } } } @@ -61,33 +69,55 @@ public class IslandLayer { private SDF getIsland(BlockPos pos) { SDF island = islands.get(pos); if (island == null) { - island = new SDFCapedCone().setHeight(10).setRadius1(0).setRadius2(30); + RANDOM.setSeed(getSeed(pos.getX(), pos.getZ())); + island = new SDFScale().setScale(RANDOM.nextFloat() + 0.5F).setSource(ISLAND); islands.put(pos, island); } return island; } private float getRelativeDistance(SDF sdf, BlockPos center, double px, double py, double pz) { - float x = (float) (px - center.getX()); - float y = (float) (py - center.getY()); - float z = (float) (pz - center.getZ()); + float x = (float) (px - center.getX()) / scale; + float y = (float) (py - center.getY()) / scale; + float z = (float) (pz - center.getZ()) / scale; return sdf.getDistance(x, y, z); } private float calculateSDF(double x, double y, double z) { float distance = 10; - for (BlockPos pos: POSITIONS) { - if (pos.getY() > minY && pos.getY() < maxY) { - SDF island = getIsland(pos); - float dist = getRelativeDistance(island, pos, x, y, z); - distance = MHelper.min(distance, dist); - } + for (BlockPos pos: positions) { + SDF island = getIsland(pos); + float dist = getRelativeDistance(island, pos, x, y, z); + distance = MHelper.min(distance, dist); } return distance; } public float getDensity(double x, double y, double z) { - updatePositions(x, y, z); return -calculateSDF(x, y, z); } + + public void clearCache() { + if (islands.size() > 32) { + islands.clear(); + } + } + + private static SDF makeCone(float radiusBottom, float radiusTop, float height, float minY) { + float hh = height * 0.5F; + SDF sdf = new SDFCapedCone().setHeight(hh).setRadius1(radiusBottom).setRadius2(radiusTop); + return new SDFTranslate().setTranslate(0, minY + hh, 0).setSource(sdf); + } + + static { + SDF cone1 = makeCone(0, 0.4F, 0.2F, -0.3F); + SDF cone2 = makeCone(0.4F, 0.5F, 0.1F, -0.1F); + SDF cone3 = makeCone(0.5F, 0.45F, 0.03F, 0.0F); + SDF cone4 = makeCone(0.45F, 0, 0.02F, 0.03F); + + SDF coneBottom = new SDFUnion().setSourceA(cone1).setSourceB(cone2); + SDF coneTop = new SDFUnion().setSourceA(cone3).setSourceB(cone4); + + ISLAND = new SDFSmoothUnion().setRadius(0.01F).setSourceA(coneTop).setSourceB(coneBottom); + } } diff --git a/src/main/java/ru/betterend/world/generator/TerrainGenerator.java b/src/main/java/ru/betterend/world/generator/TerrainGenerator.java index d5553a45..873da035 100644 --- a/src/main/java/ru/betterend/world/generator/TerrainGenerator.java +++ b/src/main/java/ru/betterend/world/generator/TerrainGenerator.java @@ -2,24 +2,64 @@ package ru.betterend.world.generator; import java.util.Random; +import ru.betterend.noise.OpenSimplexNoise; +import ru.betterend.util.MHelper; + public class TerrainGenerator { private static final double SCALE_XZ = 8.0; private static final double SCALE_Y = 4.0; - private static IslandLayer layer; + private static IslandLayer largeIslands; + private static IslandLayer mediumIslands; + private static IslandLayer smallIslands; + private static OpenSimplexNoise noise1; + private static OpenSimplexNoise noise2; + + private static OpenSimplexNoise continentalTop; + private static OpenSimplexNoise continentalBottom; + private static OpenSimplexNoise continentalSeparator; public static void initNoise(long seed) { Random random = new Random(seed); - layer = new IslandLayer(random.nextInt(), 100, 40, 90); + largeIslands = new IslandLayer(random.nextInt(), 300, 200, 63, 0); + mediumIslands = new IslandLayer(random.nextInt(), 150, 100, 63, 16); + smallIslands = new IslandLayer(random.nextInt(), 60, 50, 63, 32); + noise1 = new OpenSimplexNoise(random.nextInt()); + noise2 = new OpenSimplexNoise(random.nextInt()); + continentalTop = new OpenSimplexNoise(random.nextInt()); + continentalBottom = new OpenSimplexNoise(random.nextInt()); + continentalSeparator = new OpenSimplexNoise(random.nextInt()); } public static void fillTerrainDensity(double[] buffer, int x, int z) { + largeIslands.clearCache(); + double px = (double) x * SCALE_XZ; double pz = (double) z * SCALE_XZ; + + largeIslands.updatePositions(px, pz); + for (int y = 0; y < buffer.length; y++) { double py = (double) y * SCALE_Y; - float dist = layer.getDensity(px, py, pz); + //float dist = getContinental(px, py, pz); + //dist = dist > 1 ? dist : MHelper.max(dist, largeIslands.getDensity(px, py, pz)); + float dist = largeIslands.getDensity(px, py, pz); + dist = dist > 1 ? dist : MHelper.max(dist, mediumIslands.getDensity(px, py, pz)); + dist = dist > 1 ? dist : MHelper.max(dist, smallIslands.getDensity(px, py, pz)); + if (dist > -0.5F) { + dist += noise1.eval(px * 0.01, py * 0.01, pz * 0.01) * 0.04; + dist += noise2.eval(px * 0.05, py * 0.05, pz * 0.05) * 0.02; + dist += noise1.eval(px * 0.1, py * 0.1, pz * 0.1) * 0.01; + } buffer[y] = dist; } } + + private static float getContinental(double px, double py, double pz) { + float gradient = ((float) py - 50F) * 0.01F; + float top = (float) continentalTop.eval(px * 0.04, py * 0.04, pz * 0.04) * 0.1F - gradient; + float bottom = (float) continentalBottom.eval(px * 0.01, py * 0.01, pz * 0.01) * 0.5F + gradient; + float separate = (float) Math.abs(continentalSeparator.eval(px * 0.002, py * 0.002, pz * 0.002)); + return MHelper.min(top, bottom) * (separate * separate) + (float) noise2.eval(px * 0.1, py * 0.1, pz * 0.1) * 0.06F; + } }