Added scale and color interpolation
This commit is contained in:
@@ -27,9 +27,10 @@ void Systems::ParticleSystem::UpdateEntity(double dt, EntityID entity, EntityID
|
|||||||
std::list<ParticleData>::iterator it;
|
std::list<ParticleData>::iterator it;
|
||||||
for(it = m_ParticleEmitter[entity].begin(); it == m_ParticleEmitter[entity].end();)
|
for(it = m_ParticleEmitter[entity].begin(); it == m_ParticleEmitter[entity].end();)
|
||||||
{
|
{
|
||||||
it->TimeLived += dt;
|
|
||||||
auto particleComponent = m_World->GetComponent<Components::Particle>(it->ParticleID, "Particle");
|
auto particleComponent = m_World->GetComponent<Components::Particle>(it->ParticleID, "Particle");
|
||||||
if(it->TimeLived > particleComponent->LifeTime)
|
|
||||||
|
int timeLived = glfwGetTime() - it->SpawnTime;
|
||||||
|
if(timeLived > particleComponent->LifeTime)
|
||||||
{
|
{
|
||||||
m_ParticleEmitter[entity].erase(it);
|
m_ParticleEmitter[entity].erase(it);
|
||||||
break;
|
break;
|
||||||
@@ -39,7 +40,20 @@ void Systems::ParticleSystem::UpdateEntity(double dt, EntityID entity, EntityID
|
|||||||
it++;
|
it++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Interpolates the color for each color channel by the start and end value. Decides how much the color should be interpolated based on time.
|
||||||
|
// How big fraction the color is multiplied with
|
||||||
|
float timeProgress = timeLived / particleComponent->LifeTime;
|
||||||
|
// The difference between the start and end value
|
||||||
|
float deltaColor = glm::abs(particleComponent->ColorSpectrum[0].r - particleComponent->ColorSpectrum[1].r);
|
||||||
|
it->color.r = particleComponent->ColorSpectrum[0].r + deltaColor * timeProgress;
|
||||||
|
deltaColor = glm::abs(particleComponent->ColorSpectrum[0].g - particleComponent->ColorSpectrum[1].g);
|
||||||
|
it->color.g = particleComponent->ColorSpectrum[0].g + deltaColor * timeProgress;
|
||||||
|
deltaColor = glm::abs(particleComponent->ColorSpectrum[0].b - particleComponent->ColorSpectrum[1].b);
|
||||||
|
it->color.b = particleComponent->ColorSpectrum[0].b + deltaColor * timeProgress;
|
||||||
|
|
||||||
|
//Interpolates the scale for the particle
|
||||||
|
float deltaScale = glm::abs(particleComponent->ScaleSpectrum[0] - particleComponent->ScaleSpectrum[1]);
|
||||||
|
it->Scale = particleComponent->ScaleSpectrum[0] + deltaScale * timeProgress;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -56,13 +70,25 @@ void Systems::ParticleSystem::SpawnParticles(EntityID emitterID, float spawnCoun
|
|||||||
for(int i = 0; i < spawnCount; i++)
|
for(int i = 0; i < spawnCount; i++)
|
||||||
{
|
{
|
||||||
auto ent = m_World->CreateEntity();
|
auto ent = m_World->CreateEntity();
|
||||||
|
|
||||||
auto transform = m_World->AddComponent<Components::Transform>(ent, "Transform");
|
auto transform = m_World->AddComponent<Components::Transform>(ent, "Transform");
|
||||||
transform->Position = glm::vec3(0);
|
transform->Position = glm::vec3(0);
|
||||||
|
|
||||||
auto particle = m_World->AddComponent<Components::Particle>(ent, "Particle");
|
auto particle = m_World->AddComponent<Components::Particle>(ent, "Particle");
|
||||||
particle->LifeTime = 4;
|
particle->LifeTime = 4;
|
||||||
|
Color startColor = {.4f, .45f, .2f};
|
||||||
|
particle->ColorSpectrum.push_back(startColor);
|
||||||
|
Color endColor = {0.f, 45.f, 23.f};
|
||||||
|
particle->ColorSpectrum.push_back(endColor);
|
||||||
|
particle->ScaleSpectrum.push_back(1);
|
||||||
|
particle->ScaleSpectrum.push_back(30);
|
||||||
|
|
||||||
ParticleData data;
|
ParticleData data;
|
||||||
data.ParticleID = ent;
|
data.ParticleID = ent;
|
||||||
data.TimeLived = 0;
|
data.SpawnTime = glfwGetTime();
|
||||||
|
data.color = particle->ColorSpectrum[0];
|
||||||
|
data.Scale = particle->ScaleSpectrum[0];
|
||||||
|
|
||||||
m_ParticleEmitter[emitterID].push_back(data);
|
m_ParticleEmitter[emitterID].push_back(data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,8 @@
|
|||||||
#include "Components/Transform.h"
|
#include "Components/Transform.h"
|
||||||
#include "Components/ParticleEmitter.h"
|
#include "Components/ParticleEmitter.h"
|
||||||
#include "Components/Particle.h"
|
#include "Components/Particle.h"
|
||||||
|
#include "Color.h"
|
||||||
|
#include <GLFW/glfw3.h>
|
||||||
|
|
||||||
|
|
||||||
namespace Systems
|
namespace Systems
|
||||||
@@ -13,7 +15,9 @@ namespace Systems
|
|||||||
struct ParticleData
|
struct ParticleData
|
||||||
{
|
{
|
||||||
EntityID ParticleID;
|
EntityID ParticleID;
|
||||||
double TimeLived;
|
double SpawnTime;
|
||||||
|
float Scale;
|
||||||
|
Color color;
|
||||||
};
|
};
|
||||||
|
|
||||||
class ParticleSystem : public System
|
class ParticleSystem : public System
|
||||||
|
|||||||
Reference in New Issue
Block a user