From 97d1dda4b50f27c8ad1f92d35406c4cfbda2c5f9 Mon Sep 17 00:00:00 2001 From: seungho baek Date: Wed, 28 Jun 2023 22:23:07 +0900 Subject: [PATCH] Apply Light using Shader Manager Change-Id: I007842c1e6e1c46dc680acbb1306c70d1defb148 Signed-off-by: seungho baek --- .../src/dali-scene3d/utc-Dali-Light.cpp | 128 +++++++++++---------- .../src/dali-scene3d/utc-Dali-ShaderManager.cpp | 39 ++++++- dali-scene3d/internal/common/light-observer.h | 15 --- .../internal/controls/model/model-impl.cpp | 82 ------------- dali-scene3d/internal/controls/model/model-impl.h | 13 --- .../controls/scene-view/scene-view-impl.cpp | 123 ++------------------ .../internal/controls/scene-view/scene-view-impl.h | 18 --- .../internal/model-components/material-impl.cpp | 15 --- .../internal/model-components/model-node-impl.cpp | 37 ------ .../internal/model-components/model-node-impl.h | 18 --- .../model-components/model-primitive-impl.cpp | 56 --------- .../model-components/model-primitive-impl.h | 19 --- dali-scene3d/public-api/loader/node-definition.cpp | 15 --- dali-scene3d/public-api/loader/shader-manager.cpp | 109 +++++++++++++++++- dali-scene3d/public-api/loader/shader-manager.h | 39 +++++++ 15 files changed, 263 insertions(+), 463 deletions(-) diff --git a/automated-tests/src/dali-scene3d/utc-Dali-Light.cpp b/automated-tests/src/dali-scene3d/utc-Dali-Light.cpp index 2ac5bcb..01bb4b0 100644 --- a/automated-tests/src/dali-scene3d/utc-Dali-Light.cpp +++ b/automated-tests/src/dali-scene3d/utc-Dali-Light.cpp @@ -24,6 +24,7 @@ #include #include #include +#include #include using namespace Dali; @@ -212,7 +213,7 @@ int UtcDaliLightOnScene01(void) int UtcDaliLightAdd01(void) { ToolkitTestApplication application; - Scene3D::SceneView sceneView = Scene3D::SceneView::New(); + Scene3D::SceneView sceneView = Scene3D::SceneView::New(); sceneView.SetProperty(Dali::Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER); sceneView.SetProperty(Dali::Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER); sceneView.SetProperty(Dali::Actor::Property::WIDTH_RESIZE_POLICY, ResizePolicy::FILL_TO_PARENT); @@ -238,20 +239,22 @@ int UtcDaliLightAdd01(void) Renderer renderer = model.FindChildByName("node2").GetRendererAt(0u); DALI_TEST_CHECK(renderer); + Shader shader = renderer.GetShader(); + DALI_TEST_CHECK(shader); DALI_TEST_EQUALS(1u, sceneView.GetActivatedLightCount(), TEST_LOCATION); - auto countPropertyIndex = renderer.GetPropertyIndex("uLightCount"); - DALI_TEST_CHECK(countPropertyIndex != DALI_KEY_INVALID); - DALI_TEST_EQUALS(1, renderer.GetProperty(countPropertyIndex), TEST_LOCATION); - auto colorPropertyIndex = renderer.GetPropertyIndex("uLightColor[0]"); - DALI_TEST_EQUALS(Vector3(0.0f, 0.0f, 1.0f), renderer.GetCurrentProperty(colorPropertyIndex), 0.01f, TEST_LOCATION); - auto directionPropertyIndex = renderer.GetPropertyIndex("uLightDirection[0]"); - DALI_TEST_EQUALS(Vector3(1.0f, 0.0f, 0.0f), renderer.GetCurrentProperty(directionPropertyIndex), 0.01f, TEST_LOCATION); + auto countPropertyIndex = shader.GetPropertyIndex("uLightCount"); + DALI_TEST_CHECK(countPropertyIndex != Dali::Property::INVALID_INDEX); + DALI_TEST_EQUALS(1, shader.GetProperty(countPropertyIndex), TEST_LOCATION); + auto colorPropertyIndex = shader.GetPropertyIndex("uLightColor[0]"); + DALI_TEST_EQUALS(Vector3(0.0f, 0.0f, 1.0f), shader.GetCurrentProperty(colorPropertyIndex), 0.01f, TEST_LOCATION); + auto directionPropertyIndex = shader.GetPropertyIndex("uLightDirection[0]"); + DALI_TEST_EQUALS(Vector3(1.0f, 0.0f, 0.0f), shader.GetCurrentProperty(directionPropertyIndex), 0.01f, TEST_LOCATION); light.Enable(false); DALI_TEST_EQUALS(0u, sceneView.GetActivatedLightCount(), TEST_LOCATION); - DALI_TEST_EQUALS(0, renderer.GetProperty(countPropertyIndex), TEST_LOCATION); + DALI_TEST_EQUALS(0, shader.GetProperty(countPropertyIndex), TEST_LOCATION); END_TEST; } @@ -290,20 +293,22 @@ int UtcDaliLightAdd02(void) Renderer renderer = model.FindChildByName("node2").GetRendererAt(0u); DALI_TEST_CHECK(renderer); + Shader shader = renderer.GetShader(); + DALI_TEST_CHECK(shader); DALI_TEST_EQUALS(1u, sceneView.GetActivatedLightCount(), TEST_LOCATION); - auto countPropertyIndex = renderer.GetPropertyIndex("uLightCount"); + auto countPropertyIndex = shader.GetPropertyIndex("uLightCount"); DALI_TEST_CHECK(countPropertyIndex != DALI_KEY_INVALID); - DALI_TEST_EQUALS(1, renderer.GetProperty(countPropertyIndex), TEST_LOCATION); - auto colorPropertyIndex = renderer.GetPropertyIndex("uLightColor[0]"); - DALI_TEST_EQUALS(Vector3(0.0f, 0.0f, 1.0f), renderer.GetCurrentProperty(colorPropertyIndex), 0.01f, TEST_LOCATION); - auto directionPropertyIndex = renderer.GetPropertyIndex("uLightDirection[0]"); - DALI_TEST_EQUALS(Vector3(1.0f, 0.0f, 0.0f), renderer.GetCurrentProperty(directionPropertyIndex), 0.01f, TEST_LOCATION); + DALI_TEST_EQUALS(1, shader.GetProperty(countPropertyIndex), TEST_LOCATION); + auto colorPropertyIndex = shader.GetPropertyIndex("uLightColor[0]"); + DALI_TEST_EQUALS(Vector3(0.0f, 0.0f, 1.0f), shader.GetCurrentProperty(colorPropertyIndex), 0.01f, TEST_LOCATION); + auto directionPropertyIndex = shader.GetPropertyIndex("uLightDirection[0]"); + DALI_TEST_EQUALS(Vector3(1.0f, 0.0f, 0.0f), shader.GetCurrentProperty(directionPropertyIndex), 0.01f, TEST_LOCATION); light.Enable(false); DALI_TEST_EQUALS(0u, sceneView.GetActivatedLightCount(), TEST_LOCATION); - DALI_TEST_EQUALS(0, renderer.GetProperty(countPropertyIndex), TEST_LOCATION); + DALI_TEST_EQUALS(0, shader.GetProperty(countPropertyIndex), TEST_LOCATION); END_TEST; } @@ -340,11 +345,13 @@ int UtcDaliLightAdd03(void) Renderer renderer = model.FindChildByName("node2").GetRendererAt(0u); DALI_TEST_CHECK(renderer); + Shader shader = renderer.GetShader(); + DALI_TEST_CHECK(shader); DALI_TEST_EQUALS(0u, sceneView.GetActivatedLightCount(), TEST_LOCATION); - auto countPropertyIndex = renderer.GetPropertyIndex("uLightCount"); + auto countPropertyIndex = shader.GetPropertyIndex("uLightCount"); DALI_TEST_CHECK(countPropertyIndex != DALI_KEY_INVALID); - DALI_TEST_EQUALS(0, renderer.GetProperty(countPropertyIndex), TEST_LOCATION); + DALI_TEST_EQUALS(0, shader.GetProperty(countPropertyIndex), TEST_LOCATION); light.Enable(true); @@ -352,11 +359,11 @@ int UtcDaliLightAdd03(void) application.Render(); DALI_TEST_EQUALS(1u, sceneView.GetActivatedLightCount(), TEST_LOCATION); - DALI_TEST_EQUALS(1, renderer.GetProperty(countPropertyIndex), TEST_LOCATION); - auto colorPropertyIndex = renderer.GetPropertyIndex("uLightColor[0]"); - DALI_TEST_EQUALS(Vector3(0.0f, 0.0f, 1.0f), renderer.GetCurrentProperty(colorPropertyIndex), 0.01f, TEST_LOCATION); - auto directionPropertyIndex = renderer.GetPropertyIndex("uLightDirection[0]"); - DALI_TEST_EQUALS(Vector3(1.0f, 0.0f, 0.0f), renderer.GetCurrentProperty(directionPropertyIndex), 0.01f, TEST_LOCATION); + DALI_TEST_EQUALS(1, shader.GetProperty(countPropertyIndex), TEST_LOCATION); + auto colorPropertyIndex = shader.GetPropertyIndex("uLightColor[0]"); + DALI_TEST_EQUALS(Vector3(0.0f, 0.0f, 1.0f), shader.GetCurrentProperty(colorPropertyIndex), 0.01f, TEST_LOCATION); + auto directionPropertyIndex = shader.GetPropertyIndex("uLightDirection[0]"); + DALI_TEST_EQUALS(Vector3(1.0f, 0.0f, 0.0f), shader.GetCurrentProperty(directionPropertyIndex), 0.01f, TEST_LOCATION); light.Enable(false); @@ -364,7 +371,7 @@ int UtcDaliLightAdd03(void) application.Render(); DALI_TEST_EQUALS(0u, sceneView.GetActivatedLightCount(), TEST_LOCATION); - DALI_TEST_EQUALS(0, renderer.GetProperty(countPropertyIndex), TEST_LOCATION); + DALI_TEST_EQUALS(0, shader.GetProperty(countPropertyIndex), TEST_LOCATION); END_TEST; } @@ -404,19 +411,21 @@ int UtcDaliLightAdd04(void) Renderer renderer = model.FindChildByName("node2").GetRendererAt(0u); DALI_TEST_CHECK(renderer); + Shader shader = renderer.GetShader(); + DALI_TEST_CHECK(shader); DALI_TEST_EQUALS(2u, sceneView.GetActivatedLightCount(), TEST_LOCATION); - auto countPropertyIndex = renderer.GetPropertyIndex("uLightCount"); + auto countPropertyIndex = shader.GetPropertyIndex("uLightCount"); DALI_TEST_CHECK(countPropertyIndex != DALI_KEY_INVALID); - DALI_TEST_EQUALS(2, renderer.GetProperty(countPropertyIndex), TEST_LOCATION); - auto colorPropertyIndex1 = renderer.GetPropertyIndex("uLightColor[0]"); - DALI_TEST_EQUALS(Vector3(0.0f, 0.0f, 1.0f), renderer.GetCurrentProperty(colorPropertyIndex1), 0.01f, TEST_LOCATION); - auto directionPropertyIndex1 = renderer.GetPropertyIndex("uLightDirection[0]"); - DALI_TEST_EQUALS(Vector3(1.0f, 0.0f, 0.0f), renderer.GetCurrentProperty(directionPropertyIndex1), 0.01f, TEST_LOCATION); - auto colorPropertyIndex2 = renderer.GetPropertyIndex("uLightColor[1]"); - DALI_TEST_EQUALS(Vector3(1.0f, 0.0f, 0.0f), renderer.GetCurrentProperty(colorPropertyIndex2), 0.01f, TEST_LOCATION); - auto directionPropertyIndex2 = renderer.GetPropertyIndex("uLightDirection[1]"); - DALI_TEST_EQUALS(Vector3(0.0f, 0.0f, -1.0f), renderer.GetCurrentProperty(directionPropertyIndex2), 0.01f, TEST_LOCATION); + DALI_TEST_EQUALS(2, shader.GetProperty(countPropertyIndex), TEST_LOCATION); + auto colorPropertyIndex1 = shader.GetPropertyIndex("uLightColor[0]"); + DALI_TEST_EQUALS(Vector3(0.0f, 0.0f, 1.0f), shader.GetCurrentProperty(colorPropertyIndex1), 0.01f, TEST_LOCATION); + auto directionPropertyIndex1 = shader.GetPropertyIndex("uLightDirection[0]"); + DALI_TEST_EQUALS(Vector3(1.0f, 0.0f, 0.0f), shader.GetCurrentProperty(directionPropertyIndex1), 0.01f, TEST_LOCATION); + auto colorPropertyIndex2 = shader.GetPropertyIndex("uLightColor[1]"); + DALI_TEST_EQUALS(Vector3(1.0f, 0.0f, 0.0f), shader.GetCurrentProperty(colorPropertyIndex2), 0.01f, TEST_LOCATION); + auto directionPropertyIndex2 = shader.GetPropertyIndex("uLightDirection[1]"); + DALI_TEST_EQUALS(Vector3(0.0f, 0.0f, -1.0f), shader.GetCurrentProperty(directionPropertyIndex2), 0.01f, TEST_LOCATION); light1.Enable(false); @@ -424,11 +433,11 @@ int UtcDaliLightAdd04(void) application.Render(); DALI_TEST_EQUALS(1u, sceneView.GetActivatedLightCount(), TEST_LOCATION); - DALI_TEST_EQUALS(1, renderer.GetProperty(countPropertyIndex), TEST_LOCATION); + DALI_TEST_EQUALS(1, shader.GetProperty(countPropertyIndex), TEST_LOCATION); // After light1 is disable, shader uniforms of lights are reordered. - DALI_TEST_EQUALS(Vector3(1.0f, 0.0f, 0.0f), renderer.GetCurrentProperty(colorPropertyIndex1), 0.01f, TEST_LOCATION); - DALI_TEST_EQUALS(Vector3(0.0f, 0.0f, -1.0f), renderer.GetCurrentProperty(directionPropertyIndex1), 0.01f, TEST_LOCATION); + DALI_TEST_EQUALS(Vector3(1.0f, 0.0f, 0.0f), shader.GetCurrentProperty(colorPropertyIndex1), 0.01f, TEST_LOCATION); + DALI_TEST_EQUALS(Vector3(0.0f, 0.0f, -1.0f), shader.GetCurrentProperty(directionPropertyIndex1), 0.01f, TEST_LOCATION); END_TEST; } @@ -476,23 +485,22 @@ int UtcDaliLightAdd05(void) Renderer renderer = model.FindChildByName("node2").GetRendererAt(0u); DALI_TEST_CHECK(renderer); - - std::vector enableAnswers; - enableAnswers.assign(maxLightCount, 1); + Shader shader = renderer.GetShader(); + DALI_TEST_CHECK(shader); DALI_TEST_EQUALS(maxLightCount, sceneView.GetActivatedLightCount(), TEST_LOCATION); - auto countPropertyIndex = renderer.GetPropertyIndex("uLightCount"); + auto countPropertyIndex = shader.GetPropertyIndex("uLightCount"); DALI_TEST_CHECK(countPropertyIndex != DALI_KEY_INVALID); - DALI_TEST_EQUALS(static_cast(maxLightCount), renderer.GetProperty(countPropertyIndex), TEST_LOCATION); + DALI_TEST_EQUALS(static_cast(maxLightCount), shader.GetProperty(countPropertyIndex), TEST_LOCATION); for(uint32_t i = 0; i < maxLightCount; ++i) { std::string colorStringKey = std::string("uLightColor[") + std::to_string(i) + "]"; - auto colorPropertyIndex = renderer.GetPropertyIndex(colorStringKey); - DALI_TEST_EQUALS(Vector3(0.0f, 0.0f, 1.0f), renderer.GetCurrentProperty(colorPropertyIndex), 0.01f, TEST_LOCATION); + auto colorPropertyIndex = shader.GetPropertyIndex(colorStringKey); + DALI_TEST_EQUALS(Vector3(0.0f, 0.0f, 1.0f), shader.GetCurrentProperty(colorPropertyIndex), 0.01f, TEST_LOCATION); std::string directionStringKey = std::string("uLightDirection[") + std::to_string(i) + "]"; - auto directionPropertyIndex = renderer.GetPropertyIndex(directionStringKey); - DALI_TEST_EQUALS(Vector3(1.0f, 0.0f, 0.0f), renderer.GetCurrentProperty(directionPropertyIndex), 0.01f, TEST_LOCATION); + auto directionPropertyIndex = shader.GetPropertyIndex(directionStringKey); + DALI_TEST_EQUALS(Vector3(1.0f, 0.0f, 0.0f), shader.GetCurrentProperty(directionPropertyIndex), 0.01f, TEST_LOCATION); } lightList[2].Enable(false); @@ -501,11 +509,12 @@ int UtcDaliLightAdd05(void) application.Render(); DALI_TEST_EQUALS(maxLightCount, sceneView.GetActivatedLightCount(), TEST_LOCATION); - DALI_TEST_EQUALS(static_cast(maxLightCount), renderer.GetProperty(countPropertyIndex), TEST_LOCATION); + DALI_TEST_EQUALS(static_cast(maxLightCount), shader.GetProperty(countPropertyIndex), TEST_LOCATION); for(uint32_t i = 0; i < maxLightCount; ++i) { + tet_printf("i : %d\n", i); Vector3 color, direction; - if(i == 2) + if(i == maxLightCount - 1) { color = Vector3(1.0f, 0.0f, 0.0f); direction = Vector3(0.0f, 0.0f, -1.0f); @@ -516,12 +525,12 @@ int UtcDaliLightAdd05(void) direction = Vector3(1.0f, 0.0f, 0.0f); } std::string colorStringKey = std::string("uLightColor[") + std::to_string(i) + "]"; - auto colorPropertyIndex = renderer.GetPropertyIndex(colorStringKey); - DALI_TEST_EQUALS(color, renderer.GetCurrentProperty(colorPropertyIndex), 0.01f, TEST_LOCATION); + auto colorPropertyIndex = shader.GetPropertyIndex(colorStringKey); + DALI_TEST_EQUALS(color, shader.GetCurrentProperty(colorPropertyIndex), 0.01f, TEST_LOCATION); std::string directionStringKey = std::string("uLightDirection[") + std::to_string(i) + "]"; - auto directionPropertyIndex = renderer.GetPropertyIndex(directionStringKey); - DALI_TEST_EQUALS(direction, renderer.GetCurrentProperty(directionPropertyIndex), 0.01f, TEST_LOCATION); + auto directionPropertyIndex = shader.GetPropertyIndex(directionStringKey); + DALI_TEST_EQUALS(direction, shader.GetCurrentProperty(directionPropertyIndex), 0.01f, TEST_LOCATION); } for(uint32_t i = 0; i < lightList.size(); ++i) @@ -569,23 +578,24 @@ int UtcDaliLightModelAddAndRemove(void) Renderer renderer = model.FindChildByName("node2").GetRendererAt(0u); DALI_TEST_CHECK(renderer); + Shader shader = renderer.GetShader(); + DALI_TEST_CHECK(shader); DALI_TEST_EQUALS(1u, sceneView.GetActivatedLightCount(), TEST_LOCATION); - auto countPropertyIndex = renderer.GetPropertyIndex("uLightCount"); + auto countPropertyIndex = shader.GetPropertyIndex("uLightCount"); DALI_TEST_CHECK(countPropertyIndex != DALI_KEY_INVALID); - DALI_TEST_EQUALS(1, renderer.GetProperty(countPropertyIndex), TEST_LOCATION); + DALI_TEST_EQUALS(1, shader.GetProperty(countPropertyIndex), TEST_LOCATION); - auto colorPropertyIndex = renderer.GetPropertyIndex("uLightColor[0]"); - DALI_TEST_EQUALS(Vector3(0.0f, 0.0f, 1.0f), renderer.GetCurrentProperty(colorPropertyIndex), 0.01f, TEST_LOCATION); - auto directionPropertyIndex = renderer.GetPropertyIndex("uLightDirection[0]"); - DALI_TEST_EQUALS(Vector3(1.0f, 0.0f, 0.0f), renderer.GetCurrentProperty(directionPropertyIndex), 0.01f, TEST_LOCATION); + auto colorPropertyIndex = shader.GetPropertyIndex("uLightColor[0]"); + DALI_TEST_EQUALS(Vector3(0.0f, 0.0f, 1.0f), shader.GetCurrentProperty(colorPropertyIndex), 0.01f, TEST_LOCATION); + auto directionPropertyIndex = shader.GetPropertyIndex("uLightDirection[0]"); + DALI_TEST_EQUALS(Vector3(1.0f, 0.0f, 0.0f), shader.GetCurrentProperty(directionPropertyIndex), 0.01f, TEST_LOCATION); DALI_TEST_EQUALS(1u, sceneView.GetActivatedLightCount(), TEST_LOCATION); model.Unparent(); DALI_TEST_EQUALS(1u, sceneView.GetActivatedLightCount(), TEST_LOCATION); - DALI_TEST_EQUALS(0, renderer.GetProperty(countPropertyIndex), TEST_LOCATION); END_TEST; } diff --git a/automated-tests/src/dali-scene3d/utc-Dali-ShaderManager.cpp b/automated-tests/src/dali-scene3d/utc-Dali-ShaderManager.cpp index b8c3b35..f272ba9 100644 --- a/automated-tests/src/dali-scene3d/utc-Dali-ShaderManager.cpp +++ b/automated-tests/src/dali-scene3d/utc-Dali-ShaderManager.cpp @@ -60,7 +60,7 @@ struct PermutationSet } // namespace -int UtcDaliShaderDefinitionFactoryProduceShader(void) +int UtcDaliShaderManagerProduceShader(void) { Context ctx; ctx.resources.mMaterials.push_back({}); @@ -282,4 +282,39 @@ int UtcDaliShaderDefinitionFactoryProduceShader(void) } END_TEST; -} \ No newline at end of file +} + +int UtcDaliShaderManagerAddAndRemoveLights(void) +{ + TestApplication app; + ShaderManager shaderManager; + + ShaderOption option1; + option1.AddOption(ShaderOption::Type::THREE_TEXTURE); + Dali::Shader shader1 = shaderManager.ProduceShader(option1); + + ShaderOption option2; + option2.AddOption(ShaderOption::Type::BASE_COLOR_TEXTURE); + Dali::Shader shader2 = shaderManager.ProduceShader(option2); + + DALI_TEST_NOT_EQUALS(shader1, shader2, 0.1f, TEST_LOCATION); + DALI_TEST_EQUALS(shader1.GetProperty(shader1.GetPropertyIndex("uLightCount")), 0, TEST_LOCATION); + DALI_TEST_EQUALS(shader2.GetProperty(shader2.GetPropertyIndex("uLightCount")), 0, TEST_LOCATION); + + Scene3D::Light light = Scene3D::Light::New(); + shaderManager.AddLight(light); + + DALI_TEST_EQUALS(shader1.GetProperty(shader1.GetPropertyIndex("uLightCount")), 1, TEST_LOCATION); + DALI_TEST_EQUALS(shader2.GetProperty(shader2.GetPropertyIndex("uLightCount")), 1, TEST_LOCATION); + + ShaderOption option3; + option3.AddOption(ShaderOption::Type::METALLIC_ROUGHNESS_TEXTURE); + Dali::Shader shader3 = shaderManager.ProduceShader(option3); + + DALI_TEST_NOT_EQUALS(shader1, shader3, 0.1f, TEST_LOCATION); + DALI_TEST_NOT_EQUALS(shader2, shader3, 0.1f, TEST_LOCATION); + + DALI_TEST_EQUALS(shader3.GetProperty(shader3.GetPropertyIndex("uLightCount")), 1, TEST_LOCATION); + + END_TEST; +} diff --git a/dali-scene3d/internal/common/light-observer.h b/dali-scene3d/internal/common/light-observer.h index c54c73b..608e100 100644 --- a/dali-scene3d/internal/common/light-observer.h +++ b/dali-scene3d/internal/common/light-observer.h @@ -60,21 +60,6 @@ public: * @param[in] scaleFactor scale factor that controls light source intensity in [0.0f, 1.0f]. */ virtual void NotifyImageBasedLightScaleFactor(float scaleFactor) = 0; - - /** - * @brief Notifies new light is added on SceneView - * - * @param[in] lightIndex Index of added light. - * @param[in] light Added light. - */ - virtual void NotifyLightAdded(uint32_t lightIndex, Scene3D::Light light) = 0; - - /** - * @brief Notifies a light is removed from SceneView - * - * @param[in] lightIndex Index of light to be removed. - */ - virtual void NotifyLightRemoved(uint32_t lightIndex) = 0; }; } // namespace Internal diff --git a/dali-scene3d/internal/controls/model/model-impl.cpp b/dali-scene3d/internal/controls/model/model-impl.cpp index 7a6662d..1feb6b5 100644 --- a/dali-scene3d/internal/controls/model/model-impl.cpp +++ b/dali-scene3d/internal/controls/model/model-impl.cpp @@ -172,45 +172,6 @@ void AddModelTreeToAABB(BoundingVolume& AABB, const Dali::Scene3D::Loader::Scene } } -void AddLightRecursively(Scene3D::ModelNode node, Scene3D::Light light, uint32_t lightIndex) -{ - if(!node) - { - return; - } - GetImplementation(node).AddLight(light, lightIndex); - - uint32_t childrenCount = node.GetChildCount(); - for(uint32_t i = 0; i < childrenCount; ++i) - { - Scene3D::ModelNode childNode = Scene3D::ModelNode::DownCast(node.GetChildAt(i)); - if(childNode) - { - AddLightRecursively(childNode, light, lightIndex); - } - } -} - -void RemoveLightRecursively(Scene3D::ModelNode node, uint32_t lightIndex) -{ - if(!node) - { - return; - } - - GetImplementation(node).RemoveLight(lightIndex); - - uint32_t childrenCount = node.GetChildCount(); - for(uint32_t i = 0; i < childrenCount; ++i) - { - Scene3D::ModelNode childNode = Scene3D::ModelNode::DownCast(node.GetChildAt(i)); - if(childNode) - { - RemoveLightRecursively(childNode, lightIndex); - } - } -} - void UpdateBlendShapeNodeMapRecursively(Model::BlendShapeModelNodeMap& resultMap, const Scene3D::ModelNode& node) { if(!node) @@ -325,15 +286,6 @@ void Model::AddModelNode(Scene3D::ModelNode modelNode) UpdateImageBasedLightScaleFactor(); } - uint32_t maxLightCount = Scene3D::Internal::Light::GetMaximumEnabledLightCount(); - for(uint32_t i = 0; i < maxLightCount; ++i) - { - if(mLights[i]) - { - AddLightRecursively(modelNode, mLights[i], i); - } - } - if(Self().GetProperty(Dali::Actor::Property::CONNECTED_TO_SCENE)) { NotifyResourceReady(); @@ -345,15 +297,6 @@ void Model::RemoveModelNode(Scene3D::ModelNode modelNode) if(mModelRoot) { UpdateShaderRecursively(modelNode, nullptr); - - uint32_t maxLightCount = Scene3D::Internal::Light::GetMaximumEnabledLightCount(); - for(uint32_t i = 0; i < maxLightCount; ++i) - { - if(mLights[i]) - { - RemoveLightRecursively(modelNode, i); - } - } mModelRoot.Remove(modelNode); } } @@ -794,7 +737,6 @@ void Model::OnInitialize() { // Make ParentOrigin as Center. Self().SetProperty(Dali::Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER); - mLights.resize(Scene3D::Internal::Light::GetMaximumEnabledLightCount()); } void Model::OnSceneConnection(int depth) @@ -1090,21 +1032,6 @@ void Model::NotifyImageBasedLightScaleFactor(float scaleFactor) } } -void Model::NotifyLightAdded(uint32_t lightIndex, Scene3D::Light light) -{ - mLights[lightIndex] = light; - AddLightRecursively(mModelRoot, light, lightIndex); -} - -void Model::NotifyLightRemoved(uint32_t lightIndex) -{ - if(mLights[lightIndex]) - { - RemoveLightRecursively(mModelRoot, lightIndex); - mLights[lightIndex].Reset(); - } -} - void Model::OnModelLoadComplete() { if(!mModelLoadTask->HasSucceeded()) @@ -1135,15 +1062,6 @@ void Model::OnModelLoadComplete() mDefaultSpecularTexture = resources.mEnvironmentMaps.front().second.mSpecular; } - uint32_t maxLightCount = Scene3D::Internal::Light::GetMaximumEnabledLightCount(); - for(uint32_t i = 0; i < maxLightCount; ++i) - { - if(mLights[i]) - { - AddLightRecursively(mModelRoot, mLights[i], i); - } - } - UpdateImageBasedLightTexture(); UpdateImageBasedLightScaleFactor(); Self().SetProperty(Dali::Actor::Property::ANCHOR_POINT, Vector3(mModelPivot.x, 1.0f - mModelPivot.y, mModelPivot.z)); diff --git a/dali-scene3d/internal/controls/model/model-impl.h b/dali-scene3d/internal/controls/model/model-impl.h index cb64cac..23337a4 100644 --- a/dali-scene3d/internal/controls/model/model-impl.h +++ b/dali-scene3d/internal/controls/model/model-impl.h @@ -281,16 +281,6 @@ public: // Overrides LightObserver Methods. */ void NotifyImageBasedLightScaleFactor(float scaleFactor) override; - /** - * @copydoc Dali::Scene3D::Internal::LightObserver::NotifyLightAdded() - */ - void NotifyLightAdded(uint32_t lightIndex, Scene3D::Light light) override; - - /** - * @copydoc Dali::Scene3D::Internal::LightObserver::NotifyLightRemoved() - */ - void NotifyLightRemoved(uint32_t lightIndex) override; - private: /** * @brief Asynchronously model loading finished. @@ -363,9 +353,6 @@ private: Dali::Scene3D::Loader::ShaderManagerPtr mShaderManager; - // Light - std::vector mLights; - // List of ModelNode for name of blend shape. BlendShapeModelNodeMap mBlendShapeModelNodeMap; diff --git a/dali-scene3d/internal/controls/scene-view/scene-view-impl.cpp b/dali-scene3d/internal/controls/scene-view/scene-view-impl.cpp index 8b18809..b4bd19d 100644 --- a/dali-scene3d/internal/controls/scene-view/scene-view-impl.cpp +++ b/dali-scene3d/internal/controls/scene-view/scene-view-impl.cpp @@ -41,6 +41,8 @@ #include #include +#include + using namespace Dali; namespace Dali @@ -281,18 +283,6 @@ void SceneView::RegisterSceneItem(Scene3D::Internal::LightObserver* item) if(item) { item->NotifyImageBasedLightTexture(mDiffuseTexture, mSpecularTexture, mIblScaleFactor, mSpecularMipmapLevels); - - if(mActivatedLightCount > 0) - { - uint32_t maxLightCount = Scene3D::Internal::Light::GetMaximumEnabledLightCount(); - for(uint32_t i = 0; i < maxLightCount; ++i) - { - if(mActivatedLights[i]) - { - item->NotifyLightAdded(i, mActivatedLights[i]); - } - } - } mItems.push_back(item); } } @@ -305,17 +295,6 @@ void SceneView::UnregisterSceneItem(Scene3D::Internal::LightObserver* item) { if(mItems[i] == item) { - if(mActivatedLightCount > 0) - { - uint32_t maxLightCount = Scene3D::Internal::Light::GetMaximumEnabledLightCount(); - for(uint32_t i = 0; i < maxLightCount; ++i) - { - if(mActivatedLights[i]) - { - item->NotifyLightRemoved(i); - } - } - } mItems.erase(mItems.begin() + i); break; } @@ -440,60 +419,40 @@ float SceneView::GetImageBasedLightScaleFactor() const void SceneView::AddLight(Scene3D::Light light) { - bool enabled = AddLightInternal(light); + bool enabled = mShaderManager->AddLight(light); mLights.push_back(std::make_pair(light, enabled)); } void SceneView::RemoveLight(Scene3D::Light light) { - if(mActivatedLights.empty()) - { - return; - } - - bool needToDisable = false; + mShaderManager->RemoveLight(light); for(uint32_t i = 0; i < mLights.size(); ++i) { if(mLights[i].first == light) { - // If mLights[i].second is true, it means the light is currently activated in Scene. - // Then it should be removed from mActivatedLights list too. - needToDisable = mLights[i].second; mLights.erase(mLights.begin() + i); break; } } - uint32_t maxNumberOfLight = Scene3D::Internal::Light::GetMaximumEnabledLightCount(); - if(needToDisable) + if(mLights.size() > mShaderManager->GetLightCount()) { - uint32_t removedIndex = RemoveLightInternal(light); - if(mActivatedLightCount < maxNumberOfLight && mLights.size() >= maxNumberOfLight) + for(auto && waitingLight : mLights) { - for(auto&& lightItem : mLights) + if(waitingLight.second) { - if(lightItem.second == false) - { - lightItem.second = AddLightInternal(lightItem.first); - break; - } + continue; } - } - // To remove empty entity of mActivatedLights, moves last object to empty position. - // Because one Light is removed, mActivatedLights[mActivatedLightCount] is current last object of the list. - if(!mActivatedLights[removedIndex] && mActivatedLightCount > 0 && removedIndex < mActivatedLightCount) - { - Scene3D::Light reorderingLight = mActivatedLights[mActivatedLightCount]; - RemoveLightInternal(reorderingLight); - AddLightInternal(reorderingLight); + waitingLight.second = mShaderManager->AddLight(waitingLight.first); + break; } } } uint32_t SceneView::GetActivatedLightCount() const { - return mActivatedLightCount; + return mShaderManager->GetLightCount(); } void SceneView::UseFramebuffer(bool useFramebuffer) @@ -959,66 +918,6 @@ void SceneView::NotifyImageBasedLightTextureChange() } } -bool SceneView::AddLightInternal(Scene3D::Light light) -{ - uint32_t maxNumberOfLight = Scene3D::Internal::Light::GetMaximumEnabledLightCount(); - if(mActivatedLightCount == 0) - { - mActivatedLights.resize(maxNumberOfLight); - } - - bool enabled = false; - if(mActivatedLightCount < maxNumberOfLight) - { - uint32_t newLightIndex = 0u; - for(; newLightIndex < maxNumberOfLight; ++newLightIndex) - { - if(!mActivatedLights[newLightIndex]) - { - mActivatedLights[newLightIndex] = light; - break; - } - } - - for(auto&& item : mItems) - { - if(item) - { - item->NotifyLightAdded(newLightIndex, light); - } - } - - mActivatedLightCount++; - enabled = true; - } - return enabled; -} - -int32_t SceneView::RemoveLightInternal(Scene3D::Light light) -{ - int32_t removedIndex = INVALID_INDEX; - uint32_t maxNumberOfLight = Scene3D::Internal::Light::GetMaximumEnabledLightCount(); - for(uint32_t i = 0; i < maxNumberOfLight; ++i) - { - if(mActivatedLights[i] == light) - { - for(auto&& item : mItems) - { - if(item) - { - item->NotifyLightRemoved(i); - } - } - mActivatedLights[i].Reset(); - mActivatedLightCount--; - removedIndex = i; - break; - } - } - - return removedIndex; -} - } // namespace Internal } // namespace Scene3D } // namespace Dali diff --git a/dali-scene3d/internal/controls/scene-view/scene-view-impl.h b/dali-scene3d/internal/controls/scene-view/scene-view-impl.h index 3ed2b2b..3f6eca7 100644 --- a/dali-scene3d/internal/controls/scene-view/scene-view-impl.h +++ b/dali-scene3d/internal/controls/scene-view/scene-view-impl.h @@ -322,22 +322,6 @@ private: */ void NotifyImageBasedLightTextureChange(); - /** - * @brief Internal method to add an Light object to this SceneView. - * - * @param[in] light Light object to be added. - * @return True if successed to enable. - */ - bool AddLightInternal(Scene3D::Light light); - - /** - * @brief Internal method to remove an Light object to this SceneView. - * - * @param[in] light Light object to be added. - * @return Index of removed light in enabled light list. If failed to remove it returns negative value; - */ - int32_t RemoveLightInternal(Scene3D::Light light); - private: Toolkit::Visual::Base mVisual; @@ -364,8 +348,6 @@ private: // Light std::vector> mLights; // Pair of Light object and flag that denotes the light is currently activated or not. - std::vector mActivatedLights; - uint32_t mActivatedLightCount{0u}; // Asynchronous Loading. EnvironmentMapLoadTaskPtr mSkyboxLoadTask; diff --git a/dali-scene3d/internal/model-components/material-impl.cpp b/dali-scene3d/internal/model-components/material-impl.cpp index 386fdd6..b7c16a7 100644 --- a/dali-scene3d/internal/model-components/material-impl.cpp +++ b/dali-scene3d/internal/model-components/material-impl.cpp @@ -690,21 +690,6 @@ void Material::SetRendererUniform(Dali::Renderer renderer) renderer.RegisterProperty(Scene3D::Loader::NodeDefinition::GetIblScaleFactorUniformName().data(), 1.0f); renderer.RegisterProperty(Scene3D::Loader::NodeDefinition::GetIblYDirectionUniformName().data(), Vector3(1.0f, -1.0, 1.0)); - std::string lightCountPropertyName(Scene3D::Internal::Light::GetLightCountUniformName()); - renderer.RegisterProperty(lightCountPropertyName, 0); - - uint32_t maxLightCount = Scene3D::Internal::Light::GetMaximumEnabledLightCount(); - for(uint32_t i = 0; i < maxLightCount; ++i) - { - std::string lightDirectionPropertyName(Scene3D::Internal::Light::GetLightDirectionUniformName()); - lightDirectionPropertyName += "[" + std::to_string(i) + "]"; - renderer.RegisterProperty(lightDirectionPropertyName, Vector3::ZAXIS); - - std::string lightColorPropertyName(Scene3D::Internal::Light::GetLightColorUniformName()); - lightColorPropertyName += "[" + std::to_string(i) + "]"; - renderer.RegisterProperty(lightColorPropertyName, Vector3(Color::WHITE)); - } - Scene3D::Loader::RendererState::Apply(mRendererState, renderer); } diff --git a/dali-scene3d/internal/model-components/model-node-impl.cpp b/dali-scene3d/internal/model-components/model-node-impl.cpp index 7d3d5e1..9d8d4b6 100644 --- a/dali-scene3d/internal/model-components/model-node-impl.cpp +++ b/dali-scene3d/internal/model-components/model-node-impl.cpp @@ -75,7 +75,6 @@ ModelNode::~ModelNode() void ModelNode::Initialize() { OnInitialize(); - mLights.resize(Scene3D::Internal::Light::GetMaximumEnabledLightCount()); } void ModelNode::OnInitialize() @@ -193,15 +192,6 @@ void ModelNode::AddModelPrimitive(Dali::Scene3D::ModelPrimitive modelPrimitive) GetImplementation(modelPrimitive).SetImageBasedLightTexture(mDiffuseTexture, mSpecularTexture, mIblScaleFactor, mSpecularMipmapLevels); } - uint32_t maxLightCount = Scene3D::Internal::Light::GetMaximumEnabledLightCount(); - for(uint32_t i = 0; i < maxLightCount; ++i) - { - if(mLights[i]) - { - GetImplementation(modelPrimitive).AddLight(mLights[i], i); - } - } - GetImplementation(modelPrimitive).UpdateShader(mShaderManager); Dali::Renderer renderer = GetImplementation(modelPrimitive).GetRenderer(); @@ -248,15 +238,6 @@ void ModelNode::RemoveModelPrimitive(uint32_t index) GetImplementation(mModelPrimitiveContainer[index]).UpdateShader(nullptr); - uint32_t maxLightCount = Scene3D::Internal::Light::GetMaximumEnabledLightCount(); - for(uint32_t i = 0; i < maxLightCount; ++i) - { - if(mLights[i]) - { - GetImplementation(mModelPrimitiveContainer[index]).RemoveLight(i); - } - } - Actor self = Self(); GetImplementation(mModelPrimitiveContainer[index]).RemovePrimitiveObserver(this); @@ -324,24 +305,6 @@ void ModelNode::SetImageBasedLightScaleFactor(float iblScaleFactor) } } -void ModelNode::AddLight(Scene3D::Light light, uint32_t lightIndex) -{ - mLights[lightIndex] = light; - for(auto&& primitive : mModelPrimitiveContainer) - { - GetImplementation(primitive).AddLight(light, lightIndex); - } -} - -void ModelNode::RemoveLight(uint32_t lightIndex) -{ - for(auto&& primitive : mModelPrimitiveContainer) - { - GetImplementation(primitive).RemoveLight(lightIndex); - } - mLights[lightIndex].Reset(); -} - void ModelNode::UpdateShader(Scene3D::Loader::ShaderManagerPtr shaderManager) { if(mShaderManager != shaderManager) diff --git a/dali-scene3d/internal/model-components/model-node-impl.h b/dali-scene3d/internal/model-components/model-node-impl.h index 1a56d60..2cc4f1a 100644 --- a/dali-scene3d/internal/model-components/model-node-impl.h +++ b/dali-scene3d/internal/model-components/model-node-impl.h @@ -248,21 +248,6 @@ public: // Public Method void SetImageBasedLightScaleFactor(float iblScaleFactor); /** - * @brief Adds new Light. - * - * @param[in] light Newly added light. - * @param[in] lightIndex index of this light. - */ - void AddLight(Scene3D::Light light, uint32_t lightIndex); - - /** - * @brief Removes new Light. - * - * @param[in] lightIndex index of light that will be removed. - */ - void RemoveLight(uint32_t lightIndex); - - /** * @brief Updates shaders by using current material * * @param[in] shaderManager Shader manager to create shader. @@ -319,9 +304,6 @@ private: Dali::Texture mDiffuseTexture; float mIblScaleFactor{1.0f}; uint32_t mSpecularMipmapLevels{1u}; - - // Light - std::vector mLights; /// @endcond }; diff --git a/dali-scene3d/internal/model-components/model-primitive-impl.cpp b/dali-scene3d/internal/model-components/model-primitive-impl.cpp index 870f956..0aea879 100644 --- a/dali-scene3d/internal/model-components/model-primitive-impl.cpp +++ b/dali-scene3d/internal/model-components/model-primitive-impl.cpp @@ -79,7 +79,6 @@ ModelPrimitive::~ModelPrimitive() void ModelPrimitive::Initialize() { - mLights.resize(Scene3D::Internal::Light::GetMaximumEnabledLightCount()); } void ModelPrimitive::SetRenderer(Dali::Renderer renderer) @@ -177,52 +176,6 @@ void ModelPrimitive::SetImageBasedLightScaleFactor(float iblScaleFactor) } } -void ModelPrimitive::AddLight(Scene3D::Light light, uint32_t lightIndex) -{ - if(mLights[lightIndex]) - { - RemoveLight(lightIndex); - } - - mLights[lightIndex] = light; - // TODO Remove light at lightIndex if it is already set. - if(mRenderer && mMaterial) - { - mLightCount++; - std::string lightCountPropertyName(Scene3D::Internal::Light::GetLightCountUniformName()); - mRenderer.RegisterProperty(lightCountPropertyName, mLightCount); - - std::string lightDirectionPropertyName(Scene3D::Internal::Light::GetLightDirectionUniformName()); - lightDirectionPropertyName += "[" + std::to_string(lightIndex) + "]"; - auto lightDirectionPropertyIndex = mRenderer.RegisterProperty(lightDirectionPropertyName, Vector3::ZAXIS); - Dali::Constraint lightDirectionConstraint = Dali::Constraint::New(mRenderer, lightDirectionPropertyIndex, [](Vector3& output, const PropertyInputContainer& inputs) - { output = inputs[0]->GetQuaternion().Rotate(Vector3::ZAXIS); }); - lightDirectionConstraint.AddSource(Source{light, Dali::Actor::Property::WORLD_ORIENTATION}); - lightDirectionConstraint.ApplyPost(); - lightDirectionConstraint.SetTag(INDEX_FOR_LIGHT_CONSTRAINT_TAG + lightIndex); - - std::string lightColorPropertyName(Scene3D::Internal::Light::GetLightColorUniformName()); - lightColorPropertyName += "[" + std::to_string(lightIndex) + "]"; - auto lightColorPropertyIndex = mRenderer.RegisterProperty(lightColorPropertyName, Vector3(Color::WHITE)); - Dali::Constraint lightColorConstraint = Dali::Constraint::New(mRenderer, lightColorPropertyIndex, [](Vector3& output, const PropertyInputContainer& inputs) - { output = Vector3(inputs[0]->GetVector4()); }); - lightColorConstraint.AddSource(Source{light, Dali::Actor::Property::COLOR}); - lightColorConstraint.ApplyPost(); - lightColorConstraint.SetTag(INDEX_FOR_LIGHT_CONSTRAINT_TAG + lightIndex); - } -} - -void ModelPrimitive::RemoveLight(uint32_t lightIndex) -{ - mLightCount--; - std::string lightCountPropertyName(Scene3D::Internal::Light::GetLightCountUniformName()); - mRenderer.RegisterProperty(lightCountPropertyName, mLightCount); - - mRenderer.RemoveConstraints(INDEX_FOR_LIGHT_CONSTRAINT_TAG + lightIndex); - - mLights[lightIndex].Reset(); -} - void ModelPrimitive::UpdateShader(Scene3D::Loader::ShaderManagerPtr shaderManager) { if(mShaderManager != shaderManager) @@ -398,15 +351,6 @@ void ModelPrimitive::CreateRenderer() mRenderer.SetTextures(mTextureSet); UpdateRendererUniform(); - uint32_t maxLightCount = Scene3D::Internal::Light::GetMaximumEnabledLightCount(); - for(uint32_t i = 0; i < maxLightCount; ++i) - { - if(mLights[i]) - { - AddLight(mLights[i], i); - } - } - for(auto* observer : mObservers) { observer->OnRendererCreated(mRenderer); diff --git a/dali-scene3d/internal/model-components/model-primitive-impl.h b/dali-scene3d/internal/model-components/model-primitive-impl.h index f37eba6..7334699 100644 --- a/dali-scene3d/internal/model-components/model-primitive-impl.h +++ b/dali-scene3d/internal/model-components/model-primitive-impl.h @@ -141,21 +141,6 @@ public: void SetImageBasedLightScaleFactor(float iblScaleFactor); /** - * @brief Adds a Light on this medel primitive - * - * @param[in] light Added light. - * @param[in] lightIndex Index of added light. - */ - void AddLight(Scene3D::Light light, uint32_t lightIndex); - - /** - * @brief Removes a light at the lightIndex of this model primitive - * - * @param[in] lightIndex Index of added light. - */ - void RemoveLight(uint32_t lightIndex); - - /** * @brief Updates shaders by using current material * * @param[in] shaderManager Shader manager to create shader. @@ -239,10 +224,6 @@ private: Scene3D::Loader::ShaderManagerPtr mShaderManager; - // Light - std::vector mLights; - int32_t mLightCount{0}; - // For IBL Dali::Texture mSpecularTexture; Dali::Texture mDiffuseTexture; diff --git a/dali-scene3d/public-api/loader/node-definition.cpp b/dali-scene3d/public-api/loader/node-definition.cpp index d3e8c36..f0a0eba 100644 --- a/dali-scene3d/public-api/loader/node-definition.cpp +++ b/dali-scene3d/public-api/loader/node-definition.cpp @@ -357,21 +357,6 @@ void ModelRenderable::OnCreate(const NodeDefinition& nodeDefinition, NodeDefinit renderer.RegisterProperty(IBL_INTENSITY_STRING.data(), resources.mEnvironmentMaps[envIndex].first.mIblIntensity); renderer.RegisterProperty(IBL_Y_DIRECTION.data(), resources.mEnvironmentMaps[envIndex].first.mYDirection); - std::string lightCountPropertyName(Scene3D::Internal::Light::GetLightCountUniformName()); - renderer.RegisterProperty(lightCountPropertyName, 0); - - uint32_t maxLightCount = Scene3D::Internal::Light::GetMaximumEnabledLightCount(); - for(uint32_t i = 0; i < maxLightCount; ++i) - { - std::string lightDirectionPropertyName(Scene3D::Internal::Light::GetLightDirectionUniformName()); - lightDirectionPropertyName += "[" + std::to_string(i) + "]"; - renderer.RegisterProperty(lightDirectionPropertyName, Vector3::ZAXIS); - - std::string lightColorPropertyName(Scene3D::Internal::Light::GetLightColorUniformName()); - lightColorPropertyName += "[" + std::to_string(i) + "]"; - renderer.RegisterProperty(lightColorPropertyName, Vector3(Color::WHITE)); - } - node.SetProperty(Actor::Property::COLOR, mColor); // If user uses customshader, the properties of the shader could not be changed by Material. diff --git a/dali-scene3d/public-api/loader/shader-manager.cpp b/dali-scene3d/public-api/loader/shader-manager.cpp index 0978f20..158598f 100644 --- a/dali-scene3d/public-api/loader/shader-manager.cpp +++ b/dali-scene3d/public-api/loader/shader-manager.cpp @@ -20,9 +20,11 @@ // EXTERNAL INCLUDES #include +#include #include // INTERNAL INCLUDES +#include #include #include #include @@ -31,6 +33,7 @@ namespace Dali::Scene3D::Loader { namespace { +static constexpr uint32_t INDEX_FOR_LIGHT_CONSTRAINT_TAG = 10; ShaderOption MakeOption(const MaterialDefinition& materialDef, const MeshDefinition& meshDef) { @@ -156,8 +159,9 @@ ShaderOption MakeOption(const MaterialDefinition& materialDef, const MeshDefinit struct ShaderManager::Impl { - std::map mShaderMap; - std::vector mShaders; + std::map mShaderMap; + std::vector mShaders; + std::vector mLights; }; ShaderManager::ShaderManager() @@ -197,6 +201,14 @@ Dali::Shader ShaderManager::ProduceShader(const ShaderOption& shaderOption) auto raw = shaderDef.LoadRaw(""); mImpl->mShaders.emplace_back(shaderDef.Load(std::move(raw))); result = mImpl->mShaders.back(); + + std::string lightCountPropertyName(Scene3D::Internal::Light::GetLightCountUniformName()); + result.RegisterProperty(lightCountPropertyName, static_cast(mImpl->mLights.size())); + + for(uint32_t index = 0; index < mImpl->mLights.size(); ++index) + { + SetLightConstraintToShader(index, result); + } } return result; @@ -220,4 +232,97 @@ RendererState::Type ShaderManager::GetRendererState(const MaterialDefinition& ma return rendererState; } +bool ShaderManager::AddLight(Scene3D::Light light) +{ + if(!light || mImpl->mLights.size() >= Scene3D::Internal::Light::GetMaximumEnabledLightCount()) + { + return false; + } + + int32_t lightIndex = mImpl->mLights.size(); + mImpl->mLights.push_back(light); + + for(auto&& shader : mImpl->mShaders) + { + std::string lightCountPropertyName(Scene3D::Internal::Light::GetLightCountUniformName()); + shader.RegisterProperty(lightCountPropertyName, static_cast(mImpl->mLights.size())); + } + + SetLightConstraint(lightIndex); + + return true; +} + +void ShaderManager::RemoveLight(Scene3D::Light light) +{ + uint32_t lightCount = mImpl->mLights.size(); + for(uint32_t index = 0; index < lightCount; ++index) + { + if(mImpl->mLights[index] != light) + { + continue; + } + + RemoveLightConstraint(index); + + if(!mImpl->mLights.empty() && light != mImpl->mLights.back()) + { + RemoveLightConstraint(mImpl->mLights.size() - 1); + mImpl->mLights[index] = mImpl->mLights.back(); + SetLightConstraint(index); + } + + mImpl->mLights.pop_back(); + + for(auto&& shader : mImpl->mShaders) + { + std::string lightCountPropertyName(Scene3D::Internal::Light::GetLightCountUniformName()); + shader.RegisterProperty(lightCountPropertyName, static_cast(mImpl->mLights.size())); + } + break; + } +} + +uint32_t ShaderManager::GetLightCount() const +{ + return mImpl->mLights.size(); +} + +void ShaderManager::SetLightConstraint(uint32_t lightIndex) +{ + for(auto&& shader : mImpl->mShaders) + { + SetLightConstraintToShader(lightIndex, shader); + } +} + +void ShaderManager::SetLightConstraintToShader(uint32_t lightIndex, Dali::Shader shader) +{ + std::string lightDirectionPropertyName(Scene3D::Internal::Light::GetLightDirectionUniformName()); + lightDirectionPropertyName += "[" + std::to_string(lightIndex) + "]"; + auto lightDirectionPropertyIndex = shader.RegisterProperty(lightDirectionPropertyName, Vector3::ZAXIS); + Dali::Constraint lightDirectionConstraint = Dali::Constraint::New(shader, lightDirectionPropertyIndex, [](Vector3& output, const PropertyInputContainer& inputs) + { output = inputs[0]->GetQuaternion().Rotate(Vector3::ZAXIS); }); + lightDirectionConstraint.AddSource(Source{mImpl->mLights[lightIndex], Dali::Actor::Property::WORLD_ORIENTATION}); + lightDirectionConstraint.ApplyPost(); + lightDirectionConstraint.SetTag(INDEX_FOR_LIGHT_CONSTRAINT_TAG + lightIndex); + + std::string lightColorPropertyName(Scene3D::Internal::Light::GetLightColorUniformName()); + lightColorPropertyName += "[" + std::to_string(lightIndex) + "]"; + auto lightColorPropertyIndex = shader.RegisterProperty(lightColorPropertyName, Vector3(Color::WHITE)); + Dali::Constraint lightColorConstraint = Dali::Constraint::New(shader, lightColorPropertyIndex, [](Vector3& output, const PropertyInputContainer& inputs) + { output = Vector3(inputs[0]->GetVector4()); }); + lightColorConstraint.AddSource(Source{mImpl->mLights[lightIndex], Dali::Actor::Property::COLOR}); + lightColorConstraint.ApplyPost(); + lightColorConstraint.SetTag(INDEX_FOR_LIGHT_CONSTRAINT_TAG + lightIndex); +} + +void ShaderManager::RemoveLightConstraint(uint32_t lightIndex) +{ + for(auto&& shader : mImpl->mShaders) + { + shader.RemoveConstraints(INDEX_FOR_LIGHT_CONSTRAINT_TAG + lightIndex); + } +} + } // namespace Dali::Scene3D::Loader diff --git a/dali-scene3d/public-api/loader/shader-manager.h b/dali-scene3d/public-api/loader/shader-manager.h index 4c8988a..56adf38 100644 --- a/dali-scene3d/public-api/loader/shader-manager.h +++ b/dali-scene3d/public-api/loader/shader-manager.h @@ -29,6 +29,7 @@ #include #include #include +#include namespace Dali::Scene3D::Loader { @@ -73,6 +74,44 @@ public: */ RendererState::Type GetRendererState(const MaterialDefinition& materialDefinition); + /** + * @brief Adds new lights for each of shaders. + * @param[in] light Light object to be newly added. + * @return True when the new light object is added successfully. + */ + bool AddLight(Scene3D::Light light); + + /** + * @brief Removes light from each of shaders. + * @param[in] light Light object to be removed. + */ + void RemoveLight(Scene3D::Light light); + + /** + * @brief Retrieves added light counts. + * @return The number of added light count. + */ + uint32_t GetLightCount() const; + +private: + /** + * @brief Sets constraint to the shaders with light of light index. + * @param[in] lightIndex index of light that will be connected with shaders by constraint. + */ + DALI_INTERNAL void SetLightConstraint(uint32_t lightIndex); + + /** + * @brief Sets constraint to a shader with light of light index. + * @param[in] lightIndex index of light that will be connected with input shader by constraint. + * @param[in] shader Shader that the constraint will be applied. + */ + DALI_INTERNAL void SetLightConstraintToShader(uint32_t lightIndex, Dali::Shader shader); + + /** + * @brief Removes constraint of shaders and light of light index. + * @param[in] lightIndex index of light that will be disconnected with shaders. + */ + DALI_INTERNAL void RemoveLightConstraint(uint32_t lightIndex); private: struct Impl; const std::unique_ptr mImpl; -- 2.7.4