Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

particleSystem #388

Closed
wants to merge 29 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions Engine/Source/Editor/UILayers/Inspector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -489,10 +489,10 @@ void UpdateComponentWidget<engine::SkyComponent>(engine::SceneWorld* pSceneWorld
}

template<>
void UpdateComponentWidget<engine::ParticleComponent>(engine::SceneWorld* pSceneWorld, engine::Entity entity)
void UpdateComponentWidget<engine::ParticleEmitterComponent>(engine::SceneWorld* pSceneWorld, engine::Entity entity)
{
auto* pParticleComponent = pSceneWorld->GetParticleComponent(entity);
if (!pParticleComponent)
auto* pParticleEmitterComponent = pSceneWorld->GetParticleEmitterComponent(entity);
if (!pParticleEmitterComponent)
{
return;
}
Expand Down Expand Up @@ -570,7 +570,6 @@ void Inspector::Update()
details::UpdateComponentWidget<engine::LightComponent>(pSceneWorld, selectedEntity);
details::UpdateComponentWidget<engine::SkyComponent>(pSceneWorld, selectedEntity);
details::UpdateComponentWidget<engine::TerrainComponent>(pSceneWorld, selectedEntity);
details::UpdateComponentWidget<engine::ParticleComponent>(pSceneWorld, selectedEntity);
details::UpdateComponentWidget<engine::ShaderVariantCollectionsComponent>(pSceneWorld, selectedEntity);

#ifdef ENABLE_DDGI
Expand Down
1 change: 0 additions & 1 deletion Engine/Source/Runtime/ECWorld/AllComponentsHeader.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,4 @@
#include "ECWorld/StaticMeshComponent.h"
#include "ECWorld/TerrainComponent.h"
#include "ECWorld/TransformComponent.h"
#include "ECWorld/ParticleComponent.h"
#include "ECWorld/ParticleEmitterComponent.h"
1 change: 0 additions & 1 deletion Engine/Source/Runtime/ECWorld/ParticleComponent.cpp

This file was deleted.

30 changes: 0 additions & 30 deletions Engine/Source/Runtime/ECWorld/ParticleComponent.h

This file was deleted.

56 changes: 56 additions & 0 deletions Engine/Source/Runtime/ECWorld/ParticleEmitterComponent.cpp
Original file line number Diff line number Diff line change
@@ -1 +1,57 @@
#include "ParticleEmitterComponent.h"
#include "Scene/VertexFormat.h"
#include "Rendering/Utility/VertexLayoutUtility.h"

void engine::ParticleEmitterComponent::Build()
{
cd::VertexFormat vertexFormat;
vertexFormat.AddAttributeLayout(cd::VertexAttributeType::Position, cd::AttributeValueType::Float, 3);
vertexFormat.AddAttributeLayout(cd::VertexAttributeType::Color, cd::AttributeValueType::Float, 3);

bgfx::VertexLayout vertexLayout;
VertexLayoutUtility::CreateVertexLayout(vertexLayout, vertexFormat.GetVertexLayout());

size_t vertexCount = m_particleSystem.GetMaxCount();

size_t vertexBufferSize = vertexCount * (sizeof(cd::Vec3f) + sizeof(cd::Vec3f));

m_particleVertexBuffer.resize(vertexBufferSize);

uint32_t currentDataSize = 0U;
auto currentDataPtr = m_particleVertexBuffer.data();

for (int i = 0; i < m_particleSystem.GetMaxCount(); i++)
{
std::memcpy(&currentDataPtr[currentDataSize], &m_particleSystem.GetPos(i), sizeof(cd::Vec3f));
currentDataSize += sizeof(cd::Vec3f);

std::memcpy(&currentDataPtr[currentDataSize], &m_particleSystem.GetColor(i), sizeof(cd::Vec3f));
currentDataSize += sizeof(cd::Vec3f);
}

/*
* indexBuffer
*/
size_t indexTypeSize = sizeof(uint16_t);
m_particleIndexBuffer.resize(3 * m_particleSystem.GetMaxCount() * indexTypeSize);
currentDataSize = 0U;
currentDataPtr = m_particleIndexBuffer.data();

std::vector<uint16_t> indexes;
for (uint16_t i = 0; i < m_particleSystem.GetMaxCount(); i++)
{
uint16_t vertexIndex = static_cast<uint16_t>(i * 3);
indexes.push_back(vertexIndex);
indexes.push_back(vertexIndex + 1);
indexes.push_back(vertexIndex + 2);
}

for (const auto& index : indexes)
{
std::memcpy(&currentDataPtr[currentDataSize], &index, indexTypeSize);
currentDataSize += static_cast<uint32_t>(indexTypeSize);
}

m_particleVBH = bgfx::createVertexBuffer(bgfx::makeRef(m_particleVertexBuffer.data(), static_cast<uint32_t>(m_particleVertexBuffer.size())), vertexLayout).idx;
m_particleIBH = bgfx::createIndexBuffer(bgfx::makeRef(m_particleIndexBuffer.data(), static_cast<uint32_t>(m_particleIndexBuffer.size())), 0U).idx;
}
22 changes: 21 additions & 1 deletion Engine/Source/Runtime/ECWorld/ParticleEmitterComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include "Base/Template.h"
#include "Core/StringCrc.h"
#include "Math/Vector.hpp"
#include "Math/Transform.hpp"
#include "ParticleSystem/ParticleSystem.h"

namespace engine
{
Expand All @@ -23,8 +25,26 @@ class ParticleEmitterComponent final
ParticleEmitterComponent& operator=(ParticleEmitterComponent&&) = default;
~ParticleEmitterComponent() = default;

private:
engine::ParticleSystem GetParticleSystem() { return m_particleSystem; }
void SetParticleSystem(engine::ParticleSystem& system) { m_particleSystem = system; }

uint16_t& GetParticleVBH(){ return m_particleVBH; }
void setParticleVBH(uint16_t vbh) { m_particleVBH = vbh; }

uint16_t& GetParticleIBH() { return m_particleIBH; }
void SetParticleIBH(uint16_t ibh) { m_particleIBH = ibh; }

std::vector<std::byte> &GetVertexBuffer() { return m_particleVertexBuffer; }
std::vector<std::byte> &GetIndexBuffer() { return m_particleIndexBuffer; }

void Build();

private:
ParticleSystem m_particleSystem;
std::vector<std::byte> m_particleVertexBuffer;
std::vector<std::byte> m_particleIndexBuffer;
uint16_t m_particleVBH = UINT16_MAX;
uint16_t m_particleIBH = UINT16_MAX;
};

}
1 change: 0 additions & 1 deletion Engine/Source/Runtime/ECWorld/SceneWorld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ SceneWorld::SceneWorld()
m_pShaderVariantCollectionsComponentStorage = m_pWorld->Register<engine::ShaderVariantCollectionsComponent>();
m_pSkyComponentStorage = m_pWorld->Register<engine::SkyComponent>();
m_pStaticMeshComponentStorage = m_pWorld->Register<engine::StaticMeshComponent>();
m_pParticleComponentStorage = m_pWorld->Register<engine::ParticleComponent>();
m_pParticleEmitterComponentStorage = m_pWorld->Register<engine::ParticleEmitterComponent>();
m_pTerrainComponentStorage = m_pWorld->Register<engine::TerrainComponent>();
m_pTransformComponentStorage = m_pWorld->Register<engine::TransformComponent>();
Expand Down
2 changes: 0 additions & 2 deletions Engine/Source/Runtime/ECWorld/SceneWorld.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ class SceneWorld
DEFINE_COMPONENT_STORAGE_WITH_APIS(ShaderVariantCollections);
DEFINE_COMPONENT_STORAGE_WITH_APIS(Sky);
DEFINE_COMPONENT_STORAGE_WITH_APIS(StaticMesh);
DEFINE_COMPONENT_STORAGE_WITH_APIS(Particle);
DEFINE_COMPONENT_STORAGE_WITH_APIS(ParticleEmitter);
DEFINE_COMPONENT_STORAGE_WITH_APIS(Terrain);
DEFINE_COMPONENT_STORAGE_WITH_APIS(Transform);
Expand Down Expand Up @@ -101,7 +100,6 @@ class SceneWorld
DeleteShaderVariantCollectionsComponent(entity);
DeleteSkyComponent(entity);
DeleteStaticMeshComponent(entity);
DeleteParticleComponent(entity);
DeleteParticleEmitterComponent(entity);
DeleteTerrainComponent(entity);
DeleteTransformComponent(entity);
Expand Down
99 changes: 99 additions & 0 deletions Engine/Source/Runtime/ParticleSystem/ParticleSystem.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#include "ParticleSystem.h"

void engine::ParticleSystem::AllocateParticleIndex()
{
if (!m_FreeParticleIndex.empty())
{
int index = m_FreeParticleIndex.back();
m_FreeParticleIndex.pop_back();
m_particleIndex = index;
}
else
{
++m_particleIndex;
if (m_particleIndex >= m_particleMaxCount)
{
m_particleIndex = 0;
}

if (m_isActive[m_particleIndex])
{
//full
m_particleIndex = -1;
}
}

if (m_particleIndex != -1)
{
Reset(m_particleIndex);
Active(m_particleIndex);
}
}

void engine::ParticleSystem::Reset(int index)
{
m_pos[index] = cd::Vec3f(0.0f, 0.0f, 0.0f);
m_velocity[index] = cd::Vec3f(rand() % 41, 0.0f, 0.0f);
m_acceleration[index] = cd::Vec3f(0.0f, 0.0f, 0.0f);

m_color[index] = cd::Vec3f{rand() %30, rand() %45,0.0f};

m_isActive[index] = false;
m_currentTime[index] = 0.0f;
m_lifeTime[index] = 6.0f;

}

void engine::ParticleSystem::Update(float deltaTime, int index)
{
if (m_currentTime[index] >= m_lifeTime[index])
{
m_isActive[index] = false;
return;
}

//x = v0t +1/2 at*2
m_pos[index].x() = m_pos[index].x() + m_velocity[index].x() * deltaTime + 0.5f * m_acceleration[index].x() * deltaTime * deltaTime;
m_pos[index].y() = m_pos[index].y() + m_velocity[index].y() * deltaTime + 0.5f * m_acceleration[index].y() * deltaTime * deltaTime;
m_pos[index].z() = m_pos[index].z() + m_velocity[index].z() * deltaTime + 0.5f * m_acceleration[index].z() * deltaTime * deltaTime;

m_velocity[index].x() += m_acceleration[index].x() * deltaTime;
m_velocity[index].y() += m_acceleration[index].y() * deltaTime;
m_velocity[index].z() += m_acceleration[index].z() * deltaTime;

m_currentTime[index] += deltaTime;
}

void engine::ParticleSystem::UpdateActive(float deltaTime)
{
for (int i = 0; i < m_particleMaxCount; ++i)
{
if (!m_isActive[i])
{
continue;
}

Update(deltaTime, i);
if (!m_isActive[i])
{
m_FreeParticleIndex.push_back(i);
}
else
{
++m_currentActiveCount;
}
}
}

void engine::ParticleSystem::Init()
{
m_FreeParticleIndex.resize(m_particleMaxCount);
m_pos.resize(m_particleMaxCount);
m_velocity.resize(m_particleMaxCount);
m_acceleration.resize(m_particleMaxCount);
m_color.resize(m_particleMaxCount);

m_isActive.resize(m_particleMaxCount);
m_currentTime.resize(m_particleMaxCount);
m_lifeTime.resize(m_particleMaxCount);
}
71 changes: 71 additions & 0 deletions Engine/Source/Runtime/ParticleSystem/ParticleSystem.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#pragma once

#include "Base/Template.h"
#include "Core/StringCrc.h"
#include "Math/Vector.hpp"
#include <vector>

namespace engine
{

class ParticleSystem final
{
public:
ParticleSystem() = default;
ParticleSystem(const ParticleSystem&) = default;
ParticleSystem& operator=(const ParticleSystem&) = default;
ParticleSystem(ParticleSystem&&) = default;
ParticleSystem& operator=(ParticleSystem&&) = default;
~ParticleSystem() = default;

int& GetIndex() { return m_particleIndex; }

int& GetMaxCount() { return m_particleMaxCount; }

int& GetParticleActiveCount() { return m_currentActiveCount; }

cd::Vec3f& GetPos(int index) { return m_pos[index]; }
void SetPos(cd::Vec3f pos) { m_pos[m_particleIndex] = pos; }

cd::Vec3f& GetVelocity(int index) { return m_velocity[index]; }
void SetVelocity(cd::Vec3f velocity) { m_velocity[m_particleIndex] = velocity; }

cd::Vec3f& GetAcceleration(int index) { return m_acceleration[index]; }
void SetAcceleration(cd::Vec3f acceleration) { m_acceleration[m_particleIndex] = acceleration; }

cd::Vec3f& GetColor(int index) { return m_color[index]; }
void SetColor(cd::Vec3f color) { m_color[m_particleIndex] = color; }

void Active(int index) { m_isActive[index] = true; }
bool IsActive(int index) { return m_isActive[index]; }

float& GetLifeTime(int index) { return m_lifeTime[index]; }
void SetLifeTime(float lifeTime) { m_lifeTime[m_particleIndex] = lifeTime; }

void AllocateParticleIndex();

void Reset( int index);

void Update(float deltaTime, int index);

void UpdateActive(float deltaTime);

void Init();

private:
int m_particleIndex = -1;
int m_particleMaxCount = 300;
int m_currentActiveCount = 0;

std::vector<int> m_FreeParticleIndex;
std::vector<cd::Vec3f> m_pos;
std::vector<cd::Vec3f> m_velocity;
std::vector<cd::Vec3f> m_acceleration;
std::vector<cd::Vec3f> m_color;

std::vector<bool> m_isActive;
std::vector<float> m_currentTime;
std::vector<float> m_lifeTime;
};

}
Loading