From 377a4c2284e66b496f3aeed20fd525338571ca4a Mon Sep 17 00:00:00 2001 From: Daniel Mills Date: Fri, 16 Jul 2021 03:54:09 -0400 Subject: [PATCH] Noise boxing --- .../studio/CommandIrisStudioProfile.java | 1 + .../framework/EngineParallaxManager.java | 1 + .../InterpolationMethod.java | 2 +- .../interpolation/InterpolationMethod3D.java | 28 ++++ .../interpolation/IrisInterpolation.java | 121 +++++++++++------- .../iris/engine/interpolation/NoiseBox.java | 59 +++++++++ .../iris/engine/interpolation/NoiseCube.java | 71 ++++++++++ .../iris/engine/jigsaw/PlannedStructure.java | 1 + .../iris/engine/object/IrisFeature.java | 1 + .../iris/engine/object/IrisInterpolator.java | 1 + .../interpolation/InterpolatingStream.java | 2 +- .../interpolation/InterpolatorFactory.java | 2 +- 12 files changed, 243 insertions(+), 47 deletions(-) rename src/main/java/com/volmit/iris/engine/{object => interpolation}/InterpolationMethod.java (99%) create mode 100644 src/main/java/com/volmit/iris/engine/interpolation/InterpolationMethod3D.java create mode 100644 src/main/java/com/volmit/iris/engine/interpolation/NoiseBox.java create mode 100644 src/main/java/com/volmit/iris/engine/interpolation/NoiseCube.java diff --git a/src/main/java/com/volmit/iris/core/command/studio/CommandIrisStudioProfile.java b/src/main/java/com/volmit/iris/core/command/studio/CommandIrisStudioProfile.java index 3471cbd9d..8a6c3fd5b 100644 --- a/src/main/java/com/volmit/iris/core/command/studio/CommandIrisStudioProfile.java +++ b/src/main/java/com/volmit/iris/core/command/studio/CommandIrisStudioProfile.java @@ -22,6 +22,7 @@ import com.volmit.iris.Iris; import com.volmit.iris.core.IrisDataManager; import com.volmit.iris.core.IrisProject; import com.volmit.iris.core.IrisSettings; +import com.volmit.iris.engine.interpolation.InterpolationMethod; import com.volmit.iris.engine.noise.CNG; import com.volmit.iris.engine.object.*; import com.volmit.iris.util.collection.KList; diff --git a/src/main/java/com/volmit/iris/engine/framework/EngineParallaxManager.java b/src/main/java/com/volmit/iris/engine/framework/EngineParallaxManager.java index f8d669511..5a5bb0fe3 100644 --- a/src/main/java/com/volmit/iris/engine/framework/EngineParallaxManager.java +++ b/src/main/java/com/volmit/iris/engine/framework/EngineParallaxManager.java @@ -25,6 +25,7 @@ import com.volmit.iris.engine.cache.Cache; import com.volmit.iris.engine.data.B; import com.volmit.iris.engine.data.DataProvider; import com.volmit.iris.engine.hunk.Hunk; +import com.volmit.iris.engine.interpolation.InterpolationMethod; import com.volmit.iris.engine.jigsaw.PlannedStructure; import com.volmit.iris.engine.object.*; import com.volmit.iris.engine.object.common.IObjectPlacer; diff --git a/src/main/java/com/volmit/iris/engine/object/InterpolationMethod.java b/src/main/java/com/volmit/iris/engine/interpolation/InterpolationMethod.java similarity index 99% rename from src/main/java/com/volmit/iris/engine/object/InterpolationMethod.java rename to src/main/java/com/volmit/iris/engine/interpolation/InterpolationMethod.java index 307e53454..e521ce460 100644 --- a/src/main/java/com/volmit/iris/engine/object/InterpolationMethod.java +++ b/src/main/java/com/volmit/iris/engine/interpolation/InterpolationMethod.java @@ -16,7 +16,7 @@ * along with this program. If not, see . */ -package com.volmit.iris.engine.object; +package com.volmit.iris.engine.interpolation; import com.volmit.iris.engine.object.annotations.Desc; diff --git a/src/main/java/com/volmit/iris/engine/interpolation/InterpolationMethod3D.java b/src/main/java/com/volmit/iris/engine/interpolation/InterpolationMethod3D.java new file mode 100644 index 000000000..4db437d28 --- /dev/null +++ b/src/main/java/com/volmit/iris/engine/interpolation/InterpolationMethod3D.java @@ -0,0 +1,28 @@ +/* + * Iris is a World Generator for Minecraft Bukkit Servers + * Copyright (c) 2021 Arcane Arts (Volmit Software) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.volmit.iris.engine.interpolation; + +import com.volmit.iris.engine.object.annotations.Desc; + +@Desc("An interpolation method (or function) is simply a method of smoothing a position based on surrounding points on a grid. Bicubic for example is smoother, but has 4 times the checks than Bilinear for example. Try using BILINEAR_STARCAST_9 for beautiful results.") +public enum InterpolationMethod3D { + TRILINEAR, + TRICUBIC, + TRIHERMITE +} diff --git a/src/main/java/com/volmit/iris/engine/interpolation/IrisInterpolation.java b/src/main/java/com/volmit/iris/engine/interpolation/IrisInterpolation.java index 1cd54f6d0..ab2375c8b 100644 --- a/src/main/java/com/volmit/iris/engine/interpolation/IrisInterpolation.java +++ b/src/main/java/com/volmit/iris/engine/interpolation/IrisInterpolation.java @@ -20,7 +20,6 @@ package com.volmit.iris.engine.interpolation; import com.google.common.util.concurrent.AtomicDouble; import com.volmit.iris.engine.noise.CNG; -import com.volmit.iris.engine.object.InterpolationMethod; import com.volmit.iris.engine.object.NoiseStyle; import com.volmit.iris.util.function.NoiseProvider; import com.volmit.iris.util.function.NoiseProvider3; @@ -355,16 +354,21 @@ public class IrisInterpolation { //@done } - public static double getTrilinear(int x, int y, int z, double rad, NoiseProvider3 n) { - int fx = (int) Math.floor(x / rad); - int fy = (int) Math.floor(y / rad); - int fz = (int) Math.floor(z / rad); - int x1 = (int) Math.round(fx * rad); - int y1 = (int) Math.round(fy * rad); - int z1 = (int) Math.round(fz * rad); - int x2 = (int) Math.round((fx + 1) * rad); - int y2 = (int) Math.round((fy + 1) * rad); - int z2 = (int) Math.round((fz + 1) * rad); + public static double getTrilinear(int x, int y, int z, double rad, NoiseProvider3 n) + { + return getTrilinear(x,y,z,rad,rad,rad,n); + } + + public static double getTrilinear(int x, int y, int z, double radx, double rady, double radz, NoiseProvider3 n) { + int fx = (int) Math.floor(x / radx); + int fy = (int) Math.floor(y / rady); + int fz = (int) Math.floor(z / radz); + int x1 = (int) Math.round(fx * radx); + int y1 = (int) Math.round(fy * rady); + int z1 = (int) Math.round(fz * radz); + int x2 = (int) Math.round((fx + 1) * radx); + int y2 = (int) Math.round((fy + 1) * rady); + int z2 = (int) Math.round((fz + 1) * radz); double px = rangeScale(0, 1, x1, x2, x); double py = rangeScale(0, 1, y1, y2, y); double pz = rangeScale(0, 1, z1, z2, z); @@ -383,21 +387,25 @@ public class IrisInterpolation { } public static double getTricubic(int x, int y, int z, double rad, NoiseProvider3 n) { - int fx = (int) Math.floor(x / rad); - int fy = (int) Math.floor(y / rad); - int fz = (int) Math.floor(z / rad); - int x0 = (int) Math.round((fx - 1) * rad); - int y0 = (int) Math.round((fy - 1) * rad); - int z0 = (int) Math.round((fz - 1) * rad); - int x1 = (int) Math.round(fx * rad); - int y1 = (int) Math.round(fy * rad); - int z1 = (int) Math.round(fz * rad); - int x2 = (int) Math.round((fx + 1) * rad); - int y2 = (int) Math.round((fy + 1) * rad); - int z2 = (int) Math.round((fz + 1) * rad); - int x3 = (int) Math.round((fx + 2) * rad); - int y3 = (int) Math.round((fy + 2) * rad); - int z3 = (int) Math.round((fz + 2) * rad); + return getTricubic(x,y,z,rad,rad,rad,n); + } + + public static double getTricubic(int x, int y, int z, double radx, double rady, double radz, NoiseProvider3 n) { + int fx = (int) Math.floor(x / radx); + int fy = (int) Math.floor(y / rady); + int fz = (int) Math.floor(z / radz); + int x0 = (int) Math.round((fx - 1) * radx); + int y0 = (int) Math.round((fy - 1) * rady); + int z0 = (int) Math.round((fz - 1) * radz); + int x1 = (int) Math.round(fx * radx); + int y1 = (int) Math.round(fy * rady); + int z1 = (int) Math.round(fz * radz); + int x2 = (int) Math.round((fx + 1) * radx); + int y2 = (int) Math.round((fy + 1) * rady); + int z2 = (int) Math.round((fz + 1) * radz); + int x3 = (int) Math.round((fx + 2) * radx); + int y3 = (int) Math.round((fy + 2) * rady); + int z3 = (int) Math.round((fz + 2) * radz); double px = rangeScale(0, 1, x1, x2, x); double py = rangeScale(0, 1, y1, y2, y); double pz = rangeScale(0, 1, z1, z2, z); @@ -473,26 +481,34 @@ public class IrisInterpolation { //@done } - public static double getTrihermite(int x, int y, int z, double rad, NoiseProvider3 n) { - return getTrihermite(x, y, z, rad, n, 0D, 0D); + public static double getTrihermite(int x, int y, int z, double rad, NoiseProvider3 n, double tension, double bias) { + return getTrihermite(x, y, z, rad, rad, rad, n, tension, bias); } - public static double getTrihermite(int x, int y, int z, double rad, NoiseProvider3 n, double tension, double bias) { - int fx = (int) Math.floor(x / rad); - int fy = (int) Math.floor(y / rad); - int fz = (int) Math.floor(z / rad); - int x0 = (int) Math.round((fx - 1) * rad); - int y0 = (int) Math.round((fy - 1) * rad); - int z0 = (int) Math.round((fz - 1) * rad); - int x1 = (int) Math.round(fx * rad); - int y1 = (int) Math.round(fy * rad); - int z1 = (int) Math.round(fz * rad); - int x2 = (int) Math.round((fx + 1) * rad); - int y2 = (int) Math.round((fy + 1) * rad); - int z2 = (int) Math.round((fz + 1) * rad); - int x3 = (int) Math.round((fx + 2) * rad); - int y3 = (int) Math.round((fy + 2) * rad); - int z3 = (int) Math.round((fz + 2) * rad); + public static double getTrihermite(int x, int y, int z, double rad, NoiseProvider3 n) { + return getTrihermite(x, y, z, rad, rad, rad, n, 0D, 0D); + } + + public static double getTrihermite(int x, int y, int z, double radx, double rady, double radz, NoiseProvider3 n) { + return getTrihermite(x, y, z, radx, rady, radz, n, 0D, 0D); + } + + public static double getTrihermite(int x, int y, int z, double radx, double rady, double radz, NoiseProvider3 n, double tension, double bias) { + int fx = (int) Math.floor(x / radx); + int fy = (int) Math.floor(y / rady); + int fz = (int) Math.floor(z / radz); + int x0 = (int) Math.round((fx - 1) * radx); + int y0 = (int) Math.round((fy - 1) * rady); + int z0 = (int) Math.round((fz - 1) * radz); + int x1 = (int) Math.round(fx * radx); + int y1 = (int) Math.round(fy * rady); + int z1 = (int) Math.round(fz * radz); + int x2 = (int) Math.round((fx + 1) * radx); + int y2 = (int) Math.round((fy + 1) * rady); + int z2 = (int) Math.round((fz + 1) * radz); + int x3 = (int) Math.round((fx + 2) * radx); + int y3 = (int) Math.round((fy + 2) * rady); + int z3 = (int) Math.round((fz + 2) * radz); double px = rangeScale(0, 1, x1, x2, x); double py = rangeScale(0, 1, y1, y2, y); double pz = rangeScale(0, 1, z1, z2, z); @@ -824,6 +840,23 @@ public class IrisInterpolation { return rad.get(); } + public static double getNoise3D(InterpolationMethod3D method, int x, int y, int z, double radx, double rady, double radz, NoiseProvider3 n) { + return switch (method){ + case TRILINEAR -> getTrilinear(x,y,z,radx, rady, radz, n); + case TRICUBIC -> getTricubic(x,y,z,radx, rady, radz, n); + case TRIHERMITE -> getTrihermite(x,y,z,radx, rady, radz, n); + }; + } + + + public static double getNoise3D(InterpolationMethod3D method, int x, int y, int z, double rad, NoiseProvider3 n) { + return switch (method){ + case TRILINEAR -> getTrilinear(x,y,z,rad, rad, rad, n); + case TRICUBIC -> getTricubic(x,y,z,rad, rad, rad, n); + case TRIHERMITE -> getTrihermite(x,y,z,rad, rad, rad, n); + }; + } + public static double getNoise(InterpolationMethod method, int x, int z, double h, NoiseProvider n) { if (method.equals(InterpolationMethod.BILINEAR)) { return getBilinearNoise(x, z, h, n); diff --git a/src/main/java/com/volmit/iris/engine/interpolation/NoiseBox.java b/src/main/java/com/volmit/iris/engine/interpolation/NoiseBox.java new file mode 100644 index 000000000..fa6a7daf6 --- /dev/null +++ b/src/main/java/com/volmit/iris/engine/interpolation/NoiseBox.java @@ -0,0 +1,59 @@ +/* + * Iris is a World Generator for Minecraft Bukkit Servers + * Copyright (c) 2021 Arcane Arts (Volmit Software) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.volmit.iris.engine.interpolation; + +import com.volmit.iris.util.function.NoiseProvider; + +public class NoiseBox { + private final double[] noise; + private final int w; + + public NoiseBox(int offsetX, int offsetZ, int w, int d, NoiseProvider provider, InterpolationMethod method, int rad) + { + final double[] realNoise; + this.w = w; + int wrad = w/rad; + int drad = d/rad; + noise = new double[w*d]; + realNoise = new double[(wrad+1)*(drad+1)]; + + for(int i = 0; i <= wrad; i++) + { + for(int j = 0; j <= drad; j++) + { + realNoise[(j * wrad) + i] = provider.noise(i, j); + } + } + + NoiseProvider p = (x, z) -> realNoise[(int) ((z * wrad) + x)]; + + for(int i = 0; i < w; i++) + { + for(int j = 0; j < d; j++) + { + noise[(j * w) + i] = IrisInterpolation.getNoise(method, offsetX + i, offsetZ + j, rad, p); + } + } + } + + public double get(int x, int z) + { + return noise[(z * w) + x]; + } +} diff --git a/src/main/java/com/volmit/iris/engine/interpolation/NoiseCube.java b/src/main/java/com/volmit/iris/engine/interpolation/NoiseCube.java new file mode 100644 index 000000000..4f1609bf6 --- /dev/null +++ b/src/main/java/com/volmit/iris/engine/interpolation/NoiseCube.java @@ -0,0 +1,71 @@ +/* + * Iris is a World Generator for Minecraft Bukkit Servers + * Copyright (c) 2021 Arcane Arts (Volmit Software) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.volmit.iris.engine.interpolation; + +import com.volmit.iris.engine.hunk.Hunk; +import com.volmit.iris.engine.hunk.storage.ArrayHunk; +import com.volmit.iris.util.function.NoiseProvider; +import com.volmit.iris.util.function.NoiseProvider3; + +public class NoiseCube { + private final double[] noise; + private final int w; + private final int h; + + public NoiseCube(int offsetX, int offsetY, int offsetZ, int w, int h, int d, NoiseProvider3 provider, InterpolationMethod3D method, int rad) + { + final double[] realNoise; + this.w = w; + this.h = h; + int wrad = w/rad; + int hrad = h/rad; + int drad = d/rad; + noise = new double[w*h*d]; + realNoise = new double[(wrad+1)*(hrad+1)*(drad+1)]; + + for(int i = 0; i <= wrad; i++) + { + for(int j = 0; j <= hrad; j++) + { + for(int k = 0; k <= drad; k++) + { + realNoise[(k * w * h) + (j * w) + i] = provider.noise(i, j, k); + } + } + } + + NoiseProvider3 p = (x, y, z) -> realNoise[(int) ((z * w * h) + (y * w) + x)]; + + for(int i = 0; i < w; i++) + { + for(int j = 0; j < h; j++) + { + for(int k = 0; k < d; k++) + { + noise[(k * w * h) + (j * w) + i] = IrisInterpolation.getNoise3D(method, offsetX + i, offsetY + j,offsetZ + k, rad, p); + } + } + } + } + + public double get(int x, int y, int z) + { + return noise[(z * w * h) + (y * w) + x]; + } +} diff --git a/src/main/java/com/volmit/iris/engine/jigsaw/PlannedStructure.java b/src/main/java/com/volmit/iris/engine/jigsaw/PlannedStructure.java index b94a02374..d2b82a939 100644 --- a/src/main/java/com/volmit/iris/engine/jigsaw/PlannedStructure.java +++ b/src/main/java/com/volmit/iris/engine/jigsaw/PlannedStructure.java @@ -23,6 +23,7 @@ import com.volmit.iris.core.IrisDataManager; import com.volmit.iris.engine.IrisWorlds; import com.volmit.iris.engine.framework.EngineParallaxManager; import com.volmit.iris.engine.framework.IrisAccess; +import com.volmit.iris.engine.interpolation.InterpolationMethod; import com.volmit.iris.engine.object.*; import com.volmit.iris.engine.object.common.IObjectPlacer; import com.volmit.iris.engine.parallax.ParallaxChunkMeta; diff --git a/src/main/java/com/volmit/iris/engine/object/IrisFeature.java b/src/main/java/com/volmit/iris/engine/object/IrisFeature.java index 89f283522..049fd4727 100644 --- a/src/main/java/com/volmit/iris/engine/object/IrisFeature.java +++ b/src/main/java/com/volmit/iris/engine/object/IrisFeature.java @@ -20,6 +20,7 @@ package com.volmit.iris.engine.object; import com.google.gson.Gson; import com.volmit.iris.engine.cache.AtomicCache; +import com.volmit.iris.engine.interpolation.InterpolationMethod; import com.volmit.iris.engine.interpolation.IrisInterpolation; import com.volmit.iris.engine.object.annotations.Desc; import com.volmit.iris.engine.object.annotations.MaxNumber; diff --git a/src/main/java/com/volmit/iris/engine/object/IrisInterpolator.java b/src/main/java/com/volmit/iris/engine/object/IrisInterpolator.java index 4c1f40785..aa579e755 100644 --- a/src/main/java/com/volmit/iris/engine/object/IrisInterpolator.java +++ b/src/main/java/com/volmit/iris/engine/object/IrisInterpolator.java @@ -18,6 +18,7 @@ package com.volmit.iris.engine.object; +import com.volmit.iris.engine.interpolation.InterpolationMethod; import com.volmit.iris.engine.interpolation.IrisInterpolation; import com.volmit.iris.engine.object.annotations.Desc; import com.volmit.iris.engine.object.annotations.MaxNumber; diff --git a/src/main/java/com/volmit/iris/engine/stream/interpolation/InterpolatingStream.java b/src/main/java/com/volmit/iris/engine/stream/interpolation/InterpolatingStream.java index 2b509d168..251bf3f61 100644 --- a/src/main/java/com/volmit/iris/engine/stream/interpolation/InterpolatingStream.java +++ b/src/main/java/com/volmit/iris/engine/stream/interpolation/InterpolatingStream.java @@ -19,7 +19,7 @@ package com.volmit.iris.engine.stream.interpolation; import com.volmit.iris.engine.interpolation.IrisInterpolation; -import com.volmit.iris.engine.object.InterpolationMethod; +import com.volmit.iris.engine.interpolation.InterpolationMethod; import com.volmit.iris.engine.stream.BasicStream; import com.volmit.iris.engine.stream.ProceduralStream; import com.volmit.iris.util.function.NoiseProvider; diff --git a/src/main/java/com/volmit/iris/engine/stream/interpolation/InterpolatorFactory.java b/src/main/java/com/volmit/iris/engine/stream/interpolation/InterpolatorFactory.java index 81a6685ec..01962fc02 100644 --- a/src/main/java/com/volmit/iris/engine/stream/interpolation/InterpolatorFactory.java +++ b/src/main/java/com/volmit/iris/engine/stream/interpolation/InterpolatorFactory.java @@ -18,7 +18,7 @@ package com.volmit.iris.engine.stream.interpolation; -import com.volmit.iris.engine.object.InterpolationMethod; +import com.volmit.iris.engine.interpolation.InterpolationMethod; import com.volmit.iris.engine.stream.ProceduralStream; @SuppressWarnings("ClassCanBeRecord")