Skip to content

Commit

Permalink
add random control
Browse files Browse the repository at this point in the history
  • Loading branch information
OVOAOVO committed Mar 1, 2024
1 parent 2f1fc90 commit 8651b1d
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 18 deletions.
3 changes: 3 additions & 0 deletions Engine/Source/Editor/UILayers/Inspector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -618,8 +618,11 @@ void UpdateComponentWidget<engine::ParticleEmitterComponent>(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());
Expand Down
33 changes: 20 additions & 13 deletions Engine/Source/Runtime/ECWorld/ParticleEmitterComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand All @@ -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; }

Expand Down Expand Up @@ -129,20 +131,19 @@ 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};
cd::Vec3f m_emitterAcceleration;
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;

Expand All @@ -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<std::byte> m_particleVertexBuffer;
std::vector<std::byte> m_particleIndexBuffer;
Expand Down
32 changes: 27 additions & 5 deletions Engine/Source/Runtime/Rendering/ParticleRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down

0 comments on commit 8651b1d

Please sign in to comment.