Asynchronous loading of Scene3D resources
[platform/core/uifw/dali-toolkit.git] / dali-scene3d / internal / common / model-load-task.cpp
1 /*
2  * Copyright (c) 2022 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 <filesystem>
23 #include <dali/integration-api/debug.h>
24
25 // INTERNAL INCLUDES
26 #include <dali-scene3d/public-api/loader/animation-definition.h>
27 #include <dali-scene3d/public-api/loader/camera-parameters.h>
28 #include <dali-scene3d/public-api/loader/cube-map-loader.h>
29 #include <dali-scene3d/public-api/loader/dli-loader.h>
30 #include <dali-scene3d/public-api/loader/gltf2-loader.h>
31 #include <dali-scene3d/public-api/loader/light-parameters.h>
32 #include <dali-scene3d/public-api/loader/node-definition.h>
33 #include <dali-scene3d/public-api/loader/shader-definition-factory.h>
34
35
36 namespace Dali
37 {
38 namespace Scene3D
39 {
40 namespace Internal
41 {
42 namespace
43 {
44 static constexpr Vector3 Y_DIRECTION(1.0f, -1.0f, 1.0f);
45
46 static constexpr std::string_view OBJ_EXTENSION      = ".obj";
47 static constexpr std::string_view GLTF_EXTENSION     = ".gltf";
48 static constexpr std::string_view DLI_EXTENSION      = ".dli";
49 static constexpr std::string_view METADATA_EXTENSION = "metadata";
50 } // namespace
51
52 ModelLoadTask::ModelLoadTask(const std::string& modelUrl, const std::string& resourceDirectoryUrl, CallbackBase* callback)
53 : AsyncTask(callback),
54   mModelUrl(modelUrl),
55   mResourceDirectoryUrl(resourceDirectoryUrl),
56   mHasSucceeded(false)
57 {
58 }
59
60 ModelLoadTask::~ModelLoadTask()
61 {
62 }
63
64 void ModelLoadTask::Process()
65 {
66   std::filesystem::path modelUrl(mModelUrl);
67   if(mResourceDirectoryUrl.empty())
68   {
69     mResourceDirectoryUrl = std::string(modelUrl.parent_path()) + "/";
70   }
71   std::string extension = modelUrl.extension();
72   std::transform(extension.begin(), extension.end(), extension.begin(), ::tolower);
73
74   Dali::Scene3D::Loader::ResourceBundle::PathProvider pathProvider = [&](Dali::Scene3D::Loader::ResourceType::Value type)
75   {
76     return mResourceDirectoryUrl;
77   };
78   mAnimations.clear();
79
80   std::filesystem::path metaDataUrl = modelUrl;
81   metaDataUrl.replace_extension(METADATA_EXTENSION.data());
82
83   Dali::Scene3D::Loader::LoadSceneMetadata(metaDataUrl.c_str(), mMetaData);
84
85   Dali::Scene3D::Loader::LoadResult result{mResources, mScene, mMetaData, mAnimations, mAnimGroups, mCameraParameters, mLights};
86
87   if(extension == DLI_EXTENSION)
88   {
89     Dali::Scene3D::Loader::DliLoader              loader;
90     Dali::Scene3D::Loader::DliLoader::InputParams input{
91       pathProvider(Dali::Scene3D::Loader::ResourceType::Mesh),
92       nullptr,
93       {},
94       {},
95       nullptr,
96       {}};
97     Dali::Scene3D::Loader::DliLoader::LoadParams loadParams{input, result};
98     if(!loader.LoadScene(mModelUrl, loadParams))
99     {
100       DALI_LOG_ERROR("Failed to load scene from '%s': %s\n", mModelUrl.c_str(), loader.GetParseError().c_str());
101       return;
102     }
103   }
104   else if(extension == GLTF_EXTENSION)
105   {
106     Dali::Scene3D::Loader::ShaderDefinitionFactory sdf;
107     sdf.SetResources(mResources);
108     Dali::Scene3D::Loader::LoadGltfScene(mModelUrl, sdf, result);
109   }
110   else
111   {
112     DALI_LOG_ERROR("Unsupported model type.\n");
113     return;
114   }
115
116   for(auto iRoot : mScene.GetRoots())
117   {
118     mResourceRefCounts.push_back(mResources.CreateRefCounter());
119     mScene.CountResourceRefs(iRoot, mResourceChoices, mResourceRefCounts.back());
120     mResources.CountEnvironmentReferences(mResourceRefCounts.back());
121
122     mResources.LoadRawResources(mResourceRefCounts.back(), pathProvider);
123
124     // glTF Mesh is defined in right hand coordinate system, with positive Y for Up direction.
125     // Because DALi uses left hand system, Y direciton will be flipped for environment map sampling.
126     for(auto&& env : mResources.mEnvironmentMaps)
127     {
128       env.first.mYDirection = Y_DIRECTION;
129     }
130   }
131   mHasSucceeded = true;
132 }
133
134 bool ModelLoadTask::IsReady()
135 {
136   return true;
137 }
138
139 bool ModelLoadTask::HasSucceeded() const
140 {
141   return mHasSucceeded;
142 }
143
144 } // namespace Internal
145
146 } // namespace Scene3D
147
148 } // namespace Dali