From 86b83a53778c94b275f85a07947a028abc8917c9 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Sun, 3 Nov 2024 16:49:31 +0300 Subject: [PATCH] add 'random_sub_uv' particle setting --- res/content/base/scripts/world.lua | 5 +++-- src/graphics/render/Emitter.cpp | 9 +++++++++ src/maths/UVRegion.hpp | 11 +++++++++++ src/presets/ParticlesPreset.cpp | 4 ++++ src/presets/ParticlesPreset.hpp | 4 ++++ 5 files changed, 31 insertions(+), 2 deletions(-) diff --git a/res/content/base/scripts/world.lua b/res/content/base/scripts/world.lua index db8be79a..61e1086a 100644 --- a/res/content/base/scripts/world.lua +++ b/res/content/base/scripts/world.lua @@ -1,8 +1,9 @@ function on_block_broken(id, x, y, z, playerid) particles.emit({x+0.5, y+0.5, z+0.5}, 100, { - lifetime=1.0, + lifetime=2.0, spawn_interval=0.0001, explosion={4, 4, 4}, - texture="blocks:"..block.get_textures(id)[1] + texture="blocks:"..block.get_textures(id)[1], + random_sub_uv=0.1 }) end diff --git a/src/graphics/render/Emitter.cpp b/src/graphics/render/Emitter.cpp index 7c7cb30b..c2763574 100644 --- a/src/graphics/render/Emitter.cpp +++ b/src/graphics/render/Emitter.cpp @@ -67,7 +67,16 @@ void Emitter::update( particle.emitter = this; particle.random = random.rand32(); particle.position = position; + particle.lifetime *= 1.0f - preset.lifetimeSpread * random.randFloat(); particle.velocity += glm::ballRand(1.0f) * preset.explosion; + if (preset.randomSubUV < 1.0f) { + particle.region.autoSub( + preset.randomSubUV, + preset.randomSubUV, + random.randFloat(), + random.randFloat() + ); + } particles.push_back(std::move(particle)); timer -= spawnInterval; if (count > 0) { diff --git a/src/maths/UVRegion.hpp b/src/maths/UVRegion.hpp index ec58dfb5..f7bdb2d0 100644 --- a/src/maths/UVRegion.hpp +++ b/src/maths/UVRegion.hpp @@ -22,4 +22,15 @@ struct UVRegion { inline float getHeight() const { return fabs(v2 - v1); } + + void autoSub(float w, float h, float x, float y) { + x *= 1.0f - w; + y *= 1.0f - h; + float uvw = getWidth(); + float uvh = getHeight(); + u1 = u1 + uvw * x; + v1 = v1 + uvh * y; + u2 = u1 + uvw * w; + v2 = v1 + uvh * h; + } }; diff --git a/src/presets/ParticlesPreset.cpp b/src/presets/ParticlesPreset.cpp index 61833672..490e7cc9 100644 --- a/src/presets/ParticlesPreset.cpp +++ b/src/presets/ParticlesPreset.cpp @@ -10,9 +10,11 @@ dv::value ParticlesPreset::serialize() const { root["max_distance"] = maxDistance; root["spawn_interval"] = spawnInterval; root["lifetime"] = lifetime; + root["lifetime_spread"] = lifetimeSpread; root["acceleration"] = dv::to_value(acceleration); root["explosion"] = dv::to_value(explosion); root["size"] = dv::to_value(size); + root["random_sub_uv"] = randomSubUV; return root; } @@ -23,6 +25,8 @@ void ParticlesPreset::deserialize(const dv::value& src) { src.at("max_distance").get(maxDistance); src.at("spawn_interval").get(spawnInterval); src.at("lifetime").get(lifetime); + src.at("lifetime_spread").get(lifetimeSpread); + src.at("random_sub_uv").get(randomSubUV); if (src.has("acceleration")) { dv::get_vec(src["acceleration"], acceleration); } diff --git a/src/presets/ParticlesPreset.hpp b/src/presets/ParticlesPreset.hpp index 4bd2c07d..48658f8c 100644 --- a/src/presets/ParticlesPreset.hpp +++ b/src/presets/ParticlesPreset.hpp @@ -15,6 +15,8 @@ struct ParticlesPreset : public Serializable { float spawnInterval = 0.1f; /// @brief Particle life time float lifetime = 5.0f; + /// @brief Life time spread divided by lifetime + float lifetimeSpread = 0.2f; /// @brief Initial velocity glm::vec3 velocity {}; /// @brief Velocity acceleration @@ -25,6 +27,8 @@ struct ParticlesPreset : public Serializable { glm::vec3 size {0.1f}; /// @brief Texture name std::string texture = ""; + /// @brief Size of random sub-uv region + float randomSubUV = 1.0f; dv::value serialize() const override; void deserialize(const dv::value& src) override;