Linear fade added to particles

This commit is contained in:
Stiffly
2014-05-31 22:15:00 +02:00
parent 370dbb96ad
commit a31e3faa1f
6 changed files with 80 additions and 90 deletions
+1 -1
Submodule assets updated: 74035fae26...4724dbbd28
+1
View File
@@ -15,6 +15,7 @@ namespace Components
std::vector<glm::vec3> ScaleSpectrum; std::vector<glm::vec3> ScaleSpectrum;
double LifeTime; double LifeTime;
double SpawnTime; double SpawnTime;
bool Fade;
std::vector<glm::vec3> VelocitySpectrum; std::vector<glm::vec3> VelocitySpectrum;
std::vector<float> AngularVelocitySpectrum; std::vector<float> AngularVelocitySpectrum;
std::vector<glm::vec3> OrientationSpectrum; //Keep? std::vector<glm::vec3> OrientationSpectrum; //Keep?
+2 -1
View File
@@ -30,9 +30,10 @@ struct ParticleEmitter : Component
float SpreadAngle; float SpreadAngle;
double LifeTime; double LifeTime;
bool UseGoalVelocity; bool UseGoalVelocity;
bool Fade;
glm::vec3 GoalVelocity; glm::vec3 GoalVelocity;
std::vector<float> AngularVelocitySpectrum; std::vector<float> AngularVelocitySpectrum;
std::vector<glm::vec3> OrientationSpectrum; //Keep? std::vector<glm::vec3> OrientationSpectrum; //Keep? noo..
private: private:
double TimeSinceLastSpawn; double TimeSinceLastSpawn;
+34 -58
View File
@@ -252,14 +252,14 @@ void GameWorld::Initialize()
CommitEntity(tree);*/ CommitEntity(tree);*/
auto thing = CreateEntity(); // auto thing = CreateEntity();
auto transform = AddComponent<Components::Transform>(thing); // auto transform = AddComponent<Components::Transform>(thing);
transform->Position = glm::vec3(0,15,0); // transform->Position = glm::vec3(0,15,0);
transform->Scale = glm::vec3(3); // transform->Scale = glm::vec3(3);
// auto model = AddComponent<Components::Model>(thing); // // auto model = AddComponent<Components::Model>(thing);
// model->ModelFile = "Models/Barrel/Barrel.obj"; // // model->ModelFile = "Models/Barrel/Barrel.obj";
auto sprite = AddComponent<Components::Sprite>(thing); // auto sprite = AddComponent<Components::Sprite>(thing);
sprite->SpriteFile = "Textures/Sprites/SeriousParticle.png"; // sprite->SpriteFile = "Textures/Sprites/SeriousParticle.png";
} }
EntityID tank1 = CreateTank(1); EntityID tank1 = CreateTank(1);
@@ -745,31 +745,31 @@ EntityID GameWorld::CreateTank(int playerID)
} }
CommitEntity(wheel); CommitEntity(wheel);
auto entity = CreateEntity(tank); // auto entity = CreateEntity(tank);
auto transformComponent = AddComponent<Components::Transform>(entity); // auto transformComponent = AddComponent<Components::Transform>(entity);
transformComponent->Position = glm::vec3(2, -1.7, 2.0); // transformComponent->Position = glm::vec3(2, -1.7, 2.0);
transformComponent->Scale = glm::vec3(3, 3, 3); // transformComponent->Scale = glm::vec3(3, 3, 3);
transformComponent->Orientation = glm::angleAxis(glm::pi<float>() / 2, glm::vec3(1, 0, 0)); // transformComponent->Orientation = glm::angleAxis(glm::pi<float>() / 2, glm::vec3(1, 0, 0));
auto emitterComponent = AddComponent<Components::ParticleEmitter>(entity); // auto emitterComponent = AddComponent<Components::ParticleEmitter>(entity);
emitterComponent->SpawnCount = 2; // emitterComponent->SpawnCount = 2;
emitterComponent->SpawnFrequency = 0.005; // emitterComponent->SpawnFrequency = 0.005;
emitterComponent->SpreadAngle = glm::pi<float>(); // emitterComponent->SpreadAngle = glm::pi<float>();
emitterComponent->UseGoalVelocity = false; // emitterComponent->UseGoalVelocity = false;
emitterComponent->LifeTime = 0.5; // emitterComponent->LifeTime = 0.5;
//emitterComponent->AngularVelocitySpectrum.push_back(glm::pi<float>() / 100); // //emitterComponent->AngularVelocitySpectrum.push_back(glm::pi<float>() / 100);
emitterComponent->ScaleSpectrum.push_back(glm::vec3(0.05)); // emitterComponent->ScaleSpectrum.push_back(glm::vec3(0.05));
CommitEntity(entity); // CommitEntity(entity);
//
{ // {
auto particleEntity = CreateEntity(entity); // auto particleEntity = CreateEntity(entity);
auto templateComponent = AddComponent<Components::Template>(particleEntity); // auto templateComponent = AddComponent<Components::Template>(particleEntity);
auto TEMP = AddComponent<Components::Transform>(particleEntity); // auto TEMP = AddComponent<Components::Transform>(particleEntity);
TEMP->Scale = glm::vec3(0); // TEMP->Scale = glm::vec3(0);
auto spriteComponent = AddComponent<Components::Sprite>(particleEntity); // auto spriteComponent = AddComponent<Components::Sprite>(particleEntity);
spriteComponent->SpriteFile = "Models/Textures/Sprites/Dust.png"; // spriteComponent->SpriteFile = "Models/Textures/Sprites/Dust.png";
CommitEntity(particleEntity); // CommitEntity(particleEntity);
emitterComponent->ParticleTemplate = particleEntity; // emitterComponent->ParticleTemplate = particleEntity;
} // }
} }
{ {
@@ -832,31 +832,7 @@ EntityID GameWorld::CreateTank(int playerID)
CommitEntity(wheel); CommitEntity(wheel);
#pragma endregion #pragma endregion
auto entity = CreateEntity(tank);
auto transformComponent = AddComponent<Components::Transform>(entity);
transformComponent->Position = glm::vec3(-2, -1.7, 2.0);
transformComponent->Scale = glm::vec3(3, 3, 3);
transformComponent->Orientation = glm::angleAxis(glm::pi<float>() / 2, glm::vec3(1, 0, 0));
auto emitterComponent = AddComponent<Components::ParticleEmitter>(entity);
emitterComponent->SpawnCount = 2;
emitterComponent->SpawnFrequency = 0.005;
emitterComponent->SpreadAngle = glm::pi<float>();
emitterComponent->UseGoalVelocity = false;
emitterComponent->LifeTime = 0.5;
//emitterComponent->AngularVelocitySpectrum.push_back(glm::pi<float>() / 100);
emitterComponent->ScaleSpectrum.push_back(glm::vec3(0.05));
CommitEntity(entity);
{
auto particleEntity = CreateEntity(entity);
auto templateComponent = AddComponent<Components::Template>(particleEntity);
auto TEMP = AddComponent<Components::Transform>(particleEntity);
TEMP->Scale = glm::vec3(0);
auto spriteComponent = AddComponent<Components::Sprite>(particleEntity);
spriteComponent->SpriteFile = "Models/Textures/Sprites/Dust.png";
CommitEntity(particleEntity);
emitterComponent->ParticleTemplate = particleEntity;
}
} }
CommitEntity(tank); CommitEntity(tank);
+38 -26
View File
@@ -22,12 +22,14 @@ void Systems::ParticleSystem::Update(double dt)
double timeLived = glfwGetTime() - spawnTime; double timeLived = glfwGetTime() - spawnTime;
auto eComp = m_World->GetComponent<Components::ParticleEmitter>(explosionID); auto eComp = m_World->GetComponent<Components::ParticleEmitter>(explosionID);
if(timeLived > eComp->LifeTime) if(timeLived > eComp->LifeTime && m_ParticlesToEmitter[explosionID] == NULL)
{ {
auto e = m_World->GetComponent<Components::ParticleEmitter>(explosionID);
m_World->RemoveEntity(e->ParticleTemplate);
m_World->RemoveEntity(explosionID); m_World->RemoveEntity(explosionID);
it = m_ExplosionEmitters.erase(it); it = m_ExplosionEmitters.erase(it);
//LOG_INFO("Deleted explosion emitter successfully"); //LOG_INFO("Deleted explosion emitter successfully");
LOG_INFO("Deleted explosion emitter successfully"); //LOG_INFO("Deleted explosion emitter successfully. nr of emitters%i", m_ExplosionEmitters.size());
} }
else else
{ {
@@ -47,26 +49,34 @@ void Systems::ParticleSystem::UpdateEntity(double dt, EntityID entity, EntityID
{ {
emitterComponent->TimeSinceLastSpawn += dt; emitterComponent->TimeSinceLastSpawn += dt;
auto emitterTransformComponent = m_World->GetComponent<Components::Transform>(entity); auto emitterTransformComponent = m_World->GetComponent<Components::Transform>(entity);
if(emitterComponent->TimeSinceLastSpawn > emitterComponent->SpawnFrequency) // if(emitterComponent->TimeSinceLastSpawn > emitterComponent->SpawnFrequency)
{ // {
SpawnParticles(entity); // SpawnParticles(entity);
emitterComponent->TimeSinceLastSpawn = 0; // emitterComponent->TimeSinceLastSpawn = 0;
} // }
} }
auto particleComponent = m_World->GetComponent<Components::Particle>(entity); auto particleComponent = m_World->GetComponent<Components::Particle>(entity);
if(particleComponent) if(particleComponent)
{ {
EntityID particleID = entity;
auto transformComponent = m_World->GetComponent<Components::Transform>(particleID);
EntityID particleID = entity;
double timeLived = glfwGetTime() - particleComponent->SpawnTime; double timeLived = glfwGetTime() - particleComponent->SpawnTime;
if(timeLived > particleComponent->LifeTime) if(timeLived > particleComponent->LifeTime)
{ {
m_World->RemoveEntity(particleID); m_World->RemoveEntity(particleID);
m_ParticlesToEmitter.erase(particleID);
LOG_INFO("Removed a particle: %i", particleID);
return;
} }
else else
{ {
auto transformComponent = m_World->GetComponent<Components::Transform>(particleID);
auto eComponent = m_World->GetComponent<Components::ParticleEmitter>(m_ParticlesToEmitter[particleID]);
auto sprite = m_World->GetComponent<Components::Sprite>(entity);
// FIX: calculate once // FIX: calculate once
float timeProgress = timeLived / particleComponent->LifeTime; float timeProgress = timeLived / particleComponent->LifeTime;
// ColorInterpolation(timeProgress, particleComponent->ColorSpectrum, color); // ColorInterpolation(timeProgress, particleComponent->ColorSpectrum, color);
@@ -76,8 +86,16 @@ void Systems::ParticleSystem::UpdateEntity(double dt, EntityID entity, EntityID
// Velocity interpolation // Velocity interpolation
if(particleComponent->VelocitySpectrum.size() > 1) if(particleComponent->VelocitySpectrum.size() > 1)
VectorInterpolation(timeProgress, particleComponent->VelocitySpectrum, transformComponent->Velocity); VectorInterpolation(timeProgress, particleComponent->VelocitySpectrum, transformComponent->Velocity);
if(particleComponent->Fade == true)
{
std::vector<float> spectrum;
spectrum.push_back(1);
spectrum.push_back(0);
float alpha;
ScalarInterpolation(timeProgress, spectrum, alpha);
sprite->Color.w = alpha;
}
/*// Angular velocity interpolation /*// Angular velocity interpolation
if (particleComponent->AngularVelocitySpectrum.size() != 0) if (particleComponent->AngularVelocitySpectrum.size() != 0)
{ {
@@ -123,11 +141,11 @@ void Systems::ParticleSystem::SpawnParticles(EntityID emitterID)
glm::vec3 ePosition = m_TransformSystem->AbsolutePosition(emitterID); glm::vec3 ePosition = m_TransformSystem->AbsolutePosition(emitterID);
glm::quat eOrientation = eTransform->Orientation; glm::quat eOrientation = eTransform->Orientation;
glm::vec3 paticleSpeed = glm::vec3(eComponent->Speed); glm::vec3 paticleSpeed = glm::vec3(eComponent->Speed);
for(int i = 0; i < eComponent->SpawnCount; i++) for(int i = 0; i < eComponent->SpawnCount; i++)
{ {
auto ent = m_World->CloneEntity(eComponent->ParticleTemplate); auto ent = m_World->CloneEntity(eComponent->ParticleTemplate);
LOG_INFO("Spawned a particle: %i", ent);
m_ParticlesToEmitter.insert(std::make_pair(ent, emitterID));
auto particleTransform = m_World->GetComponent<Components::Transform>(ent); auto particleTransform = m_World->GetComponent<Components::Transform>(ent);
particleTransform->Position = ePosition; particleTransform->Position = ePosition;
@@ -143,6 +161,7 @@ void Systems::ParticleSystem::SpawnParticles(EntityID emitterID)
particle->LifeTime = eComponent->LifeTime; particle->LifeTime = eComponent->LifeTime;
particle->ScaleSpectrum = eComponent->ScaleSpectrum; particle->ScaleSpectrum = eComponent->ScaleSpectrum;
particle->VelocitySpectrum.push_back(particleTransform->Velocity); particle->VelocitySpectrum.push_back(particleTransform->Velocity);
particle->Fade = eComponent->Fade;
if (eComponent->ScaleSpectrum.size() > 0) if (eComponent->ScaleSpectrum.size() > 0)
{ {
@@ -194,23 +213,13 @@ void Systems::ParticleSystem::VectorInterpolation(double timeProgress, std::vect
dAxisValue = glm::abs(spectrum[0].y - spectrum[1].y); dAxisValue = glm::abs(spectrum[0].y - spectrum[1].y);
if (spectrum[0].y > spectrum[1].y) if (spectrum[0].y > spectrum[1].y)
dAxisValue *= -1; dAxisValue *= -1;
v.y = spectrum[0].y + dAxisValue * timeProgress; v.y = spectrum[0].y + dAxisValue * timeProgress;
dAxisValue = glm::abs(spectrum[0].z - spectrum[1].z); dAxisValue = glm::abs(spectrum[0].z - spectrum[1].z);
if(spectrum[0].z > spectrum[1].z) if(spectrum[0].z > spectrum[1].z)
dAxisValue *= -1; dAxisValue *= -1;
v.z = spectrum[0].z + dAxisValue * timeProgress; v.z = spectrum[0].z + dAxisValue * timeProgress;
} }
// void Systems::ParticleSystem::ColorInterpolation(double timeProgress, std::vector<Color> spectrum, Color &c)
// {
// float dColor = glm::abs(spectrum[0].r - spectrum[1].r);
// c.r = spectrum[0].r + dColor * timeProgress;
// dColor = glm::abs(spectrum[0].g - spectrum[1].g);
// c.g = spectrum[0].g + dColor * timeProgress;
// dColor = glm::abs(spectrum[0].b - spectrum[1].b);
// c.b = spectrum[0].b + dColor * timeProgress;
// }
void Systems::ParticleSystem::ScalarInterpolation(double timeProgress, std::vector<float> spectrum, float &alpha) void Systems::ParticleSystem::ScalarInterpolation(double timeProgress, std::vector<float> spectrum, float &alpha)
{ {
float dAlpha = glm::abs(spectrum[0] - spectrum[1]); float dAlpha = glm::abs(spectrum[0] - spectrum[1]);
@@ -221,20 +230,22 @@ void Systems::ParticleSystem::ScalarInterpolation(double timeProgress, std::vect
bool Systems::ParticleSystem::CreateExplosion(const Events::CreateExplosion &e) bool Systems::ParticleSystem::CreateExplosion(const Events::CreateExplosion &e)
{ {
LOG_INFO("Spawning an explosion");
auto explosion = m_World->CreateEntity(); auto explosion = m_World->CreateEntity();
auto emitter = m_World->AddComponent<Components::ParticleEmitter>(explosion); auto emitter = m_World->AddComponent<Components::ParticleEmitter>(explosion);
emitter->LifeTime = e.LifeTime; emitter->LifeTime = e.LifeTime;
emitter->SpawnCount = e.ParticlesToSpawn; emitter->SpawnCount = e.ParticlesToSpawn;
emitter->Speed = e.Speed; emitter->Speed = e.Speed;
emitter->SpreadAngle = e.SpreadAngle; emitter->SpreadAngle = e.SpreadAngle;
emitter->ScaleSpectrum.push_back(glm::vec3(e.ParticleScale));
emitter->SpawnFrequency = e.LifeTime + 20; //temp emitter->SpawnFrequency = e.LifeTime + 20; //temp
// emitter->UseGoalVelocity = true; emitter->Fade = true;
// emitter->GoalVelocity = glm::vec3(0,-_speed, 0); // emitter->GoalVelocity = glm::vec3(0,-_speed, 0);
m_World->CommitEntity(explosion); m_World->CommitEntity(explosion);
auto particleEnt = m_World->CreateEntity(); auto particleEnt = m_World->CreateEntity();
auto templateComponent = m_World->AddComponent<Components::Template>(particleEnt);
auto TEMP = m_World->AddComponent<Components::Transform>(particleEnt); auto TEMP = m_World->AddComponent<Components::Transform>(particleEnt);
TEMP->Scale = glm::vec3(0);
auto spriteComponent = m_World->AddComponent<Components::Sprite>(particleEnt); auto spriteComponent = m_World->AddComponent<Components::Sprite>(particleEnt);
spriteComponent->SpriteFile = e.spritePath; spriteComponent->SpriteFile = e.spritePath;
m_World->CommitEntity(particleEnt); m_World->CommitEntity(particleEnt);
@@ -244,6 +255,7 @@ bool Systems::ParticleSystem::CreateExplosion(const Events::CreateExplosion &e)
transform->Position = e.Position; transform->Position = e.Position;
transform->Orientation = e.RelativeUpOrientation; transform->Orientation = e.RelativeUpOrientation;
LOG_INFO("Now we should spawn 1 particle!!!!!! not 2 :(");
SpawnParticles(explosion); SpawnParticles(explosion);
m_ExplosionEmitters[explosion] = glfwGetTime(); m_ExplosionEmitters[explosion] = glfwGetTime();
+4 -4
View File
@@ -37,12 +37,10 @@ private:
float RandomizeAngle(float spreadAngle); float RandomizeAngle(float spreadAngle);
//void ScaleInterpolation(double timeProgress, std::vector<float> spectrum, glm::vec3 &scale); //void ScaleInterpolation(double timeProgress, std::vector<float> spectrum, glm::vec3 &scale);
void VectorInterpolation(double timeProgress, std::vector<glm::vec3> spectrum, glm::vec3 &velocity); void VectorInterpolation(double timeProgress, std::vector<glm::vec3> spectrum, glm::vec3 &velocity);
//void ColorInterpolation(double timeProgress, std::vector<Color> spectrum, Color &color);
void ScalarInterpolation(double timeProgress, std::vector<float> spectrum, float &alpha); void ScalarInterpolation(double timeProgress, std::vector<float> spectrum, float &alpha);
void Billboard(); void Billboard();
//std::map<EntityID, std::list<ParticleData>> m_ParticleEmitter; //std::map<EntityID, std::list<ParticleData>> m_ParticleEmitter;
std::map<EntityID, double> m_TimeSinceLastSpawn;
std::map<EntityID, double> m_ExplosionEmitters;
std::shared_ptr<Systems::TransformSystem> m_TransformSystem; std::shared_ptr<Systems::TransformSystem> m_TransformSystem;
bool tempSpawnedExplosions; bool tempSpawnedExplosions;
@@ -51,7 +49,9 @@ private:
// bool OnKeyUp(const Events::KeyUp &e); // bool OnKeyUp(const Events::KeyUp &e);
EventRelay<ParticleSystem, Events::CreateExplosion> m_EExplosion; EventRelay<ParticleSystem, Events::CreateExplosion> m_EExplosion;
bool CreateExplosion(const Events::CreateExplosion &e); bool CreateExplosion(const Events::CreateExplosion &e);
std::map<EntityID, double> m_ExplosionEmitters;
std::map<EntityID, EntityID> m_ParticlesToEmitter;
}; };
} }