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