2 * Copyright (c) 2023 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali-scene3d/public-api/loader/model-loader.h>
22 #include <dali/integration-api/debug.h>
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>
32 namespace Dali::Scene3D::Loader
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";
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)
51 bool ModelLoader::LoadModel(Dali::Scene3D::Loader::ResourceBundle::PathProvider& pathProvider, bool loadOnlyRawResource)
58 bool loadSucceeded = false;
60 mLoadResult.mAnimationDefinitions.clear();
61 std::filesystem::path metaDataUrl(mModelUrl);
62 metaDataUrl.replace_extension(METADATA_EXTENSION.data());
64 Dali::Scene3D::Loader::LoadSceneMetadata(metaDataUrl.c_str(), mLoadResult.mSceneMetadata);
65 loadSucceeded = mImpl->LoadModel(mModelUrl, mLoadResult);
66 LoadResource(pathProvider, loadOnlyRawResource);
71 void ModelLoader::SetInputParameter(InputParameter& inputParameter)
73 mImpl->SetInputParameter(inputParameter);
76 Dali::Scene3D::Loader::SceneDefinition& ModelLoader::GetScene()
78 return mLoadResult.mScene;
81 Dali::Scene3D::Loader::ResourceBundle& ModelLoader::GetResources()
83 return mLoadResult.mResources;
86 std::vector<Dali::Scene3D::Loader::AnimationDefinition>& ModelLoader::GetAnimations()
88 return mLoadResult.mAnimationDefinitions;
91 std::vector<Dali::Scene3D::Loader::CameraParameters>& ModelLoader::GetCameras()
93 return mLoadResult.mCameraParameters;
96 Dali::Scene3D::Loader::Customization::Choices& ModelLoader::GetResourceChoices()
98 return mResourceChoices;
101 void ModelLoader::CreateModelLoader()
103 std::filesystem::path modelPath(mModelUrl);
104 if(mResourceDirectoryUrl.empty())
106 mResourceDirectoryUrl = std::string(modelPath.parent_path()) + "/";
108 std::string extension = modelPath.extension();
109 std::transform(extension.begin(), extension.end(), extension.begin(), ::tolower);
111 if(extension == DLI_EXTENSION)
113 mImpl = std::make_shared<Dali::Scene3D::Loader::Internal::DliLoaderImpl>();
115 else if(extension == GLTF_EXTENSION)
117 mImpl = std::make_shared<Dali::Scene3D::Loader::Internal::Gltf2LoaderImpl>();
119 else if(extension == GLB_EXTENSION)
121 mImpl = std::make_shared<Dali::Scene3D::Loader::Internal::GlbLoaderImpl>();
125 DALI_LOG_ERROR("Not supported model format : %s\n", extension.c_str());
129 void ModelLoader::LoadResource(Dali::Scene3D::Loader::ResourceBundle::PathProvider& pathProvider, bool loadOnlyRawResource)
131 if(GetResources().mRawResourcesLoaded && loadOnlyRawResource)
136 Dali::Scene3D::Loader::ResourceRefCounts resourceRefCount = mLoadResult.mResources.CreateRefCounter();
137 for(auto iRoot : GetScene().GetRoots())
139 GetScene().CountResourceRefs(iRoot, mResourceChoices, resourceRefCount);
142 GetResources().mReferenceCounts = std::move(resourceRefCount);
143 GetResources().CountEnvironmentReferences();
145 if(loadOnlyRawResource)
147 GetResources().LoadRawResources(pathProvider);
151 GetResources().LoadResources(pathProvider);
155 } // namespace Dali::Scene3D::Loader