From 991cf9df7e4505a6aa43a2a999049f15af3f9a41 Mon Sep 17 00:00:00 2001 From: Kae <80987908+Novaenia@users.noreply.github.com> Date: Thu, 12 Oct 2023 04:42:24 +1100 Subject: [PATCH] Add vertical speed support to parallax layers --- source/game/StarParallax.cpp | 20 ++++++++++++++------ source/game/StarParallax.hpp | 2 +- source/rendering/StarEnvironmentPainter.cpp | 8 ++++++-- 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/source/game/StarParallax.cpp b/source/game/StarParallax.cpp index 63025ab..fe20f95 100644 --- a/source/game/StarParallax.cpp +++ b/source/game/StarParallax.cpp @@ -13,7 +13,7 @@ ParallaxLayer::ParallaxLayer() { timeOfDayCorrelation = ""; zLevel = 0; verticalOrigin = 0; - speed = 0; + speed = { 0, 0 }; unlit = false; lightMapped = false; fadePercent = 0; @@ -32,7 +32,7 @@ ParallaxLayer::ParallaxLayer(Json const& store) : ParallaxLayer() { zLevel = store.getFloat("zLevel"); parallaxOffset = jsonToVec2F(store.get("parallaxOffset")); timeOfDayCorrelation = store.getString("timeOfDayCorrelation"); - speed = store.getFloat("speed"); + speed = { store.getFloat("speed"), store.getFloat("speedY", 0.0f) }; unlit = store.getBool("unlit"); lightMapped = store.getBool("lightMapped"); fadePercent = store.getFloat("fadePercent"); @@ -50,7 +50,8 @@ Json ParallaxLayer::store() const { {"zLevel", zLevel}, {"parallaxOffset", jsonFromVec2F(parallaxOffset)}, {"timeOfDayCorrelation", timeOfDayCorrelation}, - {"speed", speed}, + {"speed", speed[0]}, + {"speedY", speed[1]}, {"unlit", unlit}, {"lightMapped", lightMapped}, {"fadePercent", fadePercent}}; @@ -227,12 +228,19 @@ void Parallax::buildLayer(Json const& layerSettings, String const& kind) { layer.verticalOrigin = m_verticalOrigin; layer.zLevel = layer.parallaxValue.sum(); - layer.parallaxOffset = {layerSettings.getArray("offset", {0, 0})[0].toFloat(), - layerSettings.getArray("offset", {0, 0})[1].toFloat()}; // shift from bottom left to horizon level in the image + auto offset = layerSettings.getArray("offset", { 0, 0 }); + layer.parallaxOffset = { offset[0].toFloat(), offset[1].toFloat() }; // shift from bottom left to horizon level in the image if (!layerSettings.getBool("noRandomOffset", false)) layer.parallaxOffset[0] += rnd.randInt(imgMetadata->imageSize(layer.textures[0])[0]); layer.timeOfDayCorrelation = layerSettings.getString("timeOfDayCorrelation", ""); - layer.speed = rnd.randf(layerSettings.getFloat("minSpeed", 0), layerSettings.getFloat("maxSpeed", 0)); + auto minSpeed = layerSettings.get("minSpeed", 0.0f); + auto maxSpeed = layerSettings.get("maxSpeed", 0.0f); + if (!minSpeed.isType(Json::Type::Array)) + minSpeed = JsonArray{ minSpeed, 0.0f }; + if (!maxSpeed.isType(Json::Type::Array)) + maxSpeed = JsonArray{ maxSpeed, 0.0f }; + layer.speed = { rnd.randf(minSpeed.getFloat(0), maxSpeed.getFloat(0)), + rnd.randf(minSpeed.getFloat(1), maxSpeed.getFloat(1)) }; layer.unlit = layerSettings.getBool("unlit", false); layer.lightMapped = layerSettings.getBool("lightMapped", false); diff --git a/source/game/StarParallax.hpp b/source/game/StarParallax.hpp index ca82564..c28145b 100644 --- a/source/game/StarParallax.hpp +++ b/source/game/StarParallax.hpp @@ -31,7 +31,7 @@ struct ParallaxLayer { float zLevel; Vec2F parallaxOffset; String timeOfDayCorrelation; - float speed; + Vec2F speed; bool unlit; bool lightMapped; float fadePercent; diff --git a/source/rendering/StarEnvironmentPainter.cpp b/source/rendering/StarEnvironmentPainter.cpp index ff6cb35..c938ae0 100644 --- a/source/rendering/StarEnvironmentPainter.cpp +++ b/source/rendering/StarEnvironmentPainter.cpp @@ -256,10 +256,14 @@ void EnvironmentPainter::renderParallaxLayers( // texture offset in *screen pixel space* Vec2F parallaxOffset = layer.parallaxOffset * camera.pixelRatio(); - if (layer.speed != 0) { - double drift = fmod((double)layer.speed * (sky.epochTime / (double)sky.dayLength), (double)parallaxSize[0]); + if (layer.speed[0] != 0) { + double drift = fmod((double)layer.speed[0] * (sky.epochTime / (double)sky.dayLength), (double)parallaxSize[0]); parallaxOffset[0] = fmod(parallaxOffset[0] + drift * camera.pixelRatio(), parallaxPixels[0]); } + if (layer.speed[1] != 0) { + double drift = fmod((double)layer.speed[1] * (sky.epochTime / (double)sky.dayLength), (double)parallaxSize[1]); + parallaxOffset[1] = fmod(parallaxOffset[1] + drift * camera.pixelRatio(), parallaxPixels[1]); + } // parallax camera world position in *parallax space* Vec2F parallaxCameraCenter = parallaxWorldPosition - parallaxOrigin;