add 'random_sub_uv' particle setting

This commit is contained in:
MihailRis 2024-11-03 16:49:31 +03:00
parent c8187c5f25
commit 86b83a5377
5 changed files with 31 additions and 2 deletions

View File

@ -1,8 +1,9 @@
function on_block_broken(id, x, y, z, playerid) function on_block_broken(id, x, y, z, playerid)
particles.emit({x+0.5, y+0.5, z+0.5}, 100, { particles.emit({x+0.5, y+0.5, z+0.5}, 100, {
lifetime=1.0, lifetime=2.0,
spawn_interval=0.0001, spawn_interval=0.0001,
explosion={4, 4, 4}, explosion={4, 4, 4},
texture="blocks:"..block.get_textures(id)[1] texture="blocks:"..block.get_textures(id)[1],
random_sub_uv=0.1
}) })
end end

View File

@ -67,7 +67,16 @@ void Emitter::update(
particle.emitter = this; particle.emitter = this;
particle.random = random.rand32(); particle.random = random.rand32();
particle.position = position; particle.position = position;
particle.lifetime *= 1.0f - preset.lifetimeSpread * random.randFloat();
particle.velocity += glm::ballRand(1.0f) * preset.explosion; 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)); particles.push_back(std::move(particle));
timer -= spawnInterval; timer -= spawnInterval;
if (count > 0) { if (count > 0) {

View File

@ -22,4 +22,15 @@ struct UVRegion {
inline float getHeight() const { inline float getHeight() const {
return fabs(v2 - v1); 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;
}
}; };

View File

@ -10,9 +10,11 @@ dv::value ParticlesPreset::serialize() const {
root["max_distance"] = maxDistance; root["max_distance"] = maxDistance;
root["spawn_interval"] = spawnInterval; root["spawn_interval"] = spawnInterval;
root["lifetime"] = lifetime; root["lifetime"] = lifetime;
root["lifetime_spread"] = lifetimeSpread;
root["acceleration"] = dv::to_value(acceleration); root["acceleration"] = dv::to_value(acceleration);
root["explosion"] = dv::to_value(explosion); root["explosion"] = dv::to_value(explosion);
root["size"] = dv::to_value(size); root["size"] = dv::to_value(size);
root["random_sub_uv"] = randomSubUV;
return root; return root;
} }
@ -23,6 +25,8 @@ void ParticlesPreset::deserialize(const dv::value& src) {
src.at("max_distance").get(maxDistance); src.at("max_distance").get(maxDistance);
src.at("spawn_interval").get(spawnInterval); src.at("spawn_interval").get(spawnInterval);
src.at("lifetime").get(lifetime); src.at("lifetime").get(lifetime);
src.at("lifetime_spread").get(lifetimeSpread);
src.at("random_sub_uv").get(randomSubUV);
if (src.has("acceleration")) { if (src.has("acceleration")) {
dv::get_vec(src["acceleration"], acceleration); dv::get_vec(src["acceleration"], acceleration);
} }

View File

@ -15,6 +15,8 @@ struct ParticlesPreset : public Serializable {
float spawnInterval = 0.1f; float spawnInterval = 0.1f;
/// @brief Particle life time /// @brief Particle life time
float lifetime = 5.0f; float lifetime = 5.0f;
/// @brief Life time spread divided by lifetime
float lifetimeSpread = 0.2f;
/// @brief Initial velocity /// @brief Initial velocity
glm::vec3 velocity {}; glm::vec3 velocity {};
/// @brief Velocity acceleration /// @brief Velocity acceleration
@ -25,6 +27,8 @@ struct ParticlesPreset : public Serializable {
glm::vec3 size {0.1f}; glm::vec3 size {0.1f};
/// @brief Texture name /// @brief Texture name
std::string texture = ""; std::string texture = "";
/// @brief Size of random sub-uv region
float randomSubUV = 1.0f;
dv::value serialize() const override; dv::value serialize() const override;
void deserialize(const dv::value& src) override; void deserialize(const dv::value& src) override;