flutter3d
Showcase Changelog 62 packages API reference

Eighty sparks in one draw, fire and smoke in two: the source

since 0.8.3 The Flame bridge

Regions the guide quotes: batch (line 105), pools (line 119), sparks (line 143), draw (line 153).

1/// Many small Flame components drawn as one instanced batch, and fire and
2/// smoke from particle pools running on Flame's clock.
3///
4/// Quoted by `flame_crowd.md` and shown whole in the Source tab.
5library;
6
7import 'dart:math' as math;
8
9import 'package:flame/components.dart';
10import 'package:flame/game.dart';
11import 'package:flame_flutter3d/flame_flutter3d.dart';
12import 'package:flutter/widgets.dart';
13import 'package:flutter3d/flutter3d.dart';
14import 'package:flutter3d_particles/flutter3d_particles.dart';
15import 'package:flutter3d_showcase/src/demo/demo.dart';
16import 'package:flutter3d_showcase/src/demo/flame_layer.dart';
18/// How many sparks circle the brazier.
19const int _sparks = 80;
21/// The `flame-crowd` page: a swarm of Flame spark components over a floor,
22/// drawn as one instanced batch, with fire and smoke from particle pools.
23/// Its check is that the sparks reach the frame as a single batch.
24final class FlameCrowdDemo extends ShowcaseDemo {
25 late final DemoContext _context;
26 late final Scene _scene;
27 late final _Brazier _game;
28 late final Widget _body = flameOrbit(
29 _context,
30 Flutter3dFlameWidget(
31 game: _game,
32 existing: BorrowedRenderer(
33 device: _context.device,
34 renderer: _context.renderer,
35 ),
36 ),
37 );
39 @override
40 void configureView(DemoContext context) {
41 context.orbit
42 ..distance = 10.0
43 ..pitch = 0.55
44 ..yaw = 0.4;
45 context.orbit.target.setValues(0.0, 0.8, 0.0);
46 }
48 @override
49 Scene build(DemoContext context) {
50 _context = context;
51 _scene = Scene()
52 ..ambientColor = LinearColor(0.45, 0.5, 0.6)
53 ..ambientIntensity = 0.25 * LightNode.legacyUnit
54 ..add(
55 MeshNode(
56 DeviceMesh.upload(
57 context.device,
58 CuboidShape(size: Vector3(10.0, 0.1, 10.0)).build(),
59 ),
60 RenderMaterial(
61 name: 'floor',
62 baseColor: LinearColor.fromSrgb(0.5, 0.52, 0.5, 1.0),
63 ),
64 name: 'floor',
65 )..setPosition(0.0, -0.05, 0.0),
66 )
67 ..add(
68 LightNode(name: 'sun', intensity: 2.0 * LightNode.legacyUnit)
69 ..setLocalForward(Vector3(-0.3, -0.7, -0.4)),
70 );
71 _game = _Brazier(context.camera)..open3d(context.device, scene: _scene);
72 // In the scene from the start, empty until the sparks take their slots.
73 _scene.add(_game.batch);
74 return _scene;
75 }
77 @override
78 void update(DemoContext context, double dt) =>
79 context.orbit.syncProjectionDepth(context.camera);
81 /// The Flame game the page runs, for a test that steps it without a window.
82 @visibleForTesting
83 FlameGame get game => _game;
85 @override
86 Widget? customBody(BuildContext buildContext, DemoContext context) => _body;
88 @override
89 void verify(Scene scene, FrameResult frame) {
90 if (frame.drawCalls < 1) throw StateError('the floor was not drawn');
91 final batches = scene.meshes.whereType<InstancedMeshNode>();
92 if (batches.length != 1) {
93 throw StateError(
94 'the sparks should be one batch, found ${batches.length}',
95 );
96 }
97 }
100final class _Brazier extends FlameGame with HasFlutter3d {
101 _Brazier(this._eye);
103 final CameraNode _eye;
105 /// Every spark's mesh and material, drawn in one call however many there
106 /// are; each spark takes a slot in it while it is in the game.
107 late final InstancedMeshNode batch = InstancedMeshNode(
108 DeviceMesh.upload(device, CuboidShape(size: Vector3.all(0.12)).build()),
109 RenderMaterial(
110 name: 'spark',
111 baseColor: LinearColor.fromSrgb(1.0, 0.8, 0.4, 1.0),
112 emissive: LinearColor(1.0, 0.6, 0.2),
113 emissiveStrength: 3.0 * RenderMaterial.defaultEmissiveStrength,
114 ),
115 capacity: _sparks,
116 name: 'sparks',
117 );
119 /// Fire adds light and smoke takes it away: two pools, two draws.
120 final Particles3dComponent _fire = Particles3dComponent(
121 system: ParticleSystem(capacity: 256),
122 plane: BridgePlane.ground(),
123 );
124 final Particles3dComponent _smoke = Particles3dComponent(
125 system: ParticleSystem(capacity: 128),
126 plane: BridgePlane.ground(),
127 );
129 late final DeviceMesh _puff = DeviceMesh.upload(
130 device,
131 const SphereShape(radius: 0.3, segments: 10, rings: 6).build(),
132 );
133 late final DeviceMesh _shard = DeviceMesh.upload(
134 device,
135 CuboidShape(size: Vector3.all(0.25)).build(),
136 );
138 double _clock = 0.0;
140 @override
141 CameraNode createCamera3d() => _eye;
143 @override
144 void onOpen3d() {
145 addAll(<Component>[
146 for (var i = 0; i < _sparks; i++)
147 _Spark(batch, i * 2.0 * math.pi / _sparks, 1.5 + (i % 4) * 0.5),
148 _fire,
149 _smoke,
150 ]);
151 }
153 @override
154 void onRenderer3d(Renderer renderer) {
155 _fire.drawWith(renderer, _shard);
156 _smoke.drawWith(renderer, _puff, blend: MeshParticleContributor.darkening);
157 }
159 @override
160 void update(double dt) {
161 super.update(dt);
162 if (!has3d) return;
163 _clock += dt;
164 if (_clock < 0.08) return;
165 _clock = 0.0;
166 _fire.burstAt(_flame, Vector2.zero(), elevation: 0.3);
167 _smoke.burstAt(_soot, Vector2.zero(), elevation: 1.2);
168 }
170 static ParticleEffect get _flame => ParticleEffect(
171 count: 4,
172 emitter: const ConeEmitter(
173 speed: Range(1.0, 2.5),
174 halfAngle: 25.0 * math.pi / 180.0,
175 ),
176 lifetime: const Range(0.4, 0.7),
177 size: const Range(0.8, 1.1),
178 color: LinearColor(3.0, 1.6, 0.4, 1.0),
179 affectors: const <ParticleAffector>[ParticleSizeOverLife()],
180 );
182 static ParticleEffect get _soot => ParticleEffect(
183 count: 1,
184 emitter: const ConeEmitter(
185 speed: Range(0.6, 1.2),
186 halfAngle: 20.0 * math.pi / 180.0,
187 ),
188 lifetime: const Range(2.0, 2.6),
189 size: const Range(0.9, 1.2),
190 color: LinearColor(0.25, 0.27, 0.32, 1.0),
191 affectors: const <ParticleAffector>[
192 ParticleDrag(1.0),
193 ParticleSizeOverLife(from: 0.5, to: 2.5),
194 ParticleFade(startsAt: 0.5),
195 ],
196 );
199/// One spark circling the brazier: an ordinary Flame component, moved by
200/// setting its position, drawn through its slot in the batch.
201final class _Spark extends InstancedObject3dComponent {
202 _Spark(InstancedMeshNode batch, this._phase, this._radius)
203 : super(batch: batch, plane: BridgePlane.ground(), elevation: 0.5);
205 final double _phase;
206 final double _radius;
207 double _t = 0.0;
209 @override
210 void update(double dt) {
211 super.update(dt);
212 _t += dt;
213 final turn = _phase + _t * (1.2 / _radius);
214 position.setValues(_radius * math.cos(turn), _radius * math.sin(turn));
215 elevation = 0.5 + 0.4 * math.sin(_t * 3.0 + _phase * 5.0);
216 }