Fix transform issue of skinned mesh
[platform/core/uifw/dali-toolkit.git] / dali-scene3d / internal / controls / model / model-impl.cpp
1 /*
2  * Copyright (c) 2023 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali-scene3d/internal/controls/model/model-impl.h>
20
21 // EXTERNAL INCLUDES
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>
31 #include <filesystem>
32
33 // INTERNAL INCLUDES
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/model-components/model-node-impl.h>
37 #include <dali-scene3d/public-api/controls/model/model.h>
38 #include <dali-scene3d/public-api/loader/animation-definition.h>
39 #include <dali-scene3d/public-api/loader/camera-parameters.h>
40 #include <dali-scene3d/public-api/loader/light-parameters.h>
41 #include <dali-scene3d/public-api/loader/load-result.h>
42 #include <dali-scene3d/public-api/loader/node-definition.h>
43 #include <dali-scene3d/public-api/loader/scene-definition.h>
44 #include <dali-scene3d/public-api/loader/shader-definition-factory.h>
45
46 using namespace Dali;
47
48 namespace Dali
49 {
50 namespace Scene3D
51 {
52 namespace Internal
53 {
54 namespace
55 {
56 /**
57  * Creates control through type registry
58  */
59 BaseHandle Create()
60 {
61   return Scene3D::Model::New(std::string());
62 }
63
64 // Setup properties, signals and actions using the type-registry.
65 DALI_TYPE_REGISTRATION_BEGIN(Scene3D::Model, Toolkit::Control, Create);
66 DALI_TYPE_REGISTRATION_END()
67
68 static constexpr Vector3 Y_DIRECTION(1.0f, -1.0f, 1.0f);
69
70 static constexpr bool DEFAULT_MODEL_CHILDREN_SENSITIVE = false;
71 static constexpr bool DEFAULT_MODEL_CHILDREN_FOCUSABLE = false;
72
73 struct BoundingVolume
74 {
75   void Init()
76   {
77     pointMin = Vector3(std::numeric_limits<float>::max(), std::numeric_limits<float>::max(), std::numeric_limits<float>::max());
78     pointMax = Vector3(std::numeric_limits<float>::min(), std::numeric_limits<float>::min(), std::numeric_limits<float>::min());
79   }
80
81   void ConsiderNewPointInVolume(const Vector3& position)
82   {
83     pointMin.x = std::min(position.x, pointMin.x);
84     pointMin.y = std::min(position.y, pointMin.y);
85     pointMin.z = std::min(position.z, pointMin.z);
86
87     pointMax.x = std::max(position.x, pointMax.x);
88     pointMax.y = std::max(position.y, pointMax.y);
89     pointMax.z = std::max(position.z, pointMax.z);
90   }
91
92   Vector3 CalculateSize()
93   {
94     return pointMax - pointMin;
95   }
96
97   Vector3 CalculatePivot()
98   {
99     Vector3 pivot = pointMin / (pointMin - pointMax);
100     for(uint32_t i = 0; i < 3; ++i)
101     {
102       // To avoid divid by zero
103       if(Dali::Equals(pointMin[i], pointMax[i]))
104       {
105         pivot[i] = 0.5f;
106       }
107     }
108     return pivot;
109   }
110
111   Vector3 pointMin;
112   Vector3 pointMax;
113 };
114
115 void ConfigureBlendShapeShaders(
116   Dali::Scene3D::Loader::ResourceBundle& resources, const Dali::Scene3D::Loader::SceneDefinition& scene, Actor root, std::vector<Dali::Scene3D::Loader::BlendshapeShaderConfigurationRequest>&& requests)
117 {
118   std::vector<std::string> errors;
119   auto                     onError = [&errors](const std::string& msg)
120   { errors.push_back(msg); };
121   if(!scene.ConfigureBlendshapeShaders(resources, root, std::move(requests), onError))
122   {
123     Dali::Scene3D::Loader::ExceptionFlinger flinger(ASSERT_LOCATION);
124     for(auto& msg : errors)
125     {
126       flinger << msg << '\n';
127     }
128   }
129 }
130
131 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)
132 {
133   static constexpr uint32_t BOX_POINT_COUNT             = 8;
134   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}};
135
136   Matrix                                       nodeMatrix;
137   const Dali::Scene3D::Loader::NodeDefinition* node        = scene.GetNode(iNode);
138   Matrix                                       localMatrix = node->GetLocalSpace();
139   Matrix::Multiply(nodeMatrix, localMatrix, parentMatrix);
140
141   Vector3 volume[2];
142   if(node->GetExtents(nodeParams.mResources, volume[0], volume[1]))
143   {
144     for(uint32_t i = 0; i < BOX_POINT_COUNT; ++i)
145     {
146       Vector4 position       = Vector4(volume[BBIndex[i][0]].x, volume[BBIndex[i][1]].y, volume[BBIndex[i][2]].z, 1.0f);
147       Vector4 objectPosition = nodeMatrix * position;
148       objectPosition /= objectPosition.w;
149
150       AABB.ConsiderNewPointInVolume(Vector3(objectPosition));
151     }
152   }
153
154   if(node->mCustomization)
155   {
156     if(!node->mChildren.empty())
157     {
158       auto                         choice = choices.Get(node->mCustomization->mTag);
159       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));
160
161       AddModelTreeToAABB(AABB, scene, choices, node->mChildren[i], nodeParams, nodeMatrix);
162     }
163   }
164   else
165   {
166     for(auto i : node->mChildren)
167     {
168       AddModelTreeToAABB(AABB, scene, choices, i, nodeParams, nodeMatrix);
169     }
170   }
171 }
172
173 } // anonymous namespace
174
175 Model::Model(const std::string& modelUrl, const std::string& resourceDirectoryUrl)
176 : Control(ControlBehaviour(DISABLE_SIZE_NEGOTIATION | DISABLE_STYLE_CHANGE_SIGNALS)),
177   mModelUrl(modelUrl),
178   mResourceDirectoryUrl(resourceDirectoryUrl),
179   mModelRoot(),
180   mNaturalSize(Vector3::ZERO),
181   mModelPivot(AnchorPoint::CENTER),
182   mSceneIblScaleFactor(1.0f),
183   mIblScaleFactor(1.0f),
184   mSceneSpecularMipmapLevels(1u),
185   mSpecularMipmapLevels(1u),
186   mModelChildrenSensitive(DEFAULT_MODEL_CHILDREN_SENSITIVE),
187   mModelChildrenFocusable(DEFAULT_MODEL_CHILDREN_FOCUSABLE),
188   mModelResourceReady(false),
189   mIblDiffuseResourceReady(true),
190   mIblSpecularResourceReady(true),
191   mIblDiffuseDirty(false),
192   mIblSpecularDirty(false)
193 {
194 }
195
196 Model::~Model()
197 {
198   if(ModelCacheManager::Get() && !mModelUrl.empty())
199   {
200     ModelCacheManager::Get().UnreferenceModelCache(mModelUrl);
201   }
202
203   ResetResourceTasks();
204 }
205
206 Dali::Scene3D::Model Model::New(const std::string& modelUrl, const std::string& resourceDirectoryUrl)
207 {
208   Model* impl = new Model(modelUrl, resourceDirectoryUrl);
209
210   Dali::Scene3D::Model handle = Dali::Scene3D::Model(*impl);
211
212   // Second-phase init of the implementation
213   // This can only be done after the CustomActor connection has been made...
214   impl->Initialize();
215
216   return handle;
217 }
218
219 const Scene3D::ModelNode Model::GetModelRoot() const
220 {
221   return mModelRoot;
222 }
223
224 void Model::AddModelNode(Scene3D::ModelNode modelNode)
225 {
226   if(!mModelRoot)
227   {
228     CreateModelRoot();
229   }
230
231   mModelRoot.Add(modelNode);
232   if(mModelUrl.empty())
233   {
234     mModelResourceReady = true;
235   }
236
237   if(mIblDiffuseResourceReady && mIblSpecularResourceReady)
238   {
239     UpdateImageBasedLightTexture();
240     UpdateImageBasedLightScaleFactor();
241   }
242
243   if(Self().GetProperty<bool>(Dali::Actor::Property::CONNECTED_TO_SCENE))
244   {
245     NotifyResourceReady();
246   }
247 }
248
249 void Model::RemoveModelNode(Scene3D::ModelNode modelNode)
250 {
251   if(mModelRoot)
252   {
253     mModelRoot.Remove(modelNode);
254   }
255 }
256
257 void Model::SetChildrenSensitive(bool enable)
258 {
259   if(mModelChildrenSensitive != enable)
260   {
261     mModelChildrenSensitive = enable;
262     if(mModelRoot)
263     {
264       mModelRoot.SetProperty(Dali::Actor::Property::SENSITIVE, mModelChildrenSensitive);
265     }
266   }
267 }
268
269 bool Model::GetChildrenSensitive() const
270 {
271   return mModelChildrenSensitive;
272 }
273
274 void Model::SetChildrenFocusable(bool enable)
275 {
276   if(mModelChildrenFocusable != enable)
277   {
278     mModelChildrenFocusable = enable;
279     if(mModelRoot)
280     {
281       mModelRoot.SetProperty(Dali::Actor::Property::KEYBOARD_FOCUSABLE, mModelChildrenFocusable);
282       mModelRoot.SetProperty(Dali::DevelActor::Property::KEYBOARD_FOCUSABLE_CHILDREN, mModelChildrenFocusable);
283     }
284   }
285 }
286
287 bool Model::GetChildrenFocusable() const
288 {
289   return mModelChildrenFocusable;
290 }
291
292 void Model::SetImageBasedLightSource(const std::string& diffuseUrl, const std::string& specularUrl, float scaleFactor)
293 {
294   bool needIblReset = false;
295   bool isOnScene    = Self().GetProperty<bool>(Dali::Actor::Property::CONNECTED_TO_SCENE);
296   if(mDiffuseIblUrl != diffuseUrl)
297   {
298     mDiffuseIblUrl = diffuseUrl;
299     if(mDiffuseIblUrl.empty())
300     {
301       needIblReset = true;
302     }
303     else
304     {
305       mIblDiffuseDirty         = true;
306       mIblDiffuseResourceReady = false;
307     }
308   }
309
310   if(mSpecularIblUrl != specularUrl)
311   {
312     mSpecularIblUrl = specularUrl;
313     if(mSpecularIblUrl.empty())
314     {
315       needIblReset = true;
316     }
317     else
318     {
319       mIblSpecularDirty         = true;
320       mIblSpecularResourceReady = false;
321     }
322   }
323
324   // If one or both of diffuse url and specular url are empty,
325   // we don't need to request to load texture.
326   if(needIblReset)
327   {
328     ResetResourceTask(mIblDiffuseLoadTask);
329     ResetResourceTask(mIblSpecularLoadTask);
330
331     mIblDiffuseDirty          = false;
332     mIblSpecularDirty         = false;
333     mIblDiffuseResourceReady  = true;
334     mIblSpecularResourceReady = true;
335
336     mDiffuseTexture.Reset();
337     mSpecularTexture.Reset();
338     UpdateImageBasedLightTexture();
339   }
340   else
341   {
342     if(isOnScene && mIblDiffuseDirty)
343     {
344       ResetResourceTask(mIblDiffuseLoadTask);
345       mIblDiffuseLoadTask = new EnvironmentMapLoadTask(mDiffuseIblUrl, Scene3D::EnvironmentMapType::CUBEMAP, MakeCallback(this, &Model::OnIblDiffuseLoadComplete));
346       Dali::AsyncTaskManager::Get().AddTask(mIblDiffuseLoadTask);
347       mIblDiffuseDirty = false;
348     }
349
350     if(isOnScene && mIblSpecularDirty)
351     {
352       ResetResourceTask(mIblSpecularLoadTask);
353       mIblSpecularLoadTask = new EnvironmentMapLoadTask(mSpecularIblUrl, Scene3D::EnvironmentMapType::CUBEMAP, MakeCallback(this, &Model::OnIblSpecularLoadComplete));
354       Dali::AsyncTaskManager::Get().AddTask(mIblSpecularLoadTask);
355       mIblSpecularDirty = false;
356     }
357   }
358
359   if(!Dali::Equals(mIblScaleFactor, scaleFactor))
360   {
361     mIblScaleFactor = scaleFactor;
362     UpdateImageBasedLightScaleFactor();
363   }
364
365   // If diffuse and specular textures are already loaded, emits resource ready signal here.
366   NotifyResourceReady();
367 }
368
369 void Model::SetImageBasedLightScaleFactor(float scaleFactor)
370 {
371   mIblScaleFactor = scaleFactor;
372   if(mDiffuseTexture && mSpecularTexture)
373   {
374     UpdateImageBasedLightScaleFactor();
375   }
376 }
377
378 float Model::GetImageBasedLightScaleFactor() const
379 {
380   return mIblScaleFactor;
381 }
382
383 uint32_t Model::GetAnimationCount() const
384 {
385   return mAnimations.size();
386 }
387
388 Dali::Animation Model::GetAnimation(uint32_t index) const
389 {
390   Dali::Animation animation;
391   if(mAnimations.size() > index)
392   {
393     animation = mAnimations[index].second;
394   }
395   return animation;
396 }
397
398 Dali::Animation Model::GetAnimation(const std::string& name) const
399 {
400   Dali::Animation animation;
401   if(!name.empty())
402   {
403     for(auto&& animationData : mAnimations)
404     {
405       if(animationData.first == name)
406       {
407         animation = animationData.second;
408         break;
409       }
410     }
411   }
412   return animation;
413 }
414
415 uint32_t Model::GetCameraCount() const
416 {
417   return mCameraParameters.size();
418 }
419
420 Dali::CameraActor Model::GenerateCamera(uint32_t index) const
421 {
422   Dali::CameraActor camera;
423   if(mCameraParameters.size() > index)
424   {
425     camera = Dali::CameraActor::New3DCamera();
426     if(!mCameraParameters[index].ConfigureCamera(camera, false))
427     {
428       DALI_LOG_ERROR("Fail to generate %u's camera actor : Some property was not defined. Please check model file.\n", index);
429       camera.Reset();
430       return camera;
431     }
432
433     ApplyCameraTransform(camera);
434   }
435   return camera;
436 }
437
438 bool Model::ApplyCamera(uint32_t index, Dali::CameraActor camera) const
439 {
440   if(camera && mCameraParameters.size() > index)
441   {
442     if(!mCameraParameters[index].ConfigureCamera(camera, false))
443     {
444       DALI_LOG_ERROR("Fail to apply %u's camera actor : Some property was not defined. Please check model file.\n", index);
445       return false;
446     }
447
448     ApplyCameraTransform(camera);
449     return true;
450   }
451   return false;
452 }
453
454 Scene3D::ModelNode Model::FindChildModelNodeByName(std::string_view nodeName)
455 {
456   Actor childActor = Self().FindChildByName(nodeName);
457   return Scene3D::ModelNode::DownCast(childActor);
458 }
459
460 ///////////////////////////////////////////////////////////
461 //
462 // Private methods
463 //
464
465 void Model::OnInitialize()
466 {
467   // Make ParentOrigin as Center.
468   Self().SetProperty(Dali::Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
469 }
470
471 void Model::OnSceneConnection(int depth)
472 {
473   if(!mModelLoadTask && !mModelResourceReady && !mModelUrl.empty())
474   {
475     // Request model load only if we setup url.
476     if(ModelCacheManager::Get())
477     {
478       ModelCacheManager::Get().ReferenceModelCache(mModelUrl);
479     }
480     mModelLoadTask = new ModelLoadTask(mModelUrl, mResourceDirectoryUrl, MakeCallback(this, &Model::OnModelLoadComplete));
481     Dali::AsyncTaskManager::Get().AddTask(mModelLoadTask);
482   }
483
484   // If diffuse and specular url is not valid, IBL does not need to be loaded.
485   if(!mDiffuseIblUrl.empty() && !mSpecularIblUrl.empty())
486   {
487     SetImageBasedLightSource(mDiffuseIblUrl, mSpecularIblUrl, mIblScaleFactor);
488   }
489
490   Actor parent = Self().GetParent();
491   while(parent)
492   {
493     Scene3D::SceneView sceneView = Scene3D::SceneView::DownCast(parent);
494     if(sceneView)
495     {
496       GetImpl(sceneView).RegisterSceneItem(this);
497       mParentSceneView = sceneView;
498       break;
499     }
500     parent = parent.GetParent();
501   }
502
503   NotifyResourceReady();
504   Control::OnSceneConnection(depth);
505 }
506
507 void Model::OnSceneDisconnection()
508 {
509   Scene3D::SceneView sceneView = mParentSceneView.GetHandle();
510   if(sceneView)
511   {
512     GetImpl(sceneView).UnregisterSceneItem(this);
513     mParentSceneView.Reset();
514   }
515   Control::OnSceneDisconnection();
516 }
517
518 Vector3 Model::GetNaturalSize()
519 {
520   if(!mModelRoot)
521   {
522     DALI_LOG_ERROR("Model is still not loaded.\n");
523     return Vector3::ZERO;
524   }
525
526   return mNaturalSize;
527 }
528
529 float Model::GetHeightForWidth(float width)
530 {
531   Extents padding;
532   padding = Self().GetProperty<Extents>(Toolkit::Control::Property::PADDING);
533   return Control::GetHeightForWidth(width) + padding.top + padding.bottom;
534 }
535
536 float Model::GetWidthForHeight(float height)
537 {
538   Extents padding;
539   padding = Self().GetProperty<Extents>(Toolkit::Control::Property::PADDING);
540   return Control::GetWidthForHeight(height) + padding.start + padding.end;
541 }
542
543 void Model::OnRelayout(const Vector2& size, RelayoutContainer& container)
544 {
545   Control::OnRelayout(size, container);
546   ScaleModel();
547 }
548
549 bool Model::IsResourceReady() const
550 {
551   return mModelResourceReady && mIblDiffuseResourceReady && mIblSpecularResourceReady;
552 }
553
554 void Model::CreateModelRoot()
555 {
556   mModelRoot = Scene3D::ModelNode::New();
557   mModelRoot.SetProperty(Actor::Property::COLOR_MODE, ColorMode::USE_OWN_MULTIPLY_PARENT_COLOR);
558   mModelRoot.SetProperty(Dali::Actor::Property::SCALE, Y_DIRECTION);
559   mModelRoot.SetProperty(Dali::Actor::Property::SENSITIVE, mModelChildrenSensitive);
560   mModelRoot.SetProperty(Dali::Actor::Property::KEYBOARD_FOCUSABLE, mModelChildrenFocusable);
561   mModelRoot.SetProperty(Dali::DevelActor::Property::KEYBOARD_FOCUSABLE_CHILDREN, mModelChildrenFocusable);
562   Self().Add(mModelRoot);
563 }
564
565 void Model::ScaleModel()
566 {
567   if(!mModelRoot)
568   {
569     return;
570   }
571
572   float   scale = 1.0f;
573   Vector3 size  = Self().GetProperty<Vector3>(Dali::Actor::Property::SIZE);
574   if(size.x > 0.0f && size.y > 0.0f)
575   {
576     scale = MAXFLOAT;
577     scale = std::min(size.x / mNaturalSize.x, scale);
578     scale = std::min(size.y / mNaturalSize.y, scale);
579   }
580   // Models in glTF and dli are defined as right hand coordinate system.
581   // DALi uses left hand coordinate system. Scaling negative is for change winding order.
582   mModelRoot.SetProperty(Dali::Actor::Property::SCALE, Y_DIRECTION * scale);
583 }
584
585 void Model::FitModelPosition()
586 {
587   if(!mModelRoot)
588   {
589     return;
590   }
591   // Loaded model pivot is not the model center.
592   mModelRoot.SetProperty(Dali::Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
593   mModelRoot.SetProperty(Dali::Actor::Property::ANCHOR_POINT, Vector3::ONE - mModelPivot);
594 }
595
596 void Model::UpdateImageBasedLightTextureRecursively(Scene3D::ModelNode node, Dali::Texture diffuseTexture, Dali::Texture specularTexture, float iblScaleFactor, uint32_t specularMipmapLevels)
597 {
598   if(!node)
599   {
600     return;
601   }
602
603   GetImplementation(node).SetImageBasedLightTexture(diffuseTexture, specularTexture, iblScaleFactor, specularMipmapLevels);
604   uint32_t childrenCount = node.GetChildCount();
605   for(uint32_t i = 0; i < childrenCount; ++i)
606   {
607     Scene3D::ModelNode childNode = Scene3D::ModelNode::DownCast(node.GetChildAt(i));
608     if(!childNode)
609     {
610       continue;
611     }
612     UpdateImageBasedLightTextureRecursively(childNode, diffuseTexture, specularTexture, iblScaleFactor, specularMipmapLevels);
613   }
614 }
615
616 void Model::UpdateImageBasedLightScaleFactorRecursively(Scene3D::ModelNode node, float iblScaleFactor)
617 {
618   if(!node)
619   {
620     return;
621   }
622
623   node.RegisterProperty(Dali::Scene3D::Loader::NodeDefinition::GetIblScaleFactorUniformName().data(), iblScaleFactor);
624   GetImplementation(node).SetImageBasedLightScaleFactor(iblScaleFactor);
625
626   uint32_t childrenCount = node.GetChildCount();
627   for(uint32_t i = 0; i < childrenCount; ++i)
628   {
629     Scene3D::ModelNode childNode = Scene3D::ModelNode::DownCast(node.GetChildAt(i));
630     if(!childNode)
631     {
632       continue;
633     }
634     UpdateImageBasedLightScaleFactorRecursively(childNode, iblScaleFactor);
635   }
636 }
637
638 void Model::UpdateImageBasedLightTexture()
639 {
640   Dali::Texture currentDiffuseTexture          = (mDiffuseTexture && mSpecularTexture) ? mDiffuseTexture : mSceneDiffuseTexture;
641   Dali::Texture currentSpecularTexture         = (mDiffuseTexture && mSpecularTexture) ? mSpecularTexture : mSceneSpecularTexture;
642   float         currentIblScaleFactor          = (mDiffuseTexture && mSpecularTexture) ? mIblScaleFactor : mSceneIblScaleFactor;
643   uint32_t      currentIblSpecularMipmapLevels = (mDiffuseTexture && mSpecularTexture) ? mSpecularMipmapLevels : mSceneSpecularMipmapLevels;
644
645   if(!currentDiffuseTexture || !currentSpecularTexture)
646   {
647     currentDiffuseTexture          = mDefaultDiffuseTexture;
648     currentSpecularTexture         = mDefaultSpecularTexture;
649     currentIblScaleFactor          = Dali::Scene3D::Loader::EnvironmentDefinition::GetDefaultIntensity();
650     currentIblSpecularMipmapLevels = 1u;
651   }
652
653   UpdateImageBasedLightTextureRecursively(mModelRoot, currentDiffuseTexture, currentSpecularTexture, currentIblScaleFactor, currentIblSpecularMipmapLevels);
654 }
655
656 void Model::UpdateImageBasedLightScaleFactor()
657 {
658   if((!mDiffuseTexture || !mSpecularTexture) &&
659      (!mSceneDiffuseTexture || !mSceneSpecularTexture))
660   {
661     return;
662   }
663
664   float currentIblScaleFactor = (mDiffuseTexture && mSpecularTexture) ? mIblScaleFactor : mSceneIblScaleFactor;
665   UpdateImageBasedLightScaleFactorRecursively(mModelRoot, currentIblScaleFactor);
666 }
667
668 void Model::ApplyCameraTransform(Dali::CameraActor camera) const
669 {
670   Vector3    selfPosition    = Self().GetProperty<Vector3>(Actor::Property::POSITION);
671   Quaternion selfOrientation = Self().GetProperty<Quaternion>(Actor::Property::ORIENTATION);
672   Vector3    selfScale       = Self().GetProperty<Vector3>(Actor::Property::SCALE);
673
674   Vector3    cameraPosition    = camera.GetProperty<Vector3>(Actor::Property::POSITION);
675   Quaternion cameraOrientation = camera.GetProperty<Quaternion>(Actor::Property::ORIENTATION);
676   Vector3    cameraScale       = camera.GetProperty<Vector3>(Actor::Property::SCALE);
677
678   // Models in glTF and dli are defined as right hand coordinate system.
679   // DALi uses left hand coordinate system. Scaling negative is for change winding order.
680   if(!Dali::Equals(Y_DIRECTION.Dot(Vector3::YAXIS), 1.0f))
681   {
682     // Reflect by XZ plane
683     cameraPosition.y = -cameraPosition.y;
684     Quaternion yDirectionQuaternion;
685     yDirectionQuaternion.mVector = Vector3::YAXIS;
686     // Reflect orientation
687     cameraOrientation = yDirectionQuaternion * cameraOrientation * yDirectionQuaternion;
688   }
689
690   Vector3    resultPosition;
691   Quaternion resultOrientation;
692   Vector3    resultScale;
693
694   Matrix selfMatrix(false);
695   Matrix cameraMatrix(false);
696   Matrix resultMatrix(false);
697   selfMatrix.SetTransformComponents(selfScale, selfOrientation, selfPosition);
698   cameraMatrix.SetTransformComponents(cameraScale, cameraOrientation, cameraPosition);
699   Matrix::Multiply(resultMatrix, cameraMatrix, selfMatrix);
700   resultMatrix.GetTransformComponents(resultPosition, resultOrientation, resultScale);
701
702   camera.SetProperty(Actor::Property::POSITION, resultPosition);
703   camera.SetProperty(Actor::Property::ORIENTATION, resultOrientation);
704   camera.SetProperty(Actor::Property::SCALE, resultScale);
705 }
706
707 void Model::NotifyImageBasedLightTexture(Dali::Texture diffuseTexture, Dali::Texture specularTexture, float scaleFactor, uint32_t specularMipmapLevels)
708 {
709   if(mSceneDiffuseTexture != diffuseTexture || mSceneSpecularTexture != specularTexture)
710   {
711     mSceneDiffuseTexture       = diffuseTexture;
712     mSceneSpecularTexture      = specularTexture;
713     mSceneIblScaleFactor       = scaleFactor;
714     mSceneSpecularMipmapLevels = specularMipmapLevels;
715     // If Model IBL is not set, use SceneView's IBL.
716     if(!mDiffuseTexture || !mSpecularTexture)
717     {
718       UpdateImageBasedLightTexture();
719     }
720   }
721 }
722
723 void Model::NotifyImageBasedLightScaleFactor(float scaleFactor)
724 {
725   mSceneIblScaleFactor = scaleFactor;
726   if(mSceneDiffuseTexture && mSceneSpecularTexture)
727   {
728     UpdateImageBasedLightScaleFactor();
729   }
730 }
731
732 void Model::OnModelLoadComplete()
733 {
734   if(!mModelLoadTask->HasSucceeded())
735   {
736     ResetResourceTasks();
737
738     if(ModelCacheManager::Get() && !mModelUrl.empty())
739     {
740       ModelCacheManager::Get().UnreferenceModelCache(mModelUrl);
741     }
742
743     return;
744   }
745
746   if(!mModelRoot)
747   {
748     CreateModelRoot();
749   }
750   CreateModel();
751
752   auto& resources = mModelLoadTask->GetResources();
753   auto& scene     = mModelLoadTask->GetScene();
754   CreateAnimations(scene);
755   ResetCameraParameters();
756   if(!resources.mEnvironmentMaps.empty())
757   {
758     mDefaultDiffuseTexture  = resources.mEnvironmentMaps.front().second.mDiffuse;
759     mDefaultSpecularTexture = resources.mEnvironmentMaps.front().second.mSpecular;
760   }
761
762   UpdateImageBasedLightTexture();
763   UpdateImageBasedLightScaleFactor();
764   Self().SetProperty(Dali::Actor::Property::ANCHOR_POINT, Vector3(mModelPivot.x, 1.0f - mModelPivot.y, mModelPivot.z));
765
766   mModelResourceReady = true;
767   NotifyResourceReady();
768   ResetResourceTask(mModelLoadTask);
769 }
770
771 void Model::OnIblDiffuseLoadComplete()
772 {
773   mDiffuseTexture = mIblDiffuseLoadTask->GetLoadedTexture();
774   ResetResourceTask(mIblDiffuseLoadTask);
775   mIblDiffuseResourceReady = true;
776   if(mIblDiffuseResourceReady && mIblSpecularResourceReady)
777   {
778     OnIblLoadComplete();
779   }
780 }
781
782 void Model::OnIblSpecularLoadComplete()
783 {
784   mSpecularTexture      = mIblSpecularLoadTask->GetLoadedTexture();
785   mSpecularMipmapLevels = mIblSpecularLoadTask->GetMipmapLevels();
786   ResetResourceTask(mIblSpecularLoadTask);
787   mIblSpecularResourceReady = true;
788   if(mIblDiffuseResourceReady && mIblSpecularResourceReady)
789   {
790     OnIblLoadComplete();
791   }
792 }
793
794 void Model::OnIblLoadComplete()
795 {
796   UpdateImageBasedLightTexture();
797   NotifyResourceReady();
798 }
799
800 void Model::ResetResourceTasks()
801 {
802   if(!Dali::Adaptor::IsAvailable())
803   {
804     return;
805   }
806   ResetResourceTask(mModelLoadTask);
807   ResetResourceTask(mIblDiffuseLoadTask);
808   ResetResourceTask(mIblSpecularLoadTask);
809 }
810
811 void Model::ResetResourceTask(IntrusivePtr<AsyncTask> asyncTask)
812 {
813   if(!asyncTask)
814   {
815     return;
816   }
817   Dali::AsyncTaskManager::Get().RemoveTask(asyncTask);
818   asyncTask.Reset();
819 }
820
821 void Model::NotifyResourceReady()
822 {
823   if(!IsResourceReady())
824   {
825     return;
826   }
827   Control::SetResourceReady(false);
828 }
829
830 void Model::CreateModel()
831 {
832   BoundingVolume                                      AABB;
833   auto&                                               resources       = mModelLoadTask->GetResources();
834   auto&                                               scene           = mModelLoadTask->GetScene();
835   auto&                                               resourceChoices = mModelLoadTask->GetResourceChoices();
836   Dali::Scene3D::Loader::Transforms                   xforms{Dali::Scene3D::Loader::MatrixStack{}, Dali::Scene3D::Loader::ViewProjection{}};
837   Dali::Scene3D::Loader::NodeDefinition::CreateParams nodeParams{resources, xforms, {}, {}, {}};
838
839   // Generate Dali handles from resource bundle. Note that we generate all scene's resouce immediatly.
840   resources.GenerateResources();
841   for(auto iRoot : scene.GetRoots())
842   {
843     if(auto actor = scene.CreateNodes(iRoot, resourceChoices, nodeParams))
844     {
845       scene.ConfigureSkinningShaders(resources, actor, std::move(nodeParams.mSkinnables));
846       ConfigureBlendShapeShaders(resources, scene, actor, std::move(nodeParams.mBlendshapeRequests));
847
848       scene.ApplyConstraints(actor, std::move(nodeParams.mConstrainables));
849
850       mModelRoot.Add(actor);
851     }
852
853     AddModelTreeToAABB(AABB, scene, resourceChoices, iRoot, nodeParams, Matrix::IDENTITY);
854   }
855
856   mNaturalSize = AABB.CalculateSize();
857   mModelPivot  = AABB.CalculatePivot();
858   mModelRoot.SetProperty(Dali::Actor::Property::SIZE, mNaturalSize);
859   Vector3 controlSize = Self().GetProperty<Vector3>(Dali::Actor::Property::SIZE);
860   if(Dali::EqualsZero(controlSize.x) || Dali::EqualsZero(controlSize.y))
861   {
862     Self().SetProperty(Dali::Actor::Property::SIZE, mNaturalSize);
863   }
864   FitModelPosition();
865   ScaleModel();
866 }
867
868 void Model::CreateAnimations(Dali::Scene3D::Loader::SceneDefinition& scene)
869 {
870   mAnimations.clear();
871   if(!mModelLoadTask->GetAnimations().empty())
872   {
873     auto getActor = [&](const Scene3D::Loader::AnimatedProperty& property)
874     {
875       if(property.mNodeIndex == Scene3D::Loader::INVALID_INDEX)
876       {
877         return mModelRoot.FindChildByName(property.mNodeName);
878       }
879       auto* node = scene.GetNode(property.mNodeIndex);
880       if(node == nullptr)
881       {
882         return Dali::Actor();
883       }
884       return mModelRoot.FindChildById(node->mNodeId);
885     };
886
887     for(auto&& animation : mModelLoadTask->GetAnimations())
888     {
889       Dali::Animation anim = animation.ReAnimate(getActor);
890       mAnimations.push_back({animation.GetName(), anim});
891     }
892   }
893 }
894
895 void Model::ResetCameraParameters()
896 {
897   mCameraParameters.clear();
898   if(!mModelLoadTask->GetCameras().empty())
899   {
900     // Copy camera parameters.
901     std::copy(mModelLoadTask->GetCameras().begin(), mModelLoadTask->GetCameras().end(), std::back_inserter(mCameraParameters));
902   }
903 }
904
905 } // namespace Internal
906 } // namespace Scene3D
907 } // namespace Dali