2 * Copyright (c) 2024 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/internal/common/model-load-task.h>
22 #include <dali/integration-api/debug.h>
33 static constexpr Vector3 Y_DIRECTION(1.0f, -1.0f, 1.0f);
36 ModelLoadTask::ModelLoadTask(const std::string& modelUrl, const std::string& resourceDirectoryUrl, CallbackBase* callback)
37 : AsyncTask(callback, AsyncTask::PriorityType::LOW),
39 mResourceDirectoryUrl(resourceDirectoryUrl),
40 mModelCacheManager(Scene3D::Internal::ModelCacheManager::Get()),
41 mLoadResult(mModelCacheManager.GetModelLoadResult(mModelUrl)),
44 mModelCacheManager.ReferenceModelCache(mModelUrl);
47 ModelLoadTask::~ModelLoadTask()
49 mModelCacheManager.UnreferenceModelCache(mModelUrl);
52 void ModelLoadTask::Process()
54 if(mResourceDirectoryUrl.empty())
56 std::filesystem::path modelUrl(mModelUrl);
57 mResourceDirectoryUrl = std::string(modelUrl.parent_path()) + "/";
60 Dali::Scene3D::Loader::ResourceBundle::PathProvider pathProvider = [&](Dali::Scene3D::Loader::ResourceType::Value type) {
61 return mResourceDirectoryUrl;
64 mModelLoader = std::make_shared<Dali::Scene3D::Loader::ModelLoader>(mModelUrl, mResourceDirectoryUrl, mLoadResult);
66 bool loadSucceeded = false;
68 // Lock model url during process, so let we do not try to load same model multiple times.
69 mModelCacheManager.LockModelLoadScene(mModelUrl);
70 if(mModelCacheManager.IsSceneLoaded(mModelUrl))
76 mModelCacheManager.SetSceneLoading(mModelUrl, true);
78 loadSucceeded = mModelLoader->LoadModel(pathProvider, true);
80 // Mesh of glTF and dli is defined in right hand coordinate system, with positive Y for Up direction.
81 // Because DALi uses left hand system, Y direciton will be flipped for environment map sampling.
82 for(auto&& env : GetResources().mEnvironmentMaps)
84 env.first.mYDirection = Y_DIRECTION;
87 mModelCacheManager.SetSceneLoading(mModelUrl, false);
88 mModelCacheManager.SetSceneLoaded(mModelUrl, loadSucceeded);
90 mModelCacheManager.UnlockModelLoadScene(mModelUrl);
95 DALI_LOG_ERROR("Failed to load scene from '%s'\n", mModelUrl.c_str());
102 bool ModelLoadTask::IsReady()
107 bool ModelLoadTask::HasSucceeded() const
109 return mHasSucceeded;
112 Dali::Scene3D::Loader::SceneDefinition& ModelLoadTask::GetScene() const
114 return mModelLoader->GetScene();
117 Dali::Scene3D::Loader::ResourceBundle& ModelLoadTask::GetResources() const
119 return mModelLoader->GetResources();
122 std::vector<Dali::Scene3D::Loader::AnimationDefinition>& ModelLoadTask::GetAnimations() const
124 return mModelLoader->GetAnimations();
127 std::vector<Dali::Scene3D::Loader::CameraParameters>& ModelLoadTask::GetCameras() const
129 return mModelLoader->GetCameras();
132 Dali::Scene3D::Loader::Customization::Choices& ModelLoadTask::GetResourceChoices()
134 return mModelLoader->GetResourceChoices();
137 } // namespace Internal
139 } // namespace Scene3D