6456b6d50b2d053d6db428bf7a53b1908e77e9ae
[platform/core/uifw/dali-toolkit.git] / dali-scene3d / internal / common / model-load-task.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/common/model-load-task.h>
20
21 // EXTERNAL INCLUDES
22 #include <dali/integration-api/debug.h>
23 #include <filesystem>
24
25 namespace Dali
26 {
27 namespace Scene3D
28 {
29 namespace Internal
30 {
31 namespace
32 {
33 static constexpr Vector3 Y_DIRECTION(1.0f, -1.0f, 1.0f);
34 } // namespace
35
36 ModelLoadTask::ModelLoadTask(const std::string& modelUrl, const std::string& resourceDirectoryUrl, CallbackBase* callback)
37 : AsyncTask(callback),
38   mModelUrl(modelUrl),
39   mResourceDirectoryUrl(resourceDirectoryUrl),
40   mModelCacheManager(Scene3D::Internal::ModelCacheManager::Get()),
41   mLoadResult(mModelCacheManager.GetModelLoadResult(mModelUrl)),
42   mHasSucceeded(false)
43 {
44 }
45
46 ModelLoadTask::~ModelLoadTask()
47 {
48 }
49
50 void ModelLoadTask::Process()
51 {
52   if(mResourceDirectoryUrl.empty())
53   {
54     std::filesystem::path modelUrl(mModelUrl);
55     mResourceDirectoryUrl = std::string(modelUrl.parent_path()) + "/";
56   }
57
58   Dali::Scene3D::Loader::ResourceBundle::PathProvider pathProvider = [&](Dali::Scene3D::Loader::ResourceType::Value type) {
59     return mResourceDirectoryUrl;
60   };
61
62   mModelLoader = std::make_shared<Dali::Scene3D::Loader::ModelLoader>(mModelUrl, mResourceDirectoryUrl, mLoadResult);
63
64   bool                   loadSucceeded            = false;
65   Dali::ConditionalWait& loadSceneConditionalWait = mModelCacheManager.GetLoadSceneConditionalWaitInstance(mModelUrl);
66   {
67     ConditionalWait::ScopedLock lock(loadSceneConditionalWait);
68     if(mModelCacheManager.IsSceneLoaded(mModelUrl))
69     {
70       loadSucceeded = true;
71     }
72     else
73     {
74       mModelCacheManager.SetSceneLoading(mModelUrl, true);
75
76       loadSucceeded = mModelLoader->LoadModel(pathProvider, true);
77
78       // Mesh of glTF and dli is defined in right hand coordinate system, with positive Y for Up direction.
79       // Because DALi uses left hand system, Y direciton will be flipped for environment map sampling.
80       for(auto&& env : GetResources().mEnvironmentMaps)
81       {
82         env.first.mYDirection = Y_DIRECTION;
83       }
84
85       mModelCacheManager.SetSceneLoading(mModelUrl, false);
86       mModelCacheManager.SetSceneLoaded(mModelUrl, loadSucceeded);
87     }
88   }
89
90   if(!loadSucceeded)
91   {
92     DALI_LOG_ERROR("Failed to load scene from '%s'\n", mModelUrl.c_str());
93     return;
94   }
95
96   mHasSucceeded = true;
97 }
98
99 bool ModelLoadTask::IsReady()
100 {
101   return true;
102 }
103
104 bool ModelLoadTask::HasSucceeded() const
105 {
106   return mHasSucceeded;
107 }
108
109 Dali::Scene3D::Loader::SceneDefinition& ModelLoadTask::GetScene() const
110 {
111   return mModelLoader->GetScene();
112 }
113
114 Dali::Scene3D::Loader::ResourceBundle& ModelLoadTask::GetResources() const
115 {
116   return mModelLoader->GetResources();
117 }
118
119 std::vector<Dali::Scene3D::Loader::AnimationDefinition>& ModelLoadTask::GetAnimations() const
120 {
121   return mModelLoader->GetAnimations();
122 }
123
124 std::vector<Dali::Scene3D::Loader::CameraParameters>& ModelLoadTask::GetCameras() const
125 {
126   return mModelLoader->GetCameras();
127 }
128
129 Dali::Scene3D::Loader::Customization::Choices& ModelLoadTask::GetResourceChoices()
130 {
131   return mModelLoader->GetResourceChoices();
132 }
133
134 } // namespace Internal
135
136 } // namespace Scene3D
137
138 } // namespace Dali