Skip to content

Commit ac1d2d8

Browse files
bderoloic-sharma
authored andcommitted
[Impeller Scene] Refactor Nodes/Meshes for simplicity and GLTF compatibility (flutter#38180)
1 parent 89b85cf commit ac1d2d8

18 files changed

+234
-242
lines changed

ci/licenses_golden/licenses_flutter

+4-4
Original file line numberDiff line numberDiff line change
@@ -1662,19 +1662,19 @@ FILE: ../../../flutter/impeller/scene/importer/vertices_builder.cc
16621662
FILE: ../../../flutter/impeller/scene/importer/vertices_builder.h
16631663
FILE: ../../../flutter/impeller/scene/material.cc
16641664
FILE: ../../../flutter/impeller/scene/material.h
1665+
FILE: ../../../flutter/impeller/scene/mesh.cc
1666+
FILE: ../../../flutter/impeller/scene/mesh.h
1667+
FILE: ../../../flutter/impeller/scene/node.cc
1668+
FILE: ../../../flutter/impeller/scene/node.h
16651669
FILE: ../../../flutter/impeller/scene/scene.cc
16661670
FILE: ../../../flutter/impeller/scene/scene.h
16671671
FILE: ../../../flutter/impeller/scene/scene_context.cc
16681672
FILE: ../../../flutter/impeller/scene/scene_context.h
16691673
FILE: ../../../flutter/impeller/scene/scene_encoder.cc
16701674
FILE: ../../../flutter/impeller/scene/scene_encoder.h
1671-
FILE: ../../../flutter/impeller/scene/scene_entity.cc
1672-
FILE: ../../../flutter/impeller/scene/scene_entity.h
16731675
FILE: ../../../flutter/impeller/scene/scene_unittests.cc
16741676
FILE: ../../../flutter/impeller/scene/shaders/geometry.vert
16751677
FILE: ../../../flutter/impeller/scene/shaders/unlit.frag
1676-
FILE: ../../../flutter/impeller/scene/static_mesh_entity.cc
1677-
FILE: ../../../flutter/impeller/scene/static_mesh_entity.h
16781678
FILE: ../../../flutter/impeller/tessellator/c/tessellator.cc
16791679
FILE: ../../../flutter/impeller/tessellator/c/tessellator.h
16801680
FILE: ../../../flutter/impeller/tessellator/dart/lib/tessellator.dart

impeller/scene/BUILD.gn

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@ impeller_component("scene") {
1212
"geometry.h",
1313
"material.cc",
1414
"material.h",
15+
"mesh.cc",
16+
"mesh.h",
17+
"node.cc",
18+
"node.h",
1519
"scene.cc",
1620
"scene.h",
1721
"scene_context.cc",
1822
"scene_context.h",
1923
"scene_encoder.cc",
2024
"scene_encoder.h",
21-
"scene_entity.cc",
22-
"scene_entity.h",
23-
"static_mesh_entity.cc",
24-
"static_mesh_entity.h",
2525
]
2626

2727
public_deps = [

impeller/scene/README.md

+25-27
Original file line numberDiff line numberDiff line change
@@ -43,54 +43,52 @@ run_action.SetLoop(impeller::scene::AnimationAction::kLoopForever);
4343
run_action.SetWeight(0.3f);
4444
run_action.Play();
4545

46-
scene.Add(
46+
scene.GetRoot().AddChild(
4747
impeller::scene::DirectionalLight(
4848
/* color */ impeller::Color::AntiqueWhite(),
4949
/* intensity */ 5,
5050
/* direction */ {2, 3, 4}));
5151

52-
impeller::scene::StaticMeshEntity sphere_entity;
53-
sphere_entity.SetGlobalTransform(
52+
Node sphere_node;
53+
Mesh sphere_mesh;
54+
sphere_node.SetGlobalTransform(
5455
Matrix::MakeRotationEuler({kPiOver4, kPiOver4, 0}));
55-
sphere_entity.SetCullingMode(impeller::scene::CullingMode::kFrustum);
5656

57-
std::unique_ptr<impeller::scene::SphereGeometry> sphere =
57+
auto sphere_geometry =
5858
impeller::scene::Geometry::MakeSphere(allocator, /* radius */ 2);
5959

60-
sphere_entity.SetGeometry(sphere);
61-
6260
auto material = impeller::scene::Material::MakeStandard();
63-
material.SetAlbedo(impeller::Color::Red());
64-
material.SetRoughness(0.4);
65-
material.SetMetallic(0.2);
61+
material->SetAlbedo(impeller::Color::Red());
62+
material->SetRoughness(0.4);
63+
material->SetMetallic(0.2);
6664
// Common properties shared by all materials.
67-
material.SetEnvironmentMap(environment_hdri);
68-
material.SetFlatShaded(true);
69-
material.SetBlendConfig({
65+
material->SetEnvironmentMap(environment_hdri);
66+
material->SetFlatShaded(true);
67+
material->SetBlendConfig({
7068
impeller::BlendOperation::kAdd, // color_op
7169
impeller::BlendFactor::kOne, // source_color_factor
7270
impeller::BlendFactor::kOneMinusSourceAlpha, // destination_color_factor
7371
impeller::BlendOperation::kAdd, // alpha_op
7472
impeller::BlendFactor::kOne, // source_alpha_factor
7573
impeller::BlendFactor::kOneMinusSourceAlpha, // destination_alpha_factor
7674
});
77-
material.SetStencilConfig({
75+
material->SetStencilConfig({
7876
impeller::StencilOperation::kIncrementClamp, // operation
7977
impeller::CompareFunction::kAlways, // compare
8078
});
81-
82-
sphere_entity->SetMaterials({material});
83-
84-
85-
impeller::scene::StaticMeshEntity cube_entity;
86-
cube_entity.GetGeometry(
87-
impeller::scene::Geometry::MakeCube(allocator, {4, 4, 4}));
88-
cube_entity.SetMaterials({material});
89-
90-
cube_entity.SetLocalTransform(Matrix::MakeTranslation({4, 0, 0}));
91-
92-
sphere_entity->Add(sube_entity);
93-
scene.Add(sphere_entity);
79+
sphere_mesh.AddPrimitive({sphere_geometry, material});
80+
sphere_node.SetMesh(sphere_mesh);
81+
82+
Node cube_node;
83+
cube_node.SetLocalTransform(Matrix::MakeTranslation({4, 0, 0}));
84+
Mesh cube_mesh;
85+
auto cube_geometry = impeller::scene::Geometry::MakeCuboid(
86+
allocator, {4, 4, 4});
87+
cube_mesh.AddPrimitive({cube_geometry, material});
88+
cube_node.SetMesh(cube_mesh);
89+
90+
sphere_node.AddChild(cube_node);
91+
scene.GetRoot().AddChild(sphere_node);
9492

9593
/// Post processing.
9694

impeller/scene/geometry.cc

+4-4
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,15 @@ std::shared_ptr<VertexBufferGeometry> Geometry::MakeVertexBuffer(
4040
return result;
4141
}
4242

43-
std::shared_ptr<VertexBufferGeometry> Geometry::MakeFromFBMesh(
44-
const fb::StaticMesh& mesh,
43+
std::shared_ptr<VertexBufferGeometry> Geometry::MakeFromFBMeshPrimitive(
44+
const fb::MeshPrimitive& mesh,
4545
Allocator& allocator) {
4646
IndexType index_type;
4747
switch (mesh.indices()->type()) {
48-
case fb::IndicesType::k16Bit:
48+
case fb::IndexType::k16Bit:
4949
index_type = IndexType::k16bit;
5050
break;
51-
case fb::IndicesType::k32Bit:
51+
case fb::IndexType::k32Bit:
5252
index_type = IndexType::k32bit;
5353
break;
5454
}

impeller/scene/geometry.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ class Geometry {
2828
static std::shared_ptr<VertexBufferGeometry> MakeVertexBuffer(
2929
VertexBuffer vertex_buffer);
3030

31-
static std::shared_ptr<VertexBufferGeometry> MakeFromFBMesh(
32-
const fb::StaticMesh& mesh,
31+
static std::shared_ptr<VertexBufferGeometry> MakeFromFBMeshPrimitive(
32+
const fb::MeshPrimitive& mesh,
3333
Allocator& allocator);
3434

3535
virtual VertexBuffer GetVertexBuffer(Allocator& allocator) const = 0;

impeller/scene/importer/importer_gltf.cc

+10-10
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ static bool WithinRange(int index, size_t size) {
3333
return index >= 0 && static_cast<size_t>(index) < size;
3434
}
3535

36-
static bool ProcessStaticMesh(const tinygltf::Model& gltf,
37-
const tinygltf::Primitive& primitive,
38-
fb::StaticMeshT& static_mesh) {
36+
static bool ProcessMeshPrimitive(const tinygltf::Model& gltf,
37+
const tinygltf::Primitive& primitive,
38+
fb::MeshPrimitiveT& mesh_primitive) {
3939
//---------------------------------------------------------------------------
4040
/// Vertices.
4141
///
@@ -93,7 +93,7 @@ static bool ProcessStaticMesh(const tinygltf::Model& gltf,
9393
accessor.count); // count
9494
}
9595

96-
builder.WriteFBVertices(static_mesh.vertices);
96+
builder.WriteFBVertices(mesh_primitive.vertices);
9797
}
9898

9999
//---------------------------------------------------------------------------
@@ -112,10 +112,10 @@ static bool ProcessStaticMesh(const tinygltf::Model& gltf,
112112

113113
switch (index_accessor.componentType) {
114114
case TINYGLTF_COMPONENT_TYPE_UNSIGNED_SHORT:
115-
indices->type = fb::IndicesType::k16Bit;
115+
indices->type = fb::IndexType::k16Bit;
116116
break;
117117
case TINYGLTF_COMPONENT_TYPE_UNSIGNED_INT:
118-
indices->type = fb::IndicesType::k32Bit;
118+
indices->type = fb::IndexType::k32Bit;
119119
break;
120120
default:
121121
std::cerr << "Mesh primitive has unsupported index type "
@@ -128,7 +128,7 @@ static bool ProcessStaticMesh(const tinygltf::Model& gltf,
128128
&gltf.buffers[index_view.buffer].data[index_view.byteOffset];
129129
std::memcpy(indices->data.data(), index_buffer, indices->data.size());
130130

131-
static_mesh.indices = std::move(indices);
131+
mesh_primitive.indices = std::move(indices);
132132

133133
return true;
134134
}
@@ -177,11 +177,11 @@ static void ProcessNode(const tinygltf::Model& gltf,
177177
if (WithinRange(in_node.mesh, gltf.meshes.size())) {
178178
auto& mesh = gltf.meshes[in_node.mesh];
179179
for (const auto& primitive : mesh.primitives) {
180-
auto static_mesh = std::make_unique<fb::StaticMeshT>();
181-
if (!ProcessStaticMesh(gltf, primitive, *static_mesh)) {
180+
auto mesh_primitive = std::make_unique<fb::MeshPrimitiveT>();
181+
if (!ProcessMeshPrimitive(gltf, primitive, *mesh_primitive)) {
182182
continue;
183183
}
184-
out_node.meshes.push_back(std::move(static_mesh));
184+
out_node.mesh_primitives.push_back(std::move(mesh_primitive));
185185
}
186186
}
187187

impeller/scene/importer/importer_unittests.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ TEST(ImporterTest, CanParseGLTF) {
2626
Matrix node_transform = ToMatrix(*node.transform);
2727
ASSERT_MATRIX_NEAR(node_transform, Matrix());
2828

29-
ASSERT_EQ(node.meshes.size(), 1u);
30-
auto& mesh = *node.meshes[0];
29+
ASSERT_EQ(node.mesh_primitives.size(), 1u);
30+
auto& mesh = *node.mesh_primitives[0];
3131
ASSERT_EQ(mesh.indices->count, 918u);
3232

3333
uint16_t first_index =

impeller/scene/importer/scene.fbs

+4-4
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,15 @@ struct Vertex {
4343
color: Color;
4444
}
4545

46-
enum IndicesType:byte {
46+
enum IndexType:byte {
4747
k16Bit,
4848
k32Bit,
4949
}
5050

5151
table Indices {
5252
data: [ubyte];
5353
count: uint32;
54-
type: IndicesType;
54+
type: IndexType;
5555
}
5656

5757
table Texture {
@@ -65,7 +65,7 @@ table Material {
6565
// TODO(bdero): PBR textures.
6666
}
6767

68-
table StaticMesh {
68+
table MeshPrimitive {
6969
vertices: [Vertex];
7070
indices: Indices;
7171
material: Material;
@@ -74,7 +74,7 @@ table StaticMesh {
7474
table Node {
7575
children: [Node];
7676
transform: Matrix;
77-
meshes: [StaticMesh];
77+
mesh_primitives: [MeshPrimitive];
7878
}
7979

8080
table Scene {

impeller/scene/mesh.cc

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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 "impeller/scene/mesh.h"
6+
7+
#include <memory>
8+
9+
#include "impeller/base/validation.h"
10+
#include "impeller/scene/material.h"
11+
#include "impeller/scene/scene_encoder.h"
12+
13+
namespace impeller {
14+
namespace scene {
15+
16+
Mesh::Mesh() = default;
17+
Mesh::~Mesh() = default;
18+
19+
void Mesh::AddPrimitive(Primitive mesh) {
20+
if (mesh.geometry_ == nullptr) {
21+
VALIDATION_LOG << "Mesh geometry cannot be null.";
22+
}
23+
if (mesh.material_ == nullptr) {
24+
VALIDATION_LOG << "Mesh material cannot be null.";
25+
}
26+
27+
meshes_.push_back(std::move(mesh));
28+
}
29+
30+
bool Mesh::Render(SceneEncoder& encoder, const Matrix& transform) const {
31+
for (const auto& mesh : meshes_) {
32+
SceneCommand command = {
33+
.label = "Mesh Primitive",
34+
.transform = transform,
35+
.geometry = mesh.geometry_.get(),
36+
.material = mesh.material_.get(),
37+
};
38+
encoder.Add(command);
39+
}
40+
return true;
41+
}
42+
43+
} // namespace scene
44+
} // namespace impeller

impeller/scene/mesh.h

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
#pragma once
6+
7+
#include <memory>
8+
#include <type_traits>
9+
10+
#include "flutter/fml/macros.h"
11+
#include "impeller/scene/geometry.h"
12+
#include "impeller/scene/material.h"
13+
#include "impeller/scene/scene_encoder.h"
14+
15+
namespace impeller {
16+
namespace scene {
17+
18+
class Mesh final {
19+
public:
20+
struct Primitive {
21+
std::shared_ptr<Geometry> geometry_;
22+
std::shared_ptr<Material> material_;
23+
};
24+
25+
Mesh();
26+
~Mesh();
27+
28+
void AddPrimitive(Primitive mesh_);
29+
30+
bool Render(SceneEncoder& encoder, const Matrix& transform) const;
31+
32+
private:
33+
std::vector<Primitive> meshes_;
34+
};
35+
36+
} // namespace scene
37+
} // namespace impeller

0 commit comments

Comments
 (0)