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/gltf2-loader-impl.h>
29 #include <dali-scene3d/internal/loader/glb-loader-impl.h>
30 #include <dali-scene3d/internal/loader/model-loader-impl.h>
40 static constexpr std::string_view OBJ_EXTENSION = ".obj";
41 static constexpr std::string_view GLTF_EXTENSION = ".gltf";
42 static constexpr std::string_view GLB_EXTENSION = ".glb";
43 static constexpr std::string_view DLI_EXTENSION = ".dli";
44 static constexpr std::string_view METADATA_EXTENSION = "metadata";
47 ModelLoader::ModelLoader(const std::string& modelUrl, const std::string& resourceDirectoryUrl, Dali::Scene3D::Loader::LoadResult& loadResult)
48 : mModelUrl(modelUrl),
49 mResourceDirectoryUrl(resourceDirectoryUrl),
50 mLoadResult(loadResult)
55 bool ModelLoader::LoadModel(Dali::Scene3D::Loader::ResourceBundle::PathProvider& pathProvider, bool loadOnlyRawResource)
62 bool loadSucceeded = false;
64 mLoadResult.mAnimationDefinitions.clear();
65 std::filesystem::path metaDataUrl(mModelUrl);
66 metaDataUrl.replace_extension(METADATA_EXTENSION.data());
68 Dali::Scene3D::Loader::LoadSceneMetadata(metaDataUrl.c_str(), mLoadResult.mSceneMetadata);
69 loadSucceeded = mImpl->LoadModel(mModelUrl, mLoadResult);
70 LoadResource(pathProvider, loadOnlyRawResource);
75 void ModelLoader::SetInputParameter(InputParameter& inputParameter)
77 mImpl->SetInputParameter(inputParameter);
80 Dali::Scene3D::Loader::SceneDefinition& ModelLoader::GetScene()
82 return mLoadResult.mScene;
85 Dali::Scene3D::Loader::ResourceBundle& ModelLoader::GetResources()
87 return mLoadResult.mResources;
90 std::vector<Dali::Scene3D::Loader::AnimationDefinition>& ModelLoader::GetAnimations()
92 return mLoadResult.mAnimationDefinitions;
95 std::vector<Dali::Scene3D::Loader::CameraParameters>& ModelLoader::GetCameras()
97 return mLoadResult.mCameraParameters;
100 Dali::Scene3D::Loader::Customization::Choices& ModelLoader::GetResourceChoices()
102 return mResourceChoices;
105 void ModelLoader::CreateModelLoader()
107 std::filesystem::path modelPath(mModelUrl);
108 if(mResourceDirectoryUrl.empty())
110 mResourceDirectoryUrl = std::string(modelPath.parent_path()) + "/";
112 std::string extension = modelPath.extension();
113 std::transform(extension.begin(), extension.end(), extension.begin(), ::tolower);
115 if(extension == DLI_EXTENSION)
117 mImpl = std::make_shared<Dali::Scene3D::Loader::Internal::DliLoaderImpl>();
119 else if(extension == GLTF_EXTENSION)
121 mImpl = std::make_shared<Dali::Scene3D::Loader::Internal::Gltf2LoaderImpl>();
123 else if(extension == GLB_EXTENSION)
125 mImpl = std::make_shared<Dali::Scene3D::Loader::Internal::GlbLoaderImpl>();
129 DALI_LOG_ERROR("Not supported model format : %s\n", extension.c_str());
133 void ModelLoader::LoadResource(Dali::Scene3D::Loader::ResourceBundle::PathProvider& pathProvider, bool loadOnlyRawResource)
135 if(GetResources().mRawResourcesLoaded && loadOnlyRawResource)
140 Dali::Scene3D::Loader::ResourceRefCounts resourceRefCount = std::move(mLoadResult.mResources.CreateRefCounter());
141 for(auto iRoot : GetScene().GetRoots())
143 GetScene().CountResourceRefs(iRoot, mResourceChoices, resourceRefCount);
146 GetResources().mReferenceCounts = std::move(resourceRefCount);
147 GetResources().CountEnvironmentReferences();
149 if(loadOnlyRawResource)
151 GetResources().LoadRawResources(pathProvider);
155 GetResources().LoadResources(pathProvider);
159 } // namespace Loader
160 } // namespace Scene3D