SDF islands
This commit is contained in:
parent
71cae4850c
commit
559a1b8d81
4 changed files with 197 additions and 67 deletions
|
@ -1,6 +1,5 @@
|
||||||
package ru.betterend.mixin.common;
|
package ru.betterend.mixin.common;
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
import org.spongepowered.asm.mixin.Final;
|
import org.spongepowered.asm.mixin.Final;
|
||||||
|
@ -9,10 +8,11 @@ import org.spongepowered.asm.mixin.Shadow;
|
||||||
import org.spongepowered.asm.mixin.injection.At;
|
import org.spongepowered.asm.mixin.injection.At;
|
||||||
import org.spongepowered.asm.mixin.injection.Inject;
|
import org.spongepowered.asm.mixin.injection.Inject;
|
||||||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
|
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
|
||||||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
|
|
||||||
|
|
||||||
|
import net.minecraft.world.biome.source.BiomeSource;
|
||||||
import net.minecraft.world.gen.chunk.ChunkGeneratorSettings;
|
import net.minecraft.world.gen.chunk.ChunkGeneratorSettings;
|
||||||
import net.minecraft.world.gen.chunk.NoiseChunkGenerator;
|
import net.minecraft.world.gen.chunk.NoiseChunkGenerator;
|
||||||
|
import ru.betterend.world.generator.TerrainGenerator;
|
||||||
|
|
||||||
@Mixin(NoiseChunkGenerator.class)
|
@Mixin(NoiseChunkGenerator.class)
|
||||||
public abstract class NoiseChunkGeneratorMixin {
|
public abstract class NoiseChunkGeneratorMixin {
|
||||||
|
@ -20,41 +20,15 @@ public abstract class NoiseChunkGeneratorMixin {
|
||||||
@Shadow
|
@Shadow
|
||||||
protected Supplier<ChunkGeneratorSettings> settings;
|
protected Supplier<ChunkGeneratorSettings> settings;
|
||||||
|
|
||||||
@Final
|
@Inject(method = "<init>(Lnet/minecraft/world/biome/source/BiomeSource;Lnet/minecraft/world/biome/source/BiomeSource;JLjava/util/function/Supplier;)V", at = @At("TAIL"))
|
||||||
@Shadow
|
private void beOnInit(BiomeSource populationSource, BiomeSource biomeSource, long seed, Supplier<ChunkGeneratorSettings> settings, CallbackInfo info) {
|
||||||
private int noiseSizeY;
|
TerrainGenerator.initNoise(seed);
|
||||||
|
}
|
||||||
/*@Inject(method = "sampleNoise", at = @At("HEAD"), cancellable = true)
|
|
||||||
private void beSampleEndNoise(int x, int y, int z, double horizontalScale, double verticalScale, double horizontalStretch, double verticalStretch, CallbackInfoReturnable<Double> info) {
|
|
||||||
if (settings.get().equals(ChunkGeneratorSettings.END)) {
|
|
||||||
double value = ((x + y + z) & 1) == 0 ? 1 : -1;
|
|
||||||
info.setReturnValue(value);
|
|
||||||
info.cancel();
|
|
||||||
}
|
|
||||||
}*/
|
|
||||||
|
|
||||||
@Inject(method = "sampleNoiseColumn([DII)V", at = @At("HEAD"), cancellable = true, allow = 2)
|
@Inject(method = "sampleNoiseColumn([DII)V", at = @At("HEAD"), cancellable = true, allow = 2)
|
||||||
private void beSampleNoiseColumn(double[] buffer, int x, int z, CallbackInfo info) {
|
private void beSampleNoiseColumn(double[] buffer, int x, int z, CallbackInfo info) {
|
||||||
if (settings.get().equals(ChunkGeneratorSettings.END)) {
|
if (settings.get().equals(ChunkGeneratorSettings.END)) {
|
||||||
//System.out.println("Replace!");
|
TerrainGenerator.fillTerrainDensity(buffer, x, z);
|
||||||
//Arrays.fill(buffer, -100);
|
|
||||||
//Arrays.fill(buffer, 0, buffer.length >> 1, 100);
|
|
||||||
//info.cancel();
|
|
||||||
|
|
||||||
float center = buffer.length * 0.5F;
|
|
||||||
/*float sin = (float) (Math.sin(x * 0.1) * Math.sin(z * 0.1)) * center * 0.5F;
|
|
||||||
int min = (int) (center - sin);
|
|
||||||
int max = (int) (center + sin);
|
|
||||||
if (max > min) {
|
|
||||||
Arrays.fill(buffer, min, max, 1);
|
|
||||||
}*/
|
|
||||||
|
|
||||||
float sin = (float) (Math.sin(x * 0.5) * Math.sin(z * 0.5)) * center * 0.5F;
|
|
||||||
for (int y = 0; y < buffer.length; y++) {
|
|
||||||
float value = (y - center);
|
|
||||||
buffer[y] = sin * 0.01F - Math.abs(value) * 0.01F;
|
|
||||||
}
|
|
||||||
|
|
||||||
info.cancel();
|
info.cancel();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,19 +2,19 @@ package ru.betterend.noise;
|
||||||
|
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
|
||||||
import net.minecraft.util.math.MathHelper;
|
import net.minecraft.util.math.BlockPos;
|
||||||
import ru.betterend.util.MHelper;
|
import ru.betterend.util.MHelper;
|
||||||
|
|
||||||
public class VoronoiNoise {
|
public class VoronoiNoise {
|
||||||
private static final Random RANDOM = new Random();
|
private static final Random RANDOM = new Random();
|
||||||
final int seed;
|
final int seed;
|
||||||
final double scale;
|
|
||||||
final double separation;
|
public VoronoiNoise() {
|
||||||
|
this(0);
|
||||||
|
}
|
||||||
|
|
||||||
public VoronoiNoise(int seed, double side, double separation) {
|
public VoronoiNoise(int seed) {
|
||||||
this.seed = seed;
|
this.seed = seed;
|
||||||
this.scale = 1.0 / side;
|
|
||||||
this.separation = MathHelper.clamp(separation, 0, 1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private int getSeed(int x, int y, int z) {
|
private int getSeed(int x, int y, int z) {
|
||||||
|
@ -22,38 +22,76 @@ public class VoronoiNoise {
|
||||||
h = (h ^ (h >> 13)) * 1274126177;
|
h = (h ^ (h >> 13)) * 1274126177;
|
||||||
return h ^ (h >> 16);
|
return h ^ (h >> 16);
|
||||||
}
|
}
|
||||||
|
|
||||||
public double sample(float x, float y, float z) {
|
|
||||||
return sample(MHelper.floor(x), MHelper.floor(y), MHelper.floor(z));
|
|
||||||
}
|
|
||||||
|
|
||||||
public double sample(int x, int y, int z) {
|
public double sample(double x, double y, double z) {
|
||||||
double posX = x * scale;
|
int ix = MHelper.floor(x);
|
||||||
double posY = y * scale;
|
int iy = MHelper.floor(y);
|
||||||
double posZ = z * scale;
|
int iz = MHelper.floor(z);
|
||||||
int posXI = MHelper.floor(posX);
|
|
||||||
int posYI = MHelper.floor(posY);
|
float px = (float) (x - ix);
|
||||||
int posZI = MHelper.floor(posZ);
|
float py = (float) (y - iy);
|
||||||
double distance = Double.MAX_VALUE;
|
float pz = (float) (z - iz);
|
||||||
for (int px = -1; px < 2; px++) {
|
|
||||||
double pointX = posXI + px;
|
float d = 10;
|
||||||
for (int py = -1; py < 2; py++) {
|
|
||||||
double pointY = posYI + py;
|
for (int pox = -1; pox < 2; pox++) {
|
||||||
for (int pz = -1; pz < 2; pz++) {
|
for (int poy = -1; poy < 2; poy++) {
|
||||||
double pointZ = posZI + pz;
|
for (int poz = -1; poz < 2; poz++) {
|
||||||
RANDOM.setSeed(getSeed(posXI + px, posYI + py, posZI + pz));
|
RANDOM.setSeed(getSeed(pox + ix, poy + iy, poz + iz));
|
||||||
|
float pointX = pox + RANDOM.nextFloat();
|
||||||
double posXN = pointX + RANDOM.nextDouble() * separation;
|
float pointY = poy + RANDOM.nextFloat();
|
||||||
double posYN = pointY + RANDOM.nextDouble() * separation;
|
float pointZ = poz + RANDOM.nextFloat();
|
||||||
double posZN = pointZ + RANDOM.nextDouble() * separation;
|
float d2 = MHelper.lengthSqr(pointX - px, pointY - py, pointZ - pz);
|
||||||
|
if (d2 < d) {
|
||||||
double dist2 = MHelper.lengthSqr(posXN - posX, posYN - posY, posZN - posZ);
|
d = d2;
|
||||||
if (dist2 < distance) {
|
|
||||||
distance = dist2;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return distance;
|
|
||||||
|
return Math.sqrt(d);
|
||||||
|
}
|
||||||
|
|
||||||
|
public BlockPos[] getPos(double x, double y, double z, double scale) {
|
||||||
|
int ix = MHelper.floor(x);
|
||||||
|
int iy = MHelper.floor(y);
|
||||||
|
int iz = MHelper.floor(z);
|
||||||
|
|
||||||
|
float px = (float) (x - ix);
|
||||||
|
float py = (float) (y - iy);
|
||||||
|
float pz = (float) (z - iz);
|
||||||
|
|
||||||
|
float d = 10;
|
||||||
|
float selX = 0;
|
||||||
|
float selY = 0;
|
||||||
|
float selZ = 0;
|
||||||
|
float selXPre = 0;
|
||||||
|
float selYPre = 0;
|
||||||
|
float selZPre = 0;
|
||||||
|
|
||||||
|
for (int pox = -1; pox < 2; pox++) {
|
||||||
|
for (int poy = -1; poy < 2; poy++) {
|
||||||
|
for (int poz = -1; poz < 2; poz++) {
|
||||||
|
RANDOM.setSeed(getSeed(pox + ix, poy + iy, poz + iz));
|
||||||
|
float pointX = pox + RANDOM.nextFloat();
|
||||||
|
float pointY = poy + RANDOM.nextFloat();
|
||||||
|
float pointZ = poz + RANDOM.nextFloat();
|
||||||
|
float d2 = MHelper.lengthSqr(pointX - px, pointY - py, pointZ - pz);
|
||||||
|
if (d2 < d) {
|
||||||
|
d = d2;
|
||||||
|
selXPre = selX;
|
||||||
|
selYPre = selY;
|
||||||
|
selZPre = selZ;
|
||||||
|
selX = pointX;
|
||||||
|
selY = pointY;
|
||||||
|
selZ = pointZ;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
BlockPos p1 = new BlockPos((ix + (double) selX) * scale, (iy + (double) selY) * scale, (iz + (double) selZ) * scale);
|
||||||
|
BlockPos p2 = new BlockPos((ix + (double) selXPre) * scale, (iy + (double) selYPre) * scale, (iz + (double) selZPre) * scale);
|
||||||
|
return new BlockPos[] {p1, p2};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
93
src/main/java/ru/betterend/world/generator/IslandLayer.java
Normal file
93
src/main/java/ru/betterend/world/generator/IslandLayer.java
Normal file
|
@ -0,0 +1,93 @@
|
||||||
|
package ru.betterend.world.generator;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
import com.google.common.collect.Maps;
|
||||||
|
|
||||||
|
import net.minecraft.util.math.BlockPos;
|
||||||
|
import ru.betterend.util.MHelper;
|
||||||
|
import ru.betterend.util.sdf.SDF;
|
||||||
|
import ru.betterend.util.sdf.primitive.SDFCapedCone;
|
||||||
|
|
||||||
|
public class IslandLayer {
|
||||||
|
private static final List<BlockPos> POSITIONS = new ArrayList<BlockPos>(9);
|
||||||
|
private static final Random RANDOM = new Random();
|
||||||
|
|
||||||
|
private final Map<BlockPos, SDF> islands = Maps.newHashMap();
|
||||||
|
private final double distance;
|
||||||
|
private final int seed;
|
||||||
|
private final int minY;
|
||||||
|
private final int maxY;
|
||||||
|
|
||||||
|
public IslandLayer(int seed, double distance, int minY, int maxY) {
|
||||||
|
this.distance = distance;
|
||||||
|
this.seed = seed;
|
||||||
|
this.minY = minY;
|
||||||
|
this.maxY = maxY;
|
||||||
|
}
|
||||||
|
|
||||||
|
private int getSeed(int x, int y, int z) {
|
||||||
|
int h = seed + x * 374761393 + y * 668265263 + z;
|
||||||
|
h = (h ^ (h >> 13)) * 1274126177;
|
||||||
|
return h ^ (h >> 16);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void updatePositions(double x, double y, 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));
|
||||||
|
double posX = (px + RANDOM.nextFloat()) * distance;
|
||||||
|
double posY = (py + RANDOM.nextFloat()) * distance;
|
||||||
|
double posZ = (pz + RANDOM.nextFloat()) * distance;
|
||||||
|
POSITIONS.add(new BlockPos(posX, posY, posZ));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private SDF getIsland(BlockPos pos) {
|
||||||
|
SDF island = islands.get(pos);
|
||||||
|
if (island == null) {
|
||||||
|
island = new SDFCapedCone().setHeight(10).setRadius1(0).setRadius2(30);
|
||||||
|
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());
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return distance;
|
||||||
|
}
|
||||||
|
|
||||||
|
public float getDensity(double x, double y, double z) {
|
||||||
|
updatePositions(x, y, z);
|
||||||
|
return -calculateSDF(x, y, z);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,25 @@
|
||||||
|
package ru.betterend.world.generator;
|
||||||
|
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
public class TerrainGenerator {
|
||||||
|
private static final double SCALE_XZ = 8.0;
|
||||||
|
private static final double SCALE_Y = 4.0;
|
||||||
|
|
||||||
|
private static IslandLayer layer;
|
||||||
|
|
||||||
|
public static void initNoise(long seed) {
|
||||||
|
Random random = new Random(seed);
|
||||||
|
layer = new IslandLayer(random.nextInt(), 100, 40, 90);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void fillTerrainDensity(double[] buffer, int x, int z) {
|
||||||
|
double px = (double) x * SCALE_XZ;
|
||||||
|
double pz = (double) z * SCALE_XZ;
|
||||||
|
for (int y = 0; y < buffer.length; y++) {
|
||||||
|
double py = (double) y * SCALE_Y;
|
||||||
|
float dist = layer.getDensity(px, py, pz);
|
||||||
|
buffer[y] = dist;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue