From 33a811245408b7353f07074f2b237238dc4dfbe2 Mon Sep 17 00:00:00 2001 From: minjaesong Date: Sun, 30 Jul 2023 03:29:14 +0900 Subject: [PATCH] skybox: taller grad window, smooth grad clamping --- src/com/jme3/math/FastMath.java | 13 +++++++ .../terrarum/modulebasegame/clut/Skybox.kt | 34 +++++++++++++++---- .../terrarum/ui/BasicDebugInfoWindow.kt | 2 +- .../torvald/terrarum/weather/WeatherMixer.kt | 5 ++- 4 files changed, 46 insertions(+), 8 deletions(-) diff --git a/src/com/jme3/math/FastMath.java b/src/com/jme3/math/FastMath.java index fa44f1adf..b0c9cf60a 100644 --- a/src/com/jme3/math/FastMath.java +++ b/src/com/jme3/math/FastMath.java @@ -166,6 +166,19 @@ final public class FastMath { return ((1f - scale) * startValue) + (scale * endValue); } + public static double interpolateLinear(double scale, double startValue, double endValue) { + if (startValue == endValue) { + return startValue; + } + if (scale <= 0.0) { + return startValue; + } + if (scale >= 1.0) { + return endValue; + } + return ((1.0 - scale) * startValue) + (scale * endValue); + } + /** * Linear interpolation from startValue to endValue by the given percent. * Basically: ((1 - percent) * startValue) + (percent * endValue) diff --git a/src/net/torvald/terrarum/modulebasegame/clut/Skybox.kt b/src/net/torvald/terrarum/modulebasegame/clut/Skybox.kt index b518d9059..392619879 100644 --- a/src/net/torvald/terrarum/modulebasegame/clut/Skybox.kt +++ b/src/net/torvald/terrarum/modulebasegame/clut/Skybox.kt @@ -1,8 +1,10 @@ package net.torvald.terrarum.modulebasegame.clut +import com.badlogic.gdx.graphics.Color import com.badlogic.gdx.graphics.Pixmap import com.badlogic.gdx.graphics.Texture import com.badlogic.gdx.utils.Disposable +import com.jme3.math.FastMath import net.torvald.colourutil.CIEXYZ import net.torvald.colourutil.toColor import net.torvald.colourutil.toRGB @@ -100,18 +102,35 @@ object Skybox : Disposable { private fun polynomialDecay(p: Double, q: Int, x: Double): Double { val sign = if (q % 2 == 1) -1 else 1 val a1 = -1.0 / p - val a2 = -1.0 / (1.0 - p) + val a2 = 1.0 / (1.0 - p) val q = q.toDouble() return if (x < p) sign * a1.pow(q - 1.0) * x.pow(q) + 1.0 else - sign * a1.pow(q - 1.0) * (x - 1.0).pow(q) + sign * a2.pow(q - 1.0) * (x - 1.0).pow(q) + } + + private fun polynomialDecay2(p: Double, q: Int, x: Double): Double { + val sign = if (q % 2 == 1) 1 else -1 + val a1 = -1.0 / p + val a2 = 1.0 / (1.0 - p) + val q = q.toDouble() + return if (x < p) + sign * a1.pow(q - 1.0) * x.pow(q) + else + sign * a2.pow(q - 1.0) * (x - 1.0).pow(q) + 1.0 } private fun superellipsoidDecay(p: Double, x: Double): Double { return 1.0 - (1.0 - (1.0 - x).pow(1.0 / p)).pow(p) } + private fun Double.coerceInSmoothly(low: Double, high: Double): Double { + val x = this.coerceIn(low, high) + val x2 = ((x - low) * (high - low).pow(-1.0)) + return FastMath.interpolateLinear(polynomialDecay2(0.5, 2, x2), low, high) + } + private fun getTexturmaps(albedo: Double): Array { return Array(elevCnt * turbCnt) { @@ -124,14 +143,16 @@ object Skybox : Disposable { // printdbg(this, "elev $elevationDeg turb $turbidity") - for (y in 0 until gradSize) { + for (yp in 0 until gradSize) { + val yi = yp - 6 val xf = -elevationDeg / 90.0 - var yf = (y + 0.5) / gradSize.toDouble() + var yf = (yi / 116.0).coerceInSmoothly(0.0, 0.95) + // experiments visualisation: https://www.desmos.com/calculator/5crifaekwa // if (elevationDeg < 0) yf *= 1.0 - pow(xf, 0.333) // if (elevationDeg < 0) yf *= -2.0 * asin(xf - 1.0) / PI if (elevationDeg < 0) yf *= superellipsoidDecay(1.0 / 3.0, xf) - val theta = yf.mapCircle() * HALF_PI + val theta = (yf.mapCircle() * HALF_PI) // vertical angle, where 0 is zenith, ±90 is ground (which is odd) val xyz = CIEXYZ( @@ -142,8 +163,9 @@ object Skybox : Disposable { val xyz2 = xyz.scaleToFit(elevationDeg) val rgb = xyz2.toRGB().toColor() +// pixmap.setColor(if (yp in 17 until 17 + 94) Color.LIME else Color.CORAL) pixmap.setColor(rgb) - pixmap.drawPixel(0, y) + pixmap.drawPixel(0, yp) } val texture = Texture(pixmap).also { diff --git a/src/net/torvald/terrarum/ui/BasicDebugInfoWindow.kt b/src/net/torvald/terrarum/ui/BasicDebugInfoWindow.kt index 6cdfa3628..5e8ffd6d3 100644 --- a/src/net/torvald/terrarum/ui/BasicDebugInfoWindow.kt +++ b/src/net/torvald/terrarum/ui/BasicDebugInfoWindow.kt @@ -208,7 +208,7 @@ class BasicDebugInfoWindow : UICanvas() { App.fontSmallNumbers.draw(batch, "$SOL $soldegCol$soldegStr", gap + 7f*(sol), line(mvY)) App.fontSmallNumbers.draw(batch, "$TAU $turbCol$turbidity", gap + 7f*(sol), line(mvY + 1)) - + App.fontSmallNumbers.draw(batch, "${ccG}p ${WeatherMixer.parallaxPos.toDouble().toIntAndFrac(1,2)}", gap + 7f*51, line(mvY)) try { world?.let { diff --git a/src/net/torvald/terrarum/weather/WeatherMixer.kt b/src/net/torvald/terrarum/weather/WeatherMixer.kt index a486b2d85..e22d1c163 100644 --- a/src/net/torvald/terrarum/weather/WeatherMixer.kt +++ b/src/net/torvald/terrarum/weather/WeatherMixer.kt @@ -152,7 +152,9 @@ internal object WeatherMixer : RNGConsumer { } var turbidity = 4.0; private set - private var gH = 1.5f * App.scr.height + private var gH = (4f/3f) * App.scr.height + + internal var parallaxPos = 0f; private set private val HALF_DAY = DAY_LENGTH / 2 /** @@ -187,6 +189,7 @@ internal object WeatherMixer : RNGConsumer { -+ <- 0.0 = */ val parallax = ((parallaxZeroPos - WorldCamera.gdxCamY.div(TILE_SIZEF)) / parallaxDomainSize).times(-1f).coerceIn(-1f, 1f) + parallaxPos = parallax // println(parallax) // parallax value works as intended. gdxBlendNormalStraightAlpha()