2 * Copyright (c) 2023 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <dali-scene3d/internal/controls/model/model-impl.h>
22 #include <dali-toolkit/dali-toolkit.h>
23 #include <dali-toolkit/internal/controls/control/control-data-impl.h>
24 #include <dali-toolkit/internal/graphics/builtin-shader-extern-gen.h>
25 #include <dali/devel-api/actors/actor-devel.h>
26 #include <dali/integration-api/adaptor-framework/adaptor.h>
27 #include <dali/integration-api/debug.h>
28 #include <dali/public-api/math/math-utils.h>
29 #include <dali/public-api/object/type-registry-helper.h>
30 #include <dali/public-api/object/type-registry.h>
34 #include <dali-scene3d/internal/common/model-cache-manager.h>
35 #include <dali-scene3d/internal/controls/scene-view/scene-view-impl.h>
36 #include <dali-scene3d/internal/light/light-impl.h>
37 #include <dali-scene3d/internal/model-components/model-node-impl.h>
38 #include <dali-scene3d/public-api/controls/model/model.h>
39 #include <dali-scene3d/public-api/loader/animation-definition.h>
40 #include <dali-scene3d/public-api/loader/camera-parameters.h>
41 #include <dali-scene3d/public-api/loader/light-parameters.h>
42 #include <dali-scene3d/public-api/loader/load-result.h>
43 #include <dali-scene3d/public-api/loader/node-definition.h>
44 #include <dali-scene3d/public-api/loader/scene-definition.h>
45 #include <dali-scene3d/public-api/loader/shader-definition-factory.h>
58 * Creates control through type registry
62 return Scene3D::Model::New(std::string());
65 // Setup properties, signals and actions using the type-registry.
66 DALI_TYPE_REGISTRATION_BEGIN(Scene3D::Model, Toolkit::Control, Create);
67 DALI_TYPE_REGISTRATION_END()
69 static constexpr Vector3 Y_DIRECTION(1.0f, -1.0f, 1.0f);
70 static constexpr float SIZE_STEP_CONDITION = 0.1f;
72 static constexpr bool DEFAULT_MODEL_CHILDREN_SENSITIVE = false;
73 static constexpr bool DEFAULT_MODEL_CHILDREN_FOCUSABLE = false;
79 pointMin = Vector3(std::numeric_limits<float>::max(), std::numeric_limits<float>::max(), std::numeric_limits<float>::max());
80 pointMax = Vector3(std::numeric_limits<float>::min(), std::numeric_limits<float>::min(), std::numeric_limits<float>::min());
83 void ConsiderNewPointInVolume(const Vector3& position)
85 pointMin.x = std::min(position.x, pointMin.x);
86 pointMin.y = std::min(position.y, pointMin.y);
87 pointMin.z = std::min(position.z, pointMin.z);
89 pointMax.x = std::max(position.x, pointMax.x);
90 pointMax.y = std::max(position.y, pointMax.y);
91 pointMax.z = std::max(position.z, pointMax.z);
94 Vector3 CalculateSize()
96 return pointMax - pointMin;
99 Vector3 CalculatePivot()
101 Vector3 pivot = pointMin / (pointMin - pointMax);
102 for(uint32_t i = 0; i < 3; ++i)
104 // To avoid divid by zero
105 if(Dali::Equals(pointMin[i], pointMax[i]))
117 void ConfigureBlendShapeShaders(
118 Dali::Scene3D::Loader::ResourceBundle& resources, const Dali::Scene3D::Loader::SceneDefinition& scene, Actor root, std::vector<Dali::Scene3D::Loader::BlendshapeShaderConfigurationRequest>&& requests)
120 std::vector<std::string> errors;
121 auto onError = [&errors](const std::string& msg)
122 { errors.push_back(msg); };
123 if(!scene.ConfigureBlendshapeShaders(resources, root, std::move(requests), onError))
125 Dali::Scene3D::Loader::ExceptionFlinger flinger(ASSERT_LOCATION);
126 for(auto& msg : errors)
128 flinger << msg << '\n';
133 void AddModelTreeToAABB(BoundingVolume& AABB, const Dali::Scene3D::Loader::SceneDefinition& scene, const Dali::Scene3D::Loader::Customization::Choices& choices, Dali::Scene3D::Loader::Index iNode, Dali::Scene3D::Loader::NodeDefinition::CreateParams& nodeParams, Matrix parentMatrix)
135 static constexpr uint32_t BOX_POINT_COUNT = 8;
136 static uint32_t BBIndex[BOX_POINT_COUNT][3] = {{0, 0, 0}, {0, 1, 0}, {1, 0, 0}, {1, 1, 0}, {0, 0, 1}, {0, 1, 1}, {1, 0, 1}, {1, 1, 1}};
139 const Dali::Scene3D::Loader::NodeDefinition* node = scene.GetNode(iNode);
140 Matrix localMatrix = node->GetLocalSpace();
141 Matrix::Multiply(nodeMatrix, localMatrix, parentMatrix);
144 if(node->GetExtents(nodeParams.mResources, volume[0], volume[1]))
146 for(uint32_t i = 0; i < BOX_POINT_COUNT; ++i)
148 Vector4 position = Vector4(volume[BBIndex[i][0]].x, volume[BBIndex[i][1]].y, volume[BBIndex[i][2]].z, 1.0f);
149 Vector4 objectPosition = nodeMatrix * position;
150 objectPosition /= objectPosition.w;
152 AABB.ConsiderNewPointInVolume(Vector3(objectPosition));
156 if(node->mCustomization)
158 if(!node->mChildren.empty())
160 auto choice = choices.Get(node->mCustomization->mTag);
161 Dali::Scene3D::Loader::Index i = std::min(choice != Dali::Scene3D::Loader::Customization::NONE ? choice : 0, static_cast<Dali::Scene3D::Loader::Index>(node->mChildren.size() - 1));
163 AddModelTreeToAABB(AABB, scene, choices, node->mChildren[i], nodeParams, nodeMatrix);
168 for(auto i : node->mChildren)
170 AddModelTreeToAABB(AABB, scene, choices, i, nodeParams, nodeMatrix);
175 void AddLightRecursively(Scene3D::ModelNode node, Scene3D::Light light, uint32_t lightIndex)
182 GetImplementation(node).AddLight(light, lightIndex);
184 uint32_t childrenCount = node.GetChildCount();
185 for(uint32_t i = 0; i < childrenCount; ++i)
187 Scene3D::ModelNode childNode = Scene3D::ModelNode::DownCast(node.GetChildAt(i));
190 AddLightRecursively(childNode, light, lightIndex);
195 void RemoveLightRecursively(Scene3D::ModelNode node, uint32_t lightIndex)
202 GetImplementation(node).RemoveLight(lightIndex);
204 uint32_t childrenCount = node.GetChildCount();
205 for(uint32_t i = 0; i < childrenCount; ++i)
207 Scene3D::ModelNode childNode = Scene3D::ModelNode::DownCast(node.GetChildAt(i));
210 RemoveLightRecursively(childNode, lightIndex);
215 } // anonymous namespace
217 Model::Model(const std::string& modelUrl, const std::string& resourceDirectoryUrl)
218 : Control(ControlBehaviour(DISABLE_SIZE_NEGOTIATION | DISABLE_STYLE_CHANGE_SIGNALS)),
220 mResourceDirectoryUrl(resourceDirectoryUrl),
222 mNaturalSize(Vector3::ZERO),
223 mModelPivot(AnchorPoint::CENTER),
224 mSceneIblScaleFactor(1.0f),
225 mIblScaleFactor(1.0f),
226 mSceneSpecularMipmapLevels(1u),
227 mSpecularMipmapLevels(1u),
228 mModelChildrenSensitive(DEFAULT_MODEL_CHILDREN_SENSITIVE),
229 mModelChildrenFocusable(DEFAULT_MODEL_CHILDREN_FOCUSABLE),
230 mModelResourceReady(false),
231 mIblDiffuseResourceReady(true),
232 mIblSpecularResourceReady(true),
233 mIblDiffuseDirty(false),
234 mIblSpecularDirty(false)
240 ResetResourceTasks();
242 if(ModelCacheManager::Get() && !mModelUrl.empty())
244 ModelCacheManager::Get().UnreferenceModelCache(mModelUrl);
248 Dali::Scene3D::Model Model::New(const std::string& modelUrl, const std::string& resourceDirectoryUrl)
250 Model* impl = new Model(modelUrl, resourceDirectoryUrl);
252 Dali::Scene3D::Model handle = Dali::Scene3D::Model(*impl);
254 // Second-phase init of the implementation
255 // This can only be done after the CustomActor connection has been made...
261 const Scene3D::ModelNode Model::GetModelRoot() const
266 void Model::AddModelNode(Scene3D::ModelNode modelNode)
273 mModelRoot.Add(modelNode);
274 if(mModelUrl.empty())
276 mModelResourceReady = true;
279 if(mIblDiffuseResourceReady && mIblSpecularResourceReady)
281 UpdateImageBasedLightTexture();
282 UpdateImageBasedLightScaleFactor();
285 uint32_t maxLightCount = Scene3D::Internal::Light::GetMaximumEnabledLightCount();
286 for(uint32_t i = 0; i < maxLightCount; ++i)
290 AddLightRecursively(modelNode, mLights[i], i);
294 if(Self().GetProperty<bool>(Dali::Actor::Property::CONNECTED_TO_SCENE))
296 NotifyResourceReady();
300 void Model::RemoveModelNode(Scene3D::ModelNode modelNode)
304 uint32_t maxLightCount = Scene3D::Internal::Light::GetMaximumEnabledLightCount();
305 for(uint32_t i = 0; i < maxLightCount; ++i)
309 RemoveLightRecursively(modelNode, i);
312 mModelRoot.Remove(modelNode);
316 void Model::SetChildrenSensitive(bool enable)
318 if(mModelChildrenSensitive != enable)
320 mModelChildrenSensitive = enable;
323 mModelRoot.SetProperty(Dali::Actor::Property::SENSITIVE, mModelChildrenSensitive);
328 bool Model::GetChildrenSensitive() const
330 return mModelChildrenSensitive;
333 void Model::SetChildrenFocusable(bool enable)
335 if(mModelChildrenFocusable != enable)
337 mModelChildrenFocusable = enable;
340 mModelRoot.SetProperty(Dali::Actor::Property::KEYBOARD_FOCUSABLE, mModelChildrenFocusable);
341 mModelRoot.SetProperty(Dali::DevelActor::Property::KEYBOARD_FOCUSABLE_CHILDREN, mModelChildrenFocusable);
346 bool Model::GetChildrenFocusable() const
348 return mModelChildrenFocusable;
351 void Model::SetImageBasedLightSource(const std::string& diffuseUrl, const std::string& specularUrl, float scaleFactor)
353 bool needIblReset = false;
354 bool isOnScene = Self().GetProperty<bool>(Dali::Actor::Property::CONNECTED_TO_SCENE);
355 if(mDiffuseIblUrl != diffuseUrl)
357 mDiffuseIblUrl = diffuseUrl;
358 if(mDiffuseIblUrl.empty())
364 mIblDiffuseDirty = true;
365 mIblDiffuseResourceReady = false;
369 if(mSpecularIblUrl != specularUrl)
371 mSpecularIblUrl = specularUrl;
372 if(mSpecularIblUrl.empty())
378 mIblSpecularDirty = true;
379 mIblSpecularResourceReady = false;
383 // If one or both of diffuse url and specular url are empty,
384 // we don't need to request to load texture.
387 ResetResourceTask(mIblDiffuseLoadTask);
388 ResetResourceTask(mIblSpecularLoadTask);
390 mIblDiffuseDirty = false;
391 mIblSpecularDirty = false;
392 mIblDiffuseResourceReady = true;
393 mIblSpecularResourceReady = true;
395 mDiffuseTexture.Reset();
396 mSpecularTexture.Reset();
397 UpdateImageBasedLightTexture();
401 if(isOnScene && mIblDiffuseDirty)
403 ResetResourceTask(mIblDiffuseLoadTask);
404 mIblDiffuseLoadTask = new EnvironmentMapLoadTask(mDiffuseIblUrl, Scene3D::EnvironmentMapType::CUBEMAP, MakeCallback(this, &Model::OnIblDiffuseLoadComplete));
405 Dali::AsyncTaskManager::Get().AddTask(mIblDiffuseLoadTask);
406 mIblDiffuseDirty = false;
409 if(isOnScene && mIblSpecularDirty)
411 ResetResourceTask(mIblSpecularLoadTask);
412 mIblSpecularLoadTask = new EnvironmentMapLoadTask(mSpecularIblUrl, Scene3D::EnvironmentMapType::CUBEMAP, MakeCallback(this, &Model::OnIblSpecularLoadComplete));
413 Dali::AsyncTaskManager::Get().AddTask(mIblSpecularLoadTask);
414 mIblSpecularDirty = false;
418 if(!Dali::Equals(mIblScaleFactor, scaleFactor))
420 mIblScaleFactor = scaleFactor;
421 UpdateImageBasedLightScaleFactor();
424 // If diffuse and specular textures are already loaded, emits resource ready signal here.
425 NotifyResourceReady();
428 void Model::SetImageBasedLightScaleFactor(float scaleFactor)
430 mIblScaleFactor = scaleFactor;
431 if(mDiffuseTexture && mSpecularTexture)
433 UpdateImageBasedLightScaleFactor();
437 float Model::GetImageBasedLightScaleFactor() const
439 return mIblScaleFactor;
442 uint32_t Model::GetAnimationCount() const
444 return mAnimations.size();
447 Dali::Animation Model::GetAnimation(uint32_t index) const
449 Dali::Animation animation;
450 if(mAnimations.size() > index)
452 animation = mAnimations[index].second;
457 Dali::Animation Model::GetAnimation(const std::string& name) const
459 Dali::Animation animation;
462 for(auto&& animationData : mAnimations)
464 if(animationData.first == name)
466 animation = animationData.second;
474 uint32_t Model::GetCameraCount() const
476 return mCameraParameters.size();
479 Dali::CameraActor Model::GenerateCamera(uint32_t index) const
481 Dali::CameraActor camera;
482 if(mCameraParameters.size() > index)
484 camera = Dali::CameraActor::New3DCamera();
485 if(!mCameraParameters[index].ConfigureCamera(camera, false))
487 DALI_LOG_ERROR("Fail to generate %u's camera actor : Some property was not defined. Please check model file.\n", index);
492 ApplyCameraTransform(camera);
497 bool Model::ApplyCamera(uint32_t index, Dali::CameraActor camera) const
499 if(camera && mCameraParameters.size() > index)
501 if(!mCameraParameters[index].ConfigureCamera(camera, false))
503 DALI_LOG_ERROR("Fail to apply %u's camera actor : Some property was not defined. Please check model file.\n", index);
507 ApplyCameraTransform(camera);
513 Scene3D::ModelNode Model::FindChildModelNodeByName(std::string_view nodeName)
515 Actor childActor = Self().FindChildByName(nodeName);
516 return Scene3D::ModelNode::DownCast(childActor);
519 ///////////////////////////////////////////////////////////
524 void Model::OnInitialize()
526 // Make ParentOrigin as Center.
527 Self().SetProperty(Dali::Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
528 mLights.resize(Scene3D::Internal::Light::GetMaximumEnabledLightCount());
531 void Model::OnSceneConnection(int depth)
533 if(!mModelLoadTask && !mModelResourceReady && !mModelUrl.empty())
535 // Request model load only if we setup url.
536 if(ModelCacheManager::Get())
538 ModelCacheManager::Get().ReferenceModelCache(mModelUrl);
540 mModelLoadTask = new ModelLoadTask(mModelUrl, mResourceDirectoryUrl, MakeCallback(this, &Model::OnModelLoadComplete));
541 Dali::AsyncTaskManager::Get().AddTask(mModelLoadTask);
544 // If diffuse and specular url is not valid, IBL does not need to be loaded.
545 if(!mDiffuseIblUrl.empty() && !mSpecularIblUrl.empty())
547 SetImageBasedLightSource(mDiffuseIblUrl, mSpecularIblUrl, mIblScaleFactor);
550 Actor parent = Self().GetParent();
553 Scene3D::SceneView sceneView = Scene3D::SceneView::DownCast(parent);
556 GetImpl(sceneView).RegisterSceneItem(this);
557 mParentSceneView = sceneView;
560 parent = parent.GetParent();
563 NotifyResourceReady();
565 mSizeNotification = Self().AddPropertyNotification(Actor::Property::SIZE, StepCondition(SIZE_STEP_CONDITION));
566 mSizeNotification.NotifySignal().Connect(this, &Model::OnSizeNotification);
567 Control::OnSceneConnection(depth);
570 void Model::OnSceneDisconnection()
572 Scene3D::SceneView sceneView = mParentSceneView.GetHandle();
575 GetImpl(sceneView).UnregisterSceneItem(this);
576 mParentSceneView.Reset();
579 mSizeNotification.NotifySignal().Disconnect(this, &Model::OnSizeNotification);
580 Self().RemovePropertyNotification(mSizeNotification);
581 mSizeNotification.Reset();
583 Control::OnSceneDisconnection();
586 void Model::OnSizeSet(const Vector3& size)
591 Vector3 Model::GetNaturalSize()
595 DALI_LOG_ERROR("Model is still not loaded.\n");
596 return Vector3::ZERO;
602 float Model::GetHeightForWidth(float width)
605 padding = Self().GetProperty<Extents>(Toolkit::Control::Property::PADDING);
606 return Control::GetHeightForWidth(width) + padding.top + padding.bottom;
609 float Model::GetWidthForHeight(float height)
612 padding = Self().GetProperty<Extents>(Toolkit::Control::Property::PADDING);
613 return Control::GetWidthForHeight(height) + padding.start + padding.end;
616 void Model::OnRelayout(const Vector2& size, RelayoutContainer& container)
618 Control::OnRelayout(size, container);
622 bool Model::IsResourceReady() const
624 return mModelResourceReady && mIblDiffuseResourceReady && mIblSpecularResourceReady;
627 void Model::CreateModelRoot()
629 mModelRoot = Scene3D::ModelNode::New();
630 mModelRoot.SetProperty(Actor::Property::COLOR_MODE, ColorMode::USE_OWN_MULTIPLY_PARENT_COLOR);
631 mModelRoot.SetProperty(Dali::Actor::Property::SCALE, Y_DIRECTION);
632 mModelRoot.SetProperty(Dali::Actor::Property::SENSITIVE, mModelChildrenSensitive);
633 mModelRoot.SetProperty(Dali::Actor::Property::KEYBOARD_FOCUSABLE, mModelChildrenFocusable);
634 mModelRoot.SetProperty(Dali::DevelActor::Property::KEYBOARD_FOCUSABLE_CHILDREN, mModelChildrenFocusable);
635 Self().Add(mModelRoot);
638 void Model::ScaleModel(bool useCurrentSize)
646 Vector3 size = (useCurrentSize) ? Self().GetCurrentProperty<Vector3>(Dali::Actor::Property::SIZE) : Self().GetProperty<Vector3>(Dali::Actor::Property::SIZE);
647 if(size.x > 0.0f && size.y > 0.0f)
650 scale = std::min(size.x / mNaturalSize.x, scale);
651 scale = std::min(size.y / mNaturalSize.y, scale);
653 // Models in glTF and dli are defined as right hand coordinate system.
654 // DALi uses left hand coordinate system. Scaling negative is for change winding order.
655 mModelRoot.SetProperty(Dali::Actor::Property::SCALE, Y_DIRECTION * scale);
658 void Model::FitModelPosition()
664 // Loaded model pivot is not the model center.
665 mModelRoot.SetProperty(Dali::Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
666 mModelRoot.SetProperty(Dali::Actor::Property::ANCHOR_POINT, Vector3::ONE - mModelPivot);
669 void Model::UpdateImageBasedLightTextureRecursively(Scene3D::ModelNode node, Dali::Texture diffuseTexture, Dali::Texture specularTexture, float iblScaleFactor, uint32_t specularMipmapLevels)
676 GetImplementation(node).SetImageBasedLightTexture(diffuseTexture, specularTexture, iblScaleFactor, specularMipmapLevels);
677 uint32_t childrenCount = node.GetChildCount();
678 for(uint32_t i = 0; i < childrenCount; ++i)
680 Scene3D::ModelNode childNode = Scene3D::ModelNode::DownCast(node.GetChildAt(i));
685 UpdateImageBasedLightTextureRecursively(childNode, diffuseTexture, specularTexture, iblScaleFactor, specularMipmapLevels);
689 void Model::UpdateImageBasedLightScaleFactorRecursively(Scene3D::ModelNode node, float iblScaleFactor)
696 GetImplementation(node).SetImageBasedLightScaleFactor(iblScaleFactor);
698 uint32_t childrenCount = node.GetChildCount();
699 for(uint32_t i = 0; i < childrenCount; ++i)
701 Scene3D::ModelNode childNode = Scene3D::ModelNode::DownCast(node.GetChildAt(i));
706 UpdateImageBasedLightScaleFactorRecursively(childNode, iblScaleFactor);
710 void Model::UpdateImageBasedLightTexture()
712 Dali::Texture currentDiffuseTexture = (mDiffuseTexture && mSpecularTexture) ? mDiffuseTexture : mSceneDiffuseTexture;
713 Dali::Texture currentSpecularTexture = (mDiffuseTexture && mSpecularTexture) ? mSpecularTexture : mSceneSpecularTexture;
714 float currentIblScaleFactor = (mDiffuseTexture && mSpecularTexture) ? mIblScaleFactor : mSceneIblScaleFactor;
715 uint32_t currentIblSpecularMipmapLevels = (mDiffuseTexture && mSpecularTexture) ? mSpecularMipmapLevels : mSceneSpecularMipmapLevels;
717 if(!currentDiffuseTexture || !currentSpecularTexture)
719 currentDiffuseTexture = mDefaultDiffuseTexture;
720 currentSpecularTexture = mDefaultSpecularTexture;
721 currentIblScaleFactor = Dali::Scene3D::Loader::EnvironmentDefinition::GetDefaultIntensity();
722 currentIblSpecularMipmapLevels = 1u;
725 UpdateImageBasedLightTextureRecursively(mModelRoot, currentDiffuseTexture, currentSpecularTexture, currentIblScaleFactor, currentIblSpecularMipmapLevels);
728 void Model::UpdateImageBasedLightScaleFactor()
730 if((!mDiffuseTexture || !mSpecularTexture) &&
731 (!mSceneDiffuseTexture || !mSceneSpecularTexture))
736 float currentIblScaleFactor = (mDiffuseTexture && mSpecularTexture) ? mIblScaleFactor : mSceneIblScaleFactor;
737 UpdateImageBasedLightScaleFactorRecursively(mModelRoot, currentIblScaleFactor);
740 void Model::ApplyCameraTransform(Dali::CameraActor camera) const
742 Vector3 selfPosition = Self().GetProperty<Vector3>(Actor::Property::POSITION);
743 Quaternion selfOrientation = Self().GetProperty<Quaternion>(Actor::Property::ORIENTATION);
744 Vector3 selfScale = Self().GetProperty<Vector3>(Actor::Property::SCALE);
746 Vector3 cameraPosition = camera.GetProperty<Vector3>(Actor::Property::POSITION);
747 Quaternion cameraOrientation = camera.GetProperty<Quaternion>(Actor::Property::ORIENTATION);
748 Vector3 cameraScale = camera.GetProperty<Vector3>(Actor::Property::SCALE);
750 // Models in glTF and dli are defined as right hand coordinate system.
751 // DALi uses left hand coordinate system. Scaling negative is for change winding order.
752 if(!Dali::Equals(Y_DIRECTION.Dot(Vector3::YAXIS), 1.0f))
754 // Reflect by XZ plane
755 cameraPosition.y = -cameraPosition.y;
756 Quaternion yDirectionQuaternion;
757 yDirectionQuaternion.mVector = Vector3::YAXIS;
758 // Reflect orientation
759 cameraOrientation = yDirectionQuaternion * cameraOrientation * yDirectionQuaternion;
762 Vector3 resultPosition;
763 Quaternion resultOrientation;
766 Matrix selfMatrix(false);
767 Matrix cameraMatrix(false);
768 Matrix resultMatrix(false);
769 selfMatrix.SetTransformComponents(selfScale, selfOrientation, selfPosition);
770 cameraMatrix.SetTransformComponents(cameraScale, cameraOrientation, cameraPosition);
771 Matrix::Multiply(resultMatrix, cameraMatrix, selfMatrix);
772 resultMatrix.GetTransformComponents(resultPosition, resultOrientation, resultScale);
774 camera.SetProperty(Actor::Property::POSITION, resultPosition);
775 camera.SetProperty(Actor::Property::ORIENTATION, resultOrientation);
776 camera.SetProperty(Actor::Property::SCALE, resultScale);
779 void Model::NotifyImageBasedLightTexture(Dali::Texture diffuseTexture, Dali::Texture specularTexture, float scaleFactor, uint32_t specularMipmapLevels)
781 if(mSceneDiffuseTexture != diffuseTexture || mSceneSpecularTexture != specularTexture)
783 mSceneDiffuseTexture = diffuseTexture;
784 mSceneSpecularTexture = specularTexture;
785 mSceneIblScaleFactor = scaleFactor;
786 mSceneSpecularMipmapLevels = specularMipmapLevels;
787 // If Model IBL is not set, use SceneView's IBL.
788 if(!mDiffuseTexture || !mSpecularTexture)
790 UpdateImageBasedLightTexture();
795 void Model::NotifyImageBasedLightScaleFactor(float scaleFactor)
797 mSceneIblScaleFactor = scaleFactor;
798 if(mSceneDiffuseTexture && mSceneSpecularTexture)
800 UpdateImageBasedLightScaleFactor();
804 void Model::NotifyLightAdded(uint32_t lightIndex, Scene3D::Light light)
806 mLights[lightIndex] = light;
807 AddLightRecursively(mModelRoot, light, lightIndex);
810 void Model::NotifyLightRemoved(uint32_t lightIndex)
812 if(mLights[lightIndex])
814 RemoveLightRecursively(mModelRoot, lightIndex);
815 mLights[lightIndex].Reset();
819 void Model::OnModelLoadComplete()
821 if(!mModelLoadTask->HasSucceeded())
823 ResetResourceTasks();
825 if(ModelCacheManager::Get() && !mModelUrl.empty())
827 ModelCacheManager::Get().UnreferenceModelCache(mModelUrl);
839 auto& resources = mModelLoadTask->GetResources();
840 auto& scene = mModelLoadTask->GetScene();
841 CreateAnimations(scene);
842 ResetCameraParameters();
843 if(!resources.mEnvironmentMaps.empty())
845 mDefaultDiffuseTexture = resources.mEnvironmentMaps.front().second.mDiffuse;
846 mDefaultSpecularTexture = resources.mEnvironmentMaps.front().second.mSpecular;
849 uint32_t maxLightCount = Scene3D::Internal::Light::GetMaximumEnabledLightCount();
850 for(uint32_t i = 0; i < maxLightCount; ++i)
854 AddLightRecursively(mModelRoot, mLights[i], i);
858 UpdateImageBasedLightTexture();
859 UpdateImageBasedLightScaleFactor();
860 Self().SetProperty(Dali::Actor::Property::ANCHOR_POINT, Vector3(mModelPivot.x, 1.0f - mModelPivot.y, mModelPivot.z));
862 mModelResourceReady = true;
863 NotifyResourceReady();
864 ResetResourceTask(mModelLoadTask);
867 void Model::OnIblDiffuseLoadComplete()
869 mDiffuseTexture = mIblDiffuseLoadTask->GetLoadedTexture();
870 ResetResourceTask(mIblDiffuseLoadTask);
871 mIblDiffuseResourceReady = true;
872 if(mIblDiffuseResourceReady && mIblSpecularResourceReady)
878 void Model::OnIblSpecularLoadComplete()
880 mSpecularTexture = mIblSpecularLoadTask->GetLoadedTexture();
881 mSpecularMipmapLevels = mIblSpecularLoadTask->GetMipmapLevels();
882 ResetResourceTask(mIblSpecularLoadTask);
883 mIblSpecularResourceReady = true;
884 if(mIblDiffuseResourceReady && mIblSpecularResourceReady)
890 void Model::OnIblLoadComplete()
892 UpdateImageBasedLightTexture();
893 NotifyResourceReady();
896 void Model::OnSizeNotification(Dali::PropertyNotification& source)
901 void Model::ResetResourceTasks()
903 if(!Dali::Adaptor::IsAvailable())
907 ResetResourceTask(mModelLoadTask);
908 ResetResourceTask(mIblDiffuseLoadTask);
909 ResetResourceTask(mIblSpecularLoadTask);
912 void Model::ResetResourceTask(IntrusivePtr<AsyncTask> asyncTask)
918 Dali::AsyncTaskManager::Get().RemoveTask(asyncTask);
922 void Model::NotifyResourceReady()
924 if(!IsResourceReady())
928 Control::SetResourceReady();
931 void Model::CreateModel()
934 auto& resources = mModelLoadTask->GetResources();
935 auto& scene = mModelLoadTask->GetScene();
936 auto& resourceChoices = mModelLoadTask->GetResourceChoices();
937 Dali::Scene3D::Loader::Transforms xforms{Dali::Scene3D::Loader::MatrixStack{}, Dali::Scene3D::Loader::ViewProjection{}};
938 Dali::Scene3D::Loader::NodeDefinition::CreateParams nodeParams{resources, xforms, {}, {}, {}};
940 // Generate Dali handles from resource bundle. Note that we generate all scene's resouce immediatly.
941 resources.GenerateResources();
942 for(auto iRoot : scene.GetRoots())
944 if(auto actor = scene.CreateNodes(iRoot, resourceChoices, nodeParams))
946 scene.ConfigureSkinningShaders(resources, actor, std::move(nodeParams.mSkinnables));
947 ConfigureBlendShapeShaders(resources, scene, actor, std::move(nodeParams.mBlendshapeRequests));
949 scene.ApplyConstraints(actor, std::move(nodeParams.mConstrainables));
951 mModelRoot.Add(actor);
954 AddModelTreeToAABB(AABB, scene, resourceChoices, iRoot, nodeParams, Matrix::IDENTITY);
957 mNaturalSize = AABB.CalculateSize();
958 mModelPivot = AABB.CalculatePivot();
959 mModelRoot.SetProperty(Dali::Actor::Property::SIZE, mNaturalSize);
960 Vector3 controlSize = Self().GetProperty<Vector3>(Dali::Actor::Property::SIZE);
961 if(Dali::EqualsZero(controlSize.x) || Dali::EqualsZero(controlSize.y))
963 Self().SetProperty(Dali::Actor::Property::SIZE, mNaturalSize);
969 void Model::CreateAnimations(Dali::Scene3D::Loader::SceneDefinition& scene)
972 if(!mModelLoadTask->GetAnimations().empty())
974 auto getActor = [&](const Scene3D::Loader::AnimatedProperty& property)
976 if(property.mNodeIndex == Scene3D::Loader::INVALID_INDEX)
978 return mModelRoot.FindChildByName(property.mNodeName);
980 auto* node = scene.GetNode(property.mNodeIndex);
983 return Dali::Actor();
985 return mModelRoot.FindChildById(node->mNodeId);
988 for(auto&& animation : mModelLoadTask->GetAnimations())
990 Dali::Animation anim = animation.ReAnimate(getActor);
991 mAnimations.push_back({animation.GetName(), anim});
996 void Model::ResetCameraParameters()
998 mCameraParameters.clear();
999 if(!mModelLoadTask->GetCameras().empty())
1001 // Copy camera parameters.
1002 std::copy(mModelLoadTask->GetCameras().begin(), mModelLoadTask->GetCameras().end(), std::back_inserter(mCameraParameters));
1006 } // namespace Internal
1007 } // namespace Scene3D