From d7f3e0cfb378e3691c3f81a6e72db53a502c1e88 Mon Sep 17 00:00:00 2001 From: Antoine Pilote Date: Sat, 23 Sep 2023 22:59:51 -0400 Subject: [PATCH] Added RandomScale to particle emitters --- .../ComponentsPanel/ParticleEmitterPanel.h | 28 +++++++++---------- Nuake/src/Rendering/SceneRenderer.cpp | 8 +++++- .../Components/ParticleEmitterComponent.cpp | 16 +++++++++-- .../Components/ParticleEmitterComponent.h | 5 +++- Nuake/src/Scene/Systems/Particle.h | 1 + Nuake/src/Scene/Systems/ParticleEmitter.cpp | 25 +++++++++++++++++ Nuake/src/Scene/Systems/ParticleEmitter.h | 9 +++++- Nuake/src/Scene/Systems/ParticleSystem.cpp | 3 +- 8 files changed, 73 insertions(+), 22 deletions(-) diff --git a/Editor/src/ComponentsPanel/ParticleEmitterPanel.h b/Editor/src/ComponentsPanel/ParticleEmitterPanel.h index 3043dfb6..b114b839 100644 --- a/Editor/src/ComponentsPanel/ParticleEmitterPanel.h +++ b/Editor/src/ComponentsPanel/ParticleEmitterPanel.h @@ -24,19 +24,7 @@ public: auto& component = entity.GetComponent(); 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(); diff --git a/Nuake/src/Rendering/SceneRenderer.cpp b/Nuake/src/Rendering/SceneRenderer.cpp index 5a9dc81d..68f07183 100644 --- a/Nuake/src/Rendering/SceneRenderer.cpp +++ b/Nuake/src/Rendering/SceneRenderer.cpp @@ -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); } diff --git a/Nuake/src/Scene/Components/ParticleEmitterComponent.cpp b/Nuake/src/Scene/Components/ParticleEmitterComponent.cpp index 27940e1c..bb061562 100644 --- a/Nuake/src/Scene/Components/ParticleEmitterComponent.cpp +++ b/Nuake/src/Scene/Components/ParticleEmitterComponent.cpp @@ -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"]; diff --git a/Nuake/src/Scene/Components/ParticleEmitterComponent.h b/Nuake/src/Scene/Components/ParticleEmitterComponent.h index 5b3e802e..7a046fa4 100644 --- a/Nuake/src/Scene/Components/ParticleEmitterComponent.h +++ b/Nuake/src/Scene/Components/ParticleEmitterComponent.h @@ -16,10 +16,13 @@ namespace Nuake Ref ParticleMaterial = CreateRef(); - 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; diff --git a/Nuake/src/Scene/Systems/Particle.h b/Nuake/src/Scene/Systems/Particle.h index 59f93989..113a9c86 100644 --- a/Nuake/src/Scene/Systems/Particle.h +++ b/Nuake/src/Scene/Systems/Particle.h @@ -7,6 +7,7 @@ namespace Nuake { Vector3 Position; Vector3 Velocity; + float Scale; Color Color; float Life; }; diff --git a/Nuake/src/Scene/Systems/ParticleEmitter.cpp b/Nuake/src/Scene/Systems/ParticleEmitter.cpp index 546cff31..9f57cfd9 100644 --- a/Nuake/src/Scene/Systems/ParticleEmitter.cpp +++ b/Nuake/src/Scene/Systems/ParticleEmitter.cpp @@ -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(-Radius, Radius); } } + + void ParticleEmitter::SetLifeRandom(float lifeRandom) + { + if (LifeRandom != lifeRandom) + { + LifeRandom = lifeRandom; + m_LifeRandom = std::uniform_real_distribution(-lifeRandom, lifeRandom); + } + } + + void ParticleEmitter::SetScaleRandom(float scaleRandom) + { + if (ScaleRandom != scaleRandom) + { + ScaleRandom = scaleRandom; + m_ScaleRandom = std::uniform_real_distribution(-scaleRandom, scaleRandom); + } + } } \ No newline at end of file diff --git a/Nuake/src/Scene/Systems/ParticleEmitter.h b/Nuake/src/Scene/Systems/ParticleEmitter.h index 4e72253d..fd1a9eab 100644 --- a/Nuake/src/Scene/Systems/ParticleEmitter.h +++ b/Nuake/src/Scene/Systems/ParticleEmitter.h @@ -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 m_Random; - + std::uniform_real_distribution m_ScaleRandom; + std::uniform_real_distribution 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 Particles; }; } \ No newline at end of file diff --git a/Nuake/src/Scene/Systems/ParticleSystem.cpp b/Nuake/src/Scene/Systems/ParticleSystem.cpp index d27dc628..27ee0f89 100644 --- a/Nuake/src/Scene/Systems/ParticleSystem.cpp +++ b/Nuake/src/Scene/Systems/ParticleSystem.cpp @@ -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;