From f9ed320490ed7302f13a72416965a644f6d306d2 Mon Sep 17 00:00:00 2001 From: OVOAOVO <2410651402@qq.com> Date: Tue, 5 Mar 2024 15:02:34 +0800 Subject: [PATCH 01/14] change emitcomponent build format --- .../ECWorld/ParticleEmitterComponent.cpp | 188 ++++++++---------- .../ECWorld/ParticleEmitterComponent.h | 37 +++- .../Runtime/Rendering/ParticleRenderer.cpp | 25 ++- 3 files changed, 129 insertions(+), 121 deletions(-) diff --git a/Engine/Source/Runtime/ECWorld/ParticleEmitterComponent.cpp b/Engine/Source/Runtime/ECWorld/ParticleEmitterComponent.cpp index 32dc1ac7..7c913c4d 100644 --- a/Engine/Source/Runtime/ECWorld/ParticleEmitterComponent.cpp +++ b/Engine/Source/Runtime/ECWorld/ParticleEmitterComponent.cpp @@ -16,152 +16,128 @@ void ParticleEmitterComponent::Build() if (m_pMeshData == nullptr) { - PaddingVertexBuffer(); - PaddingIndexBuffer(); + PaddingSpriteVertexBuffer(); + PaddingSpriteIndexBuffer(); - m_particleVertexBufferHandle = bgfx::createVertexBuffer(bgfx::makeRef(m_particleVertexBuffer.data(), static_cast(m_particleVertexBuffer.size())), vertexLayout).idx; - m_particleIndexBufferHandle = bgfx::createIndexBuffer(bgfx::makeRef(m_particleIndexBuffer.data(), static_cast(m_particleIndexBuffer.size())), 0U).idx; + //PaddingRibbonVertexBuffer(); + //PaddingRibbonIndexBuffer(); + + m_spriteParticleVertexBufferHandle = bgfx::createVertexBuffer(bgfx::makeRef(m_spriteParticleVertexBuffer.data(), static_cast(m_spriteParticleVertexBuffer.size())), vertexLayout).idx; + m_spriteParticleIndexBufferHandle = bgfx::createIndexBuffer(bgfx::makeRef(m_spriteParticleIndexBuffer.data(), static_cast(m_spriteParticleIndexBuffer.size())), 0U).idx; + + /* m_ribbonParticleVertexBufferHandle = bgfx::createVertexBuffer(bgfx::makeRef(m_ribbonParticleVertexBuffer.data(), static_cast(m_ribbonParticleVertexBuffer.size())), vertexLayout).idx; + m_ribbonParticleIndexBufferHandle = bgfx::createIndexBuffer(bgfx::makeRef(m_ribbonParticleIndexBuffer.data(), static_cast(m_ribbonParticleIndexBuffer.size())), 0U).idx; + */ } else { - m_particleVertexBuffer = cd::BuildVertexBufferForStaticMesh(*m_pMeshData, *m_pRequiredVertexFormat).value(); + //TODO: mesh only have sprite now we need to add more different particle type + m_spriteParticleVertexBuffer = cd::BuildVertexBufferForStaticMesh(*m_pMeshData, *m_pRequiredVertexFormat).value(); for (const auto& optionalVec : cd::BuildIndexBufferesForMesh(*m_pMeshData)) { const std::vector& vec = optionalVec.value(); - m_particleIndexBuffer.insert(m_particleIndexBuffer.end(), vec.begin(), vec.end()); + m_spriteParticleIndexBuffer.insert(m_spriteParticleIndexBuffer.end(), vec.begin(), vec.end()); } - m_particleVertexBufferHandle = bgfx::createVertexBuffer(bgfx::makeRef(m_particleVertexBuffer.data(), static_cast(m_particleVertexBuffer.size())), vertexLayout).idx; - m_particleIndexBufferHandle = bgfx::createIndexBuffer(bgfx::makeRef(m_particleIndexBuffer.data(), static_cast(m_particleIndexBuffer.size())), 0U).idx; + m_spriteParticleVertexBufferHandle = bgfx::createVertexBuffer(bgfx::makeRef(m_spriteParticleVertexBuffer.data(), static_cast(m_spriteParticleVertexBuffer.size())), vertexLayout).idx; + m_spriteParticleIndexBufferHandle = bgfx::createIndexBuffer(bgfx::makeRef(m_spriteParticleIndexBuffer.data(), static_cast(m_spriteParticleIndexBuffer.size())), 0U).idx; } } -void ParticleEmitterComponent::PaddingVertexBuffer() +void ParticleEmitterComponent::PaddingSpriteVertexBuffer() { //m_particleVertexBuffer.clear(); //m_particleVertexBuffer.insert(m_particleVertexBuffer.end(), m_particlePool.GetRenderDataBuffer().begin(), m_particlePool.GetRenderDataBuffer().end()); - m_particleVertexBuffer.clear(); - const bool containsPosition = m_pRequiredVertexFormat->Contains(cd::VertexAttributeType::Position); const bool containsColor = m_pRequiredVertexFormat->Contains(cd::VertexAttributeType::Color); const bool containsUV = m_pRequiredVertexFormat->Contains(cd::VertexAttributeType::UV); //vertexbuffer - if (m_emitterParticleType == ParticleType::Sprite) - { - constexpr int meshVertexCount = Particle::GetMeshVertexCount(); - const int MAX_VERTEX_COUNT = m_particlePool.GetParticleMaxCount() * meshVertexCount; - size_t vertexCount = MAX_VERTEX_COUNT; - const uint32_t vertexFormatStride = m_pRequiredVertexFormat->GetStride(); - - m_particleVertexBuffer.resize(vertexCount * vertexFormatStride); + constexpr int meshVertexCount = Particle::GetMeshVertexCount(); + const int MAX_VERTEX_COUNT = m_particlePool.GetParticleMaxCount() * meshVertexCount; + size_t vertexCount = MAX_VERTEX_COUNT; + const uint32_t vertexFormatStride = m_pRequiredVertexFormat->GetStride(); - uint32_t currentDataSize = 0U; - auto currentDataPtr = m_particleVertexBuffer.data(); + m_spriteParticleVertexBuffer.resize(vertexCount * vertexFormatStride); - std::vector vertexDataBuffer; - vertexDataBuffer.resize(MAX_VERTEX_COUNT); - // pos color uv - // only a picture now - for (int i = 0; i < MAX_VERTEX_COUNT; i += meshVertexCount) - { - vertexDataBuffer[i] = { cd::Vec3f(-1.0f,-1.0f,0.0f),cd::Vec4f(1.0f,1.0f,1.0f,1.0f),cd::Vec2f(1.0f,1.0f) }; - vertexDataBuffer[i + 1] = { cd::Vec3f(1.0f,-1.0f,0.0f),cd::Vec4f(1.0f,1.0f,1.0f,1.0f),cd::Vec2f(0.0f,1.0f) }; - vertexDataBuffer[i + 2] = { cd::Vec3f(1.0f,1.0f,0.0f),cd::Vec4f(1.0f,1.0f,1.0f,1.0f),cd::Vec2f(0.0f,0.0f) }; - vertexDataBuffer[i + 3] = { cd::Vec3f(-1.0f,1.0f,0.0f),cd::Vec4f(1.0f,1.0f,1.0f,1.0f),cd::Vec2f(1.0f,0.0f) }; - } - - for (int i = 0; i < MAX_VERTEX_COUNT; ++i) - { - if (containsPosition) - { - std::memcpy(¤tDataPtr[currentDataSize], &vertexDataBuffer[i].pos, sizeof(cd::Point)); - currentDataSize += sizeof(cd::Point); - } - - if (containsColor) - { - std::memcpy(¤tDataPtr[currentDataSize], &vertexDataBuffer[i].color, sizeof(cd::Color)); - currentDataSize += sizeof(cd::Color); - } - - if (containsUV) - { - std::memcpy(¤tDataPtr[currentDataSize], &vertexDataBuffer[i].uv, sizeof(cd::UV)); - currentDataSize += sizeof(cd::UV); - } - } - } - else if (m_emitterParticleType == engine::ParticleType::Ribbon) - { + uint32_t currentDataSize = 0U; + auto currentDataPtr = m_spriteParticleVertexBuffer.data(); - } - else if (m_emitterParticleType == engine::ParticleType::Track) + std::vector vertexDataBuffer; + vertexDataBuffer.resize(MAX_VERTEX_COUNT); + // pos color uv + // only a picture now + for (int i = 0; i < MAX_VERTEX_COUNT; i += meshVertexCount) { - + vertexDataBuffer[i] = { cd::Vec3f(-1.0f,-1.0f,0.0f),cd::Vec4f(1.0f,1.0f,1.0f,1.0f),cd::Vec2f(1.0f,1.0f) }; + vertexDataBuffer[i + 1] = { cd::Vec3f(1.0f,-1.0f,0.0f),cd::Vec4f(1.0f,1.0f,1.0f,1.0f),cd::Vec2f(0.0f,1.0f) }; + vertexDataBuffer[i + 2] = { cd::Vec3f(1.0f,1.0f,0.0f),cd::Vec4f(1.0f,1.0f,1.0f,1.0f),cd::Vec2f(0.0f,0.0f) }; + vertexDataBuffer[i + 3] = { cd::Vec3f(-1.0f,1.0f,0.0f),cd::Vec4f(1.0f,1.0f,1.0f,1.0f),cd::Vec2f(1.0f,0.0f) }; } - else if (m_emitterParticleType == engine::ParticleType::Ring) - { - } - else if (m_emitterParticleType == engine::ParticleType::Model) + for (int i = 0; i < MAX_VERTEX_COUNT; ++i) { + if (containsPosition) + { + std::memcpy(¤tDataPtr[currentDataSize], &vertexDataBuffer[i].pos, sizeof(cd::Point)); + currentDataSize += sizeof(cd::Point); + } - } -} - -void ParticleEmitterComponent::PaddingIndexBuffer() -{ - m_particleIndexBuffer.clear(); - if (m_emitterParticleType == engine::ParticleType::Sprite) - { - constexpr int meshVertexCount = Particle::GetMeshVertexCount(); - const bool useU16Index = meshVertexCount <= static_cast(std::numeric_limits::max()) + 1U; - const uint32_t indexTypeSize = useU16Index ? sizeof(uint16_t) : sizeof(uint32_t); - const int MAX_VERTEX_COUNT = m_particlePool.GetParticleMaxCount() * meshVertexCount; - int indexCountForOneSprite = 6; - const uint32_t indicesCount = MAX_VERTEX_COUNT / meshVertexCount * indexCountForOneSprite; - m_particleIndexBuffer.resize(indicesCount * indexTypeSize); - /// - /* size_t indexTypeSize = sizeof(uint16_t); - m_particleIndexBuffer.resize(m_particleSystem.GetMaxCount() / 4 * 6 * indexTypeSize);*/ - uint32_t currentDataSize = 0U; - auto currentDataPtr = m_particleIndexBuffer.data(); - - std::vector indexes; - for (uint16_t i = 0; i < MAX_VERTEX_COUNT; i += meshVertexCount) + if (containsColor) { - uint16_t vertexIndex = static_cast(i); - indexes.push_back(vertexIndex); - indexes.push_back(vertexIndex + 1); - indexes.push_back(vertexIndex + 2); - indexes.push_back(vertexIndex); - indexes.push_back(vertexIndex + 2); - indexes.push_back(vertexIndex + 3); + std::memcpy(¤tDataPtr[currentDataSize], &vertexDataBuffer[i].color, sizeof(cd::Color)); + currentDataSize += sizeof(cd::Color); } - for (const auto& index : indexes) + if (containsUV) { - std::memcpy(¤tDataPtr[currentDataSize], &index, indexTypeSize); - currentDataSize += static_cast(indexTypeSize); + std::memcpy(¤tDataPtr[currentDataSize], &vertexDataBuffer[i].uv, sizeof(cd::UV)); + currentDataSize += sizeof(cd::UV); } } - else if (m_emitterParticleType == engine::ParticleType::Ribbon) +} + +void ParticleEmitterComponent::PaddingSpriteIndexBuffer() +{ + constexpr int meshVertexCount = Particle::GetMeshVertexCount(); + const bool useU16Index = meshVertexCount <= static_cast(std::numeric_limits::max()) + 1U; + const uint32_t indexTypeSize = useU16Index ? sizeof(uint16_t) : sizeof(uint32_t); + const int MAX_VERTEX_COUNT = m_particlePool.GetParticleMaxCount() * meshVertexCount; + int indexCountForOneSprite = 6; + const uint32_t indicesCount = MAX_VERTEX_COUNT / meshVertexCount * indexCountForOneSprite; + m_spriteParticleIndexBuffer.resize(indicesCount * indexTypeSize); + /// +/* size_t indexTypeSize = sizeof(uint16_t); + m_particleIndexBuffer.resize(m_particleSystem.GetMaxCount() / 4 * 6 * indexTypeSize);*/ + uint32_t currentDataSize = 0U; + auto currentDataPtr = m_spriteParticleIndexBuffer.data(); + + std::vector indexes; + for (uint16_t i = 0; i < MAX_VERTEX_COUNT; i += meshVertexCount) { - + uint16_t vertexIndex = static_cast(i); + indexes.push_back(vertexIndex); + indexes.push_back(vertexIndex + 1); + indexes.push_back(vertexIndex + 2); + indexes.push_back(vertexIndex); + indexes.push_back(vertexIndex + 2); + indexes.push_back(vertexIndex + 3); } - else if (m_emitterParticleType == engine::ParticleType::Track) - { - } - else if (m_emitterParticleType == engine::ParticleType::Ring) + for (const auto& index : indexes) { - + std::memcpy(¤tDataPtr[currentDataSize], &index, indexTypeSize); + currentDataSize += static_cast(indexTypeSize); } - else if (m_emitterParticleType == engine::ParticleType::Model) - { +} - } +void ParticleEmitterComponent::PaddingRibbonVertexBuffer() +{ } +void ParticleEmitterComponent::PaddingRibbonIndexBuffer() +{ +} + + void ParticleEmitterComponent::BuildParticleShape() { if (m_emitterShape == ParticleEmitterShape::Box) diff --git a/Engine/Source/Runtime/ECWorld/ParticleEmitterComponent.h b/Engine/Source/Runtime/ECWorld/ParticleEmitterComponent.h index 51bc53c4..c9230647 100644 --- a/Engine/Source/Runtime/ECWorld/ParticleEmitterComponent.h +++ b/Engine/Source/Runtime/ECWorld/ParticleEmitterComponent.h @@ -87,11 +87,17 @@ class ParticleEmitterComponent final float& GetLifeTime() { return m_emitterLifeTime; } void SetEmitterLifeTime(float lifetime) { m_emitterLifeTime = lifetime; } - uint16_t& GetParticleVertexBufferHandle() { return m_particleVertexBufferHandle; } - uint16_t& GetParticleIndexBufferHandle() { return m_particleIndexBufferHandle; } + uint16_t& GetSpriteParticleVertexBufferHandle() { return m_spriteParticleVertexBufferHandle; } + uint16_t& GetSpriteParticleIndexBufferHandle() { return m_spriteParticleIndexBufferHandle; } - std::vector& GetVertexBuffer() { return m_particleVertexBuffer; } - std::vector& GetIndexBuffer() { return m_particleIndexBuffer; } + std::vector& GetSpriteVertexBuffer() { return m_spriteParticleVertexBuffer; } + std::vector& GetSpriteIndexBuffer() { return m_spriteParticleIndexBuffer; } + + uint16_t& GetRibbonParticleVertexBufferHandle() { return m_ribbonParticleVertexBufferHandle; } + uint16_t& GetRibbonParticleIndexBufferHandle() { return m_ribbonParticleIndexBufferHandle; } + + std::vector& GetRibbonVertexBuffer() { return m_ribbonParticleVertexBuffer; } + std::vector& GetRibbonIndexBuffer() { return m_ribbonParticleIndexBuffer; } uint16_t& GetEmitterShapeVertexBufferHandle() { return m_emitterShapeVertexBufferHandle; } uint16_t& GetEmitterShapeIndexBufferHandle() { return m_emitterShapeIndexBufferHandle; } @@ -107,8 +113,11 @@ class ParticleEmitterComponent final void SetRequiredVertexFormat(const cd::VertexFormat* pVertexFormat) { m_pRequiredVertexFormat = pVertexFormat; } //void UpdateBuffer(); - void PaddingVertexBuffer(); - void PaddingIndexBuffer(); + void PaddingSpriteVertexBuffer(); + void PaddingSpriteIndexBuffer(); + + void PaddingRibbonVertexBuffer(); + void PaddingRibbonIndexBuffer(); void BuildParticleShape(); void RePaddingShapeBuffer(); @@ -161,11 +170,19 @@ class ParticleEmitterComponent final cd::Vec4f color; cd::UV uv; }; + + //Sprite const cd::VertexFormat* m_pRequiredVertexFormat = nullptr; - std::vector m_particleVertexBuffer; - std::vector m_particleIndexBuffer; - uint16_t m_particleVertexBufferHandle = UINT16_MAX; - uint16_t m_particleIndexBufferHandle = UINT16_MAX; + std::vector m_spriteParticleVertexBuffer; + std::vector m_spriteParticleIndexBuffer; + uint16_t m_spriteParticleVertexBufferHandle = UINT16_MAX; + uint16_t m_spriteParticleIndexBufferHandle = UINT16_MAX; + //Ribbon + std::vector m_ribbonParticleVertexBuffer; + std::vector m_ribbonParticleIndexBuffer; + uint16_t m_ribbonParticleVertexBufferHandle = UINT16_MAX; + uint16_t m_ribbonParticleIndexBufferHandle = UINT16_MAX; + //emitter shape vertex/index ParticleEmitterShape m_emitterShape = ParticleEmitterShape::Box; diff --git a/Engine/Source/Runtime/Rendering/ParticleRenderer.cpp b/Engine/Source/Runtime/Rendering/ParticleRenderer.cpp index ed72070b..79fca54a 100644 --- a/Engine/Source/Runtime/Rendering/ParticleRenderer.cpp +++ b/Engine/Source/Runtime/Rendering/ParticleRenderer.cpp @@ -159,9 +159,16 @@ void ParticleRenderer::Render(float deltaTime) constexpr StringCrc ParticleSampler("s_texColor"); bgfx::setTexture(0, GetRenderContext()->GetUniform(ParticleSampler), m_particleTextureHandle); - bgfx::setVertexBuffer(0, bgfx::VertexBufferHandle{ pEmitterComponent->GetParticleVertexBufferHandle() }); - bgfx::setIndexBuffer(bgfx::IndexBufferHandle{ pEmitterComponent->GetParticleIndexBufferHandle() }); - + if (pEmitterComponent->GetEmitterParticleType() == engine::ParticleType::Sprite) + { + bgfx::setVertexBuffer(0, bgfx::VertexBufferHandle{ pEmitterComponent->GetSpriteParticleVertexBufferHandle() }); + bgfx::setIndexBuffer(bgfx::IndexBufferHandle{ pEmitterComponent->GetSpriteParticleIndexBufferHandle() }); + } + //else if (pEmitterComponent->GetEmitterParticleType() == engine::ParticleType::Ribbon) + //{ + // bgfx::setVertexBuffer(0, bgfx::VertexBufferHandle{ pEmitterComponent->GetRibbonParticleVertexBufferHandle() }); + // bgfx::setIndexBuffer(bgfx::IndexBufferHandle{ pEmitterComponent->GetRibbonParticleIndexBufferHandle() }); + //} bgfx::setInstanceDataBuffer(&idb); @@ -208,8 +215,16 @@ void ParticleRenderer::Render(float deltaTime) constexpr StringCrc ParticleSampler("s_texColor"); bgfx::setTexture(0, GetRenderContext()->GetUniform(ParticleSampler), m_particleTextureHandle); - bgfx::setVertexBuffer(0, bgfx::VertexBufferHandle{ pEmitterComponent->GetParticleVertexBufferHandle() }); - bgfx::setIndexBuffer(bgfx::IndexBufferHandle{ pEmitterComponent->GetParticleIndexBufferHandle() }); + if (pEmitterComponent->GetEmitterParticleType() == engine::ParticleType::Sprite) + { + bgfx::setVertexBuffer(0, bgfx::VertexBufferHandle{ pEmitterComponent->GetSpriteParticleVertexBufferHandle() }); + bgfx::setIndexBuffer(bgfx::IndexBufferHandle{ pEmitterComponent->GetSpriteParticleIndexBufferHandle() }); + } + //else if (pEmitterComponent->GetEmitterParticleType() == engine::ParticleType::Ribbon) + //{ + // bgfx::setVertexBuffer(0, bgfx::VertexBufferHandle{ pEmitterComponent->GetRibbonParticleVertexBufferHandle() }); + // bgfx::setIndexBuffer(bgfx::IndexBufferHandle{ pEmitterComponent->GetRibbonParticleIndexBufferHandle() }); + //} bgfx::setState(state_tristrip); From 159a8456d90f32c5b21534425696f609833d619c Mon Sep 17 00:00:00 2001 From: OVOAOVO <2410651402@qq.com> Date: Tue, 5 Mar 2024 15:48:43 +0800 Subject: [PATCH 02/14] Add Range Of Particle Life Time --- Engine/Source/Editor/UILayers/Inspector.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Engine/Source/Editor/UILayers/Inspector.cpp b/Engine/Source/Editor/UILayers/Inspector.cpp index be0cb447..739fd4cb 100644 --- a/Engine/Source/Editor/UILayers/Inspector.cpp +++ b/Engine/Source/Editor/UILayers/Inspector.cpp @@ -622,10 +622,10 @@ void UpdateComponentWidget(engine::SceneWorld* 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::ImGuiBoolProperty("RandomVelocity State", pParticleEmitterComponent->GetRandomVelocityState()); ImGuiUtils::ImGuiVectorProperty("Acceleration", pParticleEmitterComponent->GetEmitterAcceleration()); ImGuiUtils::ColorPickerProperty("Color", pParticleEmitterComponent->GetEmitterColor()); - ImGuiUtils::ImGuiFloatProperty("LifeTime", pParticleEmitterComponent->GetLifeTime()); + ImGuiUtils::ImGuiFloatProperty("LifeTime", pParticleEmitterComponent->GetLifeTime(),cd::Unit::None, 0, 6); if (ImGuiUtils::ImGuiBoolProperty("Instance State", pParticleEmitterComponent->GetInstanceState())) { pParticleEmitterComponent->ActivateShaderFeature(engine::ShaderFeature::PARTICLE_INSTANCE); From 8aad638bfe7db0d52118a970b4da6db486bd9428 Mon Sep 17 00:00:00 2001 From: OVOAOVO <2410651402@qq.com> Date: Mon, 11 Mar 2024 14:33:59 +0800 Subject: [PATCH 03/14] beta ribbon --- .../ECWorld/ParticleEmitterComponent.cpp | 164 +++++++++++++++++- .../ECWorld/ParticleEmitterComponent.h | 2 + .../Runtime/ParticleSystem/ParticlePool.h | 2 +- .../Runtime/Rendering/ParticleRenderer.cpp | 40 ++--- 4 files changed, 181 insertions(+), 27 deletions(-) diff --git a/Engine/Source/Runtime/ECWorld/ParticleEmitterComponent.cpp b/Engine/Source/Runtime/ECWorld/ParticleEmitterComponent.cpp index 7c913c4d..ce3aeda0 100644 --- a/Engine/Source/Runtime/ECWorld/ParticleEmitterComponent.cpp +++ b/Engine/Source/Runtime/ECWorld/ParticleEmitterComponent.cpp @@ -19,15 +19,14 @@ void ParticleEmitterComponent::Build() PaddingSpriteVertexBuffer(); PaddingSpriteIndexBuffer(); - //PaddingRibbonVertexBuffer(); - //PaddingRibbonIndexBuffer(); + PaddingRibbonVertexBuffer(); + PaddingRibbonIndexBuffer(); m_spriteParticleVertexBufferHandle = bgfx::createVertexBuffer(bgfx::makeRef(m_spriteParticleVertexBuffer.data(), static_cast(m_spriteParticleVertexBuffer.size())), vertexLayout).idx; m_spriteParticleIndexBufferHandle = bgfx::createIndexBuffer(bgfx::makeRef(m_spriteParticleIndexBuffer.data(), static_cast(m_spriteParticleIndexBuffer.size())), 0U).idx; - /* m_ribbonParticleVertexBufferHandle = bgfx::createVertexBuffer(bgfx::makeRef(m_ribbonParticleVertexBuffer.data(), static_cast(m_ribbonParticleVertexBuffer.size())), vertexLayout).idx; - m_ribbonParticleIndexBufferHandle = bgfx::createIndexBuffer(bgfx::makeRef(m_ribbonParticleIndexBuffer.data(), static_cast(m_ribbonParticleIndexBuffer.size())), 0U).idx; - */ + m_ribbonParticleVertexBufferHandle = bgfx::createDynamicVertexBuffer(bgfx::makeRef(m_ribbonParticleVertexBuffer.data(), static_cast(m_ribbonParticleVertexBuffer.size())), vertexLayout).idx; + m_ribbonParticleIndexBufferHandle = bgfx::createDynamicIndexBuffer(bgfx::makeRef(m_ribbonParticleIndexBuffer.data(), static_cast(m_ribbonParticleIndexBuffer.size())), 0U).idx; } else { @@ -131,12 +130,167 @@ void ParticleEmitterComponent::PaddingSpriteIndexBuffer() void ParticleEmitterComponent::PaddingRibbonVertexBuffer() { + const bool containsPosition = m_pRequiredVertexFormat->Contains(cd::VertexAttributeType::Position); + const bool containsColor = m_pRequiredVertexFormat->Contains(cd::VertexAttributeType::Color); + const bool containsUV = m_pRequiredVertexFormat->Contains(cd::VertexAttributeType::UV); + //vertexbuffer + constexpr int meshVertexCount = Particle::GetMeshVertexCount(); + const int MAX_VERTEX_COUNT = m_particlePool.GetParticleMaxCount() * meshVertexCount; + size_t vertexCount = MAX_VERTEX_COUNT; + const uint32_t vertexFormatStride = m_pRequiredVertexFormat->GetStride(); + + m_ribbonParticleVertexBuffer.resize(vertexCount * vertexFormatStride); + + uint32_t currentDataSize = 0U; + auto currentDataPtr = m_ribbonParticleVertexBuffer.data(); + + std::vector vertexDataBuffer; + vertexDataBuffer.resize(MAX_VERTEX_COUNT); + // pos color uv + // only a picture now + for (int i = 0; i < MAX_VERTEX_COUNT; i += meshVertexCount) + { + vertexDataBuffer[i] = { cd::Vec3f(0.0f,-1.0f,0.0f),cd::Vec4f(1.0f,1.0f,1.0f,1.0f),cd::Vec2f(0.5f,0.5f)}; + vertexDataBuffer[i+1] = { cd::Vec3f(0.0f,1.0f,0.0f),cd::Vec4f(1.0f,1.0f,1.0f,1.0f),cd::Vec2f(0.5f,0.5f)}; + } + + for (int i = 0; i < MAX_VERTEX_COUNT; ++i) + { + if (containsPosition) + { + std::memcpy(¤tDataPtr[currentDataSize], &vertexDataBuffer[i].pos, sizeof(cd::Point)); + currentDataSize += sizeof(cd::Point); + } + + if (containsColor) + { + std::memcpy(¤tDataPtr[currentDataSize], &vertexDataBuffer[i].color, sizeof(cd::Color)); + currentDataSize += sizeof(cd::Color); + } + + if (containsUV) + { + std::memcpy(¤tDataPtr[currentDataSize], &vertexDataBuffer[i].uv, sizeof(cd::UV)); + currentDataSize += sizeof(cd::UV); + } + } } void ParticleEmitterComponent::PaddingRibbonIndexBuffer() { + constexpr int meshVertexCount = Particle::GetMeshVertexCount();; + const bool useU16Index = meshVertexCount <= static_cast(std::numeric_limits::max()) + 1U; + const uint32_t indexTypeSize = useU16Index ? sizeof(uint16_t) : sizeof(uint32_t); + const int MAX_VERTEX_COUNT = m_particlePool.GetParticleMaxCount() * meshVertexCount; + int indexCountForOneRibbon = 3; + const uint32_t indicesCount = MAX_VERTEX_COUNT / meshVertexCount * indexCountForOneRibbon; + m_ribbonParticleIndexBuffer.resize(indicesCount * indexTypeSize); + /// +/* size_t indexTypeSize = sizeof(uint16_t); + m_particleIndexBuffer.resize(m_particleSystem.GetMaxCount() / 4 * 6 * indexTyp + eSize);*/ + uint32_t currentDataSize = 0U; + auto currentDataPtr = m_ribbonParticleIndexBuffer.data(); + + std::vector indexes; + + for (uint16_t i = 0; i < MAX_VERTEX_COUNT; i += meshVertexCount) + { + uint16_t vertexIndex = static_cast(i); + + indexes.push_back(vertexIndex); + indexes.push_back(vertexIndex+1); + indexes.push_back(vertexIndex+2); + + } + + for (const auto& index : indexes) + { + std::memcpy(¤tDataPtr[currentDataSize], &index, indexTypeSize); + currentDataSize += static_cast(indexTypeSize); + } +} + + +void ParticleEmitterComponent::RePaddingRibbonVertexBuffer() +{ + const bool containsPosition = m_pRequiredVertexFormat->Contains(cd::VertexAttributeType::Position); + const bool containsColor = m_pRequiredVertexFormat->Contains(cd::VertexAttributeType::Color); + const bool containsUV = m_pRequiredVertexFormat->Contains(cd::VertexAttributeType::UV); + //vertexbuffer + constexpr int meshVertexCount = Particle::GetMeshVertexCount(); + const int MAX_VERTEX_COUNT = m_particlePool.GetParticleMaxCount() * meshVertexCount; + size_t vertexCount = MAX_VERTEX_COUNT; + const uint32_t vertexFormatStride = m_pRequiredVertexFormat->GetStride(); + + m_ribbonParticleVertexBuffer.resize(vertexCount * vertexFormatStride); + + uint32_t currentDataSize = 0U; + auto currentDataPtr = m_ribbonParticleVertexBuffer.data(); + + std::vector vertexDataBuffer; + vertexDataBuffer.resize(MAX_VERTEX_COUNT); + // pos color uv + // only a picture now + for (int i = 0,index = 0; i < MAX_VERTEX_COUNT; i += meshVertexCount,index++) + { + vertexDataBuffer[i] = { m_particlePool.GetParticle(index).GetPos()+cd::Vec3f(0.0f,1.0f,0.0f),cd::Vec4f(1.0f,1.0f,1.0f,1.0f),cd::Vec2f(0.5f,0.5f)}; + vertexDataBuffer[i + 1] = { m_particlePool.GetParticle(index).GetPos()+cd::Vec3f(0.0f,-1.0f,0.0f),cd::Vec4f(1.0f,1.0f,1.0f,1.0f),cd::Vec2f(0.5f,0.5f)}; + } + + for (int i = 0; i < MAX_VERTEX_COUNT; ++i) + { + if (containsPosition) + { + std::memcpy(¤tDataPtr[currentDataSize], &vertexDataBuffer[i].pos, sizeof(cd::Point)); + currentDataSize += sizeof(cd::Point); + } + + if (containsColor) + { + std::memcpy(¤tDataPtr[currentDataSize], &vertexDataBuffer[i].color, sizeof(cd::Color)); + currentDataSize += sizeof(cd::Color); + } + + if (containsUV) + { + std::memcpy(¤tDataPtr[currentDataSize], &vertexDataBuffer[i].uv, sizeof(cd::UV)); + currentDataSize += sizeof(cd::UV); + } + } } +void ParticleEmitterComponent::RePaddingRibbonIndexBuffer() +{ + constexpr int meshVertexCount = Particle::GetMeshVertexCount();; + const bool useU16Index = meshVertexCount <= static_cast(std::numeric_limits::max()) + 1U; + const uint32_t indexTypeSize = useU16Index ? sizeof(uint16_t) : sizeof(uint32_t); + const int MAX_VERTEX_COUNT = m_particlePool.GetParticleMaxCount() * meshVertexCount; + int indexCountForOneRibbon = 3; + const uint32_t indicesCount = MAX_VERTEX_COUNT / meshVertexCount * indexCountForOneRibbon; + m_ribbonParticleIndexBuffer.resize(indicesCount * indexTypeSize); + /// +/* size_t indexTypeSize = sizeof(uint16_t); + m_particleIndexBuffer.resize(m_particleSystem.GetMaxCount() / 4 * 6 * indexTyp + eSize);*/ + uint32_t currentDataSize = 0U; + auto currentDataPtr = m_ribbonParticleIndexBuffer.data(); + + std::vector indexes; + for (uint16_t i = 0; i < MAX_VERTEX_COUNT; i += meshVertexCount) + { + uint16_t vertexIndex = static_cast(i); + indexes.push_back(vertexIndex); + indexes.push_back(vertexIndex + 1); + indexes.push_back(vertexIndex + 2); + } + + for (const auto& index : indexes) + { + std::memcpy(¤tDataPtr[currentDataSize], &index, indexTypeSize); + currentDataSize += static_cast(indexTypeSize); + } +} void ParticleEmitterComponent::BuildParticleShape() { diff --git a/Engine/Source/Runtime/ECWorld/ParticleEmitterComponent.h b/Engine/Source/Runtime/ECWorld/ParticleEmitterComponent.h index c9230647..e679555c 100644 --- a/Engine/Source/Runtime/ECWorld/ParticleEmitterComponent.h +++ b/Engine/Source/Runtime/ECWorld/ParticleEmitterComponent.h @@ -118,6 +118,8 @@ class ParticleEmitterComponent final void PaddingRibbonVertexBuffer(); void PaddingRibbonIndexBuffer(); + void RePaddingRibbonVertexBuffer(); + void RePaddingRibbonIndexBuffer(); void BuildParticleShape(); void RePaddingShapeBuffer(); diff --git a/Engine/Source/Runtime/ParticleSystem/ParticlePool.h b/Engine/Source/Runtime/ParticleSystem/ParticlePool.h index 54bc1657..41ad15d5 100644 --- a/Engine/Source/Runtime/ParticleSystem/ParticlePool.h +++ b/Engine/Source/Runtime/ParticleSystem/ParticlePool.h @@ -31,7 +31,7 @@ class ParticlePool final void AllParticlesReset(); private: - int m_maxParticleCount = 75; + int m_maxParticleCount = 1; int m_currentActiveCount = 0; int m_currentParticleCount = 0; std::vector m_particles; diff --git a/Engine/Source/Runtime/Rendering/ParticleRenderer.cpp b/Engine/Source/Runtime/Rendering/ParticleRenderer.cpp index 79fca54a..eca235a6 100644 --- a/Engine/Source/Runtime/Rendering/ParticleRenderer.cpp +++ b/Engine/Source/Runtime/Rendering/ParticleRenderer.cpp @@ -164,15 +164,14 @@ void ParticleRenderer::Render(float deltaTime) bgfx::setVertexBuffer(0, bgfx::VertexBufferHandle{ pEmitterComponent->GetSpriteParticleVertexBufferHandle() }); bgfx::setIndexBuffer(bgfx::IndexBufferHandle{ pEmitterComponent->GetSpriteParticleIndexBufferHandle() }); } - //else if (pEmitterComponent->GetEmitterParticleType() == engine::ParticleType::Ribbon) - //{ - // bgfx::setVertexBuffer(0, bgfx::VertexBufferHandle{ pEmitterComponent->GetRibbonParticleVertexBufferHandle() }); - // bgfx::setIndexBuffer(bgfx::IndexBufferHandle{ pEmitterComponent->GetRibbonParticleIndexBufferHandle() }); - //} - - bgfx::setInstanceDataBuffer(&idb); + else if (pEmitterComponent->GetEmitterParticleType() == engine::ParticleType::Ribbon) + { + bgfx::setVertexBuffer(0, bgfx::VertexBufferHandle{ pEmitterComponent->GetRibbonParticleVertexBufferHandle() }); + bgfx::setIndexBuffer(bgfx::IndexBufferHandle{ pEmitterComponent->GetRibbonParticleIndexBufferHandle() }); + } bgfx::setState(state_tristrip); + bgfx::setInstanceDataBuffer(&idb); if (pEmitterComponent->GetRenderMode() == engine::ParticleRenderMode::Mesh) { @@ -210,23 +209,27 @@ void ParticleRenderer::Render(float deltaTime) pitch, yaw, roll, pEmitterComponent->GetParticlePool().GetParticle(ii).GetPos().x(), pEmitterComponent->GetParticlePool().GetParticle(ii).GetPos().y(), pEmitterComponent->GetParticlePool().GetParticle(ii).GetPos().z()); } - bgfx::setTransform(mtx); - constexpr StringCrc ParticleSampler("s_texColor"); bgfx::setTexture(0, GetRenderContext()->GetUniform(ParticleSampler), m_particleTextureHandle); + bgfx::setState(state_tristrip); if (pEmitterComponent->GetEmitterParticleType() == engine::ParticleType::Sprite) { bgfx::setVertexBuffer(0, bgfx::VertexBufferHandle{ pEmitterComponent->GetSpriteParticleVertexBufferHandle() }); bgfx::setIndexBuffer(bgfx::IndexBufferHandle{ pEmitterComponent->GetSpriteParticleIndexBufferHandle() }); } - //else if (pEmitterComponent->GetEmitterParticleType() == engine::ParticleType::Ribbon) - //{ - // bgfx::setVertexBuffer(0, bgfx::VertexBufferHandle{ pEmitterComponent->GetRibbonParticleVertexBufferHandle() }); - // bgfx::setIndexBuffer(bgfx::IndexBufferHandle{ pEmitterComponent->GetRibbonParticleIndexBufferHandle() }); - //} - - bgfx::setState(state_tristrip); + else if (pEmitterComponent->GetEmitterParticleType() == engine::ParticleType::Ribbon) + { + //bgfx::setState(state_lines); + pEmitterComponent->RePaddingRibbonVertexBuffer(); + pEmitterComponent->RePaddingRibbonIndexBuffer(); + const bgfx::Memory* pParticleVertexBuffer = bgfx::makeRef(pEmitterComponent->GetRibbonVertexBuffer().data(), static_cast(pEmitterComponent->GetRibbonVertexBuffer().size())); + const bgfx::Memory* pParticleIndexBuffer = bgfx::makeRef(pEmitterComponent->GetRibbonIndexBuffer().data(), static_cast(pEmitterComponent->GetRibbonIndexBuffer().size())); + bgfx::update(bgfx::DynamicVertexBufferHandle{ pEmitterComponent->GetRibbonParticleVertexBufferHandle()}, 0, pParticleVertexBuffer); + bgfx::update(bgfx::DynamicIndexBufferHandle{pEmitterComponent->GetRibbonParticleIndexBufferHandle()}, 0, pParticleIndexBuffer); + bgfx::setVertexBuffer(0, bgfx::DynamicVertexBufferHandle{ pEmitterComponent->GetRibbonParticleVertexBufferHandle() }); + bgfx::setIndexBuffer(bgfx::DynamicIndexBufferHandle{ pEmitterComponent->GetRibbonParticleIndexBufferHandle() }); + } if (pEmitterComponent->GetRenderMode() == engine::ParticleRenderMode::Mesh) { @@ -239,11 +242,6 @@ void ParticleRenderer::Render(float deltaTime) } } - //pEmitterComponent->RePaddingShapeBuffer(); - //const bgfx::Memory* pParticleVertexBuffer = bgfx::makeRef(pEmitterComponent->GetEmitterShapeVertexBuffer().data(), static_cast(pEmitterComponent->GetEmitterShapeVertexBuffer().size())); - //const bgfx::Memory* pParticleIndexBuffer = bgfx::makeRef(pEmitterComponent->GetEmitterShapeIndexBuffer().data(), static_cast(pEmitterComponent->GetEmitterShapeIndexBuffer().size())); - //bgfx::update(bgfx::DynamicVertexBufferHandle{ pEmitterComponent->GetEmitterShapeVertexBufferHandle()}, 0, pParticleVertexBuffer); - //bgfx::update(bgfx::DynamicIndexBufferHandle{pEmitterComponent->GetEmitterShapeIndexBufferHandle()}, 0, pParticleIndexBuffer); constexpr StringCrc emitShapeRangeCrc(shapeRange); bgfx::setUniform(GetRenderContext()->GetUniform(emitShapeRangeCrc), &pEmitterComponent->GetEmitterShapeRange(), 1); bgfx::setTransform(m_pCurrentSceneWorld->GetTransformComponent(entity)->GetWorldMatrix().begin()); From a334501ace2bfc611185dbeb324d6b2f80c63188 Mon Sep 17 00:00:00 2001 From: OVOAOVO <2410651402@qq.com> Date: Tue, 12 Mar 2024 15:58:25 +0800 Subject: [PATCH 04/14] fix Ribbon index --- .../ECWorld/ParticleEmitterComponent.cpp | 20 ++++++++++++------- .../Runtime/ParticleSystem/ParticlePool.h | 4 ++-- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/Engine/Source/Runtime/ECWorld/ParticleEmitterComponent.cpp b/Engine/Source/Runtime/ECWorld/ParticleEmitterComponent.cpp index ce3aeda0..104d1fb7 100644 --- a/Engine/Source/Runtime/ECWorld/ParticleEmitterComponent.cpp +++ b/Engine/Source/Runtime/ECWorld/ParticleEmitterComponent.cpp @@ -150,8 +150,8 @@ void ParticleEmitterComponent::PaddingRibbonVertexBuffer() // only a picture now for (int i = 0; i < MAX_VERTEX_COUNT; i += meshVertexCount) { - vertexDataBuffer[i] = { cd::Vec3f(0.0f,-1.0f,0.0f),cd::Vec4f(1.0f,1.0f,1.0f,1.0f),cd::Vec2f(0.5f,0.5f)}; - vertexDataBuffer[i+1] = { cd::Vec3f(0.0f,1.0f,0.0f),cd::Vec4f(1.0f,1.0f,1.0f,1.0f),cd::Vec2f(0.5f,0.5f)}; + vertexDataBuffer[i] = { cd::Vec3f(0.0f,1.0f,0.0f),cd::Vec4f(1.0f,1.0f,1.0f,1.0f),cd::Vec2f(0.5f,0.5f)}; + vertexDataBuffer[i+1] = { cd::Vec3f(0.0f,-1.0f,0.0f),cd::Vec4f(1.0f,1.0f,1.0f,1.0f),cd::Vec2f(0.5f,0.5f)}; } for (int i = 0; i < MAX_VERTEX_COUNT; ++i) @@ -181,8 +181,8 @@ void ParticleEmitterComponent::PaddingRibbonIndexBuffer() constexpr int meshVertexCount = Particle::GetMeshVertexCount();; const bool useU16Index = meshVertexCount <= static_cast(std::numeric_limits::max()) + 1U; const uint32_t indexTypeSize = useU16Index ? sizeof(uint16_t) : sizeof(uint32_t); - const int MAX_VERTEX_COUNT = m_particlePool.GetParticleMaxCount() * meshVertexCount; - int indexCountForOneRibbon = 3; + const int MAX_VERTEX_COUNT = (m_particlePool.GetParticleMaxCount() - 1)* meshVertexCount; + int indexCountForOneRibbon = 6; const uint32_t indicesCount = MAX_VERTEX_COUNT / meshVertexCount * indexCountForOneRibbon; m_ribbonParticleIndexBuffer.resize(indicesCount * indexTypeSize); /// @@ -197,11 +197,14 @@ void ParticleEmitterComponent::PaddingRibbonIndexBuffer() for (uint16_t i = 0; i < MAX_VERTEX_COUNT; i += meshVertexCount) { uint16_t vertexIndex = static_cast(i); - indexes.push_back(vertexIndex); indexes.push_back(vertexIndex+1); indexes.push_back(vertexIndex+2); + indexes.push_back(vertexIndex + 2); + indexes.push_back(vertexIndex + 1); + indexes.push_back(vertexIndex + 3); + } for (const auto& index : indexes) @@ -265,8 +268,8 @@ void ParticleEmitterComponent::RePaddingRibbonIndexBuffer() constexpr int meshVertexCount = Particle::GetMeshVertexCount();; const bool useU16Index = meshVertexCount <= static_cast(std::numeric_limits::max()) + 1U; const uint32_t indexTypeSize = useU16Index ? sizeof(uint16_t) : sizeof(uint32_t); - const int MAX_VERTEX_COUNT = m_particlePool.GetParticleMaxCount() * meshVertexCount; - int indexCountForOneRibbon = 3; + const int MAX_VERTEX_COUNT = (m_particlePool.GetParticleMaxCount()-1 )* meshVertexCount; + int indexCountForOneRibbon = 6; const uint32_t indicesCount = MAX_VERTEX_COUNT / meshVertexCount * indexCountForOneRibbon; m_ribbonParticleIndexBuffer.resize(indicesCount * indexTypeSize); /// @@ -283,6 +286,9 @@ void ParticleEmitterComponent::RePaddingRibbonIndexBuffer() indexes.push_back(vertexIndex); indexes.push_back(vertexIndex + 1); indexes.push_back(vertexIndex + 2); + indexes.push_back(vertexIndex + 2); + indexes.push_back(vertexIndex + 1); + indexes.push_back(vertexIndex + 3); } for (const auto& index : indexes) diff --git a/Engine/Source/Runtime/ParticleSystem/ParticlePool.h b/Engine/Source/Runtime/ParticleSystem/ParticlePool.h index 41ad15d5..8079a220 100644 --- a/Engine/Source/Runtime/ParticleSystem/ParticlePool.h +++ b/Engine/Source/Runtime/ParticleSystem/ParticlePool.h @@ -23,7 +23,7 @@ class ParticlePool final int AllocateParticleIndex(); Particle& GetParticle(int index) { return m_particles[index]; } - int GetParticleCount() { return m_currentActiveCount; } + int GetParticleActiveCount() { return m_currentActiveCount; } int& GetParticleMaxCount() { return m_maxParticleCount; } void SetParticleMaxCount(int count) { m_maxParticleCount = count; } @@ -31,7 +31,7 @@ class ParticlePool final void AllParticlesReset(); private: - int m_maxParticleCount = 1; + int m_maxParticleCount = 75; int m_currentActiveCount = 0; int m_currentParticleCount = 0; std::vector m_particles; From 118681f27d93c841ff3aa96b0ed74491f600ab2d Mon Sep 17 00:00:00 2001 From: OVOAOVO <2410651402@qq.com> Date: Tue, 19 Mar 2024 18:01:37 +0800 Subject: [PATCH 05/14] cs Ribbon(no texture) --- .../UniformDefines/U_Particle.sh | 1 + .../shaders/cs_particleRibbon.sc | 32 ++++ .../shaders/fs_particleRibbon.sc | 13 ++ .../{fs_particle.sc => fs_particleSprite.sc} | 0 .../shaders/vs_particleRibbon.sc | 13 ++ .../{vs_particle.sc => vs_particleSprite.sc} | 0 Engine/Source/Editor/EditorApp.cpp | 2 +- Engine/Source/Editor/UILayers/Inspector.cpp | 4 +- .../ECWorld/ParticleEmitterComponent.cpp | 156 +++++------------ .../ECWorld/ParticleEmitterComponent.h | 17 +- .../Runtime/Rendering/ParticleRenderer.cpp | 162 ++++++++++++------ .../Runtime/Rendering/ParticleRenderer.h | 8 +- 12 files changed, 231 insertions(+), 177 deletions(-) create mode 100644 Engine/BuiltInShaders/UniformDefines/U_Particle.sh create mode 100644 Engine/BuiltInShaders/shaders/cs_particleRibbon.sc create mode 100644 Engine/BuiltInShaders/shaders/fs_particleRibbon.sc rename Engine/BuiltInShaders/shaders/{fs_particle.sc => fs_particleSprite.sc} (100%) create mode 100644 Engine/BuiltInShaders/shaders/vs_particleRibbon.sc rename Engine/BuiltInShaders/shaders/{vs_particle.sc => vs_particleSprite.sc} (100%) diff --git a/Engine/BuiltInShaders/UniformDefines/U_Particle.sh b/Engine/BuiltInShaders/UniformDefines/U_Particle.sh new file mode 100644 index 00000000..812b8b5a --- /dev/null +++ b/Engine/BuiltInShaders/UniformDefines/U_Particle.sh @@ -0,0 +1 @@ +#define PT_RIBBON_VERTEX_STAGE 1 \ No newline at end of file diff --git a/Engine/BuiltInShaders/shaders/cs_particleRibbon.sc b/Engine/BuiltInShaders/shaders/cs_particleRibbon.sc new file mode 100644 index 00000000..13c40443 --- /dev/null +++ b/Engine/BuiltInShaders/shaders/cs_particleRibbon.sc @@ -0,0 +1,32 @@ +#include "../common/bgfx_compute.sh" +#include "../UniformDefines/U_Particle.sh" + +BUFFER_RW(AllRibbonParticleVertex, vec4, PT_RIBBON_VERTEX_STAGE); + +uniform vec4 u_ribbonCount; +// x : allRibbonVertexCount +// y : particleActiveCount + +uniform vec4 u_ribbonMaxPos[75]; +//here have max particle count (max limit of inspector) + +NUM_THREADS(1u, 1u, 1u) +void main() +{ + for(uint i = 0,index= 0; i RegisterShaderProgram(WorldProgramCrc, { "vs_PBR", "fs_PBR" }); m_pRenderContext->RegisterShaderProgram(AnimationProgramCrc, { "vs_animation", "fs_animation" }); m_pRenderContext->RegisterShaderProgram(TerrainProgramCrc, { "vs_terrain", "fs_terrain" }); - m_pRenderContext->RegisterShaderProgram(ParticleProgramCrc, { "vs_particle","fs_particle" }); + m_pRenderContext->RegisterShaderProgram(ParticleProgramCrc, { "vs_particleSprite","fs_particleSprite" }); m_pRenderContext->RegisterShaderProgram(CelluloidProgramCrc, { "vs_celluloid", "fs_celluloid" }); m_pSceneWorld = std::make_unique(); diff --git a/Engine/Source/Editor/UILayers/Inspector.cpp b/Engine/Source/Editor/UILayers/Inspector.cpp index fc538210..d2d4dc43 100644 --- a/Engine/Source/Editor/UILayers/Inspector.cpp +++ b/Engine/Source/Editor/UILayers/Inspector.cpp @@ -662,7 +662,7 @@ void UpdateComponentWidget(engine::SceneWorld* if (isOpen) { - ImGuiUtils::ImGuiEnumProperty("Render Mode", pParticleEmitterComponent->GetRenderMode()); + ImGuiUtils::ImGuiEnumProperty("Render Mode(Work Type Sprite)", pParticleEmitterComponent->GetRenderMode()); ImGuiUtils::ImGuiEnumProperty("Particle Type", pParticleEmitterComponent->GetEmitterParticleType()); //ImGuiUtils::ImGuiEnumProperty("Emitter Shape", pParticleEmitterComponent->GetEmitterShape()); ImGuiUtils::ImGuiVectorProperty("Emitter Range", pParticleEmitterComponent->GetEmitterShapeRange()); @@ -674,7 +674,7 @@ void UpdateComponentWidget(engine::SceneWorld* ImGuiUtils::ImGuiVectorProperty("Acceleration", pParticleEmitterComponent->GetEmitterAcceleration()); ImGuiUtils::ColorPickerProperty("Color", pParticleEmitterComponent->GetEmitterColor()); ImGuiUtils::ImGuiFloatProperty("LifeTime", pParticleEmitterComponent->GetLifeTime(),cd::Unit::None, 0, 6); - if (ImGuiUtils::ImGuiBoolProperty("Instance State", pParticleEmitterComponent->GetInstanceState())) + if (ImGuiUtils::ImGuiBoolProperty("Instance State(Work Type Sprite)", pParticleEmitterComponent->GetInstanceState())) { pParticleEmitterComponent->ActivateShaderFeature(engine::ShaderFeature::PARTICLE_INSTANCE); } diff --git a/Engine/Source/Runtime/ECWorld/ParticleEmitterComponent.cpp b/Engine/Source/Runtime/ECWorld/ParticleEmitterComponent.cpp index 104d1fb7..e94f6a79 100644 --- a/Engine/Source/Runtime/ECWorld/ParticleEmitterComponent.cpp +++ b/Engine/Source/Runtime/ECWorld/ParticleEmitterComponent.cpp @@ -10,10 +10,12 @@ namespace engine void ParticleEmitterComponent::Build() { + //Sprite BuildParticleShape(); bgfx::VertexLayout vertexLayout; VertexLayoutUtility::CreateVertexLayout(vertexLayout, m_pRequiredVertexFormat->GetVertexAttributeLayouts()); + //VertexBuffer IndexBuffer if (m_pMeshData == nullptr) { PaddingSpriteVertexBuffer(); @@ -24,9 +26,6 @@ void ParticleEmitterComponent::Build() m_spriteParticleVertexBufferHandle = bgfx::createVertexBuffer(bgfx::makeRef(m_spriteParticleVertexBuffer.data(), static_cast(m_spriteParticleVertexBuffer.size())), vertexLayout).idx; m_spriteParticleIndexBufferHandle = bgfx::createIndexBuffer(bgfx::makeRef(m_spriteParticleIndexBuffer.data(), static_cast(m_spriteParticleIndexBuffer.size())), 0U).idx; - - m_ribbonParticleVertexBufferHandle = bgfx::createDynamicVertexBuffer(bgfx::makeRef(m_ribbonParticleVertexBuffer.data(), static_cast(m_ribbonParticleVertexBuffer.size())), vertexLayout).idx; - m_ribbonParticleIndexBufferHandle = bgfx::createDynamicIndexBuffer(bgfx::makeRef(m_ribbonParticleIndexBuffer.data(), static_cast(m_ribbonParticleIndexBuffer.size())), 0U).idx; } else { @@ -130,19 +129,36 @@ void ParticleEmitterComponent::PaddingSpriteIndexBuffer() void ParticleEmitterComponent::PaddingRibbonVertexBuffer() { - const bool containsPosition = m_pRequiredVertexFormat->Contains(cd::VertexAttributeType::Position); - const bool containsColor = m_pRequiredVertexFormat->Contains(cd::VertexAttributeType::Color); - const bool containsUV = m_pRequiredVertexFormat->Contains(cd::VertexAttributeType::UV); //vertexbuffer constexpr int meshVertexCount = Particle::GetMeshVertexCount(); const int MAX_VERTEX_COUNT = m_particlePool.GetParticleMaxCount() * meshVertexCount; size_t vertexCount = MAX_VERTEX_COUNT; - const uint32_t vertexFormatStride = m_pRequiredVertexFormat->GetStride(); - - m_ribbonParticleVertexBuffer.resize(vertexCount * vertexFormatStride); - - uint32_t currentDataSize = 0U; - auto currentDataPtr = m_ribbonParticleVertexBuffer.data(); + uint32_t csVertexCount = MAX_VERTEX_COUNT; + //prePos Vertex format/layout + cd::VertexFormat m_pRibbonPrePosVertexFormat; + m_pRibbonPrePosVertexFormat.AddVertexAttributeLayout(cd::VertexAttributeType::Position, cd::GetAttributeValueType(), cd::Point::Size); + m_pRibbonPrePosVertexFormat.AddVertexAttributeLayout(cd::VertexAttributeType::BoneWeight, cd::AttributeValueType::Float, 1U); + bgfx::VertexLayout prePosLayout; + VertexLayoutUtility::CreateVertexLayout(prePosLayout, m_pRibbonPrePosVertexFormat.GetVertexAttributeLayouts()); + //remain Vertex format/layout + cd::VertexFormat m_pRibbonRemainVertexFormat; + m_pRibbonRemainVertexFormat.AddVertexAttributeLayout(cd::VertexAttributeType::Color, cd::GetAttributeValueType(), cd::Color::Size); + m_pRibbonRemainVertexFormat.AddVertexAttributeLayout(cd::VertexAttributeType::UV, cd::GetAttributeValueType(), cd::UV::Size); + bgfx::VertexLayout color_UV_Layout; + VertexLayoutUtility::CreateVertexLayout(color_UV_Layout, m_pRibbonRemainVertexFormat.GetVertexAttributeLayouts()); + + //const uint32_t posVertexFormatStride = m_pRibbonPrePosVertexFormat.GetStride(); + const uint32_t remainVertexFormatStride = m_pRibbonRemainVertexFormat.GetStride(); + + //m_ribbonParticlePrePosVertexBuffer.resize(vertexCount * posVertexFormatStride); + m_ribbonParticleRemainVertexBuffer.resize(vertexCount * remainVertexFormatStride); + + //uint32_t currentPosDataSize = 0U; + //auto currentPosDataPtr = m_ribbonParticlePrePosVertexBuffer.data(); + uint32_t currentRemainDataSize = 0U; + auto currentRemainDataPtr = m_ribbonParticleRemainVertexBuffer.data(); + //float placeholder = 0.0f; + //uint32_t placeholderSize = sizeof(placeholder); std::vector vertexDataBuffer; vertexDataBuffer.resize(MAX_VERTEX_COUNT); @@ -156,24 +172,20 @@ void ParticleEmitterComponent::PaddingRibbonVertexBuffer() for (int i = 0; i < MAX_VERTEX_COUNT; ++i) { - if (containsPosition) - { - std::memcpy(¤tDataPtr[currentDataSize], &vertexDataBuffer[i].pos, sizeof(cd::Point)); - currentDataSize += sizeof(cd::Point); - } - - if (containsColor) - { - std::memcpy(¤tDataPtr[currentDataSize], &vertexDataBuffer[i].color, sizeof(cd::Color)); - currentDataSize += sizeof(cd::Color); - } - - if (containsUV) - { - std::memcpy(¤tDataPtr[currentDataSize], &vertexDataBuffer[i].uv, sizeof(cd::UV)); - currentDataSize += sizeof(cd::UV); - } + //std::memcpy(¤tPosDataPtr[currentPosDataSize], &vertexDataBuffer[i].pos, sizeof(cd::Point)); + //currentPosDataSize += sizeof(cd::Point); + ////vec3 filled to vec4 + //std::memcpy(¤tPosDataPtr[currentPosDataSize], &placeholder, placeholderSize); + //currentPosDataSize += placeholderSize; + + std::memcpy(¤tRemainDataPtr[currentRemainDataSize], &vertexDataBuffer[i].color, sizeof(cd::Color)); + currentRemainDataSize += sizeof(cd::Color); + std::memcpy(¤tRemainDataPtr[currentRemainDataSize], &vertexDataBuffer[i].uv, sizeof(cd::UV)); + currentRemainDataSize += sizeof(cd::UV); } + m_ribbonParticlePrePosVertexBufferHandle = bgfx::createDynamicVertexBuffer(csVertexCount, prePosLayout, BGFX_BUFFER_COMPUTE_READ_WRITE).idx; + const bgfx::Memory* pRibbonParticleRemainVBRef = bgfx::makeRef(m_ribbonParticleRemainVertexBuffer.data(), static_cast(m_ribbonParticleRemainVertexBuffer.size())); + m_ribbonParticleRemainVertexBufferHandle = bgfx::createVertexBuffer(pRibbonParticleRemainVBRef, color_UV_Layout).idx; } void ParticleEmitterComponent::PaddingRibbonIndexBuffer() @@ -204,91 +216,6 @@ void ParticleEmitterComponent::PaddingRibbonIndexBuffer() indexes.push_back(vertexIndex + 2); indexes.push_back(vertexIndex + 1); indexes.push_back(vertexIndex + 3); - - } - - for (const auto& index : indexes) - { - std::memcpy(¤tDataPtr[currentDataSize], &index, indexTypeSize); - currentDataSize += static_cast(indexTypeSize); - } -} - - -void ParticleEmitterComponent::RePaddingRibbonVertexBuffer() -{ - const bool containsPosition = m_pRequiredVertexFormat->Contains(cd::VertexAttributeType::Position); - const bool containsColor = m_pRequiredVertexFormat->Contains(cd::VertexAttributeType::Color); - const bool containsUV = m_pRequiredVertexFormat->Contains(cd::VertexAttributeType::UV); - //vertexbuffer - constexpr int meshVertexCount = Particle::GetMeshVertexCount(); - const int MAX_VERTEX_COUNT = m_particlePool.GetParticleMaxCount() * meshVertexCount; - size_t vertexCount = MAX_VERTEX_COUNT; - const uint32_t vertexFormatStride = m_pRequiredVertexFormat->GetStride(); - - m_ribbonParticleVertexBuffer.resize(vertexCount * vertexFormatStride); - - uint32_t currentDataSize = 0U; - auto currentDataPtr = m_ribbonParticleVertexBuffer.data(); - - std::vector vertexDataBuffer; - vertexDataBuffer.resize(MAX_VERTEX_COUNT); - // pos color uv - // only a picture now - for (int i = 0,index = 0; i < MAX_VERTEX_COUNT; i += meshVertexCount,index++) - { - vertexDataBuffer[i] = { m_particlePool.GetParticle(index).GetPos()+cd::Vec3f(0.0f,1.0f,0.0f),cd::Vec4f(1.0f,1.0f,1.0f,1.0f),cd::Vec2f(0.5f,0.5f)}; - vertexDataBuffer[i + 1] = { m_particlePool.GetParticle(index).GetPos()+cd::Vec3f(0.0f,-1.0f,0.0f),cd::Vec4f(1.0f,1.0f,1.0f,1.0f),cd::Vec2f(0.5f,0.5f)}; - } - - for (int i = 0; i < MAX_VERTEX_COUNT; ++i) - { - if (containsPosition) - { - std::memcpy(¤tDataPtr[currentDataSize], &vertexDataBuffer[i].pos, sizeof(cd::Point)); - currentDataSize += sizeof(cd::Point); - } - - if (containsColor) - { - std::memcpy(¤tDataPtr[currentDataSize], &vertexDataBuffer[i].color, sizeof(cd::Color)); - currentDataSize += sizeof(cd::Color); - } - - if (containsUV) - { - std::memcpy(¤tDataPtr[currentDataSize], &vertexDataBuffer[i].uv, sizeof(cd::UV)); - currentDataSize += sizeof(cd::UV); - } - } -} - -void ParticleEmitterComponent::RePaddingRibbonIndexBuffer() -{ - constexpr int meshVertexCount = Particle::GetMeshVertexCount();; - const bool useU16Index = meshVertexCount <= static_cast(std::numeric_limits::max()) + 1U; - const uint32_t indexTypeSize = useU16Index ? sizeof(uint16_t) : sizeof(uint32_t); - const int MAX_VERTEX_COUNT = (m_particlePool.GetParticleMaxCount()-1 )* meshVertexCount; - int indexCountForOneRibbon = 6; - const uint32_t indicesCount = MAX_VERTEX_COUNT / meshVertexCount * indexCountForOneRibbon; - m_ribbonParticleIndexBuffer.resize(indicesCount * indexTypeSize); - /// -/* size_t indexTypeSize = sizeof(uint16_t); - m_particleIndexBuffer.resize(m_particleSystem.GetMaxCount() / 4 * 6 * indexTyp - eSize);*/ - uint32_t currentDataSize = 0U; - auto currentDataPtr = m_ribbonParticleIndexBuffer.data(); - - std::vector indexes; - for (uint16_t i = 0; i < MAX_VERTEX_COUNT; i += meshVertexCount) - { - uint16_t vertexIndex = static_cast(i); - indexes.push_back(vertexIndex); - indexes.push_back(vertexIndex + 1); - indexes.push_back(vertexIndex + 2); - indexes.push_back(vertexIndex + 2); - indexes.push_back(vertexIndex + 1); - indexes.push_back(vertexIndex + 3); } for (const auto& index : indexes) @@ -296,6 +223,7 @@ void ParticleEmitterComponent::RePaddingRibbonIndexBuffer() std::memcpy(¤tDataPtr[currentDataSize], &index, indexTypeSize); currentDataSize += static_cast(indexTypeSize); } + m_ribbonParticleIndexBufferHandle = bgfx::createIndexBuffer(bgfx::makeRef(m_ribbonParticleIndexBuffer.data(), static_cast(m_ribbonParticleIndexBuffer.size())), 0U).idx; } void ParticleEmitterComponent::BuildParticleShape() diff --git a/Engine/Source/Runtime/ECWorld/ParticleEmitterComponent.h b/Engine/Source/Runtime/ECWorld/ParticleEmitterComponent.h index e679555c..bab5c867 100644 --- a/Engine/Source/Runtime/ECWorld/ParticleEmitterComponent.h +++ b/Engine/Source/Runtime/ECWorld/ParticleEmitterComponent.h @@ -93,10 +93,12 @@ class ParticleEmitterComponent final std::vector& GetSpriteVertexBuffer() { return m_spriteParticleVertexBuffer; } std::vector& GetSpriteIndexBuffer() { return m_spriteParticleIndexBuffer; } - uint16_t& GetRibbonParticleVertexBufferHandle() { return m_ribbonParticleVertexBufferHandle; } + uint16_t& GetRibbonParticlePrePosVertexBufferHandle() { return m_ribbonParticlePrePosVertexBufferHandle; } + uint16_t& GetRibbonParticleRemainVertexBufferHandle() { return m_ribbonParticleRemainVertexBufferHandle; } uint16_t& GetRibbonParticleIndexBufferHandle() { return m_ribbonParticleIndexBufferHandle; } - std::vector& GetRibbonVertexBuffer() { return m_ribbonParticleVertexBuffer; } + std::vector& GetRibbonPrePosVertexBuffer() { return m_ribbonParticlePrePosVertexBuffer; } + std::vector& GetRibbonRemainVertexBuffer() { return m_ribbonParticleRemainVertexBuffer; } std::vector& GetRibbonIndexBuffer() { return m_ribbonParticleIndexBuffer; } uint16_t& GetEmitterShapeVertexBufferHandle() { return m_emitterShapeVertexBufferHandle; } @@ -118,8 +120,6 @@ class ParticleEmitterComponent final void PaddingRibbonVertexBuffer(); void PaddingRibbonIndexBuffer(); - void RePaddingRibbonVertexBuffer(); - void RePaddingRibbonIndexBuffer(); void BuildParticleShape(); void RePaddingShapeBuffer(); @@ -137,7 +137,7 @@ class ParticleEmitterComponent final //ParticleSystem m_particleSystem; ParticlePool m_particlePool; - engine::ParticleType m_emitterParticleType; + engine::ParticleType m_emitterParticleType ; //emitter data int m_spawnCount = 75; @@ -179,10 +179,13 @@ class ParticleEmitterComponent final std::vector m_spriteParticleIndexBuffer; uint16_t m_spriteParticleVertexBufferHandle = UINT16_MAX; uint16_t m_spriteParticleIndexBufferHandle = UINT16_MAX; + //Ribbon - std::vector m_ribbonParticleVertexBuffer; + std::vector m_ribbonParticlePrePosVertexBuffer; + std::vector m_ribbonParticleRemainVertexBuffer; std::vector m_ribbonParticleIndexBuffer; - uint16_t m_ribbonParticleVertexBufferHandle = UINT16_MAX; + uint16_t m_ribbonParticlePrePosVertexBufferHandle = UINT16_MAX; + uint16_t m_ribbonParticleRemainVertexBufferHandle = UINT16_MAX; uint16_t m_ribbonParticleIndexBufferHandle = UINT16_MAX; diff --git a/Engine/Source/Runtime/Rendering/ParticleRenderer.cpp b/Engine/Source/Runtime/Rendering/ParticleRenderer.cpp index eca235a6..d92d035e 100644 --- a/Engine/Source/Runtime/Rendering/ParticleRenderer.cpp +++ b/Engine/Source/Runtime/Rendering/ParticleRenderer.cpp @@ -1,10 +1,11 @@ -#include "Log/Log.h" #include "ParticleRenderer.h" + #include "ECWorld/CameraComponent.h" #include "ECWorld/SceneWorld.h" #include "ECWorld/ParticleForceFieldComponent.h" #include "ECWorld/TransformComponent.h" #include "Rendering/RenderContext.h" +#include "../UniformDefines/U_Particle.sh" namespace engine { @@ -14,6 +15,8 @@ constexpr const char* particlePos = "u_particlePos"; constexpr const char* particleScale = "u_particleScale"; constexpr const char* shapeRange = "u_shapeRange"; constexpr const char* particleColor = "u_particleColor"; +constexpr const char* ribbonCount = "u_ribbonCount"; +constexpr const char* ribbonMaxPos = "u_ribbonMaxPos"; uint64_t state_tristrip = BGFX_STATE_WRITE_MASK | BGFX_STATE_MSAA | BGFX_STATE_DEPTH_TEST_LESS | BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_SRC_ALPHA, BGFX_STATE_BLEND_INV_SRC_ALPHA) | BGFX_STATE_PT_TRISTRIP; @@ -21,18 +24,25 @@ BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_SRC_ALPHA, BGFX_STATE_BLEND_INV_SRC_ALPHA uint64_t state_lines = BGFX_STATE_WRITE_MASK | BGFX_STATE_MSAA | BGFX_STATE_DEPTH_TEST_LESS | BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_SRC_ALPHA, BGFX_STATE_BLEND_INV_SRC_ALPHA) | BGFX_STATE_PT_LINES; -constexpr const char* ParticleProgram = "ParticleProgram"; +constexpr const char* SpriteParticleProgram = "SpriteParticleProgram"; +constexpr const char* RibbonParticleCsProgram = "RibbonParticleCsProgram"; +constexpr const char* RibbonParticleProgram = "RibbonParticleProgram"; constexpr const char* ParticleEmitterShapeProgram = "ParticleEmitterShapeProgram"; constexpr const char* WO_BillboardParticleProgram = "WO_BillboardParticleProgram"; -constexpr StringCrc ParticleProgramCrc = StringCrc{ "ParticleProgram" }; +constexpr StringCrc SpriteParticleProgramCrc = StringCrc{ "SpriteParticleProgram" }; +constexpr StringCrc RibbonParticleProgramCsCrc = StringCrc{ "RibbonParticleCsProgram" }; +constexpr StringCrc RibbonParticleProgramCrc = StringCrc{ "RibbonParticleProgram" }; + constexpr StringCrc ParticleEmitterShapeProgramCrc = StringCrc{ "ParticleEmitterShapeProgram" }; constexpr StringCrc WO_BillboardParticleProgramCrc = StringCrc{ "WO_BillboardParticleProgram" }; } void ParticleRenderer::Init() { - GetRenderContext()->RegisterShaderProgram(ParticleProgramCrc, { "vs_particle", "fs_particle" }); + GetRenderContext()->RegisterShaderProgram(SpriteParticleProgramCrc, { "vs_particleSprite", "fs_particleSprite" }); + GetRenderContext()->RegisterShaderProgram(RibbonParticleProgramCsCrc, { "cs_particleRibbon" }); + GetRenderContext()->RegisterShaderProgram(RibbonParticleProgramCrc, { "vs_particleRibbon", "fs_particleRibbon" }); GetRenderContext()->RegisterShaderProgram(ParticleEmitterShapeProgramCrc, {"vs_particleEmitterShape", "fs_particleEmitterShape"}); GetRenderContext()->RegisterShaderProgram(WO_BillboardParticleProgramCrc, { "vs_wo_billboardparticle","fs_wo_billboardparticle" }); @@ -42,14 +52,21 @@ void ParticleRenderer::Init() void ParticleRenderer::Warmup() { constexpr const char* particleTexture = "Textures/textures/Particle.png"; - m_particleTextureHandle = GetRenderContext()->CreateTexture(particleTexture); + m_particleSpriteTextureHandle = GetRenderContext()->CreateTexture(particleTexture); + m_particleRibbonTextureHandle = GetRenderContext()->CreateTexture(particleTexture); + GetRenderContext()->CreateUniform("s_texColor", bgfx::UniformType::Sampler); + GetRenderContext()->CreateUniform("r_texColor", bgfx::UniformType::Sampler); GetRenderContext()->CreateUniform(particlePos, bgfx::UniformType::Vec4, 1); GetRenderContext()->CreateUniform(particleScale, bgfx::UniformType::Vec4, 1); GetRenderContext()->CreateUniform(shapeRange, bgfx::UniformType::Vec4, 1); GetRenderContext()->CreateUniform(particleColor, bgfx::UniformType::Vec4, 1); + GetRenderContext()->CreateUniform(ribbonCount, bgfx::UniformType::Vec4, 1); + GetRenderContext()->CreateUniform(ribbonMaxPos, bgfx::UniformType::Vec4, 75); - GetRenderContext()->UploadShaderProgram(ParticleProgram); + GetRenderContext()->UploadShaderProgram(SpriteParticleProgram); + GetRenderContext()->UploadShaderProgram(RibbonParticleCsProgram); + GetRenderContext()->UploadShaderProgram(RibbonParticleProgram); GetRenderContext()->UploadShaderProgram(ParticleEmitterShapeProgram); GetRenderContext()->UploadShaderProgram(WO_BillboardParticleProgram); } @@ -97,22 +114,8 @@ void ParticleRenderer::Render(float deltaTime) if (particleIndex != -1) { Particle& particle = pEmitterComponent->GetParticlePool().GetParticle(particleIndex); - 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()); - } + SetRandomPosState(particle, particleTransform.GetTranslation(), randomPos, pEmitterComponent->GetRandomPosState()); + SetRandomVelocityState(particle, pEmitterComponent->GetEmitterVelocity(), randomVelocity, pEmitterComponent->GetRandomVelocityState()); particle.SetRotationForceField(m_forcefieldRotationFoce); particle.SetRotationForceFieldRange(m_forcefieldRange); particle.SetAcceleration(pEmitterComponent->GetEmitterAcceleration()); @@ -158,7 +161,7 @@ void ParticleRenderer::Render(float deltaTime) bgfx::setUniform(GetRenderContext()->GetUniform(ParticleScaleCrc), &particleTransform.GetScale(), 1); constexpr StringCrc ParticleSampler("s_texColor"); - bgfx::setTexture(0, GetRenderContext()->GetUniform(ParticleSampler), m_particleTextureHandle); + bgfx::setTexture(0, GetRenderContext()->GetUniform(ParticleSampler), m_particleSpriteTextureHandle); if (pEmitterComponent->GetEmitterParticleType() == engine::ParticleType::Sprite) { bgfx::setVertexBuffer(0, bgfx::VertexBufferHandle{ pEmitterComponent->GetSpriteParticleVertexBufferHandle() }); @@ -166,21 +169,15 @@ void ParticleRenderer::Render(float deltaTime) } else if (pEmitterComponent->GetEmitterParticleType() == engine::ParticleType::Ribbon) { - bgfx::setVertexBuffer(0, bgfx::VertexBufferHandle{ pEmitterComponent->GetRibbonParticleVertexBufferHandle() }); + bgfx::setVertexBuffer(0, bgfx::DynamicVertexBufferHandle{ pEmitterComponent->GetRibbonParticlePrePosVertexBufferHandle() }); + bgfx::setVertexBuffer(1, bgfx::VertexBufferHandle{ pEmitterComponent->GetRibbonParticleRemainVertexBufferHandle() }); bgfx::setIndexBuffer(bgfx::IndexBufferHandle{ pEmitterComponent->GetRibbonParticleIndexBufferHandle() }); } bgfx::setState(state_tristrip); bgfx::setInstanceDataBuffer(&idb); - if (pEmitterComponent->GetRenderMode() == engine::ParticleRenderMode::Mesh) - { - GetRenderContext()->Submit(GetViewID(), ParticleProgram); - } - else if (pEmitterComponent->GetRenderMode() == engine::ParticleRenderMode::Billboard) - { - GetRenderContext()->Submit(GetViewID(), WO_BillboardParticleProgram); - } + SetRenderMode(pEmitterComponent->GetRenderMode(), pEmitterComponent->GetEmitterParticleType()); } else { @@ -210,42 +207,54 @@ void ParticleRenderer::Render(float deltaTime) pEmitterComponent->GetParticlePool().GetParticle(ii).GetPos().x(), pEmitterComponent->GetParticlePool().GetParticle(ii).GetPos().y(), pEmitterComponent->GetParticlePool().GetParticle(ii).GetPos().z()); } bgfx::setTransform(mtx); - constexpr StringCrc ParticleSampler("s_texColor"); - bgfx::setTexture(0, GetRenderContext()->GetUniform(ParticleSampler), m_particleTextureHandle); bgfx::setState(state_tristrip); if (pEmitterComponent->GetEmitterParticleType() == engine::ParticleType::Sprite) { + constexpr StringCrc spriteParticleSampler("s_texColor"); + bgfx::setTexture(0, GetRenderContext()->GetUniform(spriteParticleSampler), m_particleSpriteTextureHandle); bgfx::setVertexBuffer(0, bgfx::VertexBufferHandle{ pEmitterComponent->GetSpriteParticleVertexBufferHandle() }); bgfx::setIndexBuffer(bgfx::IndexBufferHandle{ pEmitterComponent->GetSpriteParticleIndexBufferHandle() }); } else if (pEmitterComponent->GetEmitterParticleType() == engine::ParticleType::Ribbon) { - //bgfx::setState(state_lines); - pEmitterComponent->RePaddingRibbonVertexBuffer(); - pEmitterComponent->RePaddingRibbonIndexBuffer(); - const bgfx::Memory* pParticleVertexBuffer = bgfx::makeRef(pEmitterComponent->GetRibbonVertexBuffer().data(), static_cast(pEmitterComponent->GetRibbonVertexBuffer().size())); - const bgfx::Memory* pParticleIndexBuffer = bgfx::makeRef(pEmitterComponent->GetRibbonIndexBuffer().data(), static_cast(pEmitterComponent->GetRibbonIndexBuffer().size())); - bgfx::update(bgfx::DynamicVertexBufferHandle{ pEmitterComponent->GetRibbonParticleVertexBufferHandle()}, 0, pParticleVertexBuffer); - bgfx::update(bgfx::DynamicIndexBufferHandle{pEmitterComponent->GetRibbonParticleIndexBufferHandle()}, 0, pParticleIndexBuffer); - bgfx::setVertexBuffer(0, bgfx::DynamicVertexBufferHandle{ pEmitterComponent->GetRibbonParticleVertexBufferHandle() }); - bgfx::setIndexBuffer(bgfx::DynamicIndexBufferHandle{ pEmitterComponent->GetRibbonParticleIndexBufferHandle() }); - } + constexpr StringCrc ribbonParticleSampler("r_texColor"); + bgfx::setTexture(1, GetRenderContext()->GetUniform(ribbonParticleSampler), m_particleRibbonTextureHandle); + bgfx::setBuffer(PT_RIBBON_VERTEX_STAGE, bgfx::DynamicVertexBufferHandle{ pEmitterComponent->GetRibbonParticlePrePosVertexBufferHandle() }, bgfx::Access::ReadWrite); - if (pEmitterComponent->GetRenderMode() == engine::ParticleRenderMode::Mesh) - { - GetRenderContext()->Submit(GetViewID(), ParticleProgram); - } - else if (pEmitterComponent->GetRenderMode() == engine::ParticleRenderMode::Billboard) - { - GetRenderContext()->Submit(GetViewID(), WO_BillboardParticleProgram); + //ribbonCount Uinform + constexpr StringCrc ribbontCounts(ribbonCount); + cd::Vec4f allRibbonCount{ static_cast(pEmitterComponent->GetParticlePool().GetParticleMaxCount()* Particle::GetMeshVertexCount()), + pEmitterComponent->GetParticlePool().GetParticleMaxCount(), + 0, + 0}; + GetRenderContext()->FillUniform(ribbontCounts, &allRibbonCount, 1); + + //ribbonListUniform + cd::Vec4f ribbonPosList[75]{}; + for (int i = 0; i < 75; i++) + { + ribbonPosList[i] =cd::Vec4f(pEmitterComponent->GetParticlePool().GetParticle(i).GetPos().x(), + pEmitterComponent->GetParticlePool().GetParticle(i).GetPos().y(), + pEmitterComponent->GetParticlePool().GetParticle(i).GetPos().z() + , 0.0f); + } + constexpr StringCrc maxPosList(ribbonMaxPos); + GetRenderContext()->FillUniform(maxPosList, &ribbonPosList, 75); + GetRenderContext()->Dispatch(GetViewID(), RibbonParticleCsProgram, 1U, 1U, 1U); + //pEmitterComponent->UpdateRibbonPosBuffer(); + + bgfx::setVertexBuffer(0, bgfx::DynamicVertexBufferHandle{ pEmitterComponent->GetRibbonParticlePrePosVertexBufferHandle() }); + bgfx::setVertexBuffer(1, bgfx::VertexBufferHandle{ pEmitterComponent->GetRibbonParticleRemainVertexBufferHandle() }); + bgfx::setIndexBuffer(bgfx::IndexBufferHandle{ pEmitterComponent->GetRibbonParticleIndexBufferHandle() }); } + SetRenderMode(pEmitterComponent->GetRenderMode(), pEmitterComponent->GetEmitterParticleType()); } } constexpr StringCrc emitShapeRangeCrc(shapeRange); bgfx::setUniform(GetRenderContext()->GetUniform(emitShapeRangeCrc), &pEmitterComponent->GetEmitterShapeRange(), 1); bgfx::setTransform(m_pCurrentSceneWorld->GetTransformComponent(entity)->GetWorldMatrix().begin()); - bgfx::setVertexBuffer(1, bgfx::VertexBufferHandle{ pEmitterComponent->GetEmitterShapeVertexBufferHandle() }); + bgfx::setVertexBuffer(0, bgfx::VertexBufferHandle{ pEmitterComponent->GetEmitterShapeVertexBufferHandle() }); bgfx::setIndexBuffer(bgfx::IndexBufferHandle{ pEmitterComponent->GetEmitterShapeIndexBufferHandle() }); bgfx::setState(state_lines); @@ -253,4 +262,53 @@ void ParticleRenderer::Render(float deltaTime) } } +void ParticleRenderer::SetRenderMode(engine::ParticleRenderMode& rendermode, engine::ParticleType type) +{ + if (rendermode == engine::ParticleRenderMode::Mesh) + { + if (type == engine::ParticleType::Sprite) + { + GetRenderContext()->Submit(GetViewID(), SpriteParticleProgram); + } + else if (type == engine::ParticleType::Ribbon) + { + GetRenderContext()->Submit(GetViewID(), RibbonParticleProgram); + } + } + else if (rendermode == engine::ParticleRenderMode::Billboard) + { + if (type == engine::ParticleType::Sprite) + { + GetRenderContext()->Submit(GetViewID(), WO_BillboardParticleProgram); + } + else if (type == engine::ParticleType::Ribbon) + { + //GetRenderContext()->Submit(GetViewID(), WO_BillboardParticleProgram); + } + } +} + +void ParticleRenderer::SetRandomPosState(engine::Particle& particle, cd::Vec3f value, cd::Vec3f randomvalue, bool state) +{ + if (state) + { + particle.SetPos(value + randomvalue); + } + else + { + particle.SetPos(value); + } +} +void ParticleRenderer::SetRandomVelocityState(engine::Particle& particle, cd::Vec3f value, cd::Vec3f randomvalue, bool state) +{ + if (state) + { + particle.SetSpeed(value + randomvalue); + } + else + { + particle.SetSpeed(value); + } +} + } \ No newline at end of file diff --git a/Engine/Source/Runtime/Rendering/ParticleRenderer.h b/Engine/Source/Runtime/Rendering/ParticleRenderer.h index 6766fb78..93a226c3 100644 --- a/Engine/Source/Runtime/Rendering/ParticleRenderer.h +++ b/Engine/Source/Runtime/Rendering/ParticleRenderer.h @@ -7,6 +7,7 @@ #include "ECWorld/TransformComponent.h" #include "RenderContext.h" #include "Rendering/Utility/VertexLayoutUtility.h" + namespace engine { @@ -26,9 +27,14 @@ class ParticleRenderer final : public Renderer float getRandomValue(float min, float max) { return min + static_cast(rand()) / (RAND_MAX / (max - min)); } void SetForceFieldRotationForce(ParticleForceFieldComponent* forcefield) { m_forcefieldRotationFoce = forcefield->GetRotationForce(); } void SetForceFieldRange(ParticleForceFieldComponent* forcefield ,cd::Vec3f scale) { m_forcefieldRange = forcefield->GetForceFieldRange()*scale; } + + void SetRenderMode(engine::ParticleRenderMode& rendermode, engine::ParticleType type); + void SetRandomPosState(engine::Particle& particle, cd::Vec3f value, cd::Vec3f randomvalue, bool state); + void SetRandomVelocityState(engine::Particle& particle, cd::Vec3f value, cd::Vec3f randomvalue, bool state); private: SceneWorld* m_pCurrentSceneWorld = nullptr; - bgfx::TextureHandle m_particleTextureHandle; + bgfx::TextureHandle m_particleSpriteTextureHandle; + bgfx::TextureHandle m_particleRibbonTextureHandle; ParticleType m_currentType = ParticleType::Sprite; bool m_forcefieldRotationFoce = false; From b4eb790fcf41e08a104d4a3d2e28471b57688dec Mon Sep 17 00:00:00 2001 From: OVOAOVO <2410651402@qq.com> Date: Tue, 19 Mar 2024 20:02:54 +0800 Subject: [PATCH 06/14] cs Ribbon (fixed texture color) --- .../Source/Runtime/Rendering/ParticleRenderer.cpp | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/Engine/Source/Runtime/Rendering/ParticleRenderer.cpp b/Engine/Source/Runtime/Rendering/ParticleRenderer.cpp index d92d035e..7bff31d5 100644 --- a/Engine/Source/Runtime/Rendering/ParticleRenderer.cpp +++ b/Engine/Source/Runtime/Rendering/ParticleRenderer.cpp @@ -52,8 +52,9 @@ void ParticleRenderer::Init() void ParticleRenderer::Warmup() { constexpr const char* particleTexture = "Textures/textures/Particle.png"; + constexpr const char* ribbonTexture = "Textures/textures/Particle.png"; m_particleSpriteTextureHandle = GetRenderContext()->CreateTexture(particleTexture); - m_particleRibbonTextureHandle = GetRenderContext()->CreateTexture(particleTexture); + m_particleRibbonTextureHandle = GetRenderContext()->CreateTexture(ribbonTexture); GetRenderContext()->CreateUniform("s_texColor", bgfx::UniformType::Sampler); GetRenderContext()->CreateUniform("r_texColor", bgfx::UniformType::Sampler); @@ -160,15 +161,17 @@ void ParticleRenderer::Render(float deltaTime) constexpr StringCrc ParticleScaleCrc(particleScale); bgfx::setUniform(GetRenderContext()->GetUniform(ParticleScaleCrc), &particleTransform.GetScale(), 1); - constexpr StringCrc ParticleSampler("s_texColor"); - bgfx::setTexture(0, GetRenderContext()->GetUniform(ParticleSampler), m_particleSpriteTextureHandle); if (pEmitterComponent->GetEmitterParticleType() == engine::ParticleType::Sprite) { + constexpr StringCrc ParticleSampler("s_texColor"); + bgfx::setTexture(0, GetRenderContext()->GetUniform(ParticleSampler), m_particleSpriteTextureHandle); bgfx::setVertexBuffer(0, bgfx::VertexBufferHandle{ pEmitterComponent->GetSpriteParticleVertexBufferHandle() }); bgfx::setIndexBuffer(bgfx::IndexBufferHandle{ pEmitterComponent->GetSpriteParticleIndexBufferHandle() }); } else if (pEmitterComponent->GetEmitterParticleType() == engine::ParticleType::Ribbon) { + constexpr StringCrc ribbonParticleSampler("r_texColor"); + bgfx::setTexture(1, GetRenderContext()->GetUniform(ribbonParticleSampler), m_particleRibbonTextureHandle); bgfx::setVertexBuffer(0, bgfx::DynamicVertexBufferHandle{ pEmitterComponent->GetRibbonParticlePrePosVertexBufferHandle() }); bgfx::setVertexBuffer(1, bgfx::VertexBufferHandle{ pEmitterComponent->GetRibbonParticleRemainVertexBufferHandle() }); bgfx::setIndexBuffer(bgfx::IndexBufferHandle{ pEmitterComponent->GetRibbonParticleIndexBufferHandle() }); @@ -217,8 +220,6 @@ void ParticleRenderer::Render(float deltaTime) } else if (pEmitterComponent->GetEmitterParticleType() == engine::ParticleType::Ribbon) { - constexpr StringCrc ribbonParticleSampler("r_texColor"); - bgfx::setTexture(1, GetRenderContext()->GetUniform(ribbonParticleSampler), m_particleRibbonTextureHandle); bgfx::setBuffer(PT_RIBBON_VERTEX_STAGE, bgfx::DynamicVertexBufferHandle{ pEmitterComponent->GetRibbonParticlePrePosVertexBufferHandle() }, bgfx::Access::ReadWrite); //ribbonCount Uinform @@ -242,7 +243,8 @@ void ParticleRenderer::Render(float deltaTime) GetRenderContext()->FillUniform(maxPosList, &ribbonPosList, 75); GetRenderContext()->Dispatch(GetViewID(), RibbonParticleCsProgram, 1U, 1U, 1U); //pEmitterComponent->UpdateRibbonPosBuffer(); - + constexpr StringCrc ribbonParticleSampler("r_texColor"); + bgfx::setTexture(1, GetRenderContext()->GetUniform(ribbonParticleSampler), m_particleRibbonTextureHandle); bgfx::setVertexBuffer(0, bgfx::DynamicVertexBufferHandle{ pEmitterComponent->GetRibbonParticlePrePosVertexBufferHandle() }); bgfx::setVertexBuffer(1, bgfx::VertexBufferHandle{ pEmitterComponent->GetRibbonParticleRemainVertexBufferHandle() }); bgfx::setIndexBuffer(bgfx::IndexBufferHandle{ pEmitterComponent->GetRibbonParticleIndexBufferHandle() }); From c61f3255172f4ca4cb69b1aec8629a11b918986d Mon Sep 17 00:00:00 2001 From: OVOAOVO <2410651402@qq.com> Date: Wed, 20 Mar 2024 15:49:46 +0800 Subject: [PATCH 07/14] Add Materialcomponent Uber shader --- .../Source/Editor/ECWorld/ECWorldConsumer.cpp | 5 ++- Engine/Source/Editor/UILayers/EntityList.cpp | 8 +++- Engine/Source/Editor/UILayers/Inspector.cpp | 5 ++- .../ECWorld/ParticleEmitterComponent.cpp | 42 ------------------- .../ECWorld/ParticleEmitterComponent.h | 18 -------- .../Runtime/Rendering/ParticleRenderer.cpp | 17 ++++---- .../Runtime/Rendering/ParticleRenderer.h | 2 +- 7 files changed, 21 insertions(+), 76 deletions(-) diff --git a/Engine/Source/Editor/ECWorld/ECWorldConsumer.cpp b/Engine/Source/Editor/ECWorld/ECWorldConsumer.cpp index c4a94ae4..3081c453 100644 --- a/Engine/Source/Editor/ECWorld/ECWorldConsumer.cpp +++ b/Engine/Source/Editor/ECWorld/ECWorldConsumer.cpp @@ -397,6 +397,7 @@ void ECWorldConsumer::AddParticleEmitter(engine::Entity entity, const cd::Mesh& engine::NameComponent& nameComponent = pWorld->CreateComponent(entity); nameComponent.SetName(emitter.GetName()); auto& particleEmitterComponent = pWorld->CreateComponent(entity); + auto& particleMaterialComponent = pWorld->CreateComponent(entity); // TODO : Some initialization here. auto& transformComponent = pWorld->CreateComponent(entity); cd::Vec3f pos = emitter.GetPosition(); @@ -424,8 +425,8 @@ void ECWorldConsumer::AddParticleEmitter(engine::Entity entity, const cd::Mesh& particleEmitterComponent.SetEmitterVelocity(emitter.GetVelocity()); particleEmitterComponent.SetEmitterAcceleration(emitter.GetAccelerate()); particleEmitterComponent.SetMeshData(&mesh); - particleEmitterComponent.SetMaterialType(pMaterialType); - particleEmitterComponent.ActivateShaderFeature(engine::ShaderFeature::PARTICLE_INSTANCE); + particleMaterialComponent.SetMaterialType(pMaterialType); + particleMaterialComponent.ActivateShaderFeature(engine::ShaderFeature::PARTICLE_INSTANCE); particleEmitterComponent.Build(); } diff --git a/Engine/Source/Editor/UILayers/EntityList.cpp b/Engine/Source/Editor/UILayers/EntityList.cpp index 560e599b..e32a2d2e 100644 --- a/Engine/Source/Editor/UILayers/EntityList.cpp +++ b/Engine/Source/Editor/UILayers/EntityList.cpp @@ -259,13 +259,17 @@ void EntityList::AddEntity(engine::SceneWorld* pSceneWorld) { engine::Entity entity = AddNamedEntity("ParticleEmitter"); auto& particleEmitterComponent = pWorld->CreateComponent(entity); + auto& particleMaterialComponent = pWorld->CreateComponent(entity); // TODO : Some initialization here. auto& transformComponent = pWorld->CreateComponent(entity); transformComponent.SetTransform(cd::Transform::Identity()); transformComponent.Build(); particleEmitterComponent.SetRequiredVertexFormat(&pParticleMaterialType->GetRequiredVertexFormat());//to do : modify vertexFormat - particleEmitterComponent.SetMaterialType(pParticleMaterialType); - particleEmitterComponent.ActivateShaderFeature(engine::ShaderFeature::PARTICLE_INSTANCE); + particleMaterialComponent.Init(); + particleMaterialComponent.SetMaterialType(pParticleMaterialType); + particleMaterialComponent.ActivateShaderFeature(engine::ShaderFeature::PARTICLE_INSTANCE); + //particleEmitterComponent.SetMaterialType(pParticleMaterialType); + //particleEmitterComponent.ActivateShaderFeature(engine::ShaderFeature::PARTICLE_INSTANCE); particleEmitterComponent.Build(); //auto& particleForceFieldComponent = pWorld->CreateComponent(entity); //particleForceFieldComponent.Build(); diff --git a/Engine/Source/Editor/UILayers/Inspector.cpp b/Engine/Source/Editor/UILayers/Inspector.cpp index d2d4dc43..71e10444 100644 --- a/Engine/Source/Editor/UILayers/Inspector.cpp +++ b/Engine/Source/Editor/UILayers/Inspector.cpp @@ -651,6 +651,7 @@ template<> void UpdateComponentWidget(engine::SceneWorld* pSceneWorld, engine::Entity entity) { auto* pParticleEmitterComponent = pSceneWorld->GetParticleEmitterComponent(entity); + auto* pParticleMaterialComponent = pSceneWorld->GetMaterialComponent(entity); if (!pParticleEmitterComponent) { return; @@ -676,11 +677,11 @@ void UpdateComponentWidget(engine::SceneWorld* ImGuiUtils::ImGuiFloatProperty("LifeTime", pParticleEmitterComponent->GetLifeTime(),cd::Unit::None, 0, 6); if (ImGuiUtils::ImGuiBoolProperty("Instance State(Work Type Sprite)", pParticleEmitterComponent->GetInstanceState())) { - pParticleEmitterComponent->ActivateShaderFeature(engine::ShaderFeature::PARTICLE_INSTANCE); + pParticleMaterialComponent->ActivateShaderFeature(engine::ShaderFeature::PARTICLE_INSTANCE); } else { - pParticleEmitterComponent->DeactivateShaderFeature(engine::ShaderFeature::PARTICLE_INSTANCE); + pParticleMaterialComponent->DeactivateShaderFeature(engine::ShaderFeature::PARTICLE_INSTANCE); } } diff --git a/Engine/Source/Runtime/ECWorld/ParticleEmitterComponent.cpp b/Engine/Source/Runtime/ECWorld/ParticleEmitterComponent.cpp index e94f6a79..4338be07 100644 --- a/Engine/Source/Runtime/ECWorld/ParticleEmitterComponent.cpp +++ b/Engine/Source/Runtime/ECWorld/ParticleEmitterComponent.cpp @@ -355,46 +355,4 @@ void ParticleEmitterComponent::RePaddingShapeBuffer() } } -const std::string& ParticleEmitterComponent::GetShaderProgramName() const -{ - return m_pParticleMaterialType->GetShaderSchema().GetShaderProgramName(); -} - -void ParticleEmitterComponent::ActivateShaderFeature(ShaderFeature feature) -{ - if (ShaderFeature::DEFAULT == feature) - { - return; - } - - for (const auto& conflict : m_pParticleMaterialType->GetShaderSchema().GetConflictFeatureSet(feature)) - { - m_shaderFeatures.erase(conflict); - } - - m_shaderFeatures.insert(cd::MoveTemp(feature)); - - m_isShaderFeatureDirty = true; -} - -void ParticleEmitterComponent::DeactivateShaderFeature(ShaderFeature feature) -{ - m_shaderFeatures.erase(feature); - - m_isShaderFeatureDirty = true; -} - -const std::string& ParticleEmitterComponent::GetFeaturesCombine() -{ - if (m_isShaderFeatureDirty == false) - { - return m_featureCombine; - } - - m_featureCombine = m_pParticleMaterialType->GetShaderSchema().GetFeaturesCombine(m_shaderFeatures); - m_isShaderFeatureDirty = false; - - return m_featureCombine; -} - } \ No newline at end of file diff --git a/Engine/Source/Runtime/ECWorld/ParticleEmitterComponent.h b/Engine/Source/Runtime/ECWorld/ParticleEmitterComponent.h index bab5c867..6bfaaa9c 100644 --- a/Engine/Source/Runtime/ECWorld/ParticleEmitterComponent.h +++ b/Engine/Source/Runtime/ECWorld/ParticleEmitterComponent.h @@ -56,9 +56,6 @@ class ParticleEmitterComponent final bool& GetInstanceState() { return m_useInstance; } void SetInstanceState(bool state) { m_useInstance = state; } - const engine::MaterialType* GetMaterialType() const { return m_pParticleMaterialType; } - void SetMaterialType(const engine::MaterialType* pMaterialType) { m_pParticleMaterialType = pMaterialType; } - ParticleRenderMode& GetRenderMode() { return m_renderMode; } void SetRenderMode(engine::ParticleRenderMode mode) { m_renderMode = mode; } @@ -124,15 +121,6 @@ class ParticleEmitterComponent final void BuildParticleShape(); void RePaddingShapeBuffer(); - // Uber shader data. - const std::string& GetShaderProgramName() const; - void ActivateShaderFeature(ShaderFeature feature); - void DeactivateShaderFeature(ShaderFeature feature); - void SetShaderFeatures(std::set options) { m_shaderFeatures = cd::MoveTemp(m_shaderFeatures); } - std::set& GetShaderFeatures() { return m_shaderFeatures; } - const std::set& GetShaderFeatures() const { return m_shaderFeatures; } - const std::string& GetFeaturesCombine(); - private: //ParticleSystem m_particleSystem; ParticlePool m_particlePool; @@ -155,12 +143,6 @@ class ParticleEmitterComponent final //instancing bool m_useInstance = false; - //Uber shader - const engine::MaterialType* m_pParticleMaterialType = nullptr; - bool m_isShaderFeatureDirty = false; - std::set m_shaderFeatures; - std::string m_featureCombine; - //render mode mesh/billboard/ribbon ParticleRenderMode m_renderMode = ParticleRenderMode::Mesh; const cd::Mesh* m_pMeshData = nullptr; diff --git a/Engine/Source/Runtime/Rendering/ParticleRenderer.cpp b/Engine/Source/Runtime/Rendering/ParticleRenderer.cpp index 7bff31d5..c83f0c26 100644 --- a/Engine/Source/Runtime/Rendering/ParticleRenderer.cpp +++ b/Engine/Source/Runtime/Rendering/ParticleRenderer.cpp @@ -24,13 +24,12 @@ BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_SRC_ALPHA, BGFX_STATE_BLEND_INV_SRC_ALPHA uint64_t state_lines = BGFX_STATE_WRITE_MASK | BGFX_STATE_MSAA | BGFX_STATE_DEPTH_TEST_LESS | BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_SRC_ALPHA, BGFX_STATE_BLEND_INV_SRC_ALPHA) | BGFX_STATE_PT_LINES; -constexpr const char* SpriteParticleProgram = "SpriteParticleProgram"; +//Sprite Particle in EditorApp.cpp constexpr const char* RibbonParticleCsProgram = "RibbonParticleCsProgram"; constexpr const char* RibbonParticleProgram = "RibbonParticleProgram"; constexpr const char* ParticleEmitterShapeProgram = "ParticleEmitterShapeProgram"; constexpr const char* WO_BillboardParticleProgram = "WO_BillboardParticleProgram"; -constexpr StringCrc SpriteParticleProgramCrc = StringCrc{ "SpriteParticleProgram" }; constexpr StringCrc RibbonParticleProgramCsCrc = StringCrc{ "RibbonParticleCsProgram" }; constexpr StringCrc RibbonParticleProgramCrc = StringCrc{ "RibbonParticleProgram" }; @@ -40,7 +39,6 @@ constexpr StringCrc WO_BillboardParticleProgramCrc = StringCrc{ "WO_BillboardPar void ParticleRenderer::Init() { - GetRenderContext()->RegisterShaderProgram(SpriteParticleProgramCrc, { "vs_particleSprite", "fs_particleSprite" }); GetRenderContext()->RegisterShaderProgram(RibbonParticleProgramCsCrc, { "cs_particleRibbon" }); GetRenderContext()->RegisterShaderProgram(RibbonParticleProgramCrc, { "vs_particleRibbon", "fs_particleRibbon" }); GetRenderContext()->RegisterShaderProgram(ParticleEmitterShapeProgramCrc, {"vs_particleEmitterShape", "fs_particleEmitterShape"}); @@ -65,7 +63,7 @@ void ParticleRenderer::Warmup() GetRenderContext()->CreateUniform(ribbonCount, bgfx::UniformType::Vec4, 1); GetRenderContext()->CreateUniform(ribbonMaxPos, bgfx::UniformType::Vec4, 75); - GetRenderContext()->UploadShaderProgram(SpriteParticleProgram); + //GetRenderContext()->UploadShaderProgram(SpriteParticleProgram); GetRenderContext()->UploadShaderProgram(RibbonParticleCsProgram); GetRenderContext()->UploadShaderProgram(RibbonParticleProgram); GetRenderContext()->UploadShaderProgram(ParticleEmitterShapeProgram); @@ -94,7 +92,8 @@ void ParticleRenderer::Render(float deltaTime) const cd::Transform& particleTransform = m_pCurrentSceneWorld->GetTransformComponent(entity)->GetTransform(); const cd::Quaternion& particleRotation = m_pCurrentSceneWorld->GetTransformComponent(entity)->GetTransform().GetRotation(); ParticleEmitterComponent* pEmitterComponent = m_pCurrentSceneWorld->GetParticleEmitterComponent(entity); - + MaterialComponent* pParticleMaterialComponet = m_pCurrentSceneWorld->GetMaterialComponent(entity); + const cd::Transform& pMainCameraTransform = m_pCurrentSceneWorld->GetTransformComponent(pMainCameraEntity)->GetTransform(); //const cd::Quaternion& cameraRotation = pMainCameraTransform.GetRotation(); //Not include particle attribute @@ -180,7 +179,7 @@ void ParticleRenderer::Render(float deltaTime) bgfx::setState(state_tristrip); bgfx::setInstanceDataBuffer(&idb); - SetRenderMode(pEmitterComponent->GetRenderMode(), pEmitterComponent->GetEmitterParticleType()); + SetRenderMode(pEmitterComponent->GetRenderMode(), pEmitterComponent->GetEmitterParticleType(), pParticleMaterialComponet); } else { @@ -249,7 +248,7 @@ void ParticleRenderer::Render(float deltaTime) bgfx::setVertexBuffer(1, bgfx::VertexBufferHandle{ pEmitterComponent->GetRibbonParticleRemainVertexBufferHandle() }); bgfx::setIndexBuffer(bgfx::IndexBufferHandle{ pEmitterComponent->GetRibbonParticleIndexBufferHandle() }); } - SetRenderMode(pEmitterComponent->GetRenderMode(), pEmitterComponent->GetEmitterParticleType()); + SetRenderMode(pEmitterComponent->GetRenderMode(), pEmitterComponent->GetEmitterParticleType(),pParticleMaterialComponet); } } @@ -264,13 +263,13 @@ void ParticleRenderer::Render(float deltaTime) } } -void ParticleRenderer::SetRenderMode(engine::ParticleRenderMode& rendermode, engine::ParticleType type) +void ParticleRenderer::SetRenderMode(engine::ParticleRenderMode& rendermode, engine::ParticleType type, engine::MaterialComponent* shaderFeature_MaterialCompoent) { if (rendermode == engine::ParticleRenderMode::Mesh) { if (type == engine::ParticleType::Sprite) { - GetRenderContext()->Submit(GetViewID(), SpriteParticleProgram); + GetRenderContext()->Submit(GetViewID(), shaderFeature_MaterialCompoent->GetShaderProgramName(), shaderFeature_MaterialCompoent->GetFeaturesCombine()); } else if (type == engine::ParticleType::Ribbon) { diff --git a/Engine/Source/Runtime/Rendering/ParticleRenderer.h b/Engine/Source/Runtime/Rendering/ParticleRenderer.h index 93a226c3..477ac094 100644 --- a/Engine/Source/Runtime/Rendering/ParticleRenderer.h +++ b/Engine/Source/Runtime/Rendering/ParticleRenderer.h @@ -28,7 +28,7 @@ class ParticleRenderer final : public Renderer void SetForceFieldRotationForce(ParticleForceFieldComponent* forcefield) { m_forcefieldRotationFoce = forcefield->GetRotationForce(); } void SetForceFieldRange(ParticleForceFieldComponent* forcefield ,cd::Vec3f scale) { m_forcefieldRange = forcefield->GetForceFieldRange()*scale; } - void SetRenderMode(engine::ParticleRenderMode& rendermode, engine::ParticleType type); + void SetRenderMode(engine::ParticleRenderMode& rendermode, engine::ParticleType type, engine::MaterialComponent* materialcomponent); void SetRandomPosState(engine::Particle& particle, cd::Vec3f value, cd::Vec3f randomvalue, bool state); void SetRandomVelocityState(engine::Particle& particle, cd::Vec3f value, cd::Vec3f randomvalue, bool state); private: From f327f6e3e564ce214c02b5eb98856f26d6af44c4 Mon Sep 17 00:00:00 2001 From: OVOAOVO <2410651402@qq.com> Date: Mon, 25 Mar 2024 10:40:20 +0800 Subject: [PATCH 08/14] update AssetPipeline --- Engine/Source/ThirdParty/AssetPipeline | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Engine/Source/ThirdParty/AssetPipeline b/Engine/Source/ThirdParty/AssetPipeline index 4a5644f7..b170b23c 160000 --- a/Engine/Source/ThirdParty/AssetPipeline +++ b/Engine/Source/ThirdParty/AssetPipeline @@ -1 +1 @@ -Subproject commit 4a5644f700720e95e425a415620d058a32dcf47a +Subproject commit b170b23c5a95130d8661a884113b83ce1ef70959 From 45ff69df33d956c71935bb8933800c801f959717 Mon Sep 17 00:00:00 2001 From: OVOAOVO <2410651402@qq.com> Date: Mon, 25 Mar 2024 10:42:08 +0800 Subject: [PATCH 09/14] AssetPipeline conflict --- Engine/Source/ThirdParty/AssetPipeline | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Engine/Source/ThirdParty/AssetPipeline b/Engine/Source/ThirdParty/AssetPipeline index ccd965ba..bb66bf56 160000 --- a/Engine/Source/ThirdParty/AssetPipeline +++ b/Engine/Source/ThirdParty/AssetPipeline @@ -1 +1 @@ -Subproject commit ccd965ba15b46e4cbeb55effb2e4d15790762826 +Subproject commit bb66bf56cf4188f05cf92b3af56ece3f3efa7d5c From e49be18beceb53e9f9399c1bd7d8a3a954bd82de Mon Sep 17 00:00:00 2001 From: OVOAOVO <2410651402@qq.com> Date: Mon, 25 Mar 2024 16:30:05 +0800 Subject: [PATCH 10/14] merged main --- Engine/Source/Editor/EditorApp.cpp | 2 +- .../ECWorld/ParticleEmitterComponent.h | 2 +- .../Runtime/Rendering/ParticleRenderer.cpp | 42 ++++++++++--------- 3 files changed, 25 insertions(+), 21 deletions(-) diff --git a/Engine/Source/Editor/EditorApp.cpp b/Engine/Source/Editor/EditorApp.cpp index f508423b..e6a7c582 100644 --- a/Engine/Source/Editor/EditorApp.cpp +++ b/Engine/Source/Editor/EditorApp.cpp @@ -262,7 +262,7 @@ void EditorApp::InitMaterialType() m_pRenderContext->RegisterShaderProgram("WorldProgram", "vs_PBR", "fs_PBR"); m_pRenderContext->RegisterShaderProgram("AnimationProgram", "vs_animation", "fs_animation"); m_pRenderContext->RegisterShaderProgram("TerrainProgram", "vs_terrain", "fs_terrain"); - m_pRenderContext->RegisterShaderProgram("ParticleProgram", "vs_particle", "fs_particle"); + m_pRenderContext->RegisterShaderProgram("ParticleProgram", "vs_particleSprite", "fs_particleSprite"); m_pRenderContext->RegisterShaderProgram("CelluloidProgram", "vs_celluloid", "fs_celluloid"); m_pSceneWorld = std::make_unique(); diff --git a/Engine/Source/Runtime/ECWorld/ParticleEmitterComponent.h b/Engine/Source/Runtime/ECWorld/ParticleEmitterComponent.h index 6bfaaa9c..54b77ca2 100644 --- a/Engine/Source/Runtime/ECWorld/ParticleEmitterComponent.h +++ b/Engine/Source/Runtime/ECWorld/ParticleEmitterComponent.h @@ -125,7 +125,7 @@ class ParticleEmitterComponent final //ParticleSystem m_particleSystem; ParticlePool m_particlePool; - engine::ParticleType m_emitterParticleType ; + engine::ParticleType m_emitterParticleType = engine::ParticleType::Sprite; //emitter data int m_spawnCount = 75; diff --git a/Engine/Source/Runtime/Rendering/ParticleRenderer.cpp b/Engine/Source/Runtime/Rendering/ParticleRenderer.cpp index 40ef297a..7922ad6c 100644 --- a/Engine/Source/Runtime/Rendering/ParticleRenderer.cpp +++ b/Engine/Source/Runtime/Rendering/ParticleRenderer.cpp @@ -6,6 +6,7 @@ #include "ECWorld/TransformComponent.h" #include "Log/Log.h" #include "Rendering/RenderContext.h" +#include "Rendering/Resources/ShaderResource.h" #include "../UniformDefines/U_Particle.sh" namespace engine @@ -33,19 +34,19 @@ constexpr const char* RibbonParticleProgram = "RibbonParticleProgram"; constexpr const char* ParticleEmitterShapeProgram = "ParticleEmitterShapeProgram"; constexpr const char* WO_BillboardParticleProgram = "WO_BillboardParticleProgram"; -constexpr StringCrc RibbonParticleProgramCsCrc = StringCrc{ "RibbonParticleCsProgram" }; -constexpr StringCrc RibbonParticleProgramCrc = StringCrc{ "RibbonParticleProgram" }; +constexpr StringCrc RibbonParticleProgramCsCrc = StringCrc{ RibbonParticleCsProgram }; +constexpr StringCrc RibbonParticleProgramCrc = StringCrc{ RibbonParticleProgram }; -constexpr StringCrc ParticleEmitterShapeProgramCrc = StringCrc{ "ParticleEmitterShapeProgram" }; -constexpr StringCrc WO_BillboardParticleProgramCrc = StringCrc{ "WO_BillboardParticleProgram" }; +constexpr StringCrc ParticleEmitterShapeProgramCrc = StringCrc{ ParticleEmitterShapeProgram }; +constexpr StringCrc WO_BillboardParticleProgramCrc = StringCrc{ WO_BillboardParticleProgram }; } void ParticleRenderer::Init() { - GetRenderContext()->RegisterShaderProgram(RibbonParticleProgramCsCrc, { "cs_particleRibbon" }); - GetRenderContext()->RegisterShaderProgram(RibbonParticleProgramCrc, { "vs_particleRibbon", "fs_particleRibbon" }); - GetRenderContext()->RegisterShaderProgram(ParticleEmitterShapeProgramCrc, {"vs_particleEmitterShape", "fs_particleEmitterShape"}); - GetRenderContext()->RegisterShaderProgram(WO_BillboardParticleProgramCrc, { "vs_wo_billboardparticle","fs_wo_billboardparticle" }); + AddDependentShaderResource(GetRenderContext()->RegisterShaderProgram(RibbonParticleCsProgram, "cs_particleRibbon", ShaderProgramType::Compute)); + AddDependentShaderResource(GetRenderContext()->RegisterShaderProgram(RibbonParticleProgram, "vs_particleRibbon", "fs_particleRibbon")); + AddDependentShaderResource(GetRenderContext()->RegisterShaderProgram(ParticleEmitterShapeProgram, "vs_particleEmitterShape", "fs_particleEmitterShape")); + AddDependentShaderResource(GetRenderContext()->RegisterShaderProgram(WO_BillboardParticleProgram, "vs_wo_billboardparticle", "fs_wo_billboardparticle")); constexpr const char* particleTexture = "Textures/textures/Particle.png"; constexpr const char* ribbonTexture = "Textures/textures/Particle.png"; @@ -61,11 +62,7 @@ void ParticleRenderer::Init() GetRenderContext()->CreateUniform(ribbonCount, bgfx::UniformType::Vec4, 1); GetRenderContext()->CreateUniform(ribbonMaxPos, bgfx::UniformType::Vec4, 75); - //GetRenderContext()->UploadShaderProgram(SpriteParticleProgram); - GetRenderContext()->UploadShaderProgram(RibbonParticleCsProgram); - GetRenderContext()->UploadShaderProgram(RibbonParticleProgram); - GetRenderContext()->UploadShaderProgram(ParticleEmitterShapeProgram); - GetRenderContext()->UploadShaderProgram(WO_BillboardParticleProgram); + bgfx::setViewName(GetViewID(), "ParticleRenderer"); } void ParticleRenderer::UpdateView(const float* pViewMatrix, const float* pProjectionMatrix) @@ -100,6 +97,13 @@ void ParticleRenderer::Render(float deltaTime) const cd::Quaternion& particleRotation = m_pCurrentSceneWorld->GetTransformComponent(entity)->GetTransform().GetRotation(); ParticleEmitterComponent* pEmitterComponent = m_pCurrentSceneWorld->GetParticleEmitterComponent(entity); MaterialComponent* pParticleMaterialComponet = m_pCurrentSceneWorld->GetMaterialComponent(entity); + //NOTE: This ShaderResource Not Used Just For Judge + const ShaderResource* pShaderResource = pParticleMaterialComponet->GetShaderResource(); + if (ResourceStatus::Ready != pShaderResource->GetStatus() && + ResourceStatus::Optimized != pShaderResource->GetStatus()) + { + continue; + } const cd::Transform& pMainCameraTransform = m_pCurrentSceneWorld->GetTransformComponent(pMainCameraEntity)->GetTransform(); //const cd::Quaternion& cameraRotation = pMainCameraTransform.GetRotation(); @@ -247,7 +251,7 @@ void ParticleRenderer::Render(float deltaTime) } constexpr StringCrc maxPosList(ribbonMaxPos); GetRenderContext()->FillUniform(maxPosList, &ribbonPosList, 75); - GetRenderContext()->Dispatch(GetViewID(), RibbonParticleCsProgram, 1U, 1U, 1U); + GetRenderContext()->Dispatch(GetViewID(), RibbonParticleProgramCsCrc, 1U, 1U, 1U); //pEmitterComponent->UpdateRibbonPosBuffer(); constexpr StringCrc ribbonParticleSampler("r_texColor"); bgfx::setTexture(1, GetRenderContext()->GetUniform(ribbonParticleSampler), m_particleRibbonTextureHandle); @@ -266,8 +270,7 @@ void ParticleRenderer::Render(float deltaTime) bgfx::setIndexBuffer(bgfx::IndexBufferHandle{ pEmitterComponent->GetEmitterShapeIndexBufferHandle() }); bgfx::setState(state_lines); - constexpr StringCrc programHandleIndex{ "ParticleEmitterShapeProgram" }; - GetRenderContext()->Submit(GetViewID(), programHandleIndex); + GetRenderContext()->Submit(GetViewID(), ParticleEmitterShapeProgramCrc); } } @@ -277,18 +280,19 @@ void ParticleRenderer::SetRenderMode(engine::ParticleRenderMode& rendermode, eng { if (type == engine::ParticleType::Sprite) { - GetRenderContext()->Submit(GetViewID(), shaderFeature_MaterialCompoent->GetShaderProgramName(), shaderFeature_MaterialCompoent->GetFeaturesCombine()); + const ShaderResource* pShaderResource = shaderFeature_MaterialCompoent->GetShaderResource(); + GetRenderContext()->Submit(GetViewID(), pShaderResource->GetHandle()); } else if (type == engine::ParticleType::Ribbon) { - GetRenderContext()->Submit(GetViewID(), RibbonParticleProgram); + GetRenderContext()->Submit(GetViewID(), RibbonParticleProgramCrc); } } else if (rendermode == engine::ParticleRenderMode::Billboard) { if (type == engine::ParticleType::Sprite) { - GetRenderContext()->Submit(GetViewID(), WO_BillboardParticleProgram); + GetRenderContext()->Submit(GetViewID(), WO_BillboardParticleProgramCrc); } else if (type == engine::ParticleType::Ribbon) { From 3619462d1a2f67ab74007b5a1728ef0a5b0cd150 Mon Sep 17 00:00:00 2001 From: OVOAOVO <2410651402@qq.com> Date: Mon, 25 Mar 2024 19:17:40 +0800 Subject: [PATCH 11/14] vs shader feature --- Engine/Source/Editor/ECWorld/ECWorldConsumer.cpp | 2 +- Engine/Source/Editor/UILayers/EntityList.cpp | 6 +----- Engine/Source/Editor/UILayers/Inspector.cpp | 9 ++++----- 3 files changed, 6 insertions(+), 11 deletions(-) diff --git a/Engine/Source/Editor/ECWorld/ECWorldConsumer.cpp b/Engine/Source/Editor/ECWorld/ECWorldConsumer.cpp index 8883b580..eeba0b38 100644 --- a/Engine/Source/Editor/ECWorld/ECWorldConsumer.cpp +++ b/Engine/Source/Editor/ECWorld/ECWorldConsumer.cpp @@ -431,7 +431,7 @@ void ECWorldConsumer::AddParticleEmitter(engine::Entity entity, const cd::Mesh& particleEmitterComponent.SetEmitterAcceleration(emitter.GetAccelerate()); particleEmitterComponent.SetMeshData(&mesh); particleMaterialComponent.SetMaterialType(pMaterialType); - particleMaterialComponent.ActivateShaderFeature(engine::ShaderFeature::PARTICLE_INSTANCE); + //particleMaterialComponent.ActivateShaderFeature(engine::ShaderFeature::PARTICLE_INSTANCE); particleEmitterComponent.Build(); } diff --git a/Engine/Source/Editor/UILayers/EntityList.cpp b/Engine/Source/Editor/UILayers/EntityList.cpp index fbb6687a..10b9b164 100644 --- a/Engine/Source/Editor/UILayers/EntityList.cpp +++ b/Engine/Source/Editor/UILayers/EntityList.cpp @@ -269,12 +269,8 @@ void EntityList::AddEntity(engine::SceneWorld* pSceneWorld) particleEmitterComponent.SetRequiredVertexFormat(&pParticleMaterialType->GetRequiredVertexFormat());//to do : modify vertexFormat particleMaterialComponent.Init(); particleMaterialComponent.SetMaterialType(pParticleMaterialType); - particleMaterialComponent.ActivateShaderFeature(engine::ShaderFeature::PARTICLE_INSTANCE); - //particleEmitterComponent.SetMaterialType(pParticleMaterialType); - //particleEmitterComponent.ActivateShaderFeature(engine::ShaderFeature::PARTICLE_INSTANCE); + //particleMaterialComponent.ActivateShaderFeature(engine::ShaderFeature::PARTICLE_INSTANCE); particleEmitterComponent.Build(); - //auto& particleForceFieldComponent = pWorld->CreateComponent(entity); - //particleForceFieldComponent.Build(); } else if (ImGui::MenuItem("Add Particle ForceField")) { diff --git a/Engine/Source/Editor/UILayers/Inspector.cpp b/Engine/Source/Editor/UILayers/Inspector.cpp index 0154825b..21352e6b 100644 --- a/Engine/Source/Editor/UILayers/Inspector.cpp +++ b/Engine/Source/Editor/UILayers/Inspector.cpp @@ -679,12 +679,11 @@ void UpdateComponentWidget(engine::SceneWorld* ImGuiUtils::ImGuiVectorProperty("Acceleration", pParticleEmitterComponent->GetEmitterAcceleration()); ImGuiUtils::ColorPickerProperty("Color", pParticleEmitterComponent->GetEmitterColor()); ImGuiUtils::ImGuiFloatProperty("LifeTime", pParticleEmitterComponent->GetLifeTime(),cd::Unit::None, 0, 6); - if (ImGuiUtils::ImGuiBoolProperty("Instance State(Work Type Sprite)", pParticleEmitterComponent->GetInstanceState())) - { - pParticleMaterialComponent->ActivateShaderFeature(engine::ShaderFeature::PARTICLE_INSTANCE); - } - else + bool featureChanged = ImGuiUtils::ImGuiBoolProperty("Instance State(Work Type Sprite)", pParticleEmitterComponent->GetInstanceState()); + if (featureChanged) { + pParticleEmitterComponent->GetInstanceState() ? + pParticleMaterialComponent->ActivateShaderFeature(engine::ShaderFeature::PARTICLE_INSTANCE) : pParticleMaterialComponent->DeactivateShaderFeature(engine::ShaderFeature::PARTICLE_INSTANCE); } } From 5ab835d2ab1d91effed848b7aa9723f77ce3ca4a Mon Sep 17 00:00:00 2001 From: OVOAOVO <2410651402@qq.com> Date: Mon, 1 Apr 2024 15:48:22 +0800 Subject: [PATCH 12/14] Fixed Ribbon Count Separate different vertexbuffers and indexbuffers into different Components. --- .../shaders/cs_particleRibbon.sc | 2 +- Engine/Source/Editor/UILayers/EntityList.cpp | 2 + .../Runtime/ECWorld/AllComponentsHeader.h | 1 + .../ECWorld/ParticleEmitterComponent.cpp | 102 ----------------- .../ECWorld/ParticleEmitterComponent.h | 20 ---- .../ECWorld/ParticleRibbonComponent.cpp | 105 ++++++++++++++++++ .../Runtime/ECWorld/ParticleRibbonComponent.h | 61 ++++++++++ Engine/Source/Runtime/ECWorld/SceneWorld.cpp | 1 + Engine/Source/Runtime/ECWorld/SceneWorld.h | 2 + .../Runtime/Rendering/ParticleRenderer.cpp | 30 +++-- 10 files changed, 192 insertions(+), 134 deletions(-) create mode 100644 Engine/Source/Runtime/ECWorld/ParticleRibbonComponent.cpp create mode 100644 Engine/Source/Runtime/ECWorld/ParticleRibbonComponent.h diff --git a/Engine/BuiltInShaders/shaders/cs_particleRibbon.sc b/Engine/BuiltInShaders/shaders/cs_particleRibbon.sc index 13c40443..65ea35aa 100644 --- a/Engine/BuiltInShaders/shaders/cs_particleRibbon.sc +++ b/Engine/BuiltInShaders/shaders/cs_particleRibbon.sc @@ -7,7 +7,7 @@ uniform vec4 u_ribbonCount; // x : allRibbonVertexCount // y : particleActiveCount -uniform vec4 u_ribbonMaxPos[75]; +uniform vec4 u_ribbonMaxPos[300]; //here have max particle count (max limit of inspector) NUM_THREADS(1u, 1u, 1u) diff --git a/Engine/Source/Editor/UILayers/EntityList.cpp b/Engine/Source/Editor/UILayers/EntityList.cpp index 10b9b164..2c5e2890 100644 --- a/Engine/Source/Editor/UILayers/EntityList.cpp +++ b/Engine/Source/Editor/UILayers/EntityList.cpp @@ -261,6 +261,7 @@ void EntityList::AddEntity(engine::SceneWorld* pSceneWorld) { engine::Entity entity = AddNamedEntity("ParticleEmitter"); auto& particleEmitterComponent = pWorld->CreateComponent(entity); + auto& particleRibbonComponent = pWorld->CreateComponent(entity); auto& particleMaterialComponent = pWorld->CreateComponent(entity); // TODO : Some initialization here. auto& transformComponent = pWorld->CreateComponent(entity); @@ -271,6 +272,7 @@ void EntityList::AddEntity(engine::SceneWorld* pSceneWorld) particleMaterialComponent.SetMaterialType(pParticleMaterialType); //particleMaterialComponent.ActivateShaderFeature(engine::ShaderFeature::PARTICLE_INSTANCE); particleEmitterComponent.Build(); + particleRibbonComponent.Build(); } else if (ImGui::MenuItem("Add Particle ForceField")) { diff --git a/Engine/Source/Runtime/ECWorld/AllComponentsHeader.h b/Engine/Source/Runtime/ECWorld/AllComponentsHeader.h index 7af83610..be1e9312 100644 --- a/Engine/Source/Runtime/ECWorld/AllComponentsHeader.h +++ b/Engine/Source/Runtime/ECWorld/AllComponentsHeader.h @@ -16,4 +16,5 @@ #include "ECWorld/TerrainComponent.h" #include "ECWorld/TransformComponent.h" #include "ECWorld/ParticleEmitterComponent.h" +#include "ECWorld/ParticleRibbonComponent.h" #include "ECWorld/ParticleForceFieldComponent.h" \ No newline at end of file diff --git a/Engine/Source/Runtime/ECWorld/ParticleEmitterComponent.cpp b/Engine/Source/Runtime/ECWorld/ParticleEmitterComponent.cpp index 4338be07..eecf327e 100644 --- a/Engine/Source/Runtime/ECWorld/ParticleEmitterComponent.cpp +++ b/Engine/Source/Runtime/ECWorld/ParticleEmitterComponent.cpp @@ -21,9 +21,6 @@ void ParticleEmitterComponent::Build() PaddingSpriteVertexBuffer(); PaddingSpriteIndexBuffer(); - PaddingRibbonVertexBuffer(); - PaddingRibbonIndexBuffer(); - m_spriteParticleVertexBufferHandle = bgfx::createVertexBuffer(bgfx::makeRef(m_spriteParticleVertexBuffer.data(), static_cast(m_spriteParticleVertexBuffer.size())), vertexLayout).idx; m_spriteParticleIndexBufferHandle = bgfx::createIndexBuffer(bgfx::makeRef(m_spriteParticleIndexBuffer.data(), static_cast(m_spriteParticleIndexBuffer.size())), 0U).idx; } @@ -127,105 +124,6 @@ void ParticleEmitterComponent::PaddingSpriteIndexBuffer() } } -void ParticleEmitterComponent::PaddingRibbonVertexBuffer() -{ - //vertexbuffer - constexpr int meshVertexCount = Particle::GetMeshVertexCount(); - const int MAX_VERTEX_COUNT = m_particlePool.GetParticleMaxCount() * meshVertexCount; - size_t vertexCount = MAX_VERTEX_COUNT; - uint32_t csVertexCount = MAX_VERTEX_COUNT; - //prePos Vertex format/layout - cd::VertexFormat m_pRibbonPrePosVertexFormat; - m_pRibbonPrePosVertexFormat.AddVertexAttributeLayout(cd::VertexAttributeType::Position, cd::GetAttributeValueType(), cd::Point::Size); - m_pRibbonPrePosVertexFormat.AddVertexAttributeLayout(cd::VertexAttributeType::BoneWeight, cd::AttributeValueType::Float, 1U); - bgfx::VertexLayout prePosLayout; - VertexLayoutUtility::CreateVertexLayout(prePosLayout, m_pRibbonPrePosVertexFormat.GetVertexAttributeLayouts()); - //remain Vertex format/layout - cd::VertexFormat m_pRibbonRemainVertexFormat; - m_pRibbonRemainVertexFormat.AddVertexAttributeLayout(cd::VertexAttributeType::Color, cd::GetAttributeValueType(), cd::Color::Size); - m_pRibbonRemainVertexFormat.AddVertexAttributeLayout(cd::VertexAttributeType::UV, cd::GetAttributeValueType(), cd::UV::Size); - bgfx::VertexLayout color_UV_Layout; - VertexLayoutUtility::CreateVertexLayout(color_UV_Layout, m_pRibbonRemainVertexFormat.GetVertexAttributeLayouts()); - - //const uint32_t posVertexFormatStride = m_pRibbonPrePosVertexFormat.GetStride(); - const uint32_t remainVertexFormatStride = m_pRibbonRemainVertexFormat.GetStride(); - - //m_ribbonParticlePrePosVertexBuffer.resize(vertexCount * posVertexFormatStride); - m_ribbonParticleRemainVertexBuffer.resize(vertexCount * remainVertexFormatStride); - - //uint32_t currentPosDataSize = 0U; - //auto currentPosDataPtr = m_ribbonParticlePrePosVertexBuffer.data(); - uint32_t currentRemainDataSize = 0U; - auto currentRemainDataPtr = m_ribbonParticleRemainVertexBuffer.data(); - //float placeholder = 0.0f; - //uint32_t placeholderSize = sizeof(placeholder); - - std::vector vertexDataBuffer; - vertexDataBuffer.resize(MAX_VERTEX_COUNT); - // pos color uv - // only a picture now - for (int i = 0; i < MAX_VERTEX_COUNT; i += meshVertexCount) - { - vertexDataBuffer[i] = { cd::Vec3f(0.0f,1.0f,0.0f),cd::Vec4f(1.0f,1.0f,1.0f,1.0f),cd::Vec2f(0.5f,0.5f)}; - vertexDataBuffer[i+1] = { cd::Vec3f(0.0f,-1.0f,0.0f),cd::Vec4f(1.0f,1.0f,1.0f,1.0f),cd::Vec2f(0.5f,0.5f)}; - } - - for (int i = 0; i < MAX_VERTEX_COUNT; ++i) - { - //std::memcpy(¤tPosDataPtr[currentPosDataSize], &vertexDataBuffer[i].pos, sizeof(cd::Point)); - //currentPosDataSize += sizeof(cd::Point); - ////vec3 filled to vec4 - //std::memcpy(¤tPosDataPtr[currentPosDataSize], &placeholder, placeholderSize); - //currentPosDataSize += placeholderSize; - - std::memcpy(¤tRemainDataPtr[currentRemainDataSize], &vertexDataBuffer[i].color, sizeof(cd::Color)); - currentRemainDataSize += sizeof(cd::Color); - std::memcpy(¤tRemainDataPtr[currentRemainDataSize], &vertexDataBuffer[i].uv, sizeof(cd::UV)); - currentRemainDataSize += sizeof(cd::UV); - } - m_ribbonParticlePrePosVertexBufferHandle = bgfx::createDynamicVertexBuffer(csVertexCount, prePosLayout, BGFX_BUFFER_COMPUTE_READ_WRITE).idx; - const bgfx::Memory* pRibbonParticleRemainVBRef = bgfx::makeRef(m_ribbonParticleRemainVertexBuffer.data(), static_cast(m_ribbonParticleRemainVertexBuffer.size())); - m_ribbonParticleRemainVertexBufferHandle = bgfx::createVertexBuffer(pRibbonParticleRemainVBRef, color_UV_Layout).idx; -} - -void ParticleEmitterComponent::PaddingRibbonIndexBuffer() -{ - constexpr int meshVertexCount = Particle::GetMeshVertexCount();; - const bool useU16Index = meshVertexCount <= static_cast(std::numeric_limits::max()) + 1U; - const uint32_t indexTypeSize = useU16Index ? sizeof(uint16_t) : sizeof(uint32_t); - const int MAX_VERTEX_COUNT = (m_particlePool.GetParticleMaxCount() - 1)* meshVertexCount; - int indexCountForOneRibbon = 6; - const uint32_t indicesCount = MAX_VERTEX_COUNT / meshVertexCount * indexCountForOneRibbon; - m_ribbonParticleIndexBuffer.resize(indicesCount * indexTypeSize); - /// -/* size_t indexTypeSize = sizeof(uint16_t); - m_particleIndexBuffer.resize(m_particleSystem.GetMaxCount() / 4 * 6 * indexTyp - eSize);*/ - uint32_t currentDataSize = 0U; - auto currentDataPtr = m_ribbonParticleIndexBuffer.data(); - - std::vector indexes; - - for (uint16_t i = 0; i < MAX_VERTEX_COUNT; i += meshVertexCount) - { - uint16_t vertexIndex = static_cast(i); - indexes.push_back(vertexIndex); - indexes.push_back(vertexIndex+1); - indexes.push_back(vertexIndex+2); - - indexes.push_back(vertexIndex + 2); - indexes.push_back(vertexIndex + 1); - indexes.push_back(vertexIndex + 3); - } - - for (const auto& index : indexes) - { - std::memcpy(¤tDataPtr[currentDataSize], &index, indexTypeSize); - currentDataSize += static_cast(indexTypeSize); - } - m_ribbonParticleIndexBufferHandle = bgfx::createIndexBuffer(bgfx::makeRef(m_ribbonParticleIndexBuffer.data(), static_cast(m_ribbonParticleIndexBuffer.size())), 0U).idx; -} - void ParticleEmitterComponent::BuildParticleShape() { if (m_emitterShape == ParticleEmitterShape::Box) diff --git a/Engine/Source/Runtime/ECWorld/ParticleEmitterComponent.h b/Engine/Source/Runtime/ECWorld/ParticleEmitterComponent.h index 54b77ca2..b2b7ccfd 100644 --- a/Engine/Source/Runtime/ECWorld/ParticleEmitterComponent.h +++ b/Engine/Source/Runtime/ECWorld/ParticleEmitterComponent.h @@ -90,14 +90,6 @@ class ParticleEmitterComponent final std::vector& GetSpriteVertexBuffer() { return m_spriteParticleVertexBuffer; } std::vector& GetSpriteIndexBuffer() { return m_spriteParticleIndexBuffer; } - uint16_t& GetRibbonParticlePrePosVertexBufferHandle() { return m_ribbonParticlePrePosVertexBufferHandle; } - uint16_t& GetRibbonParticleRemainVertexBufferHandle() { return m_ribbonParticleRemainVertexBufferHandle; } - uint16_t& GetRibbonParticleIndexBufferHandle() { return m_ribbonParticleIndexBufferHandle; } - - std::vector& GetRibbonPrePosVertexBuffer() { return m_ribbonParticlePrePosVertexBuffer; } - std::vector& GetRibbonRemainVertexBuffer() { return m_ribbonParticleRemainVertexBuffer; } - std::vector& GetRibbonIndexBuffer() { return m_ribbonParticleIndexBuffer; } - uint16_t& GetEmitterShapeVertexBufferHandle() { return m_emitterShapeVertexBufferHandle; } uint16_t& GetEmitterShapeIndexBufferHandle() { return m_emitterShapeIndexBufferHandle; } @@ -115,9 +107,6 @@ class ParticleEmitterComponent final void PaddingSpriteVertexBuffer(); void PaddingSpriteIndexBuffer(); - void PaddingRibbonVertexBuffer(); - void PaddingRibbonIndexBuffer(); - void BuildParticleShape(); void RePaddingShapeBuffer(); @@ -162,15 +151,6 @@ class ParticleEmitterComponent final uint16_t m_spriteParticleVertexBufferHandle = UINT16_MAX; uint16_t m_spriteParticleIndexBufferHandle = UINT16_MAX; - //Ribbon - std::vector m_ribbonParticlePrePosVertexBuffer; - std::vector m_ribbonParticleRemainVertexBuffer; - std::vector m_ribbonParticleIndexBuffer; - uint16_t m_ribbonParticlePrePosVertexBufferHandle = UINT16_MAX; - uint16_t m_ribbonParticleRemainVertexBufferHandle = UINT16_MAX; - uint16_t m_ribbonParticleIndexBufferHandle = UINT16_MAX; - - //emitter shape vertex/index ParticleEmitterShape m_emitterShape = ParticleEmitterShape::Box; cd::Vec3f m_emitterShapeRange {10.0f ,5.0f ,10.0f}; diff --git a/Engine/Source/Runtime/ECWorld/ParticleRibbonComponent.cpp b/Engine/Source/Runtime/ECWorld/ParticleRibbonComponent.cpp new file mode 100644 index 00000000..c8aeb704 --- /dev/null +++ b/Engine/Source/Runtime/ECWorld/ParticleRibbonComponent.cpp @@ -0,0 +1,105 @@ +#include "Log/Log.h" +#include "ParticleRibbonComponent.h" +#include "Rendering/Utility/VertexLayoutUtility.h" +#include "Utilities/MeshUtils.hpp" + +#include + +namespace engine +{ +void ParticleRibbonComponent::Build() +{ + PaddingRibbonVertexBuffer(); + PaddingRibbonIndexBuffer(); +} + +void ParticleRibbonComponent::PaddingRibbonVertexBuffer() +{ + //vertexbuffer + constexpr int meshVertexCount = Particle::GetMeshVertexCount(); + //75 is from particle's MaxCount,There only padding once. + const int MAX_VERTEX_COUNT = 75 * meshVertexCount; + size_t vertexCount = MAX_VERTEX_COUNT; + uint32_t csVertexCount = MAX_VERTEX_COUNT; + //prePos Vertex format/layout + cd::VertexFormat m_pRibbonPrePosVertexFormat; + m_pRibbonPrePosVertexFormat.AddVertexAttributeLayout(cd::VertexAttributeType::Position, cd::GetAttributeValueType(), cd::Point::Size); + m_pRibbonPrePosVertexFormat.AddVertexAttributeLayout(cd::VertexAttributeType::BoneWeight, cd::AttributeValueType::Float, 1U); + bgfx::VertexLayout prePosLayout; + VertexLayoutUtility::CreateVertexLayout(prePosLayout, m_pRibbonPrePosVertexFormat.GetVertexAttributeLayouts()); + //remain Vertex format/layout + cd::VertexFormat m_pRibbonRemainVertexFormat; + m_pRibbonRemainVertexFormat.AddVertexAttributeLayout(cd::VertexAttributeType::Color, cd::GetAttributeValueType(), cd::Color::Size); + m_pRibbonRemainVertexFormat.AddVertexAttributeLayout(cd::VertexAttributeType::UV, cd::GetAttributeValueType(), cd::UV::Size); + bgfx::VertexLayout color_UV_Layout; + VertexLayoutUtility::CreateVertexLayout(color_UV_Layout, m_pRibbonRemainVertexFormat.GetVertexAttributeLayouts()); + + const uint32_t remainVertexFormatStride = m_pRibbonRemainVertexFormat.GetStride(); + + m_ribbonParticleRemainVertexBuffer.resize(vertexCount * remainVertexFormatStride); + + uint32_t currentRemainDataSize = 0U; + auto currentRemainDataPtr = m_ribbonParticleRemainVertexBuffer.data(); + + std::vector vertexDataBuffer; + vertexDataBuffer.resize(MAX_VERTEX_COUNT); + // pos color uv + // only a picture now + for (int i = 0; i < MAX_VERTEX_COUNT; i += meshVertexCount) + { + vertexDataBuffer[i] = { cd::Vec3f(0.0f,1.0f,0.0f),cd::Vec4f(1.0f,1.0f,1.0f,1.0f),cd::Vec2f(0.5f,0.5f) }; + vertexDataBuffer[i + 1] = { cd::Vec3f(0.0f,-1.0f,0.0f),cd::Vec4f(1.0f,1.0f,1.0f,1.0f),cd::Vec2f(0.5f,0.5f) }; + } + + for (int i = 0; i < MAX_VERTEX_COUNT; ++i) + { + std::memcpy(¤tRemainDataPtr[currentRemainDataSize], &vertexDataBuffer[i].color, sizeof(cd::Color)); + currentRemainDataSize += sizeof(cd::Color); + std::memcpy(¤tRemainDataPtr[currentRemainDataSize], &vertexDataBuffer[i].uv, sizeof(cd::UV)); + currentRemainDataSize += sizeof(cd::UV); + } + m_ribbonParticlePrePosVertexBufferHandle = bgfx::createDynamicVertexBuffer(csVertexCount, prePosLayout, BGFX_BUFFER_COMPUTE_READ_WRITE).idx; + const bgfx::Memory* pRibbonParticleRemainVBRef = bgfx::makeRef(m_ribbonParticleRemainVertexBuffer.data(), static_cast(m_ribbonParticleRemainVertexBuffer.size())); + m_ribbonParticleRemainVertexBufferHandle = bgfx::createVertexBuffer(pRibbonParticleRemainVBRef, color_UV_Layout).idx; +} + +void ParticleRibbonComponent::PaddingRibbonIndexBuffer() +{ + constexpr int meshVertexCount = Particle::GetMeshVertexCount();; + const bool useU16Index = meshVertexCount <= static_cast(std::numeric_limits::max()) + 1U; + const uint32_t indexTypeSize = useU16Index ? sizeof(uint16_t) : sizeof(uint32_t); + //TODO: 75 is from particle's MaxCount There only padding once. + const int MAX_VERTEX_COUNT = (75 - 1) * meshVertexCount; + int indexCountForOneRibbon = 6; + const uint32_t indicesCount = MAX_VERTEX_COUNT / meshVertexCount * indexCountForOneRibbon; + m_ribbonParticleIndexBuffer.resize(indicesCount * indexTypeSize); + /// +/* size_t indexTypeSize = sizeof(uint16_t); + m_particleIndexBuffer.resize(m_particleSystem.GetMaxCount() / 4 * 6 * indexTyp + eSize);*/ + uint32_t currentDataSize = 0U; + auto currentDataPtr = m_ribbonParticleIndexBuffer.data(); + + std::vector indexes; + + for (uint16_t i = 0; i < MAX_VERTEX_COUNT; i += meshVertexCount) + { + uint16_t vertexIndex = static_cast(i); + indexes.push_back(vertexIndex); + indexes.push_back(vertexIndex + 1); + indexes.push_back(vertexIndex + 2); + + indexes.push_back(vertexIndex + 2); + indexes.push_back(vertexIndex + 1); + indexes.push_back(vertexIndex + 3); + } + + for (const auto& index : indexes) + { + std::memcpy(¤tDataPtr[currentDataSize], &index, indexTypeSize); + currentDataSize += static_cast(indexTypeSize); + } + m_ribbonParticleIndexBufferHandle = bgfx::createIndexBuffer(bgfx::makeRef(m_ribbonParticleIndexBuffer.data(), static_cast(m_ribbonParticleIndexBuffer.size())), 0U).idx; +} + +} \ No newline at end of file diff --git a/Engine/Source/Runtime/ECWorld/ParticleRibbonComponent.h b/Engine/Source/Runtime/ECWorld/ParticleRibbonComponent.h new file mode 100644 index 00000000..18baa54b --- /dev/null +++ b/Engine/Source/Runtime/ECWorld/ParticleRibbonComponent.h @@ -0,0 +1,61 @@ +#include "Base/Template.h" +#include "Core/StringCrc.h" +#include "Math/Vector.hpp" +#include "Math/Transform.hpp" +#include "Material/ShaderSchema.h" +#include "Material/MaterialType.h" +#include "ParticleSystem/ParticlePool.h" +#include "Scene/Mesh.h" +#include "Scene/Types.h" +#include "Scene/VertexFormat.h" + +namespace engine +{ + +class ParticleRibbonComponent final +{ +public: + static constexpr StringCrc GetClassName() + { + constexpr StringCrc className("ParticleRibbonComponent"); + return className; + } + + ParticleRibbonComponent() = default; + ParticleRibbonComponent(const ParticleRibbonComponent&) = default; + ParticleRibbonComponent& operator=(const ParticleRibbonComponent&) = default; + ParticleRibbonComponent(ParticleRibbonComponent&&) = default; + ParticleRibbonComponent& operator=(ParticleRibbonComponent&&) = default; + ~ParticleRibbonComponent() = default; + + void PaddingRibbonVertexBuffer(); + void PaddingRibbonIndexBuffer(); + + void Build(); + + uint16_t& GetRibbonParticlePrePosVertexBufferHandle() { return m_ribbonParticlePrePosVertexBufferHandle; } + uint16_t& GetRibbonParticleRemainVertexBufferHandle() { return m_ribbonParticleRemainVertexBufferHandle; } + uint16_t& GetRibbonParticleIndexBufferHandle() { return m_ribbonParticleIndexBufferHandle; } + + std::vector& GetRibbonPrePosVertexBuffer() { return m_ribbonParticlePrePosVertexBuffer; } + std::vector& GetRibbonRemainVertexBuffer() { return m_ribbonParticleRemainVertexBuffer; } + std::vector& GetRibbonIndexBuffer() { return m_ribbonParticleIndexBuffer; } + +private: + //particle vertex/index + struct VertexData + { + cd::Vec3f pos; + cd::Vec4f color; + cd::UV uv; + }; + + std::vector m_ribbonParticlePrePosVertexBuffer; + std::vector m_ribbonParticleRemainVertexBuffer; + std::vector m_ribbonParticleIndexBuffer; + uint16_t m_ribbonParticlePrePosVertexBufferHandle = UINT16_MAX; + uint16_t m_ribbonParticleRemainVertexBufferHandle = UINT16_MAX; + uint16_t m_ribbonParticleIndexBufferHandle = UINT16_MAX; +}; + +} \ No newline at end of file diff --git a/Engine/Source/Runtime/ECWorld/SceneWorld.cpp b/Engine/Source/Runtime/ECWorld/SceneWorld.cpp index 1d410d6d..a85a92d4 100644 --- a/Engine/Source/Runtime/ECWorld/SceneWorld.cpp +++ b/Engine/Source/Runtime/ECWorld/SceneWorld.cpp @@ -36,6 +36,7 @@ SceneWorld::SceneWorld() m_pSkyComponentStorage = m_pWorld->Register(); m_pStaticMeshComponentStorage = m_pWorld->Register(); m_pParticleEmitterComponentStorage = m_pWorld->Register(); + m_pParticleRibbonComponentStorage = m_pWorld->Register(); m_pParticleForceFieldComponentStorage = m_pWorld->Register(); m_pTerrainComponentStorage = m_pWorld->Register(); m_pTransformComponentStorage = m_pWorld->Register(); diff --git a/Engine/Source/Runtime/ECWorld/SceneWorld.h b/Engine/Source/Runtime/ECWorld/SceneWorld.h index cd125252..d2e8d3cc 100644 --- a/Engine/Source/Runtime/ECWorld/SceneWorld.h +++ b/Engine/Source/Runtime/ECWorld/SceneWorld.h @@ -41,6 +41,7 @@ class SceneWorld DEFINE_COMPONENT_STORAGE_WITH_APIS(Sky); DEFINE_COMPONENT_STORAGE_WITH_APIS(StaticMesh); DEFINE_COMPONENT_STORAGE_WITH_APIS(ParticleEmitter); + DEFINE_COMPONENT_STORAGE_WITH_APIS(ParticleRibbon); DEFINE_COMPONENT_STORAGE_WITH_APIS(ParticleForceField); DEFINE_COMPONENT_STORAGE_WITH_APIS(Terrain); DEFINE_COMPONENT_STORAGE_WITH_APIS(Transform); @@ -99,6 +100,7 @@ class SceneWorld DeleteSkyComponent(entity); DeleteStaticMeshComponent(entity); DeleteParticleEmitterComponent(entity); + DeleteParticleRibbonComponent(entity); DeleteParticleForceFieldComponent(entity); DeleteTerrainComponent(entity); DeleteTransformComponent(entity); diff --git a/Engine/Source/Runtime/Rendering/ParticleRenderer.cpp b/Engine/Source/Runtime/Rendering/ParticleRenderer.cpp index 7922ad6c..115e0cde 100644 --- a/Engine/Source/Runtime/Rendering/ParticleRenderer.cpp +++ b/Engine/Source/Runtime/Rendering/ParticleRenderer.cpp @@ -60,7 +60,7 @@ void ParticleRenderer::Init() GetRenderContext()->CreateUniform(shapeRange, bgfx::UniformType::Vec4, 1); GetRenderContext()->CreateUniform(particleColor, bgfx::UniformType::Vec4, 1); GetRenderContext()->CreateUniform(ribbonCount, bgfx::UniformType::Vec4, 1); - GetRenderContext()->CreateUniform(ribbonMaxPos, bgfx::UniformType::Vec4, 75); + GetRenderContext()->CreateUniform(ribbonMaxPos, bgfx::UniformType::Vec4, 300); bgfx::setViewName(GetViewID(), "ParticleRenderer"); } @@ -96,6 +96,7 @@ void ParticleRenderer::Render(float deltaTime) const cd::Transform& particleTransform = m_pCurrentSceneWorld->GetTransformComponent(entity)->GetTransform(); const cd::Quaternion& particleRotation = m_pCurrentSceneWorld->GetTransformComponent(entity)->GetTransform().GetRotation(); ParticleEmitterComponent* pEmitterComponent = m_pCurrentSceneWorld->GetParticleEmitterComponent(entity); + ParticleRibbonComponent* pRibbonEmitterComponet = m_pCurrentSceneWorld->GetParticleRibbonComponent(entity); MaterialComponent* pParticleMaterialComponet = m_pCurrentSceneWorld->GetMaterialComponent(entity); //NOTE: This ShaderResource Not Used Just For Judge const ShaderResource* pShaderResource = pParticleMaterialComponet->GetShaderResource(); @@ -182,9 +183,9 @@ void ParticleRenderer::Render(float deltaTime) { constexpr StringCrc ribbonParticleSampler("r_texColor"); bgfx::setTexture(1, GetRenderContext()->GetUniform(ribbonParticleSampler), m_particleRibbonTextureHandle); - bgfx::setVertexBuffer(0, bgfx::DynamicVertexBufferHandle{ pEmitterComponent->GetRibbonParticlePrePosVertexBufferHandle() }); - bgfx::setVertexBuffer(1, bgfx::VertexBufferHandle{ pEmitterComponent->GetRibbonParticleRemainVertexBufferHandle() }); - bgfx::setIndexBuffer(bgfx::IndexBufferHandle{ pEmitterComponent->GetRibbonParticleIndexBufferHandle() }); + bgfx::setVertexBuffer(0, bgfx::DynamicVertexBufferHandle{ pRibbonEmitterComponet->GetRibbonParticlePrePosVertexBufferHandle() }); + bgfx::setVertexBuffer(1, bgfx::VertexBufferHandle{ pRibbonEmitterComponet->GetRibbonParticleRemainVertexBufferHandle() }); + bgfx::setIndexBuffer(bgfx::IndexBufferHandle{ pRibbonEmitterComponet->GetRibbonParticleIndexBufferHandle() }); } bgfx::setState(state_tristrip); @@ -230,7 +231,7 @@ void ParticleRenderer::Render(float deltaTime) } else if (pEmitterComponent->GetEmitterParticleType() == engine::ParticleType::Ribbon) { - bgfx::setBuffer(PT_RIBBON_VERTEX_STAGE, bgfx::DynamicVertexBufferHandle{ pEmitterComponent->GetRibbonParticlePrePosVertexBufferHandle() }, bgfx::Access::ReadWrite); + bgfx::setBuffer(PT_RIBBON_VERTEX_STAGE, bgfx::DynamicVertexBufferHandle{ pRibbonEmitterComponet->GetRibbonParticlePrePosVertexBufferHandle() }, bgfx::Access::ReadWrite); //ribbonCount Uinform constexpr StringCrc ribbontCounts(ribbonCount); @@ -241,23 +242,30 @@ void ParticleRenderer::Render(float deltaTime) GetRenderContext()->FillUniform(ribbontCounts, &allRibbonCount, 1); //ribbonListUniform - cd::Vec4f ribbonPosList[75]{}; - for (int i = 0; i < 75; i++) + cd::Vec4f ribbonPosList[300]{}; + for (int i = 0; i < 300; i++) { + if (i >= pEmitterComponent->GetParticlePool().GetParticleMaxCount()) + { + ribbonPosList[i] = cd::Vec4f(0.0f,0.0f,0.0f,0.0f); + } + else + { ribbonPosList[i] =cd::Vec4f(pEmitterComponent->GetParticlePool().GetParticle(i).GetPos().x(), pEmitterComponent->GetParticlePool().GetParticle(i).GetPos().y(), pEmitterComponent->GetParticlePool().GetParticle(i).GetPos().z() , 0.0f); + } } constexpr StringCrc maxPosList(ribbonMaxPos); - GetRenderContext()->FillUniform(maxPosList, &ribbonPosList, 75); + GetRenderContext()->FillUniform(maxPosList, &ribbonPosList, 300); GetRenderContext()->Dispatch(GetViewID(), RibbonParticleProgramCsCrc, 1U, 1U, 1U); //pEmitterComponent->UpdateRibbonPosBuffer(); constexpr StringCrc ribbonParticleSampler("r_texColor"); bgfx::setTexture(1, GetRenderContext()->GetUniform(ribbonParticleSampler), m_particleRibbonTextureHandle); - bgfx::setVertexBuffer(0, bgfx::DynamicVertexBufferHandle{ pEmitterComponent->GetRibbonParticlePrePosVertexBufferHandle() }); - bgfx::setVertexBuffer(1, bgfx::VertexBufferHandle{ pEmitterComponent->GetRibbonParticleRemainVertexBufferHandle() }); - bgfx::setIndexBuffer(bgfx::IndexBufferHandle{ pEmitterComponent->GetRibbonParticleIndexBufferHandle() }); + bgfx::setVertexBuffer(0, bgfx::DynamicVertexBufferHandle{ pRibbonEmitterComponet->GetRibbonParticlePrePosVertexBufferHandle() }); + bgfx::setVertexBuffer(1, bgfx::VertexBufferHandle{ pRibbonEmitterComponet->GetRibbonParticleRemainVertexBufferHandle() }); + bgfx::setIndexBuffer(bgfx::IndexBufferHandle{ pRibbonEmitterComponet->GetRibbonParticleIndexBufferHandle() }); } SetRenderMode(pEmitterComponent->GetRenderMode(), pEmitterComponent->GetEmitterParticleType(),pParticleMaterialComponet); } From 2af93aad5341344db613ddbc72cf04af9ce9144c Mon Sep 17 00:00:00 2001 From: OVOAOVO <2410651402@qq.com> Date: Sat, 18 May 2024 10:20:11 +0800 Subject: [PATCH 13/14] AssetPipline update --- Engine/Source/ThirdParty/AssetPipeline | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Engine/Source/ThirdParty/AssetPipeline b/Engine/Source/ThirdParty/AssetPipeline index bb66bf56..08f18bee 160000 --- a/Engine/Source/ThirdParty/AssetPipeline +++ b/Engine/Source/ThirdParty/AssetPipeline @@ -1 +1 @@ -Subproject commit bb66bf56cf4188f05cf92b3af56ece3f3efa7d5c +Subproject commit 08f18beeaa7e344315632c8dcebcbea478529f43 From 9c27e06b16ff296da5be06f4b7a29257dd73db01 Mon Sep 17 00:00:00 2001 From: OVOAOVO <2410651402@qq.com> Date: Sat, 18 May 2024 10:21:44 +0800 Subject: [PATCH 14/14] update AssetPipline --- Engine/Source/ThirdParty/AssetPipeline | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Engine/Source/ThirdParty/AssetPipeline b/Engine/Source/ThirdParty/AssetPipeline index 489ec0fb..08f18bee 160000 --- a/Engine/Source/ThirdParty/AssetPipeline +++ b/Engine/Source/ThirdParty/AssetPipeline @@ -1 +1 @@ -Subproject commit 489ec0fb9c11318c2ca8888bc234119d4d92a493 +Subproject commit 08f18beeaa7e344315632c8dcebcbea478529f43