add particles.emit(...) (WIP)
This commit is contained in:
@@ -12,18 +12,17 @@
|
||||
Emitter::Emitter(
|
||||
const Level& level,
|
||||
std::variant<glm::vec3, entityid_t> origin,
|
||||
Particle prototype,
|
||||
ParticlesPreset preset,
|
||||
const Texture* texture,
|
||||
float spawnInterval,
|
||||
const UVRegion& region,
|
||||
int count
|
||||
)
|
||||
: level(level),
|
||||
origin(std::move(origin)),
|
||||
prototype(std::move(prototype)),
|
||||
prototype({this, 0, glm::vec3(), preset.velocity, preset.lifetime, region}),
|
||||
texture(texture),
|
||||
spawnInterval(spawnInterval),
|
||||
count(count),
|
||||
preset() {
|
||||
preset(std::move(preset)) {
|
||||
this->prototype.emitter = this;
|
||||
}
|
||||
|
||||
@@ -36,6 +35,7 @@ void Emitter::update(
|
||||
const glm::vec3& cameraPosition,
|
||||
std::vector<Particle>& particles
|
||||
) {
|
||||
const float spawnInterval = preset.spawnInterval;
|
||||
if (count == 0 || (count == -1 && spawnInterval < FLT_EPSILON)) {
|
||||
return;
|
||||
}
|
||||
@@ -67,7 +67,7 @@ void Emitter::update(
|
||||
particle.emitter = this;
|
||||
particle.random = random.rand32();
|
||||
particle.position = position;
|
||||
particle.velocity += glm::ballRand(1.0f) * explosion;
|
||||
particle.velocity += glm::ballRand(1.0f) * preset.explosion;
|
||||
particles.push_back(std::move(particle));
|
||||
timer -= spawnInterval;
|
||||
if (count > 0) {
|
||||
@@ -76,10 +76,6 @@ void Emitter::update(
|
||||
}
|
||||
}
|
||||
|
||||
void Emitter::setExplosion(const glm::vec3& magnitude) {
|
||||
explosion = magnitude;
|
||||
}
|
||||
|
||||
bool Emitter::isDead() const {
|
||||
return count == 0;
|
||||
}
|
||||
|
||||
@@ -31,24 +31,22 @@ struct Particle {
|
||||
|
||||
class Texture;
|
||||
|
||||
using EmitterOrigin = std::variant<glm::vec3, entityid_t>;
|
||||
|
||||
class Emitter {
|
||||
const Level& level;
|
||||
/// @brief Static position or entity
|
||||
std::variant<glm::vec3, entityid_t> origin;
|
||||
EmitterOrigin origin;
|
||||
/// @brief Particle prototype
|
||||
Particle prototype;
|
||||
/// @brief Particle
|
||||
const Texture* texture;
|
||||
/// @brief Particles spawn interval
|
||||
float spawnInterval;
|
||||
/// @brief Number of particles should be spawned before emitter deactivation.
|
||||
/// -1 is infinite.
|
||||
int count;
|
||||
/// @brief Spawn timer used to determine number of particles
|
||||
/// to spawn on update. May be innacurate.
|
||||
float timer = 0.0f;
|
||||
/// @brief Random velocity magnitude applying to spawned particles.
|
||||
glm::vec3 explosion {8.0f};
|
||||
|
||||
util::PseudoRandom random;
|
||||
public:
|
||||
@@ -57,9 +55,9 @@ public:
|
||||
Emitter(
|
||||
const Level& level,
|
||||
std::variant<glm::vec3, entityid_t> origin,
|
||||
Particle prototype,
|
||||
ParticlesPreset preset,
|
||||
const Texture* texture,
|
||||
float spawnInterval,
|
||||
const UVRegion& region,
|
||||
int count
|
||||
);
|
||||
|
||||
@@ -77,10 +75,7 @@ public:
|
||||
const glm::vec3& cameraPosition,
|
||||
std::vector<Particle>& particles
|
||||
);
|
||||
|
||||
/// @brief Set initial random velocity magitude
|
||||
void setExplosion(const glm::vec3& magnitude);
|
||||
|
||||
|
||||
/// @return true if the emitter has spawned all particles
|
||||
bool isDead() const;
|
||||
};
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
#include "ParticlesRenderer.hpp"
|
||||
|
||||
#include "assets/Assets.hpp"
|
||||
#include "assets/assets_util.hpp"
|
||||
#include "graphics/core/Shader.hpp"
|
||||
#include "graphics/core/Texture.hpp"
|
||||
#include "graphics/render/MainBatch.hpp"
|
||||
@@ -18,17 +17,7 @@ ParticlesRenderer::ParticlesRenderer(
|
||||
)
|
||||
: batch(std::make_unique<MainBatch>(1024)),
|
||||
level(level),
|
||||
settings(settings) {
|
||||
auto region = util::get_texture_region(assets, "blocks:grass_side", "");
|
||||
emitters.push_back(std::make_unique<Emitter>(
|
||||
level,
|
||||
glm::vec3(0, 80, 0),
|
||||
Particle {nullptr, 0, glm::vec3(), glm::vec3(), 5.0f, region.region},
|
||||
region.texture,
|
||||
0.002f,
|
||||
-1
|
||||
));
|
||||
}
|
||||
settings(settings) {}
|
||||
|
||||
ParticlesRenderer::~ParticlesRenderer() = default;
|
||||
|
||||
@@ -68,21 +57,22 @@ void ParticlesRenderer::renderParticles(const Camera& camera, float delta) {
|
||||
auto iter = vec.begin();
|
||||
while (iter != vec.end()) {
|
||||
auto& particle = *iter;
|
||||
auto& preset = particle.emitter->preset;
|
||||
|
||||
update_particle(particle, delta, chunks);
|
||||
|
||||
glm::vec4 light(1, 1, 1, 0);
|
||||
if (particle.emitter->preset.lighting) {
|
||||
if (preset.lighting) {
|
||||
light = MainBatch::sampleLight(
|
||||
particle.position, chunks, backlight
|
||||
);
|
||||
light *= 0.8f + (particle.random % 200) * 0.001f;
|
||||
light *= 0.9f + (particle.random % 100) * 0.001f;
|
||||
}
|
||||
batch->quad(
|
||||
particle.position,
|
||||
right,
|
||||
up,
|
||||
glm::vec2(0.3f),
|
||||
preset.size,
|
||||
light,
|
||||
glm::vec3(1.0f),
|
||||
particle.region
|
||||
@@ -128,3 +118,7 @@ void ParticlesRenderer::render(const Camera& camera, float delta) {
|
||||
iter++;
|
||||
}
|
||||
}
|
||||
|
||||
void ParticlesRenderer::add(std::unique_ptr<Emitter> emitter) {
|
||||
emitters.push_back(std::move(emitter));
|
||||
}
|
||||
|
||||
@@ -31,6 +31,8 @@ public:
|
||||
|
||||
void render(const Camera& camera, float delta);
|
||||
|
||||
void add(std::unique_ptr<Emitter> emitter);
|
||||
|
||||
static size_t visibleParticles;
|
||||
static size_t aliveEmitters;
|
||||
};
|
||||
|
||||
@@ -44,6 +44,7 @@
|
||||
#include "ChunksRenderer.hpp"
|
||||
#include "ModelBatch.hpp"
|
||||
#include "Skybox.hpp"
|
||||
#include "Emitter.hpp"
|
||||
|
||||
bool WorldRenderer::showChunkBorders = false;
|
||||
bool WorldRenderer::showEntitiesDebug = false;
|
||||
@@ -534,6 +535,10 @@ void WorldRenderer::drawBorders(
|
||||
lineBatch->flush();
|
||||
}
|
||||
|
||||
void WorldRenderer::addEmitter(std::unique_ptr<Emitter> emitter) {
|
||||
particles->add(std::move(emitter));
|
||||
}
|
||||
|
||||
void WorldRenderer::clear() {
|
||||
renderer->clear();
|
||||
}
|
||||
|
||||
@@ -8,8 +8,6 @@
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
#include "Emitter.hpp"
|
||||
|
||||
class Level;
|
||||
class Player;
|
||||
class Camera;
|
||||
@@ -27,6 +25,7 @@ class PostProcessing;
|
||||
class DrawContext;
|
||||
class ModelBatch;
|
||||
class Assets;
|
||||
class Emitter;
|
||||
struct EngineSettings;
|
||||
|
||||
namespace model {
|
||||
@@ -109,5 +108,7 @@ public:
|
||||
bool pause
|
||||
);
|
||||
|
||||
void addEmitter(std::unique_ptr<Emitter> emitter);
|
||||
|
||||
void clear();
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user