[dali_2.3.21] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-scene3d / public-api / loader / model-loader.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 // FILE HEADER
19 #include <dali-scene3d/public-api/loader/model-loader.h>
20
21 // EXTERNAL INCLUDES
22 #include <dali/integration-api/debug.h>
23 #include <filesystem>
24 #include <memory>
25
26 // INTERNAL INCLUDES
27 #include <dali-scene3d/internal/loader/dli-loader-impl.h>
28 #include <dali-scene3d/internal/loader/glb-loader-impl.h>
29 #include <dali-scene3d/internal/loader/gltf2-loader-impl.h>
30 #include <dali-scene3d/internal/loader/model-loader-impl.h>
31
32 namespace Dali::Scene3D::Loader
33 {
34 namespace
35 {
36 static constexpr std::string_view OBJ_EXTENSION      = ".obj";
37 static constexpr std::string_view GLTF_EXTENSION     = ".gltf";
38 static constexpr std::string_view GLB_EXTENSION      = ".glb";
39 static constexpr std::string_view DLI_EXTENSION      = ".dli";
40 static constexpr std::string_view METADATA_EXTENSION = "metadata";
41 } // namespace
42
43 ModelLoader::ModelLoader(const std::string& modelUrl, const std::string& resourceDirectoryUrl, Dali::Scene3D::Loader::LoadResult& loadResult)
44 : mModelUrl(modelUrl),
45   mResourceDirectoryUrl(resourceDirectoryUrl),
46   mLoadResult(loadResult)
47 {
48   CreateModelLoader();
49 }
50
51 bool ModelLoader::LoadModel(Dali::Scene3D::Loader::ResourceBundle::PathProvider& pathProvider, bool loadOnlyRawResource)
52 {
53   if(!mImpl)
54   {
55     return false;
56   }
57
58   bool loadSucceeded = false;
59
60   mLoadResult.mAnimationDefinitions.clear();
61   std::filesystem::path metaDataUrl(mModelUrl);
62   metaDataUrl.replace_extension(METADATA_EXTENSION.data());
63
64   Dali::Scene3D::Loader::LoadSceneMetadata(metaDataUrl.c_str(), mLoadResult.mSceneMetadata);
65   loadSucceeded = mImpl->LoadModel(mModelUrl, mLoadResult);
66   LoadResource(pathProvider, loadOnlyRawResource);
67
68   return loadSucceeded;
69 }
70
71 void ModelLoader::SetInputParameter(InputParameter& inputParameter)
72 {
73   mImpl->SetInputParameter(inputParameter);
74 }
75
76 Dali::Scene3D::Loader::SceneDefinition& ModelLoader::GetScene()
77 {
78   return mLoadResult.mScene;
79 }
80
81 Dali::Scene3D::Loader::ResourceBundle& ModelLoader::GetResources()
82 {
83   return mLoadResult.mResources;
84 }
85
86 std::vector<Dali::Scene3D::Loader::AnimationDefinition>& ModelLoader::GetAnimations()
87 {
88   return mLoadResult.mAnimationDefinitions;
89 }
90
91 std::vector<Dali::Scene3D::Loader::CameraParameters>& ModelLoader::GetCameras()
92 {
93   return mLoadResult.mCameraParameters;
94 }
95
96 Dali::Scene3D::Loader::Customization::Choices& ModelLoader::GetResourceChoices()
97 {
98   return mResourceChoices;
99 }
100
101 void ModelLoader::CreateModelLoader()
102 {
103   std::filesystem::path modelPath(mModelUrl);
104   if(mResourceDirectoryUrl.empty())
105   {
106     mResourceDirectoryUrl = std::string(modelPath.parent_path()) + "/";
107   }
108   std::string extension = modelPath.extension();
109   std::transform(extension.begin(), extension.end(), extension.begin(), ::tolower);
110
111   if(extension == DLI_EXTENSION)
112   {
113     mImpl = std::make_shared<Dali::Scene3D::Loader::Internal::DliLoaderImpl>();
114   }
115   else if(extension == GLTF_EXTENSION)
116   {
117     mImpl = std::make_shared<Dali::Scene3D::Loader::Internal::Gltf2LoaderImpl>();
118   }
119   else if(extension == GLB_EXTENSION)
120   {
121     mImpl = std::make_shared<Dali::Scene3D::Loader::Internal::GlbLoaderImpl>();
122   }
123   else
124   {
125     DALI_LOG_ERROR("Not supported model format : %s\n", extension.c_str());
126   }
127 }
128
129 void ModelLoader::LoadResource(Dali::Scene3D::Loader::ResourceBundle::PathProvider& pathProvider, bool loadOnlyRawResource)
130 {
131   if(GetResources().mRawResourcesLoaded && loadOnlyRawResource)
132   {
133     return;
134   }
135
136   Dali::Scene3D::Loader::ResourceRefCounts resourceRefCount = mLoadResult.mResources.CreateRefCounter();
137   for(auto iRoot : GetScene().GetRoots())
138   {
139     GetScene().CountResourceRefs(iRoot, mResourceChoices, resourceRefCount);
140   }
141
142   GetResources().mReferenceCounts = std::move(resourceRefCount);
143   GetResources().CountEnvironmentReferences();
144
145   if(loadOnlyRawResource)
146   {
147     GetResources().LoadRawResources(pathProvider);
148   }
149   else
150   {
151     GetResources().LoadResources(pathProvider);
152   }
153 }
154
155 } // namespace Dali::Scene3D::Loader