forked from flutter-team-archive/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimporter_gltf.cc
More file actions
246 lines (213 loc) · 8.72 KB
/
Copy pathimporter_gltf.cc
File metadata and controls
246 lines (213 loc) · 8.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "impeller/scene/importer/importer.h"
#include <array>
#include <cstring>
#include <functional>
#include <iostream>
#include <memory>
#include <vector>
#include "flutter/fml/mapping.h"
#include "impeller/geometry/matrix.h"
#include "impeller/scene/importer/conversions.h"
#include "impeller/scene/importer/scene_flatbuffers.h"
#include "impeller/scene/importer/vertices_builder.h"
#include "third_party/tinygltf/tiny_gltf.h"
namespace impeller {
namespace scene {
namespace importer {
static const std::map<std::string, VerticesBuilder::AttributeType> kAttributes =
{{"POSITION", VerticesBuilder::AttributeType::kPosition},
{"NORMAL", VerticesBuilder::AttributeType::kNormal},
{"TANGENT", VerticesBuilder::AttributeType::kTangent},
{"TEXCOORD_0", VerticesBuilder::AttributeType::kTextureCoords},
{"COLOR_0", VerticesBuilder::AttributeType::kColor},
{"JOINTS_0", VerticesBuilder::AttributeType::kJoints},
{"WEIGHTS_0", VerticesBuilder::AttributeType::kWeights}};
static bool WithinRange(int index, size_t size) {
return index >= 0 && static_cast<size_t>(index) < size;
}
static bool MeshPrimitiveIsSkinned(const tinygltf::Primitive& primitive) {
return primitive.attributes.find("JOINTS_0") != primitive.attributes.end() &&
primitive.attributes.find("WEIGHTS_0") != primitive.attributes.end();
}
static bool ProcessMeshPrimitive(const tinygltf::Model& gltf,
const tinygltf::Primitive& primitive,
fb::MeshPrimitiveT& mesh_primitive) {
//---------------------------------------------------------------------------
/// Vertices.
///
{
bool is_skinned = MeshPrimitiveIsSkinned(primitive);
std::unique_ptr<VerticesBuilder> builder =
is_skinned ? VerticesBuilder::MakeSkinned()
: VerticesBuilder::MakeUnskinned();
for (const auto& attribute : primitive.attributes) {
auto attribute_type = kAttributes.find(attribute.first);
if (attribute_type == kAttributes.end()) {
std::cerr << "Vertex attribute \"" << attribute.first
<< "\" not supported." << std::endl;
continue;
}
if (!is_skinned &&
(attribute_type->second == VerticesBuilder::AttributeType::kJoints ||
attribute_type->second ==
VerticesBuilder::AttributeType::kWeights)) {
// If the primitive doesn't have enough information to be skinned, skip
// skinning-related attributes.
continue;
}
const auto accessor = gltf.accessors[attribute.second];
const auto view = gltf.bufferViews[accessor.bufferView];
const auto buffer = gltf.buffers[view.buffer];
const unsigned char* source_start = &buffer.data[view.byteOffset];
VerticesBuilder::ComponentType type;
switch (accessor.componentType) {
case TINYGLTF_COMPONENT_TYPE_BYTE:
type = VerticesBuilder::ComponentType::kSignedByte;
break;
case TINYGLTF_COMPONENT_TYPE_UNSIGNED_BYTE:
type = VerticesBuilder::ComponentType::kUnsignedByte;
break;
case TINYGLTF_COMPONENT_TYPE_SHORT:
type = VerticesBuilder::ComponentType::kSignedShort;
break;
case TINYGLTF_COMPONENT_TYPE_UNSIGNED_SHORT:
type = VerticesBuilder::ComponentType::kUnsignedShort;
break;
case TINYGLTF_COMPONENT_TYPE_INT:
type = VerticesBuilder::ComponentType::kSignedInt;
break;
case TINYGLTF_COMPONENT_TYPE_UNSIGNED_INT:
type = VerticesBuilder::ComponentType::kUnsignedInt;
break;
case TINYGLTF_COMPONENT_TYPE_FLOAT:
type = VerticesBuilder::ComponentType::kFloat;
break;
default:
std::cerr << "Skipping attribute \"" << attribute.first
<< "\" due to invalid component type." << std::endl;
continue;
}
builder->SetAttributeFromBuffer(
attribute_type->second, // attribute
type, // component_type
source_start, // buffer_start
accessor.ByteStride(view), // stride_bytes
accessor.count); // count
}
builder->WriteFBVertices(mesh_primitive);
}
//---------------------------------------------------------------------------
/// Indices.
///
if (!WithinRange(primitive.indices, gltf.accessors.size())) {
std::cerr << "Mesh primitive has no index buffer. Skipping." << std::endl;
return false;
}
auto index_accessor = gltf.accessors[primitive.indices];
auto index_view = gltf.bufferViews[index_accessor.bufferView];
auto indices = std::make_unique<fb::IndicesT>();
switch (index_accessor.componentType) {
case TINYGLTF_COMPONENT_TYPE_UNSIGNED_SHORT:
indices->type = fb::IndexType::k16Bit;
break;
case TINYGLTF_COMPONENT_TYPE_UNSIGNED_INT:
indices->type = fb::IndexType::k32Bit;
break;
default:
std::cerr << "Mesh primitive has unsupported index type "
<< index_accessor.componentType << ". Skipping.";
return false;
}
indices->count = index_accessor.count;
indices->data.resize(index_view.byteLength);
const auto* index_buffer =
&gltf.buffers[index_view.buffer].data[index_view.byteOffset];
std::memcpy(indices->data.data(), index_buffer, indices->data.size());
mesh_primitive.indices = std::move(indices);
return true;
}
static void ProcessNode(const tinygltf::Model& gltf,
const tinygltf::Node& in_node,
fb::NodeT& out_node) {
out_node.name = in_node.name;
out_node.children = in_node.children;
//---------------------------------------------------------------------------
/// Transform.
///
Matrix transform;
if (in_node.translation.size() == 3) {
transform = transform * Matrix::MakeTranslation(
{static_cast<Scalar>(in_node.translation[0]),
static_cast<Scalar>(in_node.translation[0]),
static_cast<Scalar>(in_node.translation[0])});
}
if (in_node.rotation.size() == 4) {
transform = transform * Matrix::MakeRotation(Quaternion(
in_node.rotation[0], in_node.rotation[1],
in_node.rotation[2], in_node.rotation[3]));
}
if (in_node.scale.size() == 3) {
transform =
transform * Matrix::MakeScale({static_cast<Scalar>(in_node.scale[0]),
static_cast<Scalar>(in_node.scale[1]),
static_cast<Scalar>(in_node.scale[2])});
}
if (in_node.matrix.size() == 16) {
if (!transform.IsIdentity()) {
std::cerr << "The `matrix` attribute of node (name: " << in_node.name
<< ") is set in addition to one or more of the "
"`translation/rotation/scale` attributes. Using only the "
"`matrix` "
"attribute.";
}
transform = ToMatrix(in_node.matrix);
}
out_node.transform = ToFBMatrix(transform);
//---------------------------------------------------------------------------
/// Static meshes.
///
if (WithinRange(in_node.mesh, gltf.meshes.size())) {
auto& mesh = gltf.meshes[in_node.mesh];
for (const auto& primitive : mesh.primitives) {
auto mesh_primitive = std::make_unique<fb::MeshPrimitiveT>();
if (!ProcessMeshPrimitive(gltf, primitive, *mesh_primitive)) {
continue;
}
out_node.mesh_primitives.push_back(std::move(mesh_primitive));
}
}
}
bool ParseGLTF(const fml::Mapping& source_mapping, fb::SceneT& out_scene) {
tinygltf::Model gltf;
{
tinygltf::TinyGLTF loader;
std::string error;
std::string warning;
bool success = loader.LoadBinaryFromMemory(&gltf, &error, &warning,
source_mapping.GetMapping(),
source_mapping.GetSize());
if (!warning.empty()) {
std::cerr << "Warning while loading GLTF: " << warning << std::endl;
}
if (!error.empty()) {
std::cerr << "Error while loading GLTF: " << error << std::endl;
}
if (!success) {
return false;
}
}
const tinygltf::Scene& scene = gltf.scenes[gltf.defaultScene];
out_scene.children = scene.nodes;
for (size_t node_i = 0; node_i < gltf.nodes.size(); node_i++) {
auto node = std::make_unique<fb::NodeT>();
ProcessNode(gltf, gltf.nodes[node_i], *node);
out_scene.nodes.push_back(std::move(node));
}
return true;
}
} // namespace importer
} // namespace scene
} // namespace impeller