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>
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 AddLightRecursively(Scene3D::ModelNode node, Scene3D::Light light, uint32_t lightIndex)
181 GetImplementation(node).AddLight(light, lightIndex);
183 uint32_t childrenCount = node.GetChildCount();
184 for(uint32_t i = 0; i < childrenCount; ++i)
186 Scene3D::ModelNode childNode = Scene3D::ModelNode::DownCast(node.GetChildAt(i));
189 AddLightRecursively(childNode, light, lightIndex);
194 void RemoveLightRecursively(Scene3D::ModelNode node, uint32_t lightIndex)
201 GetImplementation(node).RemoveLight(lightIndex);
203 uint32_t childrenCount = node.GetChildCount();
204 for(uint32_t i = 0; i < childrenCount; ++i)
206 Scene3D::ModelNode childNode = Scene3D::ModelNode::DownCast(node.GetChildAt(i));
209 RemoveLightRecursively(childNode, lightIndex);
214 void UpdateBlendShapeNodeMapRecursively(Model::BlendShapeModelNodeMap& resultMap, const Scene3D::ModelNode& node)
220 const auto childCount = node.GetChildCount();
221 for(auto i = 0u; i < childCount; ++i)
223 UpdateBlendShapeNodeMapRecursively(resultMap, Scene3D::ModelNode::DownCast(node.GetChildAt(i)));
226 std::vector<std::string> blendShapeNames;
227 node.RetrieveBlendShapeNames(blendShapeNames);
228 for(const auto& iter : blendShapeNames)
230 // Append or create new list.
231 resultMap[iter].push_back(node);
235 } // anonymous namespace
237 Model::Model(const std::string& modelUrl, const std::string& resourceDirectoryUrl)
238 : Control(ControlBehaviour(DISABLE_SIZE_NEGOTIATION | DISABLE_STYLE_CHANGE_SIGNALS)),
240 mResourceDirectoryUrl(resourceDirectoryUrl),
242 mNaturalSize(Vector3::ZERO),
243 mModelPivot(AnchorPoint::CENTER),
244 mSceneIblScaleFactor(1.0f),
245 mIblScaleFactor(1.0f),
246 mSceneSpecularMipmapLevels(1u),
247 mSpecularMipmapLevels(1u),
248 mModelChildrenSensitive(DEFAULT_MODEL_CHILDREN_SENSITIVE),
249 mModelChildrenFocusable(DEFAULT_MODEL_CHILDREN_FOCUSABLE),
250 mModelResourceReady(false),
251 mIblDiffuseResourceReady(true),
252 mIblSpecularResourceReady(true),
253 mIblDiffuseDirty(false),
254 mIblSpecularDirty(false)
260 ResetResourceTasks();
262 if(ModelCacheManager::Get() && !mModelUrl.empty())
264 ModelCacheManager::Get().UnreferenceModelCache(mModelUrl);
268 Dali::Scene3D::Model Model::New(const std::string& modelUrl, const std::string& resourceDirectoryUrl)
270 Model* impl = new Model(modelUrl, resourceDirectoryUrl);
272 Dali::Scene3D::Model handle = Dali::Scene3D::Model(*impl);
274 // Second-phase init of the implementation
275 // This can only be done after the CustomActor connection has been made...
281 const Scene3D::ModelNode Model::GetModelRoot() const
286 void Model::AddModelNode(Scene3D::ModelNode modelNode)
293 mModelRoot.Add(modelNode);
294 if(mModelUrl.empty())
296 mModelResourceReady = true;
299 if(mIblDiffuseResourceReady && mIblSpecularResourceReady)
301 UpdateImageBasedLightTexture();
302 UpdateImageBasedLightScaleFactor();
305 uint32_t maxLightCount = Scene3D::Internal::Light::GetMaximumEnabledLightCount();
306 for(uint32_t i = 0; i < maxLightCount; ++i)
310 AddLightRecursively(modelNode, mLights[i], i);
314 if(Self().GetProperty<bool>(Dali::Actor::Property::CONNECTED_TO_SCENE))
316 NotifyResourceReady();
320 void Model::RemoveModelNode(Scene3D::ModelNode modelNode)
324 uint32_t maxLightCount = Scene3D::Internal::Light::GetMaximumEnabledLightCount();
325 for(uint32_t i = 0; i < maxLightCount; ++i)
329 RemoveLightRecursively(modelNode, i);
332 mModelRoot.Remove(modelNode);
336 void Model::SetChildrenSensitive(bool enable)
338 if(mModelChildrenSensitive != enable)
340 mModelChildrenSensitive = enable;
343 mModelRoot.SetProperty(Dali::Actor::Property::SENSITIVE, mModelChildrenSensitive);
348 bool Model::GetChildrenSensitive() const
350 return mModelChildrenSensitive;
353 void Model::SetChildrenFocusable(bool enable)
355 if(mModelChildrenFocusable != enable)
357 mModelChildrenFocusable = enable;
360 mModelRoot.SetProperty(Dali::Actor::Property::KEYBOARD_FOCUSABLE, mModelChildrenFocusable);
361 mModelRoot.SetProperty(Dali::DevelActor::Property::KEYBOARD_FOCUSABLE_CHILDREN, mModelChildrenFocusable);
366 bool Model::GetChildrenFocusable() const
368 return mModelChildrenFocusable;
371 void Model::SetImageBasedLightSource(const std::string& diffuseUrl, const std::string& specularUrl, float scaleFactor)
373 bool needIblReset = false;
374 bool isOnScene = Self().GetProperty<bool>(Dali::Actor::Property::CONNECTED_TO_SCENE);
375 if(mDiffuseIblUrl != diffuseUrl)
377 mDiffuseIblUrl = diffuseUrl;
378 if(mDiffuseIblUrl.empty())
384 mIblDiffuseDirty = true;
385 mIblDiffuseResourceReady = false;
389 if(mSpecularIblUrl != specularUrl)
391 mSpecularIblUrl = specularUrl;
392 if(mSpecularIblUrl.empty())
398 mIblSpecularDirty = true;
399 mIblSpecularResourceReady = false;
403 // If one or both of diffuse url and specular url are empty,
404 // we don't need to request to load texture.
407 ResetResourceTask(mIblDiffuseLoadTask);
408 ResetResourceTask(mIblSpecularLoadTask);
410 mIblDiffuseDirty = false;
411 mIblSpecularDirty = false;
412 mIblDiffuseResourceReady = true;
413 mIblSpecularResourceReady = true;
415 mDiffuseTexture.Reset();
416 mSpecularTexture.Reset();
417 UpdateImageBasedLightTexture();
421 if(isOnScene && mIblDiffuseDirty)
423 ResetResourceTask(mIblDiffuseLoadTask);
424 mIblDiffuseLoadTask = new EnvironmentMapLoadTask(mDiffuseIblUrl, Scene3D::EnvironmentMapType::CUBEMAP, MakeCallback(this, &Model::OnIblDiffuseLoadComplete));
425 Dali::AsyncTaskManager::Get().AddTask(mIblDiffuseLoadTask);
426 mIblDiffuseDirty = false;
429 if(isOnScene && mIblSpecularDirty)
431 ResetResourceTask(mIblSpecularLoadTask);
432 mIblSpecularLoadTask = new EnvironmentMapLoadTask(mSpecularIblUrl, Scene3D::EnvironmentMapType::CUBEMAP, MakeCallback(this, &Model::OnIblSpecularLoadComplete));
433 Dali::AsyncTaskManager::Get().AddTask(mIblSpecularLoadTask);
434 mIblSpecularDirty = false;
438 if(!Dali::Equals(mIblScaleFactor, scaleFactor))
440 mIblScaleFactor = scaleFactor;
441 UpdateImageBasedLightScaleFactor();
444 // If diffuse and specular textures are already loaded, emits resource ready signal here.
445 NotifyResourceReady();
448 void Model::SetImageBasedLightScaleFactor(float scaleFactor)
450 mIblScaleFactor = scaleFactor;
451 if(mDiffuseTexture && mSpecularTexture)
453 UpdateImageBasedLightScaleFactor();
457 float Model::GetImageBasedLightScaleFactor() const
459 return mIblScaleFactor;
462 uint32_t Model::GetAnimationCount() const
464 return mAnimations.size();
467 Dali::Animation Model::GetAnimation(uint32_t index) const
469 Dali::Animation animation;
470 if(mAnimations.size() > index)
472 animation = mAnimations[index].second;
477 Dali::Animation Model::GetAnimation(const std::string& name) const
479 Dali::Animation animation;
482 for(auto&& animationData : mAnimations)
484 if(animationData.first == name)
486 animation = animationData.second;
494 uint32_t Model::GetCameraCount() const
496 return mCameraParameters.size();
499 Dali::CameraActor Model::GenerateCamera(uint32_t index) const
501 Dali::CameraActor camera;
502 if(mCameraParameters.size() > index)
504 camera = Dali::CameraActor::New3DCamera();
505 if(!mCameraParameters[index].ConfigureCamera(camera, false))
507 DALI_LOG_ERROR("Fail to generate %u's camera actor : Some property was not defined. Please check model file.\n", index);
512 ApplyCameraTransform(camera);
517 bool Model::ApplyCamera(uint32_t index, Dali::CameraActor camera) const
519 if(camera && mCameraParameters.size() > index)
521 if(!mCameraParameters[index].ConfigureCamera(camera, false))
523 DALI_LOG_ERROR("Fail to apply %u's camera actor : Some property was not defined. Please check model file.\n", index);
527 ApplyCameraTransform(camera);
533 Scene3D::ModelNode Model::FindChildModelNodeByName(std::string_view nodeName)
535 Actor childActor = Self().FindChildByName(nodeName);
536 return Scene3D::ModelNode::DownCast(childActor);
539 void Model::RetrieveBlendShapeNames(std::vector<std::string>& blendShapeNames) const
541 blendShapeNames.reserve(blendShapeNames.size() + mBlendShapeModelNodeMap.size());
542 for(const auto& iter : mBlendShapeModelNodeMap)
544 blendShapeNames.push_back(iter.first);
548 void Model::RetrieveModelNodesByBlendShapeName(std::string_view blendShapeName, std::vector<Scene3D::ModelNode>& modelNodes) const
550 auto iter = mBlendShapeModelNodeMap.find(std::string(blendShapeName));
551 if(iter != mBlendShapeModelNodeMap.end())
553 const auto& modelNodeList = iter->second;
554 modelNodes.reserve(modelNodes.size() + modelNodeList.size());
555 for(const auto& nodeIter : modelNodeList)
557 modelNodes.push_back(nodeIter);
562 Dali::Animation Model::GenerateMotionDataAnimation(Scene3D::MotionData motionData)
564 Dali::Animation animation;
566 // TODO : Collect duplicated codes.
570 const uint32_t motionCount = motionData.GetMotionCount();
571 for(uint32_t i = 0u; i < motionCount; ++i)
573 auto motionIndex = motionData.GetIndex(i);
574 auto motionValue = motionData.GetValue(i);
575 if(motionIndex && motionValue)
577 if(motionIndex.GetModelNodeId() != Property::INVALID_KEY)
579 Scene3D::ModelNode modelNode;
580 if(motionIndex.GetModelNodeId().type == Property::Key::Type::STRING)
582 modelNode = FindChildModelNodeByName(motionIndex.GetModelNodeId().stringKey);
584 else if(motionIndex.GetModelNodeId().type == Property::Key::Type::INDEX)
586 // TODO : Not implement yet.
591 KeyFrames keyFrames = motionValue.GetKeyFrames();
595 // Try to use index first. If failed, try to use name
596 Property::Index animatedPropertyIndex = motionIndex.GetPropertyIndex(modelNode);
597 if(animatedPropertyIndex != Property::INVALID_INDEX)
599 if(DALI_UNLIKELY(!animation))
601 animation = Animation::New(motionData.GetDuration());
603 animation.AnimateBetween(Dali::Property(modelNode, animatedPropertyIndex), keyFrames);
607 std::string animatedPropertyName = motionIndex.GetPropertyName(modelNode);
608 Dali::Property property(modelNode, animatedPropertyName);
609 if(property.propertyIndex != Property::INVALID_INDEX)
611 if(DALI_UNLIKELY(!animation))
613 animation = Animation::New(motionData.GetDuration());
615 animation.AnimateBetween(property, keyFrames);
623 Scene3D::BlendShapeIndex blendShapeIndex = Scene3D::BlendShapeIndex::DownCast(motionIndex);
624 if(blendShapeIndex && blendShapeIndex.GetBlendShapeId().type == Property::Key::Type::STRING)
626 // Special case : For BlendShapeIndex that doesn't have ModelNodeId and has string BlendShapeId,
627 // we need to animate all kind of blendshapes
629 KeyFrames keyFrames = motionValue.GetKeyFrames();
633 std::vector<Scene3D::ModelNode> modelNodes;
634 RetrieveModelNodesByBlendShapeName(blendShapeIndex.GetBlendShapeId().stringKey, modelNodes);
636 for(auto& modelNode : modelNodes)
638 // Try to use index first. If failed, try to use name
639 Property::Index animatedPropertyIndex = motionIndex.GetPropertyIndex(modelNode);
640 if(animatedPropertyIndex != Property::INVALID_INDEX)
642 if(DALI_UNLIKELY(!animation))
644 animation = Animation::New(motionData.GetDuration());
646 animation.AnimateBetween(Dali::Property(modelNode, animatedPropertyIndex), keyFrames);
650 std::string animatedPropertyName = motionIndex.GetPropertyName(modelNode);
651 Dali::Property property(modelNode, animatedPropertyName);
653 if(property.propertyIndex != Property::INVALID_INDEX)
655 if(DALI_UNLIKELY(!animation))
657 animation = Animation::New(motionData.GetDuration());
659 animation.AnimateBetween(property, keyFrames);
673 void Model::SetMotionData(Scene3D::MotionData motionData)
675 // TODO : Collect duplicated codes.
678 const uint32_t motionCount = motionData.GetMotionCount();
679 for(uint32_t i = 0u; i < motionCount; ++i)
681 auto motionIndex = motionData.GetIndex(i);
682 auto motionValue = motionData.GetValue(i);
683 if(motionIndex && motionValue)
685 if(motionIndex.GetModelNodeId() != Property::INVALID_KEY)
687 Scene3D::ModelNode modelNode;
688 if(motionIndex.GetModelNodeId().type == Property::Key::Type::STRING)
690 modelNode = FindChildModelNodeByName(motionIndex.GetModelNodeId().stringKey);
692 else if(motionIndex.GetModelNodeId().type == Property::Key::Type::INDEX)
694 // TODO : Not implement yet.
699 Property::Value value = motionValue.GetPropertyValue();
701 if(value.GetType() != Property::Type::NONE)
703 // Try to use index first. If failed, try to use name
704 Property::Index propertyIndex = motionIndex.GetPropertyIndex(modelNode);
705 if(propertyIndex != Property::INVALID_INDEX)
707 modelNode.SetProperty(propertyIndex, value);
711 std::string propertyName = motionIndex.GetPropertyName(modelNode);
712 Dali::Property property(modelNode, propertyName);
713 if(property.propertyIndex != Property::INVALID_INDEX)
715 modelNode.SetProperty(property.propertyIndex, value);
723 Scene3D::BlendShapeIndex blendShapeIndex = Scene3D::BlendShapeIndex::DownCast(motionIndex);
724 if(blendShapeIndex && blendShapeIndex.GetBlendShapeId().type == Property::Key::Type::STRING)
726 // Special case : For BlendShapeIndex that doesn't have ModelNodeId and has string BlendShapeId,
727 // we need to animate all kind of blendshapes
729 Property::Value value = motionValue.GetPropertyValue();
731 if(value.GetType() != Property::Type::NONE)
733 std::vector<Scene3D::ModelNode> modelNodes;
734 RetrieveModelNodesByBlendShapeName(blendShapeIndex.GetBlendShapeId().stringKey, modelNodes);
736 for(auto& modelNode : modelNodes)
738 // Try to use index first. If failed, try to use name
739 Property::Index propertyIndex = motionIndex.GetPropertyIndex(modelNode);
740 if(propertyIndex != Property::INVALID_INDEX)
742 modelNode.SetProperty(propertyIndex, value);
746 std::string propertyName = motionIndex.GetPropertyName(modelNode);
747 Dali::Property property(modelNode, propertyName);
748 if(property.propertyIndex != Property::INVALID_INDEX)
750 modelNode.SetProperty(property.propertyIndex, value);
762 ///////////////////////////////////////////////////////////
767 void Model::OnInitialize()
769 // Make ParentOrigin as Center.
770 Self().SetProperty(Dali::Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
771 mLights.resize(Scene3D::Internal::Light::GetMaximumEnabledLightCount());
774 void Model::OnSceneConnection(int depth)
776 if(!mModelLoadTask && !mModelResourceReady && !mModelUrl.empty())
778 // Request model load only if we setup url.
779 if(ModelCacheManager::Get())
781 ModelCacheManager::Get().ReferenceModelCache(mModelUrl);
783 mModelLoadTask = new ModelLoadTask(mModelUrl, mResourceDirectoryUrl, MakeCallback(this, &Model::OnModelLoadComplete));
784 Dali::AsyncTaskManager::Get().AddTask(mModelLoadTask);
787 // If diffuse and specular url is not valid, IBL does not need to be loaded.
788 if(!mDiffuseIblUrl.empty() && !mSpecularIblUrl.empty())
790 SetImageBasedLightSource(mDiffuseIblUrl, mSpecularIblUrl, mIblScaleFactor);
793 Actor parent = Self().GetParent();
796 Scene3D::SceneView sceneView = Scene3D::SceneView::DownCast(parent);
799 GetImpl(sceneView).RegisterSceneItem(this);
800 mParentSceneView = sceneView;
803 parent = parent.GetParent();
806 NotifyResourceReady();
808 mSizeNotification = Self().AddPropertyNotification(Actor::Property::SIZE, StepCondition(SIZE_STEP_CONDITION));
809 mSizeNotification.NotifySignal().Connect(this, &Model::OnSizeNotification);
810 Control::OnSceneConnection(depth);
813 void Model::OnSceneDisconnection()
815 Scene3D::SceneView sceneView = mParentSceneView.GetHandle();
818 GetImpl(sceneView).UnregisterSceneItem(this);
819 mParentSceneView.Reset();
822 mSizeNotification.NotifySignal().Disconnect(this, &Model::OnSizeNotification);
823 Self().RemovePropertyNotification(mSizeNotification);
824 mSizeNotification.Reset();
826 Control::OnSceneDisconnection();
829 void Model::OnSizeSet(const Vector3& size)
834 Vector3 Model::GetNaturalSize()
838 DALI_LOG_ERROR("Model is still not loaded.\n");
839 return Vector3::ZERO;
845 float Model::GetHeightForWidth(float width)
848 padding = Self().GetProperty<Extents>(Toolkit::Control::Property::PADDING);
849 return Control::GetHeightForWidth(width) + padding.top + padding.bottom;
852 float Model::GetWidthForHeight(float height)
855 padding = Self().GetProperty<Extents>(Toolkit::Control::Property::PADDING);
856 return Control::GetWidthForHeight(height) + padding.start + padding.end;
859 void Model::OnRelayout(const Vector2& size, RelayoutContainer& container)
861 Control::OnRelayout(size, container);
865 bool Model::IsResourceReady() const
867 return mModelResourceReady && mIblDiffuseResourceReady && mIblSpecularResourceReady;
870 void Model::CreateModelRoot()
872 mModelRoot = Scene3D::ModelNode::New();
873 mModelRoot.SetProperty(Actor::Property::COLOR_MODE, ColorMode::USE_OWN_MULTIPLY_PARENT_COLOR);
874 mModelRoot.SetProperty(Dali::Actor::Property::SCALE, Y_DIRECTION);
875 mModelRoot.SetProperty(Dali::Actor::Property::SENSITIVE, mModelChildrenSensitive);
876 mModelRoot.SetProperty(Dali::Actor::Property::KEYBOARD_FOCUSABLE, mModelChildrenFocusable);
877 mModelRoot.SetProperty(Dali::DevelActor::Property::KEYBOARD_FOCUSABLE_CHILDREN, mModelChildrenFocusable);
878 Self().Add(mModelRoot);
881 void Model::ScaleModel(bool useCurrentSize)
889 Vector3 size = (useCurrentSize) ? Self().GetCurrentProperty<Vector3>(Dali::Actor::Property::SIZE) : Self().GetProperty<Vector3>(Dali::Actor::Property::SIZE);
890 if(size.x > 0.0f && size.y > 0.0f)
893 scale = std::min(size.x / mNaturalSize.x, scale);
894 scale = std::min(size.y / mNaturalSize.y, scale);
896 // Models in glTF and dli are defined as right hand coordinate system.
897 // DALi uses left hand coordinate system. Scaling negative is for change winding order.
898 mModelRoot.SetProperty(Dali::Actor::Property::SCALE, Y_DIRECTION * scale);
901 void Model::FitModelPosition()
907 // Loaded model pivot is not the model center.
908 mModelRoot.SetProperty(Dali::Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
909 mModelRoot.SetProperty(Dali::Actor::Property::ANCHOR_POINT, Vector3::ONE - mModelPivot);
912 void Model::UpdateImageBasedLightTextureRecursively(Scene3D::ModelNode node, Dali::Texture diffuseTexture, Dali::Texture specularTexture, float iblScaleFactor, uint32_t specularMipmapLevels)
919 GetImplementation(node).SetImageBasedLightTexture(diffuseTexture, specularTexture, iblScaleFactor, specularMipmapLevels);
920 uint32_t childrenCount = node.GetChildCount();
921 for(uint32_t i = 0; i < childrenCount; ++i)
923 Scene3D::ModelNode childNode = Scene3D::ModelNode::DownCast(node.GetChildAt(i));
928 UpdateImageBasedLightTextureRecursively(childNode, diffuseTexture, specularTexture, iblScaleFactor, specularMipmapLevels);
932 void Model::UpdateImageBasedLightScaleFactorRecursively(Scene3D::ModelNode node, float iblScaleFactor)
939 GetImplementation(node).SetImageBasedLightScaleFactor(iblScaleFactor);
941 uint32_t childrenCount = node.GetChildCount();
942 for(uint32_t i = 0; i < childrenCount; ++i)
944 Scene3D::ModelNode childNode = Scene3D::ModelNode::DownCast(node.GetChildAt(i));
949 UpdateImageBasedLightScaleFactorRecursively(childNode, iblScaleFactor);
953 void Model::UpdateImageBasedLightTexture()
955 Dali::Texture currentDiffuseTexture = (mDiffuseTexture && mSpecularTexture) ? mDiffuseTexture : mSceneDiffuseTexture;
956 Dali::Texture currentSpecularTexture = (mDiffuseTexture && mSpecularTexture) ? mSpecularTexture : mSceneSpecularTexture;
957 float currentIblScaleFactor = (mDiffuseTexture && mSpecularTexture) ? mIblScaleFactor : mSceneIblScaleFactor;
958 uint32_t currentIblSpecularMipmapLevels = (mDiffuseTexture && mSpecularTexture) ? mSpecularMipmapLevels : mSceneSpecularMipmapLevels;
960 if(!currentDiffuseTexture || !currentSpecularTexture)
962 currentDiffuseTexture = mDefaultDiffuseTexture;
963 currentSpecularTexture = mDefaultSpecularTexture;
964 currentIblScaleFactor = Dali::Scene3D::Loader::EnvironmentDefinition::GetDefaultIntensity();
965 currentIblSpecularMipmapLevels = 1u;
968 UpdateImageBasedLightTextureRecursively(mModelRoot, currentDiffuseTexture, currentSpecularTexture, currentIblScaleFactor, currentIblSpecularMipmapLevels);
971 void Model::UpdateImageBasedLightScaleFactor()
973 if((!mDiffuseTexture || !mSpecularTexture) &&
974 (!mSceneDiffuseTexture || !mSceneSpecularTexture))
979 float currentIblScaleFactor = (mDiffuseTexture && mSpecularTexture) ? mIblScaleFactor : mSceneIblScaleFactor;
980 UpdateImageBasedLightScaleFactorRecursively(mModelRoot, currentIblScaleFactor);
983 void Model::ApplyCameraTransform(Dali::CameraActor camera) const
985 Vector3 selfPosition = Self().GetProperty<Vector3>(Actor::Property::POSITION);
986 Quaternion selfOrientation = Self().GetProperty<Quaternion>(Actor::Property::ORIENTATION);
987 Vector3 selfScale = Self().GetProperty<Vector3>(Actor::Property::SCALE);
989 Vector3 cameraPosition = camera.GetProperty<Vector3>(Actor::Property::POSITION);
990 Quaternion cameraOrientation = camera.GetProperty<Quaternion>(Actor::Property::ORIENTATION);
991 Vector3 cameraScale = camera.GetProperty<Vector3>(Actor::Property::SCALE);
993 // Models in glTF and dli are defined as right hand coordinate system.
994 // DALi uses left hand coordinate system. Scaling negative is for change winding order.
995 if(!Dali::Equals(Y_DIRECTION.Dot(Vector3::YAXIS), 1.0f))
997 // Reflect by XZ plane
998 cameraPosition.y = -cameraPosition.y;
999 Quaternion yDirectionQuaternion;
1000 yDirectionQuaternion.mVector = Vector3::YAXIS;
1001 // Reflect orientation
1002 cameraOrientation = yDirectionQuaternion * cameraOrientation * yDirectionQuaternion;
1005 Vector3 resultPosition;
1006 Quaternion resultOrientation;
1007 Vector3 resultScale;
1009 Matrix selfMatrix(false);
1010 Matrix cameraMatrix(false);
1011 Matrix resultMatrix(false);
1012 selfMatrix.SetTransformComponents(selfScale, selfOrientation, selfPosition);
1013 cameraMatrix.SetTransformComponents(cameraScale, cameraOrientation, cameraPosition);
1014 Matrix::Multiply(resultMatrix, cameraMatrix, selfMatrix);
1015 resultMatrix.GetTransformComponents(resultPosition, resultOrientation, resultScale);
1017 camera.SetProperty(Actor::Property::POSITION, resultPosition);
1018 camera.SetProperty(Actor::Property::ORIENTATION, resultOrientation);
1019 camera.SetProperty(Actor::Property::SCALE, resultScale);
1022 void Model::NotifyImageBasedLightTexture(Dali::Texture diffuseTexture, Dali::Texture specularTexture, float scaleFactor, uint32_t specularMipmapLevels)
1024 if(mSceneDiffuseTexture != diffuseTexture || mSceneSpecularTexture != specularTexture)
1026 mSceneDiffuseTexture = diffuseTexture;
1027 mSceneSpecularTexture = specularTexture;
1028 mSceneIblScaleFactor = scaleFactor;
1029 mSceneSpecularMipmapLevels = specularMipmapLevels;
1030 // If Model IBL is not set, use SceneView's IBL.
1031 if(!mDiffuseTexture || !mSpecularTexture)
1033 UpdateImageBasedLightTexture();
1038 void Model::NotifyImageBasedLightScaleFactor(float scaleFactor)
1040 mSceneIblScaleFactor = scaleFactor;
1041 if(mSceneDiffuseTexture && mSceneSpecularTexture)
1043 UpdateImageBasedLightScaleFactor();
1047 void Model::NotifyLightAdded(uint32_t lightIndex, Scene3D::Light light)
1049 mLights[lightIndex] = light;
1050 AddLightRecursively(mModelRoot, light, lightIndex);
1053 void Model::NotifyLightRemoved(uint32_t lightIndex)
1055 if(mLights[lightIndex])
1057 RemoveLightRecursively(mModelRoot, lightIndex);
1058 mLights[lightIndex].Reset();
1062 void Model::OnModelLoadComplete()
1064 if(!mModelLoadTask->HasSucceeded())
1066 ResetResourceTasks();
1068 if(ModelCacheManager::Get() && !mModelUrl.empty())
1070 ModelCacheManager::Get().UnreferenceModelCache(mModelUrl);
1082 auto& resources = mModelLoadTask->GetResources();
1083 auto& scene = mModelLoadTask->GetScene();
1084 CreateAnimations(scene);
1085 ResetCameraParameters();
1086 if(!resources.mEnvironmentMaps.empty())
1088 mDefaultDiffuseTexture = resources.mEnvironmentMaps.front().second.mDiffuse;
1089 mDefaultSpecularTexture = resources.mEnvironmentMaps.front().second.mSpecular;
1092 uint32_t maxLightCount = Scene3D::Internal::Light::GetMaximumEnabledLightCount();
1093 for(uint32_t i = 0; i < maxLightCount; ++i)
1097 AddLightRecursively(mModelRoot, mLights[i], i);
1101 UpdateImageBasedLightTexture();
1102 UpdateImageBasedLightScaleFactor();
1103 Self().SetProperty(Dali::Actor::Property::ANCHOR_POINT, Vector3(mModelPivot.x, 1.0f - mModelPivot.y, mModelPivot.z));
1105 mModelResourceReady = true;
1106 NotifyResourceReady();
1107 ResetResourceTask(mModelLoadTask);
1110 void Model::OnIblDiffuseLoadComplete()
1112 mDiffuseTexture = mIblDiffuseLoadTask->GetLoadedTexture();
1113 ResetResourceTask(mIblDiffuseLoadTask);
1114 mIblDiffuseResourceReady = true;
1115 if(mIblDiffuseResourceReady && mIblSpecularResourceReady)
1117 OnIblLoadComplete();
1121 void Model::OnIblSpecularLoadComplete()
1123 mSpecularTexture = mIblSpecularLoadTask->GetLoadedTexture();
1124 mSpecularMipmapLevels = mIblSpecularLoadTask->GetMipmapLevels();
1125 ResetResourceTask(mIblSpecularLoadTask);
1126 mIblSpecularResourceReady = true;
1127 if(mIblDiffuseResourceReady && mIblSpecularResourceReady)
1129 OnIblLoadComplete();
1133 void Model::OnIblLoadComplete()
1135 UpdateImageBasedLightTexture();
1136 NotifyResourceReady();
1139 void Model::OnSizeNotification(Dali::PropertyNotification& source)
1144 void Model::ResetResourceTasks()
1146 if(!Dali::Adaptor::IsAvailable())
1150 ResetResourceTask(mModelLoadTask);
1151 ResetResourceTask(mIblDiffuseLoadTask);
1152 ResetResourceTask(mIblSpecularLoadTask);
1155 void Model::ResetResourceTask(IntrusivePtr<AsyncTask> asyncTask)
1161 Dali::AsyncTaskManager::Get().RemoveTask(asyncTask);
1165 void Model::NotifyResourceReady()
1167 if(!IsResourceReady())
1171 Control::SetResourceReady();
1174 void Model::CreateModel()
1176 BoundingVolume AABB;
1177 auto& resources = mModelLoadTask->GetResources();
1178 auto& scene = mModelLoadTask->GetScene();
1179 auto& resourceChoices = mModelLoadTask->GetResourceChoices();
1180 Dali::Scene3D::Loader::Transforms xforms{Dali::Scene3D::Loader::MatrixStack{}, Dali::Scene3D::Loader::ViewProjection{}};
1181 Dali::Scene3D::Loader::NodeDefinition::CreateParams nodeParams{resources, xforms, {}, {}, {}};
1183 // Generate Dali handles from resource bundle. Note that we generate all scene's resouce immediatly.
1184 resources.GenerateResources();
1185 for(auto iRoot : scene.GetRoots())
1187 if(auto modelNode = scene.CreateNodes(iRoot, resourceChoices, nodeParams))
1189 scene.ConfigureSkinningShaders(resources, modelNode, std::move(nodeParams.mSkinnables));
1190 ConfigureBlendShapeShaders(resources, scene, modelNode, std::move(nodeParams.mBlendshapeRequests));
1192 scene.ApplyConstraints(modelNode, std::move(nodeParams.mConstrainables));
1194 mModelRoot.Add(modelNode);
1197 AddModelTreeToAABB(AABB, scene, resourceChoices, iRoot, nodeParams, Matrix::IDENTITY);
1200 UpdateBlendShapeNodeMap();
1202 mNaturalSize = AABB.CalculateSize();
1203 mModelPivot = AABB.CalculatePivot();
1204 mModelRoot.SetProperty(Dali::Actor::Property::SIZE, mNaturalSize);
1205 Vector3 controlSize = Self().GetProperty<Vector3>(Dali::Actor::Property::SIZE);
1206 if(Dali::EqualsZero(controlSize.x) || Dali::EqualsZero(controlSize.y))
1208 Self().SetProperty(Dali::Actor::Property::SIZE, mNaturalSize);
1214 void Model::CreateAnimations(Dali::Scene3D::Loader::SceneDefinition& scene)
1216 mAnimations.clear();
1217 if(!mModelLoadTask->GetAnimations().empty())
1219 auto getActor = [&](const Scene3D::Loader::AnimatedProperty& property) {
1220 if(property.mNodeIndex == Scene3D::Loader::INVALID_INDEX)
1222 return mModelRoot.FindChildByName(property.mNodeName);
1224 auto* node = scene.GetNode(property.mNodeIndex);
1227 return Dali::Actor();
1229 return mModelRoot.FindChildById(node->mNodeId);
1232 for(auto&& animation : mModelLoadTask->GetAnimations())
1234 Dali::Animation anim = animation.ReAnimate(getActor);
1235 mAnimations.push_back({animation.GetName(), anim});
1240 void Model::ResetCameraParameters()
1242 mCameraParameters.clear();
1243 if(!mModelLoadTask->GetCameras().empty())
1245 // Copy camera parameters.
1246 std::copy(mModelLoadTask->GetCameras().begin(), mModelLoadTask->GetCameras().end(), std::back_inserter(mCameraParameters));
1250 void Model::UpdateBlendShapeNodeMap()
1252 // Remove privous node map
1253 mBlendShapeModelNodeMap.clear();
1255 UpdateBlendShapeNodeMapRecursively(mBlendShapeModelNodeMap, mModelRoot);
1258 } // namespace Internal
1259 } // namespace Scene3D