From 00ca23c2b8ac3f26c34a17cd8db7cf9315492b3d Mon Sep 17 00:00:00 2001 From: Bixilon Date: Wed, 2 Nov 2022 14:12:12 +0100 Subject: [PATCH] sky: calculate day and night time --- .../data/text/formatting/color/RGBColor.kt | 2 ++ .../minosoft/data/world/time/MoonPhases.kt | 18 ++++++++--------- .../minosoft/data/world/time/WorldTime.kt | 2 +- .../gui/rendering/sky/box/SkyboxRenderer.kt | 20 +++++++++++++++---- 4 files changed, 28 insertions(+), 14 deletions(-) diff --git a/src/main/java/de/bixilon/minosoft/data/text/formatting/color/RGBColor.kt b/src/main/java/de/bixilon/minosoft/data/text/formatting/color/RGBColor.kt index a57531473..6bad6bc4b 100644 --- a/src/main/java/de/bixilon/minosoft/data/text/formatting/color/RGBColor.kt +++ b/src/main/java/de/bixilon/minosoft/data/text/formatting/color/RGBColor.kt @@ -33,6 +33,8 @@ class RGBColor(val rgba: Int) : ChatCode, TextFormattable { constructor(red: Double, green: Double, blue: Double, alpha: Double = 1.0) : this(red.toFloat(), green.toFloat(), blue.toFloat(), alpha.toFloat()) + constructor(vec3: Vec3) : this(vec3.r, vec3.g, vec3.b) + val argb: Int get() = (alpha shl 24) or (red shl 16) or (green shl 8) or blue diff --git a/src/main/java/de/bixilon/minosoft/data/world/time/MoonPhases.kt b/src/main/java/de/bixilon/minosoft/data/world/time/MoonPhases.kt index 62ebdfd22..3d6860962 100644 --- a/src/main/java/de/bixilon/minosoft/data/world/time/MoonPhases.kt +++ b/src/main/java/de/bixilon/minosoft/data/world/time/MoonPhases.kt @@ -17,15 +17,15 @@ import de.bixilon.kutil.enums.EnumUtil import de.bixilon.kutil.enums.ValuesEnum // see https://minecraft.fandom.com/wiki/Moon -enum class MoonPhases { - FULL_MOON, - WANING_GIBBOUS, - LAST_QUARTER, - WANING_CRESCENT, - NEW_MOON, - WAXING_CRESCENT, - FIRST_QUARTER, - WAXING_GIBBOUS, +enum class MoonPhases(val light: Float) { + FULL_MOON(1.0f), + WANING_GIBBOUS(0.7f), + LAST_QUARTER(0.4f), + WANING_CRESCENT(0.2f), + NEW_MOON(0.0f), + WAXING_CRESCENT(0.2f), + FIRST_QUARTER(0.4f), + WAXING_GIBBOUS(0.7f), ; companion object : ValuesEnum { diff --git a/src/main/java/de/bixilon/minosoft/data/world/time/WorldTime.kt b/src/main/java/de/bixilon/minosoft/data/world/time/WorldTime.kt index 8765dca8c..6a04b6558 100644 --- a/src/main/java/de/bixilon/minosoft/data/world/time/WorldTime.kt +++ b/src/main/java/de/bixilon/minosoft/data/world/time/WorldTime.kt @@ -29,7 +29,7 @@ class WorldTime( val time = abs(time) % ProtocolDefinition.TICKS_PER_DAY val cycling = time >= 0 - val moonPhase = MoonPhases[age.toInt() % ProtocolDefinition.TICKS_PER_DAY % MoonPhases.VALUES.size] // ToDo: Verify + val moonPhase = MoonPhases[age.toInt() / ProtocolDefinition.TICKS_PER_DAY % MoonPhases.VALUES.size] // ToDo: Verify val phase = DayPhases.of(time) diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/sky/box/SkyboxRenderer.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/sky/box/SkyboxRenderer.kt index 01872eb4d..964de1f7a 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/sky/box/SkyboxRenderer.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/sky/box/SkyboxRenderer.kt @@ -14,6 +14,7 @@ package de.bixilon.minosoft.gui.rendering.sky.box import de.bixilon.kotlinglm.vec2.Vec2i +import de.bixilon.kotlinglm.vec3.Vec3 import de.bixilon.kutil.watcher.DataWatcher.Companion.observe import de.bixilon.kutil.watcher.DataWatcher.Companion.watched import de.bixilon.minosoft.data.registries.biomes.Biome @@ -29,7 +30,11 @@ import de.bixilon.minosoft.gui.rendering.sky.properties.DefaultSkyProperties import de.bixilon.minosoft.gui.rendering.sky.properties.SkyProperties import de.bixilon.minosoft.gui.rendering.system.base.texture.texture.AbstractTexture import de.bixilon.minosoft.gui.rendering.util.vec.vec3.Vec3Util.blockPosition +import de.bixilon.minosoft.gui.rendering.util.vec.vec3.Vec3Util.interpolateLinear +import de.bixilon.minosoft.gui.rendering.util.vec.vec3.Vec3Util.interpolateSine import de.bixilon.minosoft.util.KUtil.minosoft +import kotlin.math.abs +import kotlin.math.pow class SkyboxRenderer( private val sky: SkyRenderer, @@ -136,16 +141,23 @@ class SkyboxRenderer( return ChatColors.YELLOW } - private fun calculateDaytime(progress: Float): RGBColor { - return ChatColors.BLUE + private fun calculateDaytime(progress: Float): RGBColor? { + val base = calculateBiomeAvg { it.skyColor }?.toVec3() ?: return null + + return RGBColor(interpolateLinear((abs(progress - 0.5f) * 2.0f).pow(2), base, base * 0.9f)) } private fun calculateSunset(progress: Float): RGBColor { return ChatColors.RED } - private fun calculateNight(progress: Float): RGBColor { - return ChatColors.DARK_BLUE + private fun calculateNight(progress: Float): RGBColor? { + val base = calculateBiomeAvg { it.skyColor }?.toVec3() ?: return null + base *= 0.2 + + val minColor = Vec3(0.02f, 0.04f, 0.09f) * time.moonPhase.light + + return RGBColor(interpolateSine(abs(progress - 0.5f) * 2.0f, minColor, base)) } private fun calculateSkyColor(): RGBColor? {