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/internal/common/model-load-task.h>
23 #include <dali/integration-api/debug.h>
26 #include <dali-scene3d/public-api/loader/animation-definition.h>
27 #include <dali-scene3d/public-api/loader/camera-parameters.h>
28 #include <dali-scene3d/public-api/loader/dli-loader.h>
29 #include <dali-scene3d/public-api/loader/gltf2-loader.h>
30 #include <dali-scene3d/public-api/loader/light-parameters.h>
31 #include <dali-scene3d/public-api/loader/node-definition.h>
32 #include <dali-scene3d/public-api/loader/shader-definition-factory.h>
43 static constexpr Vector3 Y_DIRECTION(1.0f, -1.0f, 1.0f);
45 static constexpr std::string_view OBJ_EXTENSION = ".obj";
46 static constexpr std::string_view GLTF_EXTENSION = ".gltf";
47 static constexpr std::string_view DLI_EXTENSION = ".dli";
48 static constexpr std::string_view METADATA_EXTENSION = "metadata";
51 ModelLoadTask::ModelLoadTask(const std::string& modelUrl, const std::string& resourceDirectoryUrl, CallbackBase* callback)
52 : AsyncTask(callback),
54 mResourceDirectoryUrl(resourceDirectoryUrl),
59 ModelLoadTask::~ModelLoadTask()
63 void ModelLoadTask::Process()
65 std::filesystem::path modelUrl(mModelUrl);
66 if(mResourceDirectoryUrl.empty())
68 mResourceDirectoryUrl = std::string(modelUrl.parent_path()) + "/";
70 std::string extension = modelUrl.extension();
71 std::transform(extension.begin(), extension.end(), extension.begin(), ::tolower);
73 Dali::Scene3D::Loader::ResourceBundle::PathProvider pathProvider = [&](Dali::Scene3D::Loader::ResourceType::Value type)
75 return mResourceDirectoryUrl;
79 std::filesystem::path metaDataUrl = modelUrl;
80 metaDataUrl.replace_extension(METADATA_EXTENSION.data());
82 Dali::Scene3D::Loader::LoadSceneMetadata(metaDataUrl.c_str(), mMetaData);
84 Dali::Scene3D::Loader::LoadResult result{mResources, mScene, mMetaData, mAnimations, mAnimGroups, mCameraParameters, mLights};
86 if(extension == DLI_EXTENSION)
88 Dali::Scene3D::Loader::DliLoader loader;
89 Dali::Scene3D::Loader::DliLoader::InputParams input{
90 pathProvider(Dali::Scene3D::Loader::ResourceType::Mesh),
96 Dali::Scene3D::Loader::DliLoader::LoadParams loadParams{input, result};
97 if(!loader.LoadScene(mModelUrl, loadParams))
99 DALI_LOG_ERROR("Failed to load scene from '%s': %s\n", mModelUrl.c_str(), loader.GetParseError().c_str());
103 else if(extension == GLTF_EXTENSION)
105 Dali::Scene3D::Loader::ShaderDefinitionFactory sdf;
106 sdf.SetResources(mResources);
107 Dali::Scene3D::Loader::LoadGltfScene(mModelUrl, sdf, result);
111 DALI_LOG_ERROR("Unsupported model type.\n");
115 for(auto iRoot : mScene.GetRoots())
117 mResourceRefCounts.push_back(mResources.CreateRefCounter());
118 mScene.CountResourceRefs(iRoot, mResourceChoices, mResourceRefCounts.back());
119 mResources.CountEnvironmentReferences(mResourceRefCounts.back());
121 mResources.LoadRawResources(mResourceRefCounts.back(), pathProvider);
123 // glTF Mesh is defined in right hand coordinate system, with positive Y for Up direction.
124 // Because DALi uses left hand system, Y direciton will be flipped for environment map sampling.
125 for(auto&& env : mResources.mEnvironmentMaps)
127 env.first.mYDirection = Y_DIRECTION;
130 mHasSucceeded = true;
133 bool ModelLoadTask::IsReady()
138 bool ModelLoadTask::HasSucceeded() const
140 return mHasSucceeded;
143 } // namespace Internal
145 } // namespace Scene3D