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