Optimized ParticleSystem. Still can't see particles though D:
This commit is contained in:
@@ -14,6 +14,7 @@ namespace Components
|
||||
std::vector<Color> ColorSpectrum;
|
||||
std::vector<glm::vec3> ScaleSpectrum;
|
||||
double LifeTime;
|
||||
double SpawnTime;
|
||||
std::vector<glm::vec3> VelocitySpectrum;
|
||||
std::vector<float> AngularVelocitySpectrum;
|
||||
std::vector<glm::vec3> OrientationSpectrum; //Keep?
|
||||
|
||||
+46
-46
@@ -745,29 +745,29 @@ EntityID GameWorld::CreateTank(int playerID)
|
||||
}
|
||||
CommitEntity(wheel);
|
||||
|
||||
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 TEMP = AddComponent<Components::Transform>(particleEntity);
|
||||
TEMP->Scale = glm::vec3(0);
|
||||
auto spriteComponent = AddComponent<Components::Sprite>(particleEntity);
|
||||
spriteComponent->SpriteFile = "Models/Textures/Sprites/Dust.png";
|
||||
emitterComponent->ParticleTemplate = particleEntity;
|
||||
|
||||
CommitEntity(particleEntity);
|
||||
// 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 TEMP = AddComponent<Components::Transform>(particleEntity);
|
||||
// TEMP->Scale = glm::vec3(0);
|
||||
// auto spriteComponent = AddComponent<Components::Sprite>(particleEntity);
|
||||
// spriteComponent->SpriteFile = "Models/Textures/Sprites/Dust.png";
|
||||
// emitterComponent->ParticleTemplate = particleEntity;
|
||||
//
|
||||
// CommitEntity(particleEntity);
|
||||
}
|
||||
|
||||
{
|
||||
@@ -830,29 +830,29 @@ EntityID GameWorld::CreateTank(int playerID)
|
||||
CommitEntity(wheel);
|
||||
#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 TEMP = AddComponent<Components::Transform>(particleEntity);
|
||||
TEMP->Scale = glm::vec3(0);
|
||||
auto spriteComponent = AddComponent<Components::Sprite>(particleEntity);
|
||||
spriteComponent->SpriteFile = "Models/Textures/Sprites/Dust.png";
|
||||
emitterComponent->ParticleTemplate = particleEntity;
|
||||
|
||||
CommitEntity(particleEntity);
|
||||
// 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 TEMP = AddComponent<Components::Transform>(particleEntity);
|
||||
// TEMP->Scale = glm::vec3(0);
|
||||
// auto spriteComponent = AddComponent<Components::Sprite>(particleEntity);
|
||||
// spriteComponent->SpriteFile = "Models/Textures/Sprites/Dust.png";
|
||||
// emitterComponent->ParticleTemplate = particleEntity;
|
||||
//
|
||||
// CommitEntity(particleEntity);
|
||||
}
|
||||
|
||||
CommitEntity(tank);
|
||||
|
||||
@@ -52,64 +52,59 @@ void Systems::ParticleSystem::UpdateEntity(double dt, EntityID entity, EntityID
|
||||
SpawnParticles(entity);
|
||||
emitterComponent->TimeSinceLastSpawn = 0;
|
||||
}
|
||||
}
|
||||
|
||||
std::list<ParticleData>::iterator it;
|
||||
for(it = m_ParticleEmitter[entity].begin(); it != m_ParticleEmitter[entity].end();)
|
||||
auto particleComponent = m_World->GetComponent<Components::Particle>(entity);
|
||||
if(particleComponent)
|
||||
{
|
||||
EntityID particleID = entity;
|
||||
auto transformComponent = m_World->GetComponent<Components::Transform>(particleID);
|
||||
|
||||
double timeLived = glfwGetTime() - particleComponent->SpawnTime;
|
||||
if(timeLived > particleComponent->LifeTime)
|
||||
{
|
||||
EntityID particleID = (it)->ParticleID;
|
||||
auto transformComponent = m_World->GetComponent<Components::Transform>(particleID);
|
||||
auto particleComponent = m_World->GetComponent<Components::Particle>(particleID);
|
||||
m_World->RemoveEntity(particleID);
|
||||
}
|
||||
else
|
||||
{
|
||||
// FIX: calculate once
|
||||
float timeProgress = timeLived / particleComponent->LifeTime;
|
||||
// ColorInterpolation(timeProgress, particleComponent->ColorSpectrum, color);
|
||||
// Scale interpolation
|
||||
if(particleComponent->ScaleSpectrum.size() > 1)
|
||||
VectorInterpolation(timeProgress, particleComponent->ScaleSpectrum, transformComponent->Scale);
|
||||
// Velocity interpolation
|
||||
if(particleComponent->VelocitySpectrum.size() > 1)
|
||||
VectorInterpolation(timeProgress, particleComponent->VelocitySpectrum, transformComponent->Velocity);
|
||||
|
||||
|
||||
double timeLived = glfwGetTime() - it->SpawnTime;
|
||||
if(timeLived > particleComponent->LifeTime)
|
||||
/*// Angular velocity interpolation
|
||||
if (particleComponent->AngularVelocitySpectrum.size() != 0)
|
||||
{
|
||||
m_World->RemoveEntity(particleID);
|
||||
it = m_ParticleEmitter[entity].erase(it);
|
||||
if(particleComponent->AngularVelocitySpectrum.size() > 1)
|
||||
{
|
||||
ScalarInterpolation(timeProgress, particleComponent->AngularVelocitySpectrum, it->AngularVelocity);
|
||||
transformComponent->Orientation = glm::angleAxis(it->AngularVelocity, it->Orientation);
|
||||
}
|
||||
else
|
||||
{
|
||||
transformComponent->Orientation *= glm::angleAxis(it->AngularVelocity, it->Orientation);
|
||||
//it->Orientation = glm::angleAxis(it->AngularVelocity, it->Orientation);
|
||||
}
|
||||
}
|
||||
else
|
||||
|
||||
//Angular velocity interpolation
|
||||
if(particleComponent->OrientationSpectrum.size() > 1)
|
||||
{
|
||||
// FIX: calculate once
|
||||
float timeProgress = timeLived / particleComponent->LifeTime;
|
||||
// ColorInterpolation(timeProgress, particleComponent->ColorSpectrum, color);
|
||||
// Scale interpolation
|
||||
if(particleComponent->ScaleSpectrum.size() > 1)
|
||||
VectorInterpolation(timeProgress, particleComponent->ScaleSpectrum, transformComponent->Scale);
|
||||
// Velocity interpolation
|
||||
if(particleComponent->VelocitySpectrum.size() > 1)
|
||||
VectorInterpolation(timeProgress, particleComponent->VelocitySpectrum, transformComponent->Velocity);
|
||||
|
||||
// Angular velocity interpolation
|
||||
if (particleComponent->AngularVelocitySpectrum.size() != 0)
|
||||
{
|
||||
if(particleComponent->AngularVelocitySpectrum.size() > 1)
|
||||
{
|
||||
ScalarInterpolation(timeProgress, particleComponent->AngularVelocitySpectrum, it->AngularVelocity);
|
||||
transformComponent->Orientation = glm::angleAxis(it->AngularVelocity, it->Orientation);
|
||||
}
|
||||
else
|
||||
{
|
||||
transformComponent->Orientation *= glm::angleAxis(it->AngularVelocity, it->Orientation);
|
||||
//it->Orientation = glm::angleAxis(it->AngularVelocity, it->Orientation);
|
||||
}
|
||||
}
|
||||
|
||||
//Angular velocity interpolation
|
||||
if(particleComponent->OrientationSpectrum.size() > 1)
|
||||
{
|
||||
VectorInterpolation(timeProgress, particleComponent->OrientationSpectrum, it->Orientation);
|
||||
glm::vec3 v1 = (particleComponent->OrientationSpectrum[0]);
|
||||
glm::vec3 v2 = (it->Orientation);
|
||||
glm::vec3 v3 = glm::normalize(glm::cross(v1,v2));
|
||||
float angle = glm::acos(glm::dot(v1, v2) / (glm::length(v1) * glm::length(v2)));
|
||||
VectorInterpolation(timeProgress, particleComponent->OrientationSpectrum, it->Orientation);
|
||||
glm::vec3 v1 = (particleComponent->OrientationSpectrum[0]);
|
||||
glm::vec3 v2 = (it->Orientation);
|
||||
glm::vec3 v3 = glm::normalize(glm::cross(v1,v2));
|
||||
float angle = glm::acos(glm::dot(v1, v2) / (glm::length(v1) * glm::length(v2)));
|
||||
|
||||
transformComponent->Orientation = glm::angleAxis(angle, v3);
|
||||
}
|
||||
|
||||
|
||||
transformComponent->Position += transformComponent->Velocity * (float)dt;
|
||||
|
||||
it++;
|
||||
}
|
||||
transformComponent->Orientation = glm::angleAxis(angle, v3);
|
||||
}*/
|
||||
transformComponent->Position += transformComponent->Velocity * (float)dt;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -173,15 +168,13 @@ void Systems::ParticleSystem::SpawnParticles(EntityID emitterID)
|
||||
particle->AngularVelocitySpectrum = eComponent->AngularVelocitySpectrum;
|
||||
|
||||
|
||||
ParticleData data;
|
||||
data.ParticleID = ent;
|
||||
data.SpawnTime = glfwGetTime();
|
||||
if (particle->AngularVelocitySpectrum.size() != 0)
|
||||
data.AngularVelocity = particle->AngularVelocitySpectrum[0];
|
||||
if (particle->OrientationSpectrum.size() != 0)
|
||||
data.Orientation = particle->OrientationSpectrum[0];
|
||||
else data.Orientation = eOrientation * glm::vec3(0,0,-1);
|
||||
m_ParticleEmitter[emitterID].push_back(data);
|
||||
particle->SpawnTime = glfwGetTime();
|
||||
// if (particle->AngularVelocitySpectrum.size() != 0)
|
||||
// data.AngularVelocity = particle->AngularVelocitySpectrum[0];
|
||||
// if (particle->OrientationSpectrum.size() != 0)
|
||||
// data.Orientation = particle->OrientationSpectrum[0];
|
||||
// else data.Orientation = eOrientation * glm::vec3(0,0,-1);
|
||||
//m_ParticleEmitter[emitterID].push_back(data);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -16,15 +16,6 @@
|
||||
namespace Systems
|
||||
{
|
||||
|
||||
struct ParticleData
|
||||
{
|
||||
EntityID ParticleID;
|
||||
double SpawnTime;
|
||||
float AngularVelocity;
|
||||
glm::vec3 Orientation;
|
||||
Color color;
|
||||
};
|
||||
|
||||
class ParticleSystem : public System
|
||||
{
|
||||
public:
|
||||
@@ -49,7 +40,7 @@ private:
|
||||
//void ColorInterpolation(double timeProgress, std::vector<Color> spectrum, Color &color);
|
||||
void ScalarInterpolation(double timeProgress, std::vector<float> spectrum, float &alpha);
|
||||
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;
|
||||
|
||||
Reference in New Issue
Block a user