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-manager.h>
46 #include <dali-scene3d/public-api/model-motion/motion-index/blend-shape-index.h>
59 * Creates control through type registry
63 return Scene3D::Model::New(std::string());
66 // Setup properties, signals and actions using the type-registry.
67 DALI_TYPE_REGISTRATION_BEGIN(Scene3D::Model, Toolkit::Control, Create);
68 DALI_TYPE_REGISTRATION_END()
70 static constexpr Vector3 Y_DIRECTION(1.0f, -1.0f, 1.0f);
71 static constexpr float SIZE_STEP_CONDITION = 0.1f;
73 static constexpr bool DEFAULT_MODEL_CHILDREN_SENSITIVE = false;
74 static constexpr bool DEFAULT_MODEL_CHILDREN_FOCUSABLE = false;
80 pointMin = Vector3(std::numeric_limits<float>::max(), std::numeric_limits<float>::max(), std::numeric_limits<float>::max());
81 pointMax = Vector3(std::numeric_limits<float>::min(), std::numeric_limits<float>::min(), std::numeric_limits<float>::min());
84 void ConsiderNewPointInVolume(const Vector3& position)
86 pointMin.x = std::min(position.x, pointMin.x);
87 pointMin.y = std::min(position.y, pointMin.y);
88 pointMin.z = std::min(position.z, pointMin.z);
90 pointMax.x = std::max(position.x, pointMax.x);
91 pointMax.y = std::max(position.y, pointMax.y);
92 pointMax.z = std::max(position.z, pointMax.z);
95 Vector3 CalculateSize()
97 return pointMax - pointMin;
100 Vector3 CalculatePivot()
102 Vector3 pivot = pointMin / (pointMin - pointMax);
103 for(uint32_t i = 0; i < 3; ++i)
105 // To avoid divid by zero
106 if(Dali::Equals(pointMin[i], pointMax[i]))
118 void ConfigureBlendShapeShaders(
119 Dali::Scene3D::Loader::ResourceBundle& resources, const Dali::Scene3D::Loader::SceneDefinition& scene, Actor root, std::vector<Dali::Scene3D::Loader::BlendshapeShaderConfigurationRequest>&& requests)
121 std::vector<std::string> errors;
122 auto onError = [&errors](const std::string& msg) { 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 UpdateBlendShapeNodeMapRecursively(Model::BlendShapeModelNodeMap& resultMap, const Scene3D::ModelNode& node)
181 const auto childCount = node.GetChildCount();
182 for(auto i = 0u; i < childCount; ++i)
184 UpdateBlendShapeNodeMapRecursively(resultMap, Scene3D::ModelNode::DownCast(node.GetChildAt(i)));
187 std::vector<std::string> blendShapeNames;
188 node.RetrieveBlendShapeNames(blendShapeNames);
189 for(const auto& iter : blendShapeNames)
191 // Append or create new list.
192 resultMap[iter].push_back(node);
196 void UpdateShaderRecursively(Scene3D::ModelNode node, Scene3D::Loader::ShaderManagerPtr shaderManager)
203 GetImplementation(node).UpdateShader(shaderManager);
205 uint32_t childrenCount = node.GetChildCount();
206 for(uint32_t i = 0; i < childrenCount; ++i)
208 Scene3D::ModelNode childNode = Scene3D::ModelNode::DownCast(node.GetChildAt(i));
211 UpdateShaderRecursively(childNode, shaderManager);
216 void UpdateShadowMapTextureRecursively(Scene3D::ModelNode node, Dali::Texture shadowMapTexture)
223 GetImplementation(node).SetShadowMapTexture(shadowMapTexture);
225 uint32_t childrenCount = node.GetChildCount();
226 for(uint32_t i = 0; i < childrenCount; ++i)
228 Scene3D::ModelNode childNode = Scene3D::ModelNode::DownCast(node.GetChildAt(i));
231 UpdateShadowMapTextureRecursively(childNode, shadowMapTexture);
236 } // anonymous namespace
238 Model::Model(const std::string& modelUrl, const std::string& resourceDirectoryUrl)
239 : Control(ControlBehaviour(DISABLE_SIZE_NEGOTIATION | DISABLE_STYLE_CHANGE_SIGNALS)),
241 mResourceDirectoryUrl(resourceDirectoryUrl),
243 mShaderManager(new Scene3D::Loader::ShaderManager()),
244 mNaturalSize(Vector3::ZERO),
245 mModelPivot(AnchorPoint::CENTER),
246 mSceneIblScaleFactor(1.0f),
247 mIblScaleFactor(1.0f),
248 mSceneSpecularMipmapLevels(1u),
249 mSpecularMipmapLevels(1u),
250 mModelChildrenSensitive(DEFAULT_MODEL_CHILDREN_SENSITIVE),
251 mModelChildrenFocusable(DEFAULT_MODEL_CHILDREN_FOCUSABLE),
252 mModelResourceReady(false),
253 mIblDiffuseResourceReady(true),
254 mIblSpecularResourceReady(true),
255 mIblDiffuseDirty(false),
256 mIblSpecularDirty(false)
262 ResetResourceTasks();
264 if(ModelCacheManager::Get() && !mModelUrl.empty())
266 ModelCacheManager::Get().UnreferenceModelCache(mModelUrl);
270 Dali::Scene3D::Model Model::New(const std::string& modelUrl, const std::string& resourceDirectoryUrl)
272 Model* impl = new Model(modelUrl, resourceDirectoryUrl);
274 Dali::Scene3D::Model handle = Dali::Scene3D::Model(*impl);
276 // Second-phase init of the implementation
277 // This can only be done after the CustomActor connection has been made...
283 const Scene3D::ModelNode Model::GetModelRoot() const
288 void Model::AddModelNode(Scene3D::ModelNode modelNode)
295 mModelRoot.Add(modelNode);
296 if(mModelUrl.empty())
298 mModelResourceReady = true;
301 UpdateShaderRecursively(modelNode, mShaderManager);
303 if(mShadowMapTexture)
305 UpdateShadowMapTextureRecursively(modelNode, mShadowMapTexture);
308 if(mIblDiffuseResourceReady && mIblSpecularResourceReady)
310 UpdateImageBasedLightTexture();
311 UpdateImageBasedLightScaleFactor();
314 if(Self().GetProperty<bool>(Dali::Actor::Property::CONNECTED_TO_SCENE))
316 NotifyResourceReady();
320 void Model::RemoveModelNode(Scene3D::ModelNode modelNode)
324 UpdateShaderRecursively(modelNode, nullptr);
325 mModelRoot.Remove(modelNode);
329 void Model::SetChildrenSensitive(bool enable)
331 if(mModelChildrenSensitive != enable)
333 mModelChildrenSensitive = enable;
336 mModelRoot.SetProperty(Dali::Actor::Property::SENSITIVE, mModelChildrenSensitive);
341 bool Model::GetChildrenSensitive() const
343 return mModelChildrenSensitive;
346 void Model::SetChildrenFocusable(bool enable)
348 if(mModelChildrenFocusable != enable)
350 mModelChildrenFocusable = enable;
353 mModelRoot.SetProperty(Dali::Actor::Property::KEYBOARD_FOCUSABLE, mModelChildrenFocusable);
354 mModelRoot.SetProperty(Dali::DevelActor::Property::KEYBOARD_FOCUSABLE_CHILDREN, mModelChildrenFocusable);
359 bool Model::GetChildrenFocusable() const
361 return mModelChildrenFocusable;
364 void Model::SetImageBasedLightSource(const std::string& diffuseUrl, const std::string& specularUrl, float scaleFactor)
366 bool needIblReset = false;
367 bool isOnScene = Self().GetProperty<bool>(Dali::Actor::Property::CONNECTED_TO_SCENE);
368 if(mDiffuseIblUrl != diffuseUrl)
370 mDiffuseIblUrl = diffuseUrl;
371 if(mDiffuseIblUrl.empty())
377 mIblDiffuseDirty = true;
378 mIblDiffuseResourceReady = false;
382 if(mSpecularIblUrl != specularUrl)
384 mSpecularIblUrl = specularUrl;
385 if(mSpecularIblUrl.empty())
391 mIblSpecularDirty = true;
392 mIblSpecularResourceReady = false;
396 // If one or both of diffuse url and specular url are empty,
397 // we don't need to request to load texture.
400 ResetResourceTask(mIblDiffuseLoadTask);
401 ResetResourceTask(mIblSpecularLoadTask);
403 mIblDiffuseDirty = false;
404 mIblSpecularDirty = false;
405 mIblDiffuseResourceReady = true;
406 mIblSpecularResourceReady = true;
408 mDiffuseTexture.Reset();
409 mSpecularTexture.Reset();
410 UpdateImageBasedLightTexture();
414 if(isOnScene && mIblDiffuseDirty)
416 ResetResourceTask(mIblDiffuseLoadTask);
417 mIblDiffuseLoadTask = new EnvironmentMapLoadTask(mDiffuseIblUrl, Scene3D::EnvironmentMapType::CUBEMAP, MakeCallback(this, &Model::OnIblDiffuseLoadComplete));
418 Dali::AsyncTaskManager::Get().AddTask(mIblDiffuseLoadTask);
419 mIblDiffuseDirty = false;
422 if(isOnScene && mIblSpecularDirty)
424 ResetResourceTask(mIblSpecularLoadTask);
425 mIblSpecularLoadTask = new EnvironmentMapLoadTask(mSpecularIblUrl, Scene3D::EnvironmentMapType::CUBEMAP, MakeCallback(this, &Model::OnIblSpecularLoadComplete));
426 Dali::AsyncTaskManager::Get().AddTask(mIblSpecularLoadTask);
427 mIblSpecularDirty = false;
431 if(!Dali::Equals(mIblScaleFactor, scaleFactor))
433 mIblScaleFactor = scaleFactor;
434 UpdateImageBasedLightScaleFactor();
437 // If diffuse and specular textures are already loaded, emits resource ready signal here.
438 NotifyResourceReady();
441 void Model::SetImageBasedLightScaleFactor(float scaleFactor)
443 mIblScaleFactor = scaleFactor;
444 if(mDiffuseTexture && mSpecularTexture)
446 UpdateImageBasedLightScaleFactor();
450 float Model::GetImageBasedLightScaleFactor() const
452 return mIblScaleFactor;
455 uint32_t Model::GetAnimationCount() const
457 return mAnimations.size();
460 Dali::Animation Model::GetAnimation(uint32_t index) const
462 Dali::Animation animation;
463 if(mAnimations.size() > index)
465 animation = mAnimations[index].second;
470 Dali::Animation Model::GetAnimation(const std::string& name) const
472 Dali::Animation animation;
475 for(auto&& animationData : mAnimations)
477 if(animationData.first == name)
479 animation = animationData.second;
487 uint32_t Model::GetCameraCount() const
489 return mCameraParameters.size();
492 Dali::CameraActor Model::GenerateCamera(uint32_t index) const
494 Dali::CameraActor camera;
495 if(mCameraParameters.size() > index)
497 camera = Dali::CameraActor::New3DCamera();
498 if(!mCameraParameters[index].ConfigureCamera(camera, false))
500 DALI_LOG_ERROR("Fail to generate %u's camera actor : Some property was not defined. Please check model file.\n", index);
505 ApplyCameraTransform(camera);
510 bool Model::ApplyCamera(uint32_t index, Dali::CameraActor camera) const
512 if(camera && mCameraParameters.size() > index)
514 if(!mCameraParameters[index].ConfigureCamera(camera, false))
516 DALI_LOG_ERROR("Fail to apply %u's camera actor : Some property was not defined. Please check model file.\n", index);
520 ApplyCameraTransform(camera);
526 Scene3D::ModelNode Model::FindChildModelNodeByName(std::string_view nodeName)
528 Actor childActor = Self().FindChildByName(nodeName);
529 return Scene3D::ModelNode::DownCast(childActor);
532 void Model::RetrieveBlendShapeNames(std::vector<std::string>& blendShapeNames) const
534 blendShapeNames.reserve(blendShapeNames.size() + mBlendShapeModelNodeMap.size());
535 for(const auto& iter : mBlendShapeModelNodeMap)
537 blendShapeNames.push_back(iter.first);
541 void Model::RetrieveModelNodesByBlendShapeName(std::string_view blendShapeName, std::vector<Scene3D::ModelNode>& modelNodes) const
543 auto iter = mBlendShapeModelNodeMap.find(std::string(blendShapeName));
544 if(iter != mBlendShapeModelNodeMap.end())
546 const auto& modelNodeList = iter->second;
547 modelNodes.reserve(modelNodes.size() + modelNodeList.size());
548 for(const auto& nodeIter : modelNodeList)
550 modelNodes.push_back(nodeIter);
555 Dali::Animation Model::GenerateMotionDataAnimation(Scene3D::MotionData motionData)
557 Dali::Animation animation;
559 // TODO : Need to collect duplicated codes with SetMotionData()
563 const uint32_t motionCount = motionData.GetMotionCount();
564 for(uint32_t i = 0u; i < motionCount; ++i)
566 auto motionIndex = motionData.GetIndex(i);
567 auto motionValue = motionData.GetValue(i);
568 if(motionIndex && motionValue)
570 if(motionIndex.GetModelNodeId() != Property::INVALID_KEY)
572 Scene3D::ModelNode modelNode;
573 if(motionIndex.GetModelNodeId().type == Property::Key::Type::STRING)
575 modelNode = FindChildModelNodeByName(motionIndex.GetModelNodeId().stringKey);
577 else if(motionIndex.GetModelNodeId().type == Property::Key::Type::INDEX)
579 // TODO : Not implement yet.
584 KeyFrames keyFrames = motionValue.GetKeyFrames();
588 // Try to use index first. If failed, try to use name
589 Property::Index animatedPropertyIndex = motionIndex.GetPropertyIndex(modelNode);
590 if(animatedPropertyIndex != Property::INVALID_INDEX)
592 if(DALI_UNLIKELY(!animation))
594 animation = Animation::New(motionData.GetDuration());
596 animation.AnimateBetween(Dali::Property(modelNode, animatedPropertyIndex), keyFrames);
600 std::string animatedPropertyName = motionIndex.GetPropertyName(modelNode);
601 Dali::Property property(modelNode, animatedPropertyName);
602 if(property.propertyIndex != Property::INVALID_INDEX)
604 if(DALI_UNLIKELY(!animation))
606 animation = Animation::New(motionData.GetDuration());
608 animation.AnimateBetween(property, keyFrames);
616 Scene3D::BlendShapeIndex blendShapeIndex = Scene3D::BlendShapeIndex::DownCast(motionIndex);
617 if(blendShapeIndex && blendShapeIndex.GetBlendShapeId().type == Property::Key::Type::STRING)
619 // Special case : For BlendShapeIndex that doesn't have ModelNodeId and has string BlendShapeId,
620 // we need to animate all kind of blendshapes
622 KeyFrames keyFrames = motionValue.GetKeyFrames();
626 std::vector<Scene3D::ModelNode> modelNodes;
627 RetrieveModelNodesByBlendShapeName(blendShapeIndex.GetBlendShapeId().stringKey, modelNodes);
629 for(auto& modelNode : modelNodes)
631 // Try to use index first. If failed, try to use name
632 Property::Index animatedPropertyIndex = motionIndex.GetPropertyIndex(modelNode);
633 if(animatedPropertyIndex != Property::INVALID_INDEX)
635 if(DALI_UNLIKELY(!animation))
637 animation = Animation::New(motionData.GetDuration());
639 animation.AnimateBetween(Dali::Property(modelNode, animatedPropertyIndex), keyFrames);
643 std::string animatedPropertyName = motionIndex.GetPropertyName(modelNode);
644 Dali::Property property(modelNode, animatedPropertyName);
646 if(property.propertyIndex != Property::INVALID_INDEX)
648 if(DALI_UNLIKELY(!animation))
650 animation = Animation::New(motionData.GetDuration());
652 animation.AnimateBetween(property, keyFrames);
666 void Model::SetMotionData(Scene3D::MotionData motionData)
668 // TODO : Need to collect duplicated codes with GenerateMotionDataAnimation()
672 const uint32_t motionCount = motionData.GetMotionCount();
673 for(uint32_t i = 0u; i < motionCount; ++i)
675 auto motionIndex = motionData.GetIndex(i);
676 auto motionValue = motionData.GetValue(i);
677 if(motionIndex && motionValue)
679 if(motionIndex.GetModelNodeId() != Property::INVALID_KEY)
681 Scene3D::ModelNode modelNode;
682 if(motionIndex.GetModelNodeId().type == Property::Key::Type::STRING)
684 modelNode = FindChildModelNodeByName(motionIndex.GetModelNodeId().stringKey);
686 else if(motionIndex.GetModelNodeId().type == Property::Key::Type::INDEX)
688 // TODO : Not implement yet.
693 Property::Value value = motionValue.GetPropertyValue();
695 if(value.GetType() != Property::Type::NONE)
697 // Try to use index first. If failed, try to use name
698 Property::Index propertyIndex = motionIndex.GetPropertyIndex(modelNode);
699 if(propertyIndex != Property::INVALID_INDEX)
701 modelNode.SetProperty(propertyIndex, value);
705 std::string propertyName = motionIndex.GetPropertyName(modelNode);
706 Dali::Property property(modelNode, propertyName);
707 if(property.propertyIndex != Property::INVALID_INDEX)
709 modelNode.SetProperty(property.propertyIndex, value);
717 Scene3D::BlendShapeIndex blendShapeIndex = Scene3D::BlendShapeIndex::DownCast(motionIndex);
718 if(blendShapeIndex && blendShapeIndex.GetBlendShapeId().type == Property::Key::Type::STRING)
720 // Special case : For BlendShapeIndex that doesn't have ModelNodeId and has string BlendShapeId,
721 // we need to animate all kind of blendshapes
723 Property::Value value = motionValue.GetPropertyValue();
725 if(value.GetType() != Property::Type::NONE)
727 std::vector<Scene3D::ModelNode> modelNodes;
728 RetrieveModelNodesByBlendShapeName(blendShapeIndex.GetBlendShapeId().stringKey, modelNodes);
730 for(auto& modelNode : modelNodes)
732 // Try to use index first. If failed, try to use name
733 Property::Index propertyIndex = motionIndex.GetPropertyIndex(modelNode);
734 if(propertyIndex != Property::INVALID_INDEX)
736 modelNode.SetProperty(propertyIndex, value);
740 std::string propertyName = motionIndex.GetPropertyName(modelNode);
741 Dali::Property property(modelNode, propertyName);
742 if(property.propertyIndex != Property::INVALID_INDEX)
744 modelNode.SetProperty(property.propertyIndex, value);
756 ///////////////////////////////////////////////////////////
761 void Model::OnInitialize()
763 // Make ParentOrigin as Center.
764 Self().SetProperty(Dali::Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
767 void Model::OnSceneConnection(int depth)
769 Actor parent = Self().GetParent();
772 // If this Model has parent SceneView and the its ShaderManager is same with privious ShaderManager,
773 // this Model don't need to update shader.
774 Scene3D::SceneView sceneView = Scene3D::SceneView::DownCast(parent);
777 mParentSceneView = sceneView;
778 GetImpl(sceneView).RegisterSceneItem(this);
779 Scene3D::Loader::ShaderManagerPtr shaderManager = GetImpl(sceneView).GetShaderManager();
780 if(mShaderManager != shaderManager)
782 mShaderManager = shaderManager;
783 UpdateShaderRecursively(mModelRoot, mShaderManager);
787 parent = parent.GetParent();
790 // Model can be added on Dali::Scene directly without SceneView.
791 // So, Model's mShaderManager and shaders of child ModelNodes are needed to be reset when this Model has not parent SceneView.
792 Scene3D::SceneView parentSceneView = mParentSceneView.GetHandle();
795 mShaderManager = new Dali::Scene3D::Loader::ShaderManager();
796 UpdateShaderRecursively(mModelRoot, mShaderManager);
799 if(!mModelLoadTask && !mModelResourceReady && !mModelUrl.empty())
801 // Request model load only if we setup url.
802 if(ModelCacheManager::Get())
804 ModelCacheManager::Get().ReferenceModelCache(mModelUrl);
806 mModelLoadTask = new ModelLoadTask(mModelUrl, mResourceDirectoryUrl, MakeCallback(this, &Model::OnModelLoadComplete));
807 Dali::AsyncTaskManager::Get().AddTask(mModelLoadTask);
810 // If diffuse and specular url is not valid, IBL does not need to be loaded.
811 if(!mDiffuseIblUrl.empty() && !mSpecularIblUrl.empty())
813 SetImageBasedLightSource(mDiffuseIblUrl, mSpecularIblUrl, mIblScaleFactor);
816 NotifyResourceReady();
818 mSizeNotification = Self().AddPropertyNotification(Actor::Property::SIZE, StepCondition(SIZE_STEP_CONDITION));
819 mSizeNotification.NotifySignal().Connect(this, &Model::OnSizeNotification);
820 Control::OnSceneConnection(depth);
823 void Model::OnSceneDisconnection()
825 // If mParentSceneView is still onScene, that means this model
826 // is disconnected from mParentSceneView's sub tree.
827 // So, Unregister this Model from SceneView.
828 Scene3D::SceneView sceneView = mParentSceneView.GetHandle();
829 if(sceneView && sceneView.GetProperty<bool>(Dali::Actor::Property::CONNECTED_TO_SCENE))
831 GetImpl(sceneView).UnregisterSceneItem(this);
832 mParentSceneView.Reset();
835 mSizeNotification.NotifySignal().Disconnect(this, &Model::OnSizeNotification);
836 Self().RemovePropertyNotification(mSizeNotification);
837 mSizeNotification.Reset();
839 Control::OnSceneDisconnection();
842 void Model::OnSizeSet(const Vector3& size)
847 Vector3 Model::GetNaturalSize()
851 DALI_LOG_ERROR("Model is still not loaded.\n");
852 return Vector3::ZERO;
858 float Model::GetHeightForWidth(float width)
861 padding = Self().GetProperty<Extents>(Toolkit::Control::Property::PADDING);
862 return Control::GetHeightForWidth(width) + padding.top + padding.bottom;
865 float Model::GetWidthForHeight(float height)
868 padding = Self().GetProperty<Extents>(Toolkit::Control::Property::PADDING);
869 return Control::GetWidthForHeight(height) + padding.start + padding.end;
872 void Model::OnRelayout(const Vector2& size, RelayoutContainer& container)
874 Control::OnRelayout(size, container);
878 bool Model::IsResourceReady() const
880 return mModelResourceReady && mIblDiffuseResourceReady && mIblSpecularResourceReady;
883 void Model::CreateModelRoot()
885 mModelRoot = Scene3D::ModelNode::New();
886 mModelRoot.SetProperty(Actor::Property::COLOR_MODE, ColorMode::USE_OWN_MULTIPLY_PARENT_COLOR);
887 mModelRoot.SetProperty(Dali::Actor::Property::SCALE, Y_DIRECTION);
888 mModelRoot.SetProperty(Dali::Actor::Property::SENSITIVE, mModelChildrenSensitive);
889 mModelRoot.SetProperty(Dali::Actor::Property::KEYBOARD_FOCUSABLE, mModelChildrenFocusable);
890 mModelRoot.SetProperty(Dali::DevelActor::Property::KEYBOARD_FOCUSABLE_CHILDREN, mModelChildrenFocusable);
891 Self().Add(mModelRoot);
894 void Model::ScaleModel(bool useCurrentSize)
902 Vector3 size = (useCurrentSize) ? Self().GetCurrentProperty<Vector3>(Dali::Actor::Property::SIZE) : Self().GetProperty<Vector3>(Dali::Actor::Property::SIZE);
903 if(size.x > 0.0f && size.y > 0.0f)
906 scale = std::min(size.x / mNaturalSize.x, scale);
907 scale = std::min(size.y / mNaturalSize.y, scale);
909 // Models in glTF and dli are defined as right hand coordinate system.
910 // DALi uses left hand coordinate system. Scaling negative is for change winding order.
911 mModelRoot.SetProperty(Dali::Actor::Property::SCALE, Y_DIRECTION * scale);
914 void Model::FitModelPosition()
920 // Loaded model pivot is not the model center.
921 mModelRoot.SetProperty(Dali::Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
922 mModelRoot.SetProperty(Dali::Actor::Property::ANCHOR_POINT, Vector3::ONE - mModelPivot);
925 void Model::UpdateImageBasedLightTextureRecursively(Scene3D::ModelNode node, Dali::Texture diffuseTexture, Dali::Texture specularTexture, float iblScaleFactor, uint32_t specularMipmapLevels)
932 GetImplementation(node).SetImageBasedLightTexture(diffuseTexture, specularTexture, iblScaleFactor, specularMipmapLevels);
933 uint32_t childrenCount = node.GetChildCount();
934 for(uint32_t i = 0; i < childrenCount; ++i)
936 Scene3D::ModelNode childNode = Scene3D::ModelNode::DownCast(node.GetChildAt(i));
941 UpdateImageBasedLightTextureRecursively(childNode, diffuseTexture, specularTexture, iblScaleFactor, specularMipmapLevels);
945 void Model::UpdateImageBasedLightScaleFactorRecursively(Scene3D::ModelNode node, float iblScaleFactor)
952 GetImplementation(node).SetImageBasedLightScaleFactor(iblScaleFactor);
954 uint32_t childrenCount = node.GetChildCount();
955 for(uint32_t i = 0; i < childrenCount; ++i)
957 Scene3D::ModelNode childNode = Scene3D::ModelNode::DownCast(node.GetChildAt(i));
962 UpdateImageBasedLightScaleFactorRecursively(childNode, iblScaleFactor);
966 void Model::UpdateImageBasedLightTexture()
968 Dali::Texture currentDiffuseTexture = (mDiffuseTexture && mSpecularTexture) ? mDiffuseTexture : mSceneDiffuseTexture;
969 Dali::Texture currentSpecularTexture = (mDiffuseTexture && mSpecularTexture) ? mSpecularTexture : mSceneSpecularTexture;
970 float currentIblScaleFactor = (mDiffuseTexture && mSpecularTexture) ? mIblScaleFactor : mSceneIblScaleFactor;
971 uint32_t currentIblSpecularMipmapLevels = (mDiffuseTexture && mSpecularTexture) ? mSpecularMipmapLevels : mSceneSpecularMipmapLevels;
973 if(!currentDiffuseTexture || !currentSpecularTexture)
975 currentDiffuseTexture = mDefaultDiffuseTexture;
976 currentSpecularTexture = mDefaultSpecularTexture;
977 currentIblScaleFactor = Dali::Scene3D::Loader::EnvironmentDefinition::GetDefaultIntensity();
978 currentIblSpecularMipmapLevels = 1u;
981 UpdateImageBasedLightTextureRecursively(mModelRoot, currentDiffuseTexture, currentSpecularTexture, currentIblScaleFactor, currentIblSpecularMipmapLevels);
984 void Model::UpdateImageBasedLightScaleFactor()
986 if((!mDiffuseTexture || !mSpecularTexture) &&
987 (!mSceneDiffuseTexture || !mSceneSpecularTexture))
992 float currentIblScaleFactor = (mDiffuseTexture && mSpecularTexture) ? mIblScaleFactor : mSceneIblScaleFactor;
993 UpdateImageBasedLightScaleFactorRecursively(mModelRoot, currentIblScaleFactor);
996 void Model::ApplyCameraTransform(Dali::CameraActor camera) const
998 Vector3 selfPosition = Self().GetProperty<Vector3>(Actor::Property::POSITION);
999 Quaternion selfOrientation = Self().GetProperty<Quaternion>(Actor::Property::ORIENTATION);
1000 Vector3 selfScale = Self().GetProperty<Vector3>(Actor::Property::SCALE);
1002 Vector3 cameraPosition = camera.GetProperty<Vector3>(Actor::Property::POSITION);
1003 Quaternion cameraOrientation = camera.GetProperty<Quaternion>(Actor::Property::ORIENTATION);
1004 Vector3 cameraScale = camera.GetProperty<Vector3>(Actor::Property::SCALE);
1006 // Models in glTF and dli are defined as right hand coordinate system.
1007 // DALi uses left hand coordinate system. Scaling negative is for change winding order.
1008 if(!Dali::Equals(Y_DIRECTION.Dot(Vector3::YAXIS), 1.0f))
1010 // Reflect by XZ plane
1011 cameraPosition.y = -cameraPosition.y;
1012 Quaternion yDirectionQuaternion;
1013 yDirectionQuaternion.mVector = Vector3::YAXIS;
1014 // Reflect orientation
1015 cameraOrientation = yDirectionQuaternion * cameraOrientation * yDirectionQuaternion;
1018 Vector3 resultPosition;
1019 Quaternion resultOrientation;
1020 Vector3 resultScale;
1022 Matrix selfMatrix(false);
1023 Matrix cameraMatrix(false);
1024 Matrix resultMatrix(false);
1025 selfMatrix.SetTransformComponents(selfScale, selfOrientation, selfPosition);
1026 cameraMatrix.SetTransformComponents(cameraScale, cameraOrientation, cameraPosition);
1027 Matrix::Multiply(resultMatrix, cameraMatrix, selfMatrix);
1028 resultMatrix.GetTransformComponents(resultPosition, resultOrientation, resultScale);
1030 camera.SetProperty(Actor::Property::POSITION, resultPosition);
1031 camera.SetProperty(Actor::Property::ORIENTATION, resultOrientation);
1032 camera.SetProperty(Actor::Property::SCALE, resultScale);
1035 void Model::NotifyShadowMapTexture(Dali::Texture shadowMapTexture)
1037 if(mShadowMapTexture != shadowMapTexture)
1039 mShadowMapTexture = shadowMapTexture;
1040 UpdateShadowMapTextureRecursively(mModelRoot, mShadowMapTexture);
1044 void Model::NotifyImageBasedLightTexture(Dali::Texture diffuseTexture, Dali::Texture specularTexture, float scaleFactor, uint32_t specularMipmapLevels)
1046 if(mSceneDiffuseTexture != diffuseTexture || mSceneSpecularTexture != specularTexture)
1048 mSceneDiffuseTexture = diffuseTexture;
1049 mSceneSpecularTexture = specularTexture;
1050 mSceneIblScaleFactor = scaleFactor;
1051 mSceneSpecularMipmapLevels = specularMipmapLevels;
1052 // If Model IBL is not set, use SceneView's IBL.
1053 if(!mDiffuseTexture || !mSpecularTexture)
1055 UpdateImageBasedLightTexture();
1060 void Model::NotifyImageBasedLightScaleFactor(float scaleFactor)
1062 mSceneIblScaleFactor = scaleFactor;
1063 if(mSceneDiffuseTexture && mSceneSpecularTexture)
1065 UpdateImageBasedLightScaleFactor();
1069 void Model::OnModelLoadComplete()
1071 IntrusivePtr<Model> self = this; // Keep reference until this API finished
1073 if(!mModelLoadTask->HasSucceeded())
1075 ResetResourceTasks();
1077 if(ModelCacheManager::Get() && !mModelUrl.empty())
1079 ModelCacheManager::Get().UnreferenceModelCache(mModelUrl);
1091 auto& resources = mModelLoadTask->GetResources();
1092 auto& scene = mModelLoadTask->GetScene();
1093 CreateAnimations(scene);
1094 ResetCameraParameters();
1095 if(!resources.mEnvironmentMaps.empty())
1097 mDefaultDiffuseTexture = resources.mEnvironmentMaps.front().second.mDiffuse;
1098 mDefaultSpecularTexture = resources.mEnvironmentMaps.front().second.mSpecular;
1101 if(mShadowMapTexture)
1103 UpdateShadowMapTextureRecursively(mModelRoot, mShadowMapTexture);
1105 UpdateImageBasedLightTexture();
1106 UpdateImageBasedLightScaleFactor();
1107 Self().SetProperty(Dali::Actor::Property::ANCHOR_POINT, Vector3(mModelPivot.x, 1.0f - mModelPivot.y, mModelPivot.z));
1109 mModelResourceReady = true;
1110 ResetResourceTask(mModelLoadTask);
1111 NotifyResourceReady();
1114 void Model::OnIblDiffuseLoadComplete()
1116 mDiffuseTexture = mIblDiffuseLoadTask->GetLoadedTexture();
1117 ResetResourceTask(mIblDiffuseLoadTask);
1118 mIblDiffuseResourceReady = true;
1119 if(mIblDiffuseResourceReady && mIblSpecularResourceReady)
1121 OnIblLoadComplete();
1125 void Model::OnIblSpecularLoadComplete()
1127 mSpecularTexture = mIblSpecularLoadTask->GetLoadedTexture();
1128 mSpecularMipmapLevels = mIblSpecularLoadTask->GetMipmapLevels();
1129 ResetResourceTask(mIblSpecularLoadTask);
1130 mIblSpecularResourceReady = true;
1131 if(mIblDiffuseResourceReady && mIblSpecularResourceReady)
1133 OnIblLoadComplete();
1137 void Model::OnIblLoadComplete()
1139 UpdateImageBasedLightTexture();
1140 NotifyResourceReady();
1143 void Model::OnSizeNotification(Dali::PropertyNotification& source)
1148 void Model::ResetResourceTasks()
1150 if(!Dali::Adaptor::IsAvailable())
1154 ResetResourceTask(mModelLoadTask);
1155 ResetResourceTask(mIblDiffuseLoadTask);
1156 ResetResourceTask(mIblSpecularLoadTask);
1159 void Model::ResetResourceTask(IntrusivePtr<AsyncTask> asyncTask)
1165 Dali::AsyncTaskManager::Get().RemoveTask(asyncTask);
1169 void Model::NotifyResourceReady()
1171 if(!IsResourceReady())
1175 Control::SetResourceReady();
1178 void Model::CreateModel()
1180 BoundingVolume AABB;
1181 auto& resources = mModelLoadTask->GetResources();
1182 auto& scene = mModelLoadTask->GetScene();
1183 auto& resourceChoices = mModelLoadTask->GetResourceChoices();
1184 Dali::Scene3D::Loader::Transforms xforms{Dali::Scene3D::Loader::MatrixStack{}, Dali::Scene3D::Loader::ViewProjection{}};
1186 Dali::Scene3D::Loader::NodeDefinition::CreateParams nodeParams{resources, xforms, mShaderManager, {}, {}, {}};
1188 // Generate Dali handles from resource bundle. Note that we generate all scene's resouce immediatly.
1189 resources.GenerateResources();
1190 for(auto iRoot : scene.GetRoots())
1192 if(auto modelNode = scene.CreateNodes(iRoot, resourceChoices, nodeParams))
1194 scene.ConfigureSkinningShaders(resources, modelNode, std::move(nodeParams.mSkinnables));
1195 ConfigureBlendShapeShaders(resources, scene, modelNode, std::move(nodeParams.mBlendshapeRequests));
1197 scene.ApplyConstraints(modelNode, std::move(nodeParams.mConstrainables));
1199 mModelRoot.Add(modelNode);
1202 AddModelTreeToAABB(AABB, scene, resourceChoices, iRoot, nodeParams, Matrix::IDENTITY);
1205 UpdateBlendShapeNodeMap();
1207 mNaturalSize = AABB.CalculateSize();
1208 mModelPivot = AABB.CalculatePivot();
1209 mModelRoot.SetProperty(Dali::Actor::Property::SIZE, mNaturalSize);
1210 Vector3 controlSize = Self().GetProperty<Vector3>(Dali::Actor::Property::SIZE);
1211 if(Dali::EqualsZero(controlSize.x) || Dali::EqualsZero(controlSize.y))
1213 Self().SetProperty(Dali::Actor::Property::SIZE, mNaturalSize);
1219 void Model::CreateAnimations(Dali::Scene3D::Loader::SceneDefinition& scene)
1221 mAnimations.clear();
1222 if(!mModelLoadTask->GetAnimations().empty())
1224 auto getActor = [&](const Scene3D::Loader::AnimatedProperty& property) {
1225 if(property.mNodeIndex == Scene3D::Loader::INVALID_INDEX)
1227 return mModelRoot.FindChildByName(property.mNodeName);
1229 auto* node = scene.GetNode(property.mNodeIndex);
1232 return Dali::Actor();
1234 return mModelRoot.FindChildById(node->mNodeId);
1237 for(auto&& animation : mModelLoadTask->GetAnimations())
1239 Dali::Animation anim = animation.ReAnimate(getActor);
1240 mAnimations.push_back({animation.GetName(), anim});
1245 void Model::ResetCameraParameters()
1247 mCameraParameters.clear();
1248 if(!mModelLoadTask->GetCameras().empty())
1250 // Copy camera parameters.
1251 std::copy(mModelLoadTask->GetCameras().begin(), mModelLoadTask->GetCameras().end(), std::back_inserter(mCameraParameters));
1255 void Model::UpdateBlendShapeNodeMap()
1257 // Remove privous node map
1258 mBlendShapeModelNodeMap.clear();
1260 UpdateBlendShapeNodeMapRecursively(mBlendShapeModelNodeMap, mModelRoot);
1263 } // namespace Internal
1264 } // namespace Scene3D