| Flutter Impeller |

importer_unittests.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "flutter/testing/testing.h"
6 #include "impeller/geometry/geometry_asserts.h"
7 #include "impeller/geometry/matrix.h"
8 #include "impeller/scene/importer/conversions.h"
9 #include "impeller/scene/importer/importer.h"
10 #include "impeller/scene/importer/scene_flatbuffers.h"
11
12 namespace impeller {
13 namespace scene {
14 namespace importer {
15 namespace testing {
16
17 TEST(ImporterTest, CanParseUnskinnedGLTF) {
18 auto mapping =
19 flutter::testing::OpenFixtureAsMapping("flutter_logo_baked.glb");
20
21 fb::SceneT scene;
22 ASSERT_TRUE(ParseGLTF(*mapping, scene));
23
24 ASSERT_EQ(scene.children.size(), 1u);
25 auto& node = scene.nodes[scene.children[0]];
26
27 Matrix node_transform = ToMatrix(*node->transform);
28 ASSERT_MATRIX_NEAR(node_transform, Matrix());
29
30 ASSERT_EQ(node->mesh_primitives.size(), 1u);
31 auto& mesh = *node->mesh_primitives[0];
32 ASSERT_EQ(mesh.indices->count, 918u);
33
34 uint16_t first_index =
35 *reinterpret_cast<uint16_t*>(mesh.indices->data.data());
36 ASSERT_EQ(first_index, 45u);
37
38 ASSERT_EQ(mesh.vertices.type, fb::VertexBuffer::UnskinnedVertexBuffer);
39 auto& vertices = mesh.vertices.AsUnskinnedVertexBuffer()->vertices;
40 ASSERT_EQ(vertices.size(), 260u);
41 auto& vertex = vertices[0];
42
43 Vector3 position = ToVector3(vertex.position());
44 ASSERT_VECTOR3_NEAR(position, Vector3(-0.0100185, -0.522907, 0.133178));
45
46 Vector3 normal = ToVector3(vertex.normal());
47 ASSERT_VECTOR3_NEAR(normal, Vector3(0.556997, -0.810833, 0.179733));
48
49 Vector4 tangent = ToVector4(vertex.tangent());
50 ASSERT_VECTOR4_NEAR(tangent, Vector4(0.155901, -0.110485, -0.981574, 1));
51
52 Vector2 texture_coords = ToVector2(vertex.texture_coords());
53 ASSERT_POINT_NEAR(texture_coords, Vector2(0.727937, 0.713817));
54
55 Color color = ToColor(vertex.color());
56 ASSERT_COLOR_NEAR(color, Color(0.0221714, 0.467781, 0.921584, 1));
57 }
58
59 TEST(ImporterTest, CanParseSkinnedGLTF) {
60 auto mapping = flutter::testing::OpenFixtureAsMapping("two_triangles.glb");
61
62 fb::SceneT scene;
63 ASSERT_TRUE(ParseGLTF(*mapping, scene));
64
65 ASSERT_EQ(scene.children.size(), 1u);
66 auto& node = scene.nodes[scene.children[0]];
67
68 Matrix node_transform = ToMatrix(*node->transform);
69 ASSERT_MATRIX_NEAR(node_transform, Matrix());
70
71 ASSERT_EQ(node->mesh_primitives.size(), 0u);
72 ASSERT_EQ(node->children.size(), 2u);
73
74 // The skinned node contains both a skeleton and skinned mesh primitives that
75 // reference bones in the skeleton.
76 auto& skinned_node = scene.nodes[node->children[0]];
77 ASSERT_NE(skinned_node->skin, nullptr);
78
79 ASSERT_EQ(skinned_node->mesh_primitives.size(), 2u);
80 auto& bottom_triangle = *skinned_node->mesh_primitives[0];
81 ASSERT_EQ(bottom_triangle.indices->count, 3u);
82
83 ASSERT_EQ(bottom_triangle.vertices.type,
84 fb::VertexBuffer::SkinnedVertexBuffer);
85 auto& vertices = bottom_triangle.vertices.AsSkinnedVertexBuffer()->vertices;
86 ASSERT_EQ(vertices.size(), 3u);
87 auto& vertex = vertices[0];
88
89 Vector3 position = ToVector3(vertex.vertex().position());
90 ASSERT_VECTOR3_NEAR(position, Vector3(1, 1, 0));
91
92 Vector3 normal = ToVector3(vertex.vertex().normal());
93 ASSERT_VECTOR3_NEAR(normal, Vector3(0, 0, 1));
94
95 Vector4 tangent = ToVector4(vertex.vertex().tangent());
96 ASSERT_VECTOR4_NEAR(tangent, Vector4(1, 0, 0, -1));
97
98 Vector2 texture_coords = ToVector2(vertex.vertex().texture_coords());
99 ASSERT_POINT_NEAR(texture_coords, Vector2(0, 1));
100
101 Color color = ToColor(vertex.vertex().color());
102 ASSERT_COLOR_NEAR(color, Color(1, 1, 1, 1));
103
104 Vector4 joints = ToVector4(vertex.joints());
105 ASSERT_VECTOR4_NEAR(joints, Vector4(0, 0, 0, 0));
106
107 Vector4 weights = ToVector4(vertex.weights());
108 ASSERT_VECTOR4_NEAR(weights, Vector4(1, 0, 0, 0));
109
110 ASSERT_EQ(scene.animations.size(), 2u);
111 ASSERT_EQ(scene.animations[0]->name, "Idle");
112 ASSERT_EQ(scene.animations[1]->name, "Metronome");
113 ASSERT_EQ(scene.animations[1]->channels.size(), 6u);
114 auto& channel = scene.animations[1]->channels[3];
115 ASSERT_EQ(channel->keyframes.type, fb::Keyframes::RotationKeyframes);
116 auto* keyframes = channel->keyframes.AsRotationKeyframes();
117 ASSERT_EQ(keyframes->values.size(), 40u);
118 ASSERT_VECTOR4_NEAR(ToVector4(keyframes->values[0]),
119 Vector4(0.653281, -0.270598, 0.270598, 0.653281));
120 ASSERT_VECTOR4_NEAR(ToVector4(keyframes->values[10]),
121 Vector4(0.700151, 0.0989373, -0.0989373, 0.700151));
122 }
123
124 } // namespace testing
125 } // namespace importer
126 } // namespace scene
127 } // namespace impeller
ASSERT_COLOR_NEAR
#define ASSERT_COLOR_NEAR(a, b)
Definition: geometry_asserts.h:155
impeller::scene::importer::ParseGLTF
bool ParseGLTF(const fml::Mapping &source_mapping, fb::SceneT &out_scene)
Definition: importer_gltf.cc:450
geometry_asserts.h
impeller::Color
Definition: color.h:122
impeller::Vector4
Definition: vector.h:229
importer.h
impeller::Vector2
Point Vector2
Definition: point.h:310
impeller::scene::importer::ToVector4
Vector4 ToVector4(const fb::Vec4 &v)
Definition: conversions.cc:42
ASSERT_POINT_NEAR
#define ASSERT_POINT_NEAR(a, b)
Definition: geometry_asserts.h:156
ASSERT_VECTOR4_NEAR
#define ASSERT_VECTOR4_NEAR(a, b)
Definition: geometry_asserts.h:158
conversions.h
matrix.h
impeller::scene::importer::ToColor
Color ToColor(const fb::Color &c)
Definition: conversions.cc:46
impeller::scene::importer::ToVector3
Vector3 ToVector3(const fb::Vec3 &v)
Definition: conversions.cc:38
ASSERT_MATRIX_NEAR
#define ASSERT_MATRIX_NEAR(a, b)
Definition: geometry_asserts.h:152
impeller::scene::importer::ToVector2
Vector2 ToVector2(const fb::Vec2 &v)
Definition: conversions.cc:34
ASSERT_VECTOR3_NEAR
#define ASSERT_VECTOR3_NEAR(a, b)
Definition: geometry_asserts.h:157
impeller::TPoint< Scalar >
impeller::scene::importer::testing::TEST
TEST(ImporterTest, CanParseUnskinnedGLTF)
Definition: importer_unittests.cc:17
impeller
Definition: aiks_context.cc:10
impeller::Matrix
A 4x4 matrix using column-major storage.
Definition: matrix.h:36
impeller::Vector3
Definition: vector.h:17
impeller::scene::importer::ToMatrix
Matrix ToMatrix(const std::vector< double > &m)
Definition: conversions.cc:15