| Flutter Impeller |

scene_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 <cmath>
6 #include <memory>
7 #include <vector>
8
9 #include "flutter/fml/mapping.h"
10 #include "flutter/testing/testing.h"
11 #include "impeller/core/formats.h"
12 #include "impeller/geometry/color.h"
13 #include "impeller/geometry/constants.h"
14 #include "impeller/geometry/matrix.h"
15 #include "impeller/geometry/quaternion.h"
16 #include "impeller/geometry/vector.h"
17 #include "impeller/image/decompressed_image.h"
18 #include "impeller/playground/playground.h"
19 #include "impeller/playground/playground_test.h"
20 #include "impeller/scene/animation/animation_clip.h"
21 #include "impeller/scene/camera.h"
22 #include "impeller/scene/geometry.h"
23 #include "impeller/scene/importer/scene_flatbuffers.h"
24 #include "impeller/scene/material.h"
25 #include "impeller/scene/mesh.h"
26 #include "impeller/scene/scene.h"
27 #include "third_party/flatbuffers/include/flatbuffers/verifier.h"
28 #include "third_party/imgui/imgui.h"
29
30 namespace impeller {
31 namespace scene {
32 namespace testing {
33
34 using SceneTest = PlaygroundTest;
35 INSTANTIATE_PLAYGROUND_SUITE(SceneTest);
36
37 TEST_P(SceneTest, CuboidUnlit) {
38 auto scene_context = std::make_shared<SceneContext>(GetContext());
39
40 Renderer::RenderCallback callback = [&](RenderTarget& render_target) {
41 auto allocator = GetContext()->GetResourceAllocator();
42 auto scene = Scene(scene_context);
43
44 {
45 Mesh mesh;
46
47 auto material = Material::MakeUnlit();
48 material->SetColor(Color::Red());
49
50 Vector3 size(1, 1, 0);
51 mesh.AddPrimitive({Geometry::MakeCuboid(size), std::move(material)});
52
53 Node& root = scene.GetRoot();
54 root.SetLocalTransform(Matrix::MakeTranslation(-size / 2));
55 root.SetMesh(std::move(mesh));
56 }
57
58 // Face towards the +Z direction (+X right, +Y up).
59 auto camera = Camera::MakePerspective(
60 /* fov */ Radians(kPiOver4),
61 /* position */ {2, 2, -5})
62 .LookAt(
63 /* target */ Vector3(),
64 /* up */ {0, 1, 0});
65
66 scene.Render(render_target, camera);
67 return true;
68 };
69
70 OpenPlaygroundHere(callback);
71 }
72
73 TEST_P(SceneTest, FlutterLogo) {
74 auto allocator = GetContext()->GetResourceAllocator();
75
76 auto mapping =
77 flutter::testing::OpenFixtureAsMapping("flutter_logo_baked.glb.ipscene");
78 ASSERT_NE(mapping, nullptr);
79
80 flatbuffers::Verifier verifier(mapping->GetMapping(), mapping->GetSize());
81 ASSERT_TRUE(fb::VerifySceneBuffer(verifier));
82
83 std::shared_ptr<Node> gltf_scene =
84 Node::MakeFromFlatbuffer(*mapping, *allocator);
85 ASSERT_NE(gltf_scene, nullptr);
86 ASSERT_EQ(gltf_scene->GetChildren().size(), 1u);
87 ASSERT_EQ(gltf_scene->GetChildren()[0]->GetMesh().GetPrimitives().size(), 1u);
88
89 auto scene_context = std::make_shared<SceneContext>(GetContext());
90 auto scene = Scene(scene_context);
91 scene.GetRoot().AddChild(std::move(gltf_scene));
92 scene.GetRoot().SetLocalTransform(Matrix::MakeScale({3, 3, 3}));
93
94 Renderer::RenderCallback callback = [&](RenderTarget& render_target) {
95 Quaternion rotation({0, 1, 0}, -GetSecondsElapsed() * 0.5);
96 Vector3 start_position(-1, -1.5, -5);
97
98 // Face towards the +Z direction (+X right, +Y up).
99 auto camera = Camera::MakePerspective(
100 /* fov */ Degrees(60),
101 /* position */ rotation * start_position)
102 .LookAt(
103 /* target */ Vector3(),
104 /* up */ {0, 1, 0});
105
106 scene.Render(render_target, camera);
107 return true;
108 };
109
110 OpenPlaygroundHere(callback);
111 }
112
113 TEST_P(SceneTest, TwoTriangles) {
114 if (GetBackend() == PlaygroundBackend::kVulkan) {
115 GTEST_SKIP_("Temporarily disabled.");
116 }
117 auto allocator = GetContext()->GetResourceAllocator();
118
119 auto mapping =
120 flutter::testing::OpenFixtureAsMapping("two_triangles.glb.ipscene");
121 ASSERT_NE(mapping, nullptr);
122
123 std::shared_ptr<Node> gltf_scene =
124 Node::MakeFromFlatbuffer(*mapping, *allocator);
125 ASSERT_NE(gltf_scene, nullptr);
126
127 auto animation = gltf_scene->FindAnimationByName("Metronome");
128 ASSERT_NE(animation, nullptr);
129
130 AnimationClip* metronome_clip = gltf_scene->AddAnimation(animation);
131 ASSERT_NE(metronome_clip, nullptr);
132 metronome_clip->SetLoop(true);
133 metronome_clip->Play();
134
135 auto scene_context = std::make_shared<SceneContext>(GetContext());
136 auto scene = Scene(scene_context);
137 scene.GetRoot().AddChild(std::move(gltf_scene));
138
139 Renderer::RenderCallback callback = [&](RenderTarget& render_target) {
140 ImGui::Begin("Controls", nullptr, ImGuiWindowFlags_AlwaysAutoResize);
141 {
142 static Scalar playback_time_scale = 1;
143 static Scalar weight = 1;
144 static bool loop = true;
145
146 ImGui::SliderFloat("Playback time scale", &playback_time_scale, -5, 5);
147 ImGui::SliderFloat("Weight", &weight, -2, 2);
148 ImGui::Checkbox("Loop", &loop);
149 if (ImGui::Button("Play")) {
150 metronome_clip->Play();
151 }
152 if (ImGui::Button("Pause")) {
153 metronome_clip->Pause();
154 }
155 if (ImGui::Button("Stop")) {
156 metronome_clip->Stop();
157 }
158
159 metronome_clip->SetPlaybackTimeScale(playback_time_scale);
160 metronome_clip->SetWeight(weight);
161 metronome_clip->SetLoop(loop);
162 }
163
164 ImGui::End();
165 Node& node = *scene.GetRoot().GetChildren()[0];
166 node.SetLocalTransform(node.GetLocalTransform() *
167 Matrix::MakeRotation(0.02, {0, 1, 0, 0}));
168
169 static ImVec2 mouse_pos_prev = ImGui::GetMousePos();
170 ImVec2 mouse_pos = ImGui::GetMousePos();
171 Vector2 mouse_diff =
172 Vector2(mouse_pos.x - mouse_pos_prev.x, mouse_pos.y - mouse_pos_prev.y);
173
174 static Vector3 position(0, 1, -5);
175 static Vector3 cam_position = position;
176 auto strafe =
177 Vector3(ImGui::IsKeyDown(ImGuiKey_D) - ImGui::IsKeyDown(ImGuiKey_A),
178 ImGui::IsKeyDown(ImGuiKey_E) - ImGui::IsKeyDown(ImGuiKey_Q),
179 ImGui::IsKeyDown(ImGuiKey_W) - ImGui::IsKeyDown(ImGuiKey_S));
180 position += strafe * 0.5;
181 cam_position = cam_position.Lerp(position, 0.02);
182
183 // Face towards the +Z direction (+X right, +Y up).
184 auto camera = Camera::MakePerspective(
185 /* fov */ Degrees(60),
186 /* position */ cam_position)
187 .LookAt(
188 /* target */ cam_position + Vector3(0, 0, 1),
189 /* up */ {0, 1, 0});
190
191 scene.Render(render_target, camera);
192 return true;
193 };
194
195 OpenPlaygroundHere(callback);
196 }
197
198 TEST_P(SceneTest, Dash) {
199 auto allocator = GetContext()->GetResourceAllocator();
200
201 auto mapping = flutter::testing::OpenFixtureAsMapping("dash.glb.ipscene");
202 if (!mapping) {
203 // TODO(bdero): Just skip this playground is the dash asset isn't found. I
204 // haven't checked it in because it's way too big right now,
205 // but this is still useful to keep around for debugging
206 // purposes.
207 return;
208 }
209 ASSERT_NE(mapping, nullptr);
210
211 std::shared_ptr<Node> gltf_scene =
212 Node::MakeFromFlatbuffer(*mapping, *allocator);
213 ASSERT_NE(gltf_scene, nullptr);
214
215 auto walk_anim = gltf_scene->FindAnimationByName("Walk");
216 ASSERT_NE(walk_anim, nullptr);
217
218 AnimationClip* walk_clip = gltf_scene->AddAnimation(walk_anim);
219 ASSERT_NE(walk_clip, nullptr);
220 walk_clip->SetLoop(true);
221 walk_clip->Play();
222
223 auto run_anim = gltf_scene->FindAnimationByName("Run");
224 ASSERT_NE(walk_anim, nullptr);
225
226 AnimationClip* run_clip = gltf_scene->AddAnimation(run_anim);
227 ASSERT_NE(run_clip, nullptr);
228 run_clip->SetLoop(true);
229 run_clip->Play();
230
231 auto scene_context = std::make_shared<SceneContext>(GetContext());
232 auto scene = Scene(scene_context);
233 scene.GetRoot().AddChild(std::move(gltf_scene));
234
235 Renderer::RenderCallback callback = [&](RenderTarget& render_target) {
236 ImGui::Begin("Controls", nullptr, ImGuiWindowFlags_AlwaysAutoResize);
237 {
238 static Scalar playback_time_scale = 1;
239 static Scalar walk = 0.5;
240 static Scalar run = 0.5;
241 static bool loop = true;
242
243 ImGui::SliderFloat("Playback time scale", &playback_time_scale, -5, 5);
244 ImGui::SliderFloat("Walk weight", &walk, 0, 1);
245 ImGui::SliderFloat("Run weight", &run, 0, 1);
246 ImGui::Checkbox("Loop", &loop);
247 if (ImGui::Button("Play")) {
248 walk_clip->Play();
249 run_clip->Play();
250 }
251 if (ImGui::Button("Pause")) {
252 walk_clip->Pause();
253 run_clip->Pause();
254 }
255 if (ImGui::Button("Stop")) {
256 walk_clip->Stop();
257 run_clip->Stop();
258 }
259
260 walk_clip->SetPlaybackTimeScale(playback_time_scale);
261 walk_clip->SetWeight(walk);
262 walk_clip->SetLoop(loop);
263
264 run_clip->SetPlaybackTimeScale(playback_time_scale);
265 run_clip->SetWeight(run);
266 run_clip->SetLoop(loop);
267 }
268
269 ImGui::End();
270 Node& node = *scene.GetRoot().GetChildren()[0];
271 node.SetLocalTransform(node.GetLocalTransform() *
272 Matrix::MakeRotation(0.02, {0, 1, 0, 0}));
273
274 static ImVec2 mouse_pos_prev = ImGui::GetMousePos();
275 ImVec2 mouse_pos = ImGui::GetMousePos();
276 Vector2 mouse_diff =
277 Vector2(mouse_pos.x - mouse_pos_prev.x, mouse_pos.y - mouse_pos_prev.y);
278
279 static Vector3 position(0, 1, -5);
280 static Vector3 cam_position = position;
281 auto strafe =
282 Vector3(ImGui::IsKeyDown(ImGuiKey_D) - ImGui::IsKeyDown(ImGuiKey_A),
283 ImGui::IsKeyDown(ImGuiKey_E) - ImGui::IsKeyDown(ImGuiKey_Q),
284 ImGui::IsKeyDown(ImGuiKey_W) - ImGui::IsKeyDown(ImGuiKey_S));
285 position += strafe * 0.5;
286 cam_position = cam_position.Lerp(position, 0.02);
287
288 // Face towards the +Z direction (+X right, +Y up).
289 auto camera = Camera::MakePerspective(
290 /* fov */ Degrees(60),
291 /* position */ cam_position)
292 .LookAt(
293 /* target */ cam_position + Vector3(0, 0, 1),
294 /* up */ {0, 1, 0});
295
296 scene.Render(render_target, camera);
297 return true;
298 };
299
300 OpenPlaygroundHere(callback);
301 }
302
303 } // namespace testing
304 } // namespace scene
305 } // namespace impeller
impeller::PlaygroundBackend::kVulkan
@ kVulkan
impeller::scene::Node::GetLocalTransform
Matrix GetLocalTransform() const
Definition: node.cc:292
impeller::Scalar
float Scalar
Definition: scalar.h:18
impeller::scene::Geometry::MakeCuboid
static std::shared_ptr< CuboidGeometry > MakeCuboid(Vector3 size)
Definition: geometry.cc:30
impeller::Color::Red
static constexpr Color Red()
Definition: color.h:264
impeller::scene::Mesh
Definition: mesh.h:21
impeller::scene::AnimationClip::SetPlaybackTimeScale
void SetPlaybackTimeScale(Scalar playback_speed)
Sets the animation playback speed. Negative values make the clip play in reverse.
Definition: animation_clip.cc:61
impeller::Renderer::RenderCallback
std::function< bool(RenderTarget &render_target)> RenderCallback
Definition: renderer.h:23
impeller::Matrix::MakeRotation
static Matrix MakeRotation(Quaternion q)
Definition: matrix.h:126
impeller::scene::AnimationClip::SetLoop
void SetLoop(bool looping)
Definition: animation_clip.cc:53
formats.h
impeller::Vector2
Point Vector2
Definition: point.h:312
quaternion.h
impeller::scene::AnimationClip::Play
void Play()
Definition: animation_clip.cc:36
playground.h
impeller::scene::Node::SetMesh
void SetMesh(Mesh mesh)
Definition: node.cc:339
impeller::Matrix::MakeTranslation
static constexpr Matrix MakeTranslation(const Vector3 &t)
Definition: matrix.h:95
impeller::scene::AnimationClip
Definition: animation_clip.h:22
impeller::scene::Camera::MakePerspective
static Camera MakePerspective(Radians fov_y, Vector3 position)
Definition: camera.cc:10
impeller::scene::Node::GetChildren
std::vector< std::shared_ptr< Node > > & GetChildren()
Definition: node.cc:335
matrix.h
impeller::Quaternion
Definition: quaternion.h:14
scene.h
animation_clip.h
material.h
impeller::scene::Camera::LookAt
Camera LookAt(Vector3 target, Vector3 up=Vector3(0, -1, 0)) const
Definition: camera.cc:17
impeller::scene::Mesh::AddPrimitive
void AddPrimitive(Primitive mesh_)
Definition: mesh.cc:21
impeller::Radians
Definition: scalar.h:38
impeller::scene::AnimationClip::SetWeight
void SetWeight(Scalar weight)
Definition: animation_clip.cc:69
impeller::RenderTarget
Definition: render_target.h:49
mesh.h
camera.h
decompressed_image.h
impeller::scene::AnimationClip::Pause
void Pause()
Definition: animation_clip.cc:40
vector.h
impeller::scene::Material::MakeUnlit
static std::unique_ptr< UnlitMaterial > MakeUnlit()
Definition: material.cc:37
constants.h
impeller::TPoint< Scalar >
impeller::scene::testing::INSTANTIATE_PLAYGROUND_SUITE
INSTANTIATE_PLAYGROUND_SUITE(SceneTest)
impeller::scene::testing::TEST_P
TEST_P(SceneTest, CuboidUnlit)
Definition: scene_unittests.cc:37
impeller::scene::Node
Definition: node.h:30
impeller::Degrees
Definition: scalar.h:46
color.h
geometry.h
impeller::scene::Scene
Definition: scene.h:21
impeller::PlaygroundTest
Definition: playground_test.h:23
impeller::scene::Node::SetLocalTransform
void SetLocalTransform(Matrix transform)
Definition: node.cc:288
impeller::scene::Node::MakeFromFlatbuffer
static std::shared_ptr< Node > MakeFromFlatbuffer(const fml::Mapping &ipscene_mapping, Allocator &allocator)
Definition: node.cc:48
impeller
Definition: aiks_context.cc:10
playground_test.h
impeller::Matrix::MakeScale
static constexpr Matrix MakeScale(const Vector3 &s)
Definition: matrix.h:104
impeller::kPiOver4
constexpr float kPiOver4
Definition: constants.h:35
impeller::Vector3
Definition: vector.h:20
impeller::Vector3::Lerp
constexpr Vector3 Lerp(const Vector3 &v, Scalar t) const
Definition: vector.h:178
impeller::scene::AnimationClip::Stop
void Stop()
Definition: animation_clip.cc:44