From 565126c4bacbe5404aaf9bb0befdf60f2eafad32 Mon Sep 17 00:00:00 2001 From: Brandon Mak Date: Mon, 20 Mar 2023 09:33:04 +0800 Subject: [PATCH] Enabled custom update shaders for particles --- .../Inspector/SHEditorComponentView.hpp | 45 ++++++++++++++++++- .../Particles/SHParticleEmitterComponent.cpp | 16 +++++-- .../Particles/SHParticleEmitterComponent.h | 19 +++++--- .../Particles/SHParticleSubSystem.cpp | 4 +- .../src/Serialization/SHYAMLConverters.h | 14 ++++++ 5 files changed, 88 insertions(+), 10 deletions(-) diff --git a/SHADE_Engine/src/Editor/EditorWindow/Inspector/SHEditorComponentView.hpp b/SHADE_Engine/src/Editor/EditorWindow/Inspector/SHEditorComponentView.hpp index 4e150e10..4aa03a4b 100644 --- a/SHADE_Engine/src/Editor/EditorWindow/Inspector/SHEditorComponentView.hpp +++ b/SHADE_Engine/src/Editor/EditorWindow/Inspector/SHEditorComponentView.hpp @@ -886,7 +886,7 @@ namespace SHADE if (AssetID* payload = SHDragDrop::AcceptPayload(SHDragDrop::DRAG_RESOURCE)) { Handle texture = SHResourceManager::LoadOrGet(*payload); - gfxSystem->BuildTextures(); + gfxSystem->BuildTextures(); if (texture) { @@ -901,6 +901,49 @@ namespace SHADE SHDragDrop::EndTarget(); } } + SHEditorWidgets::InputText("Custom Update Shader", + [comp = component]() + { + auto customShader = comp->GetCustomUpdateShader(); + + if (customShader) + return customShader->GetName(); + else + return std::string{}; + + }, + [comp = component](std::string const& text) + { + }, {}, ImGuiSliderFlags_ReadOnly); + + if (SHDragDrop::BeginTarget()) + { + if (AssetID* payload = SHDragDrop::AcceptPayload(SHDragDrop::DRAG_RESOURCE)) + { + Handle shaderModule = SHResourceManager::LoadOrGet(*payload); + + if (shaderModule) + { + component->SetCustomUpdateShader(shaderModule); + component->SetCustomUpdateShaderAssetID(*payload); + } + else + { + SHLOG_WARNING("[] Attempted to load invalid shader! Custom update shader for particles not set. "); + } + + SHDragDrop::EndTarget(); + } + } + ImGui::SameLine(); + if (ImGui::Button("Reset")) + { + component->SetCustomUpdateShader({}); + component->SetCustomUpdateShaderAssetID(INVALID_ASSET_ID); + + } + + SHEditorWidgets::CheckBox("Is Passive", [comp = component]() {return comp->GetPassive(); }, [comp = component](bool flag) {comp->SetPassive(flag); }); diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Particles/SHParticleEmitterComponent.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Particles/SHParticleEmitterComponent.cpp index 9541a47b..8293604c 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Particles/SHParticleEmitterComponent.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Particles/SHParticleEmitterComponent.cpp @@ -72,11 +72,16 @@ namespace SHADE cpuEmitterData.textureIndex = index; } - void SHParticleEmitterComponent::SetTextureAssetID(AssetID id) + void SHParticleEmitterComponent::SetTextureAssetID(AssetID id) noexcept { textureAssetID = id; } + void SHParticleEmitterComponent::SetCustomUpdateShaderAssetID(AssetID id) noexcept + { + customUpdateShaderID = id; + } + void SHParticleEmitterComponent::SetMinSize(float size) noexcept { cpuEmitterData.lifeAndSizeRange.z = size; @@ -87,7 +92,7 @@ namespace SHADE cpuEmitterData.lifeAndSizeRange.w = size; } - void SHParticleEmitterComponent::SetCustomShader(Handle shaderModule) noexcept + void SHParticleEmitterComponent::SetCustomUpdateShader(Handle shaderModule) noexcept { customUpdateShader = shaderModule; } @@ -148,6 +153,11 @@ namespace SHADE return textureAssetID; } + AssetID SHParticleEmitterComponent::GetCustomUpdateShaderAssetID(void) const noexcept + { + return customUpdateShaderID; + } + float SHParticleEmitterComponent::GetMinSize(void) const noexcept { return cpuEmitterData.lifeAndSizeRange.z; @@ -159,7 +169,7 @@ namespace SHADE } - Handle SHParticleEmitterComponent::GetCustomShader(void) const noexcept + Handle SHParticleEmitterComponent::GetCustomUpdateShader(void) const noexcept { return customUpdateShader; } diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Particles/SHParticleEmitterComponent.h b/SHADE_Engine/src/Graphics/MiddleEnd/Particles/SHParticleEmitterComponent.h index d99f8403..616410cd 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Particles/SHParticleEmitterComponent.h +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Particles/SHParticleEmitterComponent.h @@ -121,6 +121,9 @@ namespace SHADE //! For the emitter to use to give particles their texture AssetID textureAssetID; + //! Custom update shaders, similarly with textures, will be identified through their AssetID + AssetID customUpdateShaderID; + public: void OnCreate(void) override final; void OnDestroy(void) override final; @@ -137,11 +140,9 @@ namespace SHADE void SetMaxSpeed (float speed) noexcept; void SetRotationSpeed (float speed) noexcept; void SetTextureIndex (uint32_t index) noexcept; - void SetTextureAssetID (AssetID id); void SetMinSize (float size) noexcept; void SetMaxSize (float size) noexcept; - void SetCustomShader (Handle shaderModule) noexcept; - + void SetCustomUpdateShader (Handle shaderModule) noexcept; uint32_t GetEmissionCount (void) const noexcept; bool GetPassive (void) const noexcept; @@ -153,11 +154,19 @@ namespace SHADE float GetMaxSpeed (void) const noexcept; float GetRotationSpeed (void) const noexcept; uint32_t GetTextureIndex (void) const noexcept; - AssetID GetTextureAssetID (void) const noexcept; float GetMinSize (void) const noexcept; float GetMaxSize (void) const noexcept; - Handle GetCustomShader (void) const noexcept; + Handle GetCustomUpdateShader (void) const noexcept; + /*-----------------------------------------------------------------------*/ + /* NON-INTERFACE FUNCTIONS */ + /*-----------------------------------------------------------------------*/ + void SetTextureAssetID(AssetID id) noexcept; + void SetCustomUpdateShaderAssetID(AssetID id) noexcept; + + AssetID GetTextureAssetID(void) const noexcept; + AssetID GetCustomUpdateShaderAssetID(void) const noexcept; + friend class SHParticleSubSystem; diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Particles/SHParticleSubSystem.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Particles/SHParticleSubSystem.cpp index 05295fd0..d99832a5 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Particles/SHParticleSubSystem.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Particles/SHParticleSubSystem.cpp @@ -446,8 +446,10 @@ namespace SHADE /*-----------------------------------------------------------------------*/ for (auto& emitter : emitters) { + // If custom update shader is a valid handle in the component if (emitter.customUpdateShader) { + // Check if pipeline associated with shader is valid, if not create or get one from the cache if (!emitter.customUpdatePipeline) emitter.customUpdatePipeline = GetCustomUpdatePipeline(emitter.customUpdateShader); @@ -456,7 +458,7 @@ namespace SHADE } else { - // bind the pipeline for updating + // bind the default upddate pipeline for updating cmdBuffer->BindPipeline(defaultUpdatePipelineData.pipeline); } diff --git a/SHADE_Engine/src/Serialization/SHYAMLConverters.h b/SHADE_Engine/src/Serialization/SHYAMLConverters.h index 0a879146..c1fa8548 100644 --- a/SHADE_Engine/src/Serialization/SHYAMLConverters.h +++ b/SHADE_Engine/src/Serialization/SHYAMLConverters.h @@ -516,6 +516,7 @@ namespace YAML static constexpr std::string_view MAX_SPEED_TAG = "Maximum Speed"; static constexpr std::string_view ROTATION_SPEED_TAG = "Rotation Speed"; static constexpr std::string_view TEXTURE_ASSET_ID_TAG = "Texture Asset ID"; + static constexpr std::string_view CUSTOM_UPDATE_SHADER_ASSET_ID_TAG = "Custom Update Shader Asset ID"; static YAML::Node encode(SHParticleEmitterComponent const& rhs) { @@ -532,6 +533,7 @@ namespace YAML node[ANGULAR_RANGES_OFFSET_TAG.data()] = rhs.GetAngularRangesAndOffsets(); node[ROTATION_SPEED_TAG.data()] = rhs.GetRotationSpeed(); node[TEXTURE_ASSET_ID_TAG.data()] = rhs.GetTextureAssetID(); + node[CUSTOM_UPDATE_SHADER_ASSET_ID_TAG.data()] = rhs.GetCustomUpdateShaderAssetID(); return node; } @@ -580,6 +582,18 @@ namespace YAML rhs.SetTextureIndex(texture->TextureArrayIndex); rhs.SetTextureAssetID(id); } + + if (node[CUSTOM_UPDATE_SHADER_ASSET_ID_TAG.data()].IsDefined()) + { + AssetID id = node[CUSTOM_UPDATE_SHADER_ASSET_ID_TAG.data()].as(); + + Handle shaderModule = SHResourceManager::LoadOrGet(id); + SHResourceManager::FinaliseChanges(); + //gfxSystem->BuildTextures(); + + rhs.SetCustomUpdateShader(shaderModule); + rhs.SetTextureAssetID(id); + } return true; }