From 8651b1d923c1509b398b6196e2dfc34eca6211e3 Mon Sep 17 00:00:00 2001 From: OVOAOVO <2410651402@qq.com> Date: Fri, 1 Mar 2024 14:37:56 +0800 Subject: [PATCH] add random control --- Engine/Source/Editor/UILayers/Inspector.cpp | 3 ++ .../ECWorld/ParticleEmitterComponent.h | 33 +++++++++++-------- .../Runtime/Rendering/ParticleRenderer.cpp | 32 +++++++++++++++--- 3 files changed, 50 insertions(+), 18 deletions(-) diff --git a/Engine/Source/Editor/UILayers/Inspector.cpp b/Engine/Source/Editor/UILayers/Inspector.cpp index f2fda137..8287398d 100644 --- a/Engine/Source/Editor/UILayers/Inspector.cpp +++ b/Engine/Source/Editor/UILayers/Inspector.cpp @@ -618,8 +618,11 @@ void UpdateComponentWidget(engine::SceneWorld* ImGuiUtils::ImGuiEnumProperty("Particle Type", pParticleEmitterComponent->GetEmitterParticleType()); //ImGuiUtils::ImGuiEnumProperty("Emitter Shape", pParticleEmitterComponent->GetEmitterShape()); ImGuiUtils::ImGuiVectorProperty("Emitter Range", pParticleEmitterComponent->GetEmitterShapeRange()); + ImGuiUtils::ImGuiBoolProperty("Random Emit Pos", pParticleEmitterComponent->GetRandomPosState()); ImGuiUtils::ImGuiIntProperty("Max Count", pParticleEmitterComponent->GetSpawnCount(), cd::Unit::None, 1, 300); ImGuiUtils::ImGuiVectorProperty("Velocity", pParticleEmitterComponent->GetEmitterVelocity()); + ImGuiUtils::ImGuiVectorProperty("Random Velocity", pParticleEmitterComponent->GetRandomVelocity()); + ImGuiUtils::ImGuiBoolProperty("RandomVelocity", pParticleEmitterComponent->GetRandomVelocityState()); ImGuiUtils::ImGuiVectorProperty("Acceleration", pParticleEmitterComponent->GetEmitterAcceleration()); ImGuiUtils::ColorPickerProperty("Color", pParticleEmitterComponent->GetEmitterColor()); ImGuiUtils::ImGuiFloatProperty("LifeTime", pParticleEmitterComponent->GetLifeTime()); diff --git a/Engine/Source/Runtime/ECWorld/ParticleEmitterComponent.h b/Engine/Source/Runtime/ECWorld/ParticleEmitterComponent.h index 83de60ac..d5e76935 100644 --- a/Engine/Source/Runtime/ECWorld/ParticleEmitterComponent.h +++ b/Engine/Source/Runtime/ECWorld/ParticleEmitterComponent.h @@ -42,8 +42,6 @@ class ParticleEmitterComponent final ParticleEmitterComponent& operator=(ParticleEmitterComponent&&) = default; ~ParticleEmitterComponent() = default; - //engine::ParticleSystem &GetParticleSystem() { return m_particleSystem; } - ParticlePool& GetParticlePool() { return m_particlePool; } int& GetSpawnCount() { return m_spawnCount; } @@ -67,12 +65,16 @@ class ParticleEmitterComponent final ParticleType& GetEmitterParticleType() { return m_emitterParticleType; } void SetEmitterParticleType(engine::ParticleType type) { m_emitterParticleType = type; } - //bool& GetRandomVelocityState() { return m_randomVelocityState; } - //void SetRandomVelocityState(bool state) { m_randomVelocityState = state; } + //random + bool& GetRandomPosState() { return m_randomPosState; } + cd::Vec3f& GetRandormPos() { return m_randomPos; } + void SetRandomPos(cd::Vec3f randomPos) { m_randomPos = randomPos; } - //cd::Vec3f& GetRandomVelocity() { return m_randomVelocity; } - //void SetRandomVelocity(cd::Vec3f velocity) { m_randomVelocity = velocity; } + bool& GetRandomVelocityState() { return m_randomVelocityState; } + cd::Vec3f& GetRandomVelocity() { return m_randomVelocity; } + void SetRandomVelocity(cd::Vec3f randomVelocity) { m_randomVelocity = randomVelocity; } + //particle data cd::Vec3f& GetEmitterVelocity() { return m_emitterVelocity; } void SetEmitterVelocity(cd::Vec3f velocity) { m_emitterVelocity = velocity; } @@ -129,13 +131,6 @@ class ParticleEmitterComponent final engine::ParticleType m_emitterParticleType; - struct VertexData - { - cd::Vec3f pos; - cd::Vec4f color; - cd::UV uv; - }; - //emitter data int m_spawnCount = 75; cd::Vec3f m_emitterVelocity {20.0f, 20.0f, 0.0f}; @@ -143,6 +138,12 @@ class ParticleEmitterComponent final cd::Vec4f m_emitterColor = cd::Vec4f::One(); float m_emitterLifeTime = 6.0f; + // random emitter data + bool m_randomPosState; + cd::Vec3f m_randomPos; + bool m_randomVelocityState; + cd::Vec3f m_randomVelocity; + //instancing bool m_useInstance = false; @@ -157,6 +158,12 @@ class ParticleEmitterComponent final const cd::Mesh* m_pMeshData = nullptr; //particle vertex/index + struct VertexData + { + cd::Vec3f pos; + cd::Vec4f color; + cd::UV uv; + }; const cd::VertexFormat* m_pRequiredVertexFormat = nullptr; std::vector m_particleVertexBuffer; std::vector m_particleIndexBuffer; diff --git a/Engine/Source/Runtime/Rendering/ParticleRenderer.cpp b/Engine/Source/Runtime/Rendering/ParticleRenderer.cpp index c8943aac..ed72070b 100644 --- a/Engine/Source/Runtime/Rendering/ParticleRenderer.cpp +++ b/Engine/Source/Runtime/Rendering/ParticleRenderer.cpp @@ -83,14 +83,36 @@ void ParticleRenderer::Render(float deltaTime) pEmitterComponent->GetParticlePool().SetParticleMaxCount(pEmitterComponent->GetSpawnCount()); pEmitterComponent->GetParticlePool().AllParticlesReset(); int particleIndex = pEmitterComponent->GetParticlePool().AllocateParticleIndex(); + + //Random value + cd::Vec3f randomPos(getRandomValue(-pEmitterComponent->GetEmitterShapeRange().x(), pEmitterComponent->GetEmitterShapeRange().x()), + getRandomValue(-pEmitterComponent->GetEmitterShapeRange().y(), pEmitterComponent->GetEmitterShapeRange().y()), + getRandomValue(-pEmitterComponent->GetEmitterShapeRange().z(), pEmitterComponent->GetEmitterShapeRange().z())); + cd::Vec3f randomVelocity(getRandomValue(-pEmitterComponent->GetRandomVelocity().x(), pEmitterComponent->GetRandomVelocity().x()), + getRandomValue(-pEmitterComponent->GetRandomVelocity().y(), pEmitterComponent->GetRandomVelocity().y()), + getRandomValue(-pEmitterComponent->GetRandomVelocity().z(), pEmitterComponent->GetRandomVelocity().z())); + pEmitterComponent->SetRandomPos(randomPos); + + //particle if (particleIndex != -1) { Particle& particle = pEmitterComponent->GetParticlePool().GetParticle(particleIndex); - cd::Vec3f random(getRandomValue(-pEmitterComponent->GetEmitterShapeRange().x(), pEmitterComponent->GetEmitterShapeRange().x()), - getRandomValue(-pEmitterComponent->GetEmitterShapeRange().y(), pEmitterComponent->GetEmitterShapeRange().y()), - getRandomValue(-pEmitterComponent->GetEmitterShapeRange().z(), pEmitterComponent->GetEmitterShapeRange().z())); - particle.SetPos(particleTransform.GetTranslation()+random); - particle.SetSpeed(pEmitterComponent->GetEmitterVelocity()); + if (pEmitterComponent->GetRandomPosState()) + { + particle.SetPos(particleTransform.GetTranslation()+ pEmitterComponent->GetRandormPos()); + } + else + { + particle.SetPos(particleTransform.GetTranslation()); + } + if (pEmitterComponent->GetRandomVelocityState()) + { + particle.SetSpeed(pEmitterComponent->GetEmitterVelocity()+ randomVelocity); + } + else + { + particle.SetSpeed(pEmitterComponent->GetEmitterVelocity()); + } particle.SetRotationForceField(m_forcefieldRotationFoce); particle.SetRotationForceFieldRange(m_forcefieldRange); particle.SetAcceleration(pEmitterComponent->GetEmitterAcceleration());