1 /*
2 * Copyright (C) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include "gltf2.h"
16
17 #include <3d/gltf/gltf.h>
18 #include <3d/intf_graphics_context.h>
19 #include <core/ecs/intf_ecs.h>
20 #include <core/ecs/intf_entity_manager.h>
21 #include <core/intf_engine.h>
22 #include <core/io/intf_file_manager.h>
23 #include <core/log.h>
24 #include <core/plugin/intf_plugin_register.h>
25 #include <render/device/intf_gpu_resource_manager.h>
26 #include <render/intf_render_context.h>
27
28 #include "data.h"
29 #include "gltf2_exporter.h"
30 #include "gltf2_importer.h"
31 #include "gltf2_loader.h"
32
33 CORE3D_BEGIN_NAMESPACE()
34 using namespace BASE_NS;
35 using namespace CORE_NS;
36
Gltf2(IGraphicsContext & graphicsContext)37 Gltf2::Gltf2(IGraphicsContext& graphicsContext)
38 : engine_(&(graphicsContext.GetRenderContext().GetEngine())), renderContext_(&graphicsContext.GetRenderContext()),
39 fileManager_(engine_->GetFileManager())
40 {}
41
Gltf2(IFileManager & fileManager)42 Gltf2::Gltf2(IFileManager& fileManager) : fileManager_(fileManager) {}
43
44 // Internal helper.
LoadGLTF(IFileManager & fileManager,const string_view uri)45 GLTFLoadResult LoadGLTF(IFileManager& fileManager, const string_view uri)
46 {
47 auto loadResult = GLTF2::LoadGLTF(fileManager, uri);
48 GLTFLoadResult result;
49 result.error = std::move(loadResult.error);
50 result.success = loadResult.success;
51 result.data = IGLTFData::Ptr { loadResult.data.release() };
52
53 return result;
54 }
55
56 // Api loading function.
LoadGLTF(const string_view uri)57 GLTFLoadResult Gltf2::LoadGLTF(const string_view uri)
58 {
59 return CORE3D_NS::LoadGLTF(fileManager_, uri);
60 }
61
LoadGLTF(array_view<uint8_t const> data)62 GLTFLoadResult Gltf2::LoadGLTF(array_view<uint8_t const> data)
63 {
64 auto loadResult = GLTF2::LoadGLTF(fileManager_, data);
65 GLTFLoadResult result;
66 result.error = std::move(loadResult.error);
67 result.success = loadResult.success;
68 result.data = IGLTFData::Ptr { loadResult.data.release() };
69 return result;
70 }
71
72 // Api import functions
ImportGltfScene(size_t sceneIndex,const IGLTFData & gltfData,const GLTFResourceData & gltfResourceData,IEcs & ecs,Entity rootEntity,GltfSceneImportFlags flags)73 Entity Gltf2::ImportGltfScene(size_t sceneIndex, const IGLTFData& gltfData, const GLTFResourceData& gltfResourceData,
74 IEcs& ecs, Entity rootEntity, GltfSceneImportFlags flags)
75 {
76 CORE_ASSERT(renderContext_);
77 if (renderContext_) {
78 const GLTF2::Data& data = static_cast<const GLTF2::Data&>(gltfData);
79 return ImportScene(renderContext_->GetDevice(), sceneIndex, data, gltfResourceData, ecs, rootEntity, flags);
80 }
81 return {};
82 }
83
CreateGLTF2Importer(IEcs & ecs)84 IGLTF2Importer::Ptr Gltf2::CreateGLTF2Importer(IEcs& ecs)
85 {
86 CORE_ASSERT(engine_ && renderContext_);
87 if (engine_ && renderContext_) {
88 if (auto pool = ecs.GetThreadPool(); pool) {
89 return CreateGLTF2Importer(ecs, *pool);
90 }
91 return IGLTF2Importer::Ptr { new GLTF2::GLTF2Importer(*engine_, *renderContext_, ecs) };
92 }
93 return nullptr;
94 }
95
CreateGLTF2Importer(IEcs & ecs,IThreadPool & pool)96 IGLTF2Importer::Ptr Gltf2::CreateGLTF2Importer(IEcs& ecs, IThreadPool& pool)
97 {
98 CORE_ASSERT(engine_ && renderContext_);
99 if (engine_ && renderContext_) {
100 return IGLTF2Importer::Ptr { new GLTF2::GLTF2Importer(*engine_, *renderContext_, ecs, pool) };
101 }
102 return nullptr;
103 }
104
105 // Api exporting function.
SaveGLTF(IEcs & ecs,const string_view uri)106 bool Gltf2::SaveGLTF(IEcs& ecs, const string_view uri)
107 {
108 CORE_ASSERT(engine_);
109 if (engine_) {
110 if (auto result = GLTF2::ExportGLTF(*engine_, ecs); result.success) {
111 if (auto file = fileManager_.CreateFile(uri); file) {
112 auto const ext = uri.rfind('.');
113 auto const extension = string_view(uri.data() + ext + 1);
114 if (extension == "gltf") {
115 for (auto const& buffer : result.data->buffers) {
116 string dataFileUri = uri.substr(0, ext) + ".bin";
117 if (auto dataFile = fileManager_.CreateFile(dataFileUri); dataFile) {
118 dataFile->Write(buffer->data.data(), buffer->data.size());
119 if (auto const path = dataFileUri.rfind('/'); path != string::npos) {
120 dataFileUri.erase(0, path + 1);
121 }
122 buffer->uri = dataFileUri;
123 }
124 }
125 GLTF2::SaveGLTF(*result.data, *file, engine_->GetVersion());
126 } else {
127 GLTF2::SaveGLB(*result.data, *file, engine_->GetVersion());
128 }
129 } else {
130 result.error += "Failed to create file: " + uri;
131 result.success = false;
132 }
133 return result.success;
134 } else {
135 return result.success;
136 }
137 }
138 return false;
139 }
140 CORE3D_END_NAMESPACE()
141