From 4083597f23874642985af8dcdc85ae3e5063bd66 Mon Sep 17 00:00:00 2001 From: dfsek Date: Sat, 4 Dec 2021 19:07:39 -0700 Subject: [PATCH] remove unneeded ChunkInterpolator interface --- .../math/interpolation/ChunkInterpolator.java | 114 ++++++++++++++-- .../interpolation/ChunkInterpolator3D.java | 125 ------------------ .../generation/math/samplers/Sampler3D.java | 8 +- 3 files changed, 106 insertions(+), 141 deletions(-) delete mode 100644 common/addons/chunk-generator-noise-3d/src/main/java/com/dfsek/terra/addons/chunkgenerator/generation/math/interpolation/ChunkInterpolator3D.java diff --git a/common/addons/chunk-generator-noise-3d/src/main/java/com/dfsek/terra/addons/chunkgenerator/generation/math/interpolation/ChunkInterpolator.java b/common/addons/chunk-generator-noise-3d/src/main/java/com/dfsek/terra/addons/chunkgenerator/generation/math/interpolation/ChunkInterpolator.java index 32db07fec..d16058dcc 100644 --- a/common/addons/chunk-generator-noise-3d/src/main/java/com/dfsek/terra/addons/chunkgenerator/generation/math/interpolation/ChunkInterpolator.java +++ b/common/addons/chunk-generator-noise-3d/src/main/java/com/dfsek/terra/addons/chunkgenerator/generation/math/interpolation/ChunkInterpolator.java @@ -1,35 +1,123 @@ /* * Copyright (c) 2020-2021 Polyhedral Development * - * The Terra API is licensed under the terms of the MIT License. For more details, - * reference the LICENSE file in the common/api directory. + * The Terra Core Addons are licensed under the terms of the MIT License. For more details, + * reference the LICENSE file in this module's root directory. */ package com.dfsek.terra.addons.chunkgenerator.generation.math.interpolation; +import net.jafama.FastMath; + +import java.util.HashMap; import java.util.Map; import com.dfsek.terra.addons.chunkgenerator.config.BiomeNoiseProperties; import com.dfsek.terra.api.util.mutable.MutableInteger; +import com.dfsek.terra.api.world.biome.generation.BiomeProvider; -public interface ChunkInterpolator { - default double computeNoise(Map gens, double x, double y, double z) { +/** + * Class to abstract away the Interpolators needed to generate a chunk.
+ * Contains method to get interpolated noise at a coordinate within the chunk. + */ +public class ChunkInterpolator { + private final Interpolator3[][][] interpGrid; + private final long seed; + + private final int min; + private final int max; + + /** + * Instantiates a 3D ChunkInterpolator3D at a pair of chunk coordinates. + * + * @param chunkX X coordinate of the chunk. + * @param chunkZ Z coordinate of the chunk. + * @param provider Biome Provider to use for biome fetching. + * @param min + * @param max + */ + public ChunkInterpolator(long seed, int chunkX, int chunkZ, BiomeProvider provider, int min, int max) { + this.min = min; + this.max = max; + this.seed = seed; + + int xOrigin = chunkX << 4; + int zOrigin = chunkZ << 4; + + int range = this.max - this.min + 1; + + int size = range >> 2; + + interpGrid = new Interpolator3[4][size][4]; + + double[][][] noiseStorage = new double[5][5][size + 1]; + + + for(int x = 0; x < 5; x++) { + for(int z = 0; z < 5; z++) { + BiomeNoiseProperties generationSettings = provider.getBiome(xOrigin + (x << 2), zOrigin + (z << 2), seed).getContext().get( + BiomeNoiseProperties.class); + Map genMap = new HashMap<>(); + + int step = generationSettings.blendStep(); + int blend = generationSettings.blendDistance(); + + for(int xi = -blend; xi <= blend; xi++) { + for(int zi = -blend; zi <= blend; zi++) { + genMap.computeIfAbsent( + provider.getBiome(xOrigin + (x << 2) + (xi * step), zOrigin + (z << 2) + (zi * step), seed) + .getContext() + .get(BiomeNoiseProperties.class), + g -> new MutableInteger(0)).increment(); // Increment by 1 + } + } + + for(int y = 0; y < size + 1; y++) { + noiseStorage[x][z][y] = computeNoise(genMap, (x << 2) + xOrigin, (y << 2) + this.min, (z << 2) + zOrigin); + } + } + } + + for(int x = 0; x < 4; x++) { + for(int z = 0; z < 4; z++) { + for(int y = 0; y < size; y++) { + interpGrid[x][y][z] = new Interpolator3( + noiseStorage[x][z][y], + noiseStorage[x + 1][z][y], + noiseStorage[x][z][y + 1], + noiseStorage[x + 1][z][y + 1], + noiseStorage[x][z + 1][y], + noiseStorage[x + 1][z + 1][y], + noiseStorage[x][z + 1][y + 1], + noiseStorage[x + 1][z + 1][y + 1]); + } + } + } + } + + private static int reRange(int value, int high) { + return FastMath.max(FastMath.min(value, high), 0); + } + + public double computeNoise(BiomeNoiseProperties generationSettings, double x, double y, double z) { + return generationSettings.base().noise(seed, x, y, z); + } + + public double computeNoise(Map gens, double x, double y, double z) { double n = 0; double div = 0; for(Map.Entry entry : gens.entrySet()) { BiomeNoiseProperties gen = entry.getKey(); int weight = entry.getValue().get(); double noise = computeNoise(gen, x, y, z); - + n += noise * weight; div += gen.blendWeight() * weight; } return n / div; } - double computeNoise(BiomeNoiseProperties generationSettings, double x, double y, double z); - /** * Gets the noise at a pair of internal chunk coordinates. * @@ -38,11 +126,13 @@ public interface ChunkInterpolator { * * @return double - The interpolated noise at the coordinates. */ - double getNoise(double x, double y, double z); + public double getNoise(double x, double y, double z) { + return interpGrid[reRange(((int) x) / 4, 3)][(FastMath.max(FastMath.min(((int) y), max), min) - min) / 4][reRange(((int) z) / 4, + 3)].trilerp( + (x % 4) / 4, (y % 4) / 4, (z % 4) / 4); + } - default double getNoise(int x, int y, - int z) { // Floating-point modulus operations are expensive. This allows implementations to optionally handle - // integers separately. - return getNoise((double) x, y, z); + public double getNoise(int x, int y, int z) { + return interpGrid[x / 4][(y - min) / 4][z / 4].trilerp((double) (x % 4) / 4, (double) (y % 4) / 4, (double) (z % 4) / 4); } } diff --git a/common/addons/chunk-generator-noise-3d/src/main/java/com/dfsek/terra/addons/chunkgenerator/generation/math/interpolation/ChunkInterpolator3D.java b/common/addons/chunk-generator-noise-3d/src/main/java/com/dfsek/terra/addons/chunkgenerator/generation/math/interpolation/ChunkInterpolator3D.java deleted file mode 100644 index 50950d51e..000000000 --- a/common/addons/chunk-generator-noise-3d/src/main/java/com/dfsek/terra/addons/chunkgenerator/generation/math/interpolation/ChunkInterpolator3D.java +++ /dev/null @@ -1,125 +0,0 @@ -/* - * Copyright (c) 2020-2021 Polyhedral Development - * - * The Terra Core Addons are licensed under the terms of the MIT License. For more details, - * reference the LICENSE file in this module's root directory. - */ - -package com.dfsek.terra.addons.chunkgenerator.generation.math.interpolation; - -import net.jafama.FastMath; - -import java.util.HashMap; -import java.util.Map; - -import com.dfsek.terra.addons.chunkgenerator.config.BiomeNoiseProperties; -import com.dfsek.terra.api.util.mutable.MutableInteger; -import com.dfsek.terra.api.world.biome.generation.BiomeProvider; - - -/** - * Class to abstract away the Interpolators needed to generate a chunk.
- * Contains method to get interpolated noise at a coordinate within the chunk. - */ -public class ChunkInterpolator3D implements ChunkInterpolator { - private final Interpolator3[][][] interpGrid; - private final long seed; - - private final int min; - private final int max; - - /** - * Instantiates a 3D ChunkInterpolator3D at a pair of chunk coordinates. - * - * @param chunkX X coordinate of the chunk. - * @param chunkZ Z coordinate of the chunk. - * @param provider Biome Provider to use for biome fetching. - * @param min - * @param max - */ - public ChunkInterpolator3D(long seed, int chunkX, int chunkZ, BiomeProvider provider, int min, int max) { - this.min = min; - this.max = max; - this.seed = seed; - - int xOrigin = chunkX << 4; - int zOrigin = chunkZ << 4; - - int range = this.max - this.min + 1; - - int size = range >> 2; - - interpGrid = new Interpolator3[4][size][4]; - - double[][][] noiseStorage = new double[5][5][size + 1]; - - - for(int x = 0; x < 5; x++) { - for(int z = 0; z < 5; z++) { - BiomeNoiseProperties generationSettings = provider.getBiome(xOrigin + (x << 2), zOrigin + (z << 2), seed).getContext().get( - BiomeNoiseProperties.class); - Map genMap = new HashMap<>(); - - int step = generationSettings.blendStep(); - int blend = generationSettings.blendDistance(); - - for(int xi = -blend; xi <= blend; xi++) { - for(int zi = -blend; zi <= blend; zi++) { - genMap.computeIfAbsent( - provider.getBiome(xOrigin + (x << 2) + (xi * step), zOrigin + (z << 2) + (zi * step), seed) - .getContext() - .get(BiomeNoiseProperties.class), - g -> new MutableInteger(0)).increment(); // Increment by 1 - } - } - - for(int y = 0; y < size + 1; y++) { - noiseStorage[x][z][y] = computeNoise(genMap, (x << 2) + xOrigin, (y << 2) + this.min, (z << 2) + zOrigin); - } - } - } - - for(int x = 0; x < 4; x++) { - for(int z = 0; z < 4; z++) { - for(int y = 0; y < size; y++) { - interpGrid[x][y][z] = new Interpolator3( - noiseStorage[x][z][y], - noiseStorage[x + 1][z][y], - noiseStorage[x][z][y + 1], - noiseStorage[x + 1][z][y + 1], - noiseStorage[x][z + 1][y], - noiseStorage[x + 1][z + 1][y], - noiseStorage[x][z + 1][y + 1], - noiseStorage[x + 1][z + 1][y + 1]); - } - } - } - } - - private static int reRange(int value, int high) { - return FastMath.max(FastMath.min(value, high), 0); - } - - public double computeNoise(BiomeNoiseProperties generationSettings, double x, double y, double z) { - return generationSettings.base().noise(seed, x, y, z); - } - - /** - * Gets the noise at a pair of internal chunk coordinates. - * - * @param x The internal X coordinate (0-15). - * @param z The internal Z coordinate (0-15). - * - * @return double - The interpolated noise at the coordinates. - */ - @Override - public double getNoise(double x, double y, double z) { - return interpGrid[reRange(((int) x) / 4, 3)][(FastMath.max(FastMath.min(((int) y), max), min) - min) / 4][reRange(((int) z) / 4, - 3)].trilerp( - (x % 4) / 4, (y % 4) / 4, (z % 4) / 4); - } - - public double getNoise(int x, int y, int z) { - return interpGrid[x / 4][(y - min) / 4][z / 4].trilerp((double) (x % 4) / 4, (double) (y % 4) / 4, (double) (z % 4) / 4); - } -} diff --git a/common/addons/chunk-generator-noise-3d/src/main/java/com/dfsek/terra/addons/chunkgenerator/generation/math/samplers/Sampler3D.java b/common/addons/chunk-generator-noise-3d/src/main/java/com/dfsek/terra/addons/chunkgenerator/generation/math/samplers/Sampler3D.java index 9b7bb7965..ea98b0af6 100644 --- a/common/addons/chunk-generator-noise-3d/src/main/java/com/dfsek/terra/addons/chunkgenerator/generation/math/samplers/Sampler3D.java +++ b/common/addons/chunk-generator-noise-3d/src/main/java/com/dfsek/terra/addons/chunkgenerator/generation/math/samplers/Sampler3D.java @@ -9,18 +9,18 @@ package com.dfsek.terra.addons.chunkgenerator.generation.math.samplers; import net.jafama.FastMath; -import com.dfsek.terra.addons.chunkgenerator.generation.math.interpolation.ChunkInterpolator3D; +import com.dfsek.terra.addons.chunkgenerator.generation.math.interpolation.ChunkInterpolator; import com.dfsek.terra.addons.chunkgenerator.generation.math.interpolation.ElevationInterpolator; import com.dfsek.terra.api.world.biome.generation.BiomeProvider; public class Sampler3D { - private final ChunkInterpolator3D interpolator; + private final ChunkInterpolator interpolator; private final ElevationInterpolator elevationInterpolator; public Sampler3D(int x, int z, long seed, int minHeight, int maxHeight, BiomeProvider provider, int elevationSmooth) { - this.interpolator = new ChunkInterpolator3D(seed, x, z, provider, - minHeight, maxHeight); + this.interpolator = new ChunkInterpolator(seed, x, z, provider, + minHeight, maxHeight); this.elevationInterpolator = new ElevationInterpolator(seed, x, z, provider, elevationSmooth); }