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/model-loader-impl.h>
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";
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)
53 bool ModelLoader::LoadModel(Dali::Scene3D::Loader::ResourceBundle::PathProvider& pathProvider, bool loadOnlyRawResource)
60 bool loadSucceeded = false;
62 mLoadResult.mAnimationDefinitions.clear();
63 std::filesystem::path metaDataUrl(mModelUrl);
64 metaDataUrl.replace_extension(METADATA_EXTENSION.data());
66 Dali::Scene3D::Loader::LoadSceneMetadata(metaDataUrl.c_str(), mLoadResult.mSceneMetadata);
67 loadSucceeded = mImpl->LoadModel(mModelUrl, mLoadResult);
68 LoadResource(pathProvider, loadOnlyRawResource);
73 void ModelLoader::SetInputParameter(InputParameter& inputParameter)
75 mImpl->SetInputParameter(inputParameter);
78 Dali::Scene3D::Loader::SceneDefinition& ModelLoader::GetScene()
80 return mLoadResult.mScene;
83 Dali::Scene3D::Loader::ResourceBundle& ModelLoader::GetResources()
85 return mLoadResult.mResources;
88 std::vector<Dali::Scene3D::Loader::AnimationDefinition>& ModelLoader::GetAnimations()
90 return mLoadResult.mAnimationDefinitions;
93 std::vector<Dali::Scene3D::Loader::CameraParameters>& ModelLoader::GetCameras()
95 return mLoadResult.mCameraParameters;
98 Dali::Scene3D::Loader::Customization::Choices& ModelLoader::GetResourceChoices()
100 return mResourceChoices;
103 void ModelLoader::CreateModelLoader()
105 std::filesystem::path modelPath(mModelUrl);
106 if(mResourceDirectoryUrl.empty())
108 mResourceDirectoryUrl = std::string(modelPath.parent_path()) + "/";
110 std::string extension = modelPath.extension();
111 std::transform(extension.begin(), extension.end(), extension.begin(), ::tolower);
113 if(extension == DLI_EXTENSION)
115 mImpl = std::make_shared<Dali::Scene3D::Loader::Internal::DliLoaderImpl>();
117 else if(extension == GLTF_EXTENSION)
119 mImpl = std::make_shared<Dali::Scene3D::Loader::Internal::Gltf2LoaderImpl>();
123 DALI_LOG_ERROR("Not supported model format : %s\n", extension.c_str());
127 void ModelLoader::LoadResource(Dali::Scene3D::Loader::ResourceBundle::PathProvider& pathProvider, bool loadOnlyRawResource)
129 if(GetResources().mRawResourcesLoaded && loadOnlyRawResource)
134 Dali::Scene3D::Loader::ResourceRefCounts resourceRefCount = std::move(mLoadResult.mResources.CreateRefCounter());
135 for(auto iRoot : GetScene().GetRoots())
137 GetScene().CountResourceRefs(iRoot, mResourceChoices, resourceRefCount);
140 GetResources().mReferenceCounts = std::move(resourceRefCount);
141 GetResources().CountEnvironmentReferences();
143 if(loadOnlyRawResource)
145 GetResources().LoadRawResources(pathProvider);
149 GetResources().LoadResources(pathProvider);
153 } // namespace Loader
154 } // namespace Scene3D