Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 83c2482

Browse files
authored
[Impeller] Add Impeller scene (#37694)
1 parent 7156b87 commit 83c2482

19 files changed

+910
-0
lines changed

ci/licenses_golden/licenses_flutter

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1582,6 +1582,21 @@ FILE: ../../../flutter/impeller/runtime_stage/runtime_stage_playground.h
15821582
FILE: ../../../flutter/impeller/runtime_stage/runtime_stage_unittests.cc
15831583
FILE: ../../../flutter/impeller/runtime_stage/runtime_types.cc
15841584
FILE: ../../../flutter/impeller/runtime_stage/runtime_types.h
1585+
FILE: ../../../flutter/impeller/scene/camera.cc
1586+
FILE: ../../../flutter/impeller/scene/camera.h
1587+
FILE: ../../../flutter/impeller/scene/geometry.cc
1588+
FILE: ../../../flutter/impeller/scene/geometry.h
1589+
FILE: ../../../flutter/impeller/scene/material.cc
1590+
FILE: ../../../flutter/impeller/scene/material.h
1591+
FILE: ../../../flutter/impeller/scene/scene.cc
1592+
FILE: ../../../flutter/impeller/scene/scene.h
1593+
FILE: ../../../flutter/impeller/scene/scene_encoder.cc
1594+
FILE: ../../../flutter/impeller/scene/scene_encoder.h
1595+
FILE: ../../../flutter/impeller/scene/scene_entity.cc
1596+
FILE: ../../../flutter/impeller/scene/scene_entity.h
1597+
FILE: ../../../flutter/impeller/scene/scene_unittests.cc
1598+
FILE: ../../../flutter/impeller/scene/static_mesh_entity.cc
1599+
FILE: ../../../flutter/impeller/scene/static_mesh_entity.h
15851600
FILE: ../../../flutter/impeller/tessellator/c/tessellator.cc
15861601
FILE: ../../../flutter/impeller/tessellator/c/tessellator.h
15871602
FILE: ../../../flutter/impeller/tessellator/dart/lib/tessellator.dart

impeller/BUILD.gn

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ impeller_component("impeller_unittests") {
8686
"image:image_unittests",
8787
"playground",
8888
"renderer:renderer_unittests",
89+
"scene:scene_unittests",
8990
"typographer:typographer_unittests",
9091
]
9192
}

impeller/scene/BUILD.gn

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
import("//flutter/impeller/tools/impeller.gni")
6+
7+
impeller_component("scene") {
8+
sources = [
9+
"camera.cc",
10+
"camera.h",
11+
"geometry.cc",
12+
"geometry.h",
13+
"material.cc",
14+
"material.h",
15+
"scene.cc",
16+
"scene.h",
17+
"scene_encoder.cc",
18+
"scene_encoder.h",
19+
"scene_entity.cc",
20+
"scene_entity.h",
21+
"static_mesh_entity.cc",
22+
"static_mesh_entity.h",
23+
]
24+
25+
public_deps = [ "../renderer" ]
26+
27+
deps = [ "//flutter/fml" ]
28+
}
29+
30+
impeller_component("scene_unittests") {
31+
testonly = true
32+
33+
sources = [ "scene_unittests.cc" ]
34+
35+
deps = [
36+
":scene",
37+
"../fixtures",
38+
"../playground:playground_test",
39+
"//flutter/testing:testing_lib",
40+
41+
#"//third_party/tinygltf",
42+
]
43+
}

impeller/scene/README.md

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
## ⚠️ **Experimental:** Do not use in production! ⚠️
2+
3+
# Impeller Scene
4+
5+
Impeller Scene is an experimental realtime 3D renderer powered by Impeller's
6+
render layer with the following design priorities:
7+
8+
* Ease of use.
9+
* Suitability for mobile.
10+
* Common case scalability.
11+
12+
The aim is to create a familiar and flexible scene graph capable of building
13+
complex dynamic scenes for games and beyond.
14+
15+
## Example
16+
17+
```cpp
18+
std::shared_ptr<impeller::Context> context =
19+
/* Create the backend-specific Impeller context */;
20+
21+
auto allocator = context->GetResourceAllocator();
22+
23+
/// Load resources.
24+
25+
auto dash_gltf = impeller::scene::LoadGLTF(allocator, "models/dash.glb");
26+
auto environment_hdri =
27+
impeller::scene::LoadHDRI(allocator, "environment/table_mountain.hdr");
28+
29+
/// Construct a scene.
30+
31+
auto scene = impeller::scene::Scene(context);
32+
33+
scene.Add(dash_gltf.scene);
34+
35+
auto& dash_player = dash_gltf.scene.CreateAnimationPlayer();
36+
auto& walk_action = dash_player.CreateClipAction(dash_gltf.GetClip("Walk"));
37+
walk_action.SetLoop(impeller::scene::AnimationAction::kLoopForever);
38+
walk_action.SetWeight(0.7f);
39+
walk_action.Seek(0.0f);
40+
walk_action.Play();
41+
auto& run_action = dash_player.CreateClipAction(dash_gltf.GetClip("Run"));
42+
run_action.SetLoop(impeller::scene::AnimationAction::kLoopForever);
43+
run_action.SetWeight(0.3f);
44+
run_action.Play();
45+
46+
scene.Add(
47+
impeller::scene::DirectionalLight(
48+
/* color */ impeller::Color::AntiqueWhite(),
49+
/* intensity */ 5,
50+
/* direction */ {2, 3, 4}));
51+
52+
impeller::scene::StaticMeshEntity sphere_entity;
53+
sphere_entity.SetGlobalTransform(
54+
Matrix::MakeRotationEuler({kPiOver4, kPiOver4, 0}));
55+
sphere_entity.SetCullingMode(impeller::scene::CullingMode::kFrustum);
56+
57+
std::unique_ptr<impeller::scene::SphereGeometry> sphere =
58+
impeller::scene::Geometry::MakeSphere(allocator, /* radius */ 2);
59+
60+
sphere_entity.SetGeometry(sphere);
61+
62+
auto material = impeller::scene::Material::MakeStandard();
63+
material.SetAlbedo(impeller::Color::Red());
64+
material.SetRoughness(0.4);
65+
material.SetMetallic(0.2);
66+
// Common properties shared by all materials.
67+
material.SetEnvironmentMap(environment_hdri);
68+
material.SetFlatShaded(true);
69+
material.SetBlendConfig({
70+
impeller::BlendOperation::kAdd, // color_op
71+
impeller::BlendFactor::kOne, // source_color_factor
72+
impeller::BlendFactor::kOneMinusSourceAlpha, // destination_color_factor
73+
impeller::BlendOperation::kAdd, // alpha_op
74+
impeller::BlendFactor::kOne, // source_alpha_factor
75+
impeller::BlendFactor::kOneMinusSourceAlpha, // destination_alpha_factor
76+
});
77+
material.SetStencilConfig({
78+
impeller::StencilOperation::kIncrementClamp, // operation
79+
impeller::CompareFunction::kAlways, // compare
80+
});
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);
94+
95+
/// Post processing.
96+
97+
auto dof = impeller::scene::PostProcessingEffect::MakeBokeh(
98+
/* aperture_size */ 0.2,
99+
/* focus_plane_distance */ 50);
100+
scene.SetPostProcessing({dof});
101+
102+
/// Render the scene.
103+
104+
auto renderer = impeller::Renderer(context);
105+
106+
while(true) {
107+
std::unique_ptr<impeller::Surface> surface = /* Wrap the window surface */;
108+
109+
renderer->Render(surface, [&scene](RenderTarget& render_target) {
110+
/// Render a perspective view.
111+
112+
auto camera =
113+
impeller::Camera::MakePerspective(
114+
/* fov */ kPiOver4,
115+
/* position */ {50, -30, 50})
116+
.LookAt(
117+
/* target */ impeller::Vector3::Zero,
118+
/* up */ {0, -1, 0});
119+
120+
scene.Render(render_target, camera);
121+
122+
/// Render an overhead view on the bottom right corner of the screen.
123+
124+
auto size = render_target.GetRenderTargetSize();
125+
auto minimap_camera =
126+
impeller::Camera::MakeOrthographic(
127+
/* view */ Rect::MakeLTRB(-100, -100, 100, 100),
128+
/* position */ {0, -50, 0})
129+
.LookAt(
130+
/* target */ impeller::Vector3::Zero,
131+
/* up */ {0, 0, 1})
132+
.WithViewport(IRect::MakeXYWH(size.width / 4, size.height / 4,
133+
size.height / 5, size.height / 5));
134+
135+
scene.Render(render_target, minimap_camera);
136+
137+
return true;
138+
});
139+
}
140+
```

impeller/scene/camera.cc

Lines changed: 37 additions & 0 deletions
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+
#include "impeller/scene/camera.h"
6+
7+
namespace impeller {
8+
namespace scene {
9+
10+
Camera Camera::MakePerspective(Scalar fov_y, Vector3 position) {
11+
Camera camera;
12+
camera.fov_y_ = fov_y;
13+
camera.position_ = position;
14+
return camera;
15+
}
16+
17+
Camera Camera::LookAt(Vector3 target, Vector3 up) const {
18+
Camera camera = *this;
19+
camera.target_ = target;
20+
camera.up_ = up;
21+
return camera;
22+
}
23+
24+
Matrix Camera::GetTransform(ISize target_size) const {
25+
if (transform_.has_value()) {
26+
return transform_.value();
27+
}
28+
29+
transform_ =
30+
Matrix::MakePerspective(Radians(fov_y_), target_size, z_near_, z_far_) *
31+
Matrix::MakeLookAt(position_, target_, up_).Invert();
32+
33+
return transform_.value();
34+
}
35+
36+
} // namespace scene
37+
} // namespace impeller

impeller/scene/camera.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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 <optional>
8+
9+
#include "impeller/geometry/matrix.h"
10+
11+
namespace impeller {
12+
namespace scene {
13+
14+
class Camera {
15+
public:
16+
static Camera MakePerspective(Scalar fov_y, Vector3 position);
17+
18+
Camera LookAt(Vector3 target, Vector3 up = Vector3(0, -1, 0)) const;
19+
20+
Matrix GetTransform(ISize target_size) const;
21+
22+
private:
23+
Scalar fov_y_ = 60;
24+
Vector3 position_ = Vector3();
25+
Vector3 target_ = Vector3(0, 0, -1);
26+
Vector3 up_ = Vector3(0, -1, 0);
27+
Scalar z_near_ = 0.1;
28+
Scalar z_far_ = 1000;
29+
30+
mutable std::optional<Matrix> transform_;
31+
};
32+
33+
} // namespace scene
34+
} // namespace impeller

impeller/scene/geometry.cc

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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/geometry.h"
6+
7+
#include <memory>
8+
9+
namespace impeller {
10+
namespace scene {
11+
12+
//------------------------------------------------------------------------------
13+
/// Geometry
14+
///
15+
16+
std::shared_ptr<CuboidGeometry> Geometry::MakeCuboid(Vector3 size) {
17+
auto result = std::make_shared<CuboidGeometry>();
18+
result->SetSize(size);
19+
return result;
20+
}
21+
22+
//------------------------------------------------------------------------------
23+
/// CuboidGeometry
24+
///
25+
26+
void CuboidGeometry::SetSize(Vector3 size) {
27+
size_ = size;
28+
}
29+
30+
VertexBuffer CuboidGeometry::GetVertexBuffer(
31+
std::shared_ptr<Allocator>& allocator) const {
32+
return {};
33+
}
34+
35+
} // namespace scene
36+
} // namespace impeller

impeller/scene/geometry.h

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
9+
#include "impeller/geometry/vector.h"
10+
#include "impeller/renderer/allocator.h"
11+
#include "impeller/renderer/vertex_buffer.h"
12+
13+
namespace impeller {
14+
namespace scene {
15+
16+
class CuboidGeometry;
17+
18+
class Geometry {
19+
public:
20+
static std::shared_ptr<CuboidGeometry> MakeCuboid(Vector3 size);
21+
22+
private:
23+
virtual VertexBuffer GetVertexBuffer(
24+
std::shared_ptr<Allocator>& allocator) const = 0;
25+
};
26+
27+
class CuboidGeometry final : public Geometry {
28+
public:
29+
void SetSize(Vector3 size);
30+
31+
private:
32+
VertexBuffer GetVertexBuffer(
33+
std::shared_ptr<Allocator>& allocator) const override;
34+
35+
Vector3 size_;
36+
};
37+
38+
} // namespace scene
39+
} // namespace impeller

0 commit comments

Comments
 (0)