Added RandomScale to particle emitters

This commit is contained in:
Antoine Pilote
2023-09-23 22:59:51 -04:00
parent 42c82e4ca3
commit d7f3e0cfb3
8 changed files with 73 additions and 22 deletions

View File

@@ -24,19 +24,7 @@ public:
auto& component = entity.GetComponent<Nuake::ParticleEmitterComponent>();
BeginComponentTable(PARTICLE EMITTER, Nuake::ParticleEmitterComponent);
{
{
ImGui::Text("Particle Color");
ImGui::TableNextColumn();
ImGui::PushItemWidth(ImGui::GetContentRegionAvail().x);
ImGui::ColorEdit3("##lightcolor", &component.ParticleColor.r, ImGuiColorEditFlags_NoInputs | ImGuiColorEditFlags_NoLabel);
ImGui::PopItemWidth();
ImGui::TableNextColumn();
ComponentTableReset(component.ParticleColor, Nuake::Vector4(1, 1, 1, 1));
}
ImGui::TableNextColumn();
{
ImGui::Text("Particle Material");
ImGui::TableNextColumn();
@@ -76,7 +64,7 @@ public:
//ImGui::EndChild();
ImGui::TableNextColumn();
ComponentTableReset(component.ParticleColor, Nuake::Vector4(1, 1, 1, 1));
//ComponentTableReset(component.ParticleColor, Nuake::Vector4(1, 1, 1, 1));
}
ImGui::TableNextColumn();
@@ -90,7 +78,7 @@ public:
}
ImGui::TableNextColumn();
{
ImGui::Text("Particle Size");
ImGui::Text("Particle Scale");
ImGui::TableNextColumn();
ImGuiHelper::DrawVec3("##particleSize", &component.ParticleScale);
@@ -99,6 +87,16 @@ public:
ComponentTableReset(component.ParticleScale, Nuake::Vector3(0.1, 0.1, 0.1));
}
ImGui::TableNextColumn();
{
ImGui::Text("Particle Scale Random");
ImGui::TableNextColumn();
ImGui::DragFloat("##particleSizeRandom", &component.ScaleRandomness, 0.01f, 0.0f);
ImGui::TableNextColumn();
ComponentTableReset(component.ScaleRandomness, 0.0f);
}
ImGui::TableNextColumn();
{
ImGui::Text("Global Space");
ImGui::TableNextColumn();

View File

@@ -419,7 +419,13 @@ namespace Nuake
particleTransform[3] = Vector4(particleGlobalPosition, 1.0f);
// Scale
particleTransform = glm::scale(particleTransform, emitterComponent.ParticleScale);
Vector3 finalScale = emitterComponent.ParticleScale;
if (p.Scale != 1.0f)
{
finalScale += emitterComponent.ParticleScale * p.Scale;
}
particleTransform = glm::scale(particleTransform, finalScale);
Renderer::SubmitMesh(Renderer::QuadMesh, particleTransform, (uint32_t)e);
}

View File

@@ -6,7 +6,6 @@ namespace Nuake
json ParticleEmitterComponent::Serialize()
{
BEGIN_SERIALIZE();
SERIALIZE_VEC4(ParticleColor);
SERIALIZE_VEC3(ParticleScale);
SERIALIZE_VAL(Amount);
SERIALIZE_VAL(Life);
@@ -16,15 +15,26 @@ namespace Nuake
SERIALIZE_VAL(Radius);
SERIALIZE_VAL(GlobalSpace);
SERIALIZE_OBJECT(ParticleMaterial);
SERIALIZE_VAL(LifeRandomness);
SERIALIZE_VAL(ScaleRandomness);
END_SERIALIZE();
}
bool ParticleEmitterComponent::Deserialize(const json& j)
{
DESERIALIZE_VEC4(j["ParticleColor"], ParticleColor);
Amount = j["Amount"];
Life = j["Life"];
if (j.contains("LifeRandomness"))
{
LifeRandomness = j["LifeRandomness"];
}
if (j.contains("ScaleRandomness"))
{
ScaleRandomness = j["ScaleRandomness"];
}
if (j.contains("Rate"))
{
Rate = j["Rate"];

View File

@@ -16,10 +16,13 @@ namespace Nuake
Ref<Material> ParticleMaterial = CreateRef<Material>();
Color ParticleColor;
float Amount;
float LifeRandomness = 0.0f;
float Life = 1.0f;
float Rate = 0.0f;
float ScaleRandomness = 0.0f;
Vector3 ParticleScale = Vector3(0.1, 0.1, 0.1);
bool GlobalSpace = false;

View File

@@ -7,6 +7,7 @@ namespace Nuake
{
Vector3 Position;
Vector3 Velocity;
float Scale;
Color Color;
float Life;
};

View File

@@ -32,6 +32,12 @@ namespace Nuake
const auto initialVelocity = Vector3();
const auto initialColor = ParticleColor;
const float initialLife = Life;
float Scale = 1.0f;
if (ScaleRandom != 0.0f)
{
Scale = m_ScaleRandom(m_MT);
}
if (IsGlobalSpace)
{
@@ -41,6 +47,7 @@ namespace Nuake
Particles.push_back({
initialPosition,
initialVelocity,
Scale,
initialColor,
initialLife
});
@@ -94,4 +101,22 @@ namespace Nuake
m_Random = std::uniform_real_distribution<float>(-Radius, Radius);
}
}
void ParticleEmitter::SetLifeRandom(float lifeRandom)
{
if (LifeRandom != lifeRandom)
{
LifeRandom = lifeRandom;
m_LifeRandom = std::uniform_real_distribution<float>(-lifeRandom, lifeRandom);
}
}
void ParticleEmitter::SetScaleRandom(float scaleRandom)
{
if (ScaleRandom != scaleRandom)
{
ScaleRandom = scaleRandom;
m_ScaleRandom = std::uniform_real_distribution<float>(-scaleRandom, scaleRandom);
}
}
}

View File

@@ -22,6 +22,9 @@ namespace Nuake
float Rate = 0.0f;
Color ParticleColor;
float LifeRandom = 0.0f;
float ScaleRandom = 0.0f;
bool IsGlobalSpace = false;
Vector3 GlobalOrigin;
@@ -29,7 +32,8 @@ namespace Nuake
float RateTimer = 0.0f;
std::mt19937 m_MT;
std::uniform_real_distribution<float> m_Random;
std::uniform_real_distribution<float> m_ScaleRandom;
std::uniform_real_distribution<float> m_LifeRandom;
void RecreateRandom();
public:
@@ -42,6 +46,9 @@ namespace Nuake
void Update(Timestep ts);
void SetRadius(float radius);
void SetScaleRandom(float scaleRandom);
void SetLifeRandom(float lifeRandom);
std::vector<Particle> Particles;
};
}

View File

@@ -28,10 +28,11 @@ namespace Nuake
// Copy emitter settings from component to real emitter
auto& emitter = emitterComponent.Emitter;
emitter.Amount = emitterComponent.Amount;
emitter.ParticleColor = emitterComponent.ParticleColor;
emitter.Gravity = emitterComponent.Gravity;
emitter.GravityRandom = emitterComponent.GravityRandom;
emitter.SetRadius(emitterComponent.Radius);
emitter.SetLifeRandom(emitterComponent.LifeRandomness);
emitter.SetScaleRandom(emitterComponent.ScaleRandomness);
emitter.Life = emitterComponent.Life;
emitter.Rate = emitterComponent.Rate;
emitter.IsGlobalSpace = emitterComponent.GlobalSpace;