Two engines, one Stack: the source
Open the live demo · Read the guide · View on GitHub
Regions the guide quotes: live (line 76), flame-layer (line 100), flutter3d-layer (line 105), unconnected (line 110).
1/// Flame's own layer and flutter3d's own layer, drawn side by side and not
2/// yet talking to each other — the state every other page in this category
3/// starts connecting.
4///
5/// Quoted by `flame_overview.md` and shown whole in the Source tab.
6library;
8import 'dart:math' as math;
10import 'package:flame/components.dart';
11import 'package:flame_flutter3d/flame_flutter3d.dart';
12import 'package:flutter/widgets.dart';
13import 'package:flutter3d/flutter3d.dart';
14import 'package:flutter3d_showcase/src/demo/demo.dart';
15import 'package:flutter3d_showcase/src/demo/flame_layer.dart';
17/// The `flame-overview` page: a flutter3d cube and a Flame shape drawn
18/// together, neither moving the other. Its check is that each stays where
19/// its own layer put it.
20final class FlameOverviewDemo extends ShowcaseDemo {
21 late final double _flameX;
22 late final double _cubeX;
24 late final Scene _scene;
25 late final MeshNode _cube;
26 late final TransparentFlameGame _game;
27 late final Widget _body = flameOrbit(
28 _context,
29 Flutter3dFlameWidget(
30 game: _game,
31 camera: _context.camera,
32 existing: BorrowedRenderer(
33 device: _context.device,
34 renderer: _context.renderer,
35 ),
36 buildScene: (GraphicsDevice device) => _scene,
37 ),
38 );
39 late final DemoContext _context;
40 double _clock = 0.0;
43 void configureView(DemoContext context) {
44 context.orbit
45 ..distance = 3.2
46 ..pitch = 0.3
47 ..yaw = 0.6;
48 context.orbit.target.setValues(0.0, 0.0, 0.0);
49 }
52 Scene build(DemoContext context) {
53 _context = context;
54 final (double flameX, double cubeX) = _run();
55 _flameX = flameX;
56 _cubeX = cubeX;
58 _cube = MeshNode(
59 DeviceMesh.upload(
60 context.device,
61 CuboidShape(size: Vector3(0.8, 0.8, 0.8)).build(),
62 ),
63 RenderMaterial(
64 name: 'cube',
65 baseColor: LinearColor.fromSrgb(0.5, 0.6, 0.8, 1.0),
66 ),
67 name: 'cube',
68 );
69 _scene = Scene()
70 ..add(_cube)
71 ..add(
72 LightNode(name: 'sun', intensity: 2.5 * LightNode.legacyUnit)
73 ..setLocalForward(Vector3(-0.3, -0.6, -0.4)),
74 );
76 // Built once, kept: the widget below is rebuilt every frame, and a game
77 // made afresh each time would restart before it drew anything.
78 _game = TransparentFlameGame();
79 final _Slider slider = _Slider();
80 _game
81 ..add(flameCaption('Flame: a component its own game moves'))
82 ..add(slider)
83 ..add(
84 flameCaption(
85 'flutter3d: a cube its own scene turns',
86 at: Vector2(16.0, 88.0),
87 ),
88 );
89 return _scene;
90 }
93 void update(DemoContext context, double dt) {
94 _clock += dt;
95 context.orbit.syncProjectionDepth(context.camera);
96 _cube.setRotation(Quaternion.axisAngle(Vector3(0.0, 1.0, 0.0), _clock));
97 }
99 static (double, double) _run() {
100 // Flame's own layer: a plain PositionComponent, the same class every
101 // sprite and shape in a Flame game already is. Nothing here has heard of
102 // flutter3d.
103 final flameShape = PositionComponent(position: Vector2(2.0, 0.0));
105 // flutter3d's own layer: a cube placed at a spot of its own, on the far
106 // side of the origin from the Flame shape. Nothing here has heard of
107 // Flame.
108 const double cubeX = 9.0;
110 // Ticking the Flame side does not move it towards the cube, and moving
111 // the cube (above) never touched the Flame shape at all: no bridge sits
112 // between them yet. That is every other page in this category, one
113 // mechanism at a time.
114 flameShape.update(1 / 60);
115 final double flameX = flameShape.position.x;
117 return (flameX, cubeX);
118 }
120 /// The Flame game the page runs, for a test that steps it without a window.
122 TransparentFlameGame get game => _game;
125 Widget? customBody(BuildContext buildContext, DemoContext context) => _body;
128 void verify(Scene scene, FrameResult frame) {
129 if (frame.drawCalls < 1) {
130 throw StateError('the cube marker was not drawn');
131 }
132 if (_flameX != 2.0) {
133 throw StateError(
134 'the Flame shape should not have moved on its own, got $_flameX',
135 );
136 }
137 if (_cubeX != 9.0) {
138 throw StateError('the flutter3d cube should be at its own spot');
139 }
140 }
141}
143/// A square that Flame's own game slides to and fro across the top.
144final class _Slider extends PositionComponent {
145 _Slider() : super(position: Vector2(16.0, 40.0), size: Vector2.all(36.0));
147 double _t = 0.0;
150 void update(double dt) {
151 super.update(dt);
152 _t += dt;
153 position.x = 16.0 + 160.0 * (0.5 - 0.5 * math.cos(_t * 1.5));
154 }
157 void render(Canvas canvas) =>
158 canvas.drawRect(size.toRect(), Paint()..color = const Color(0xFFE8A33D));
159}