A burst that lights the room: the source
Open the live demo · Read the guide · View on GitHub
Regions the guide quotes: source (line 41), follow (line 65), check (line 82).
1/// A single burst that lights the room it goes off in, the way a muzzle
2/// flash or an explosion should but a burst with nobody keyed to it cannot.
3///
4/// Quoted by `burst_light.md` and shown whole in the Source tab.
5library;
7import 'package:flutter3d/flutter3d.dart';
8import 'package:flutter3d_particles/flutter3d_particles.dart';
9import 'package:flutter3d_showcase/src/demo/demo.dart';
11final class _Flash with LightEmitter {}
13/// The `burst-light` page: one particle burst that lights its own flash.
14/// Its check is that the burst made the flash and the frame drew it.
15final class BurstLightDemo extends ShowcaseDemo {
16 late final ParticleSystem _particles;
17 late final ParticleEffect _flashEffect;
18 late final LightNode _glow;
20 final _Flash _flash = _Flash();
22 // A muzzle flash that fired once and went dark would be a demo you had to
23 // catch. A second's pause between bursts reads as a repeating flash rather
24 // than a machine gun, and only starts once the particles from the last one
25 // are gone, so it never overlaps a burst still lighting the room.
26 static const double _pause = 1.0;
27 double _cooldown = 0.0;
30 void configureView(DemoContext context) {
31 context.orbit
32 ..distance = 4.0
33 ..pitch = 0.2
34 ..yaw = 0.4;
35 }
38 Scene build(DemoContext context) {
39 _particles = ParticleSystem(capacity: 128, seed: 512);
41 // Nothing above this differs from an ordinary burst. `source` is the
42 // whole of what makes it a light: a `LightEmitter` passed here has its
43 // glow fed by exactly these particles, for as long as they live.
44 _flashEffect = ParticleEffect(
45 count: 60,
46 emitter: const SphereEmitter(speed: Range(2.0, 5.0)),
47 lifetime: const Range(0.2, 0.4),
48 size: const Range(0.05, 0.09),
49 color: LinearColor(1.0, 0.85, 0.5, 1.0),
50 );
51 _particles.burst(_flashEffect, Vector3.zero(), source: _flash);
53 context.renderer.renderSteps.addContributor(
54 ParticleContributor(_particles),
55 );
57 _glow = LightNode(name: 'flash', type: LightType.point, intensity: 0.0);
58 return Scene()..add(_glow);
59 }
62 void update(DemoContext context, double dt) {
63 _particles.update(dt);
65 final ParticleGlow glow = _flash.glow;
66 if (glow.isLocated) {
67 _glow.setPositionFrom(glow.center);
68 _glow.intensity = glow.power * 0.6 * LightNode.legacyUnit;
69 }
71 if (_particles.aliveCount == 0) {
72 _cooldown -= dt;
73 if (_cooldown <= 0.0) {
74 _particles.burst(_flashEffect, Vector3.zero(), source: _flash);
75 _cooldown = _pause;
76 }
77 }
78 }
81 void verify(Scene scene, FrameResult frame) {
82 if (_flash.glow.count == 0 || _flash.glow.power <= 0.0) {
83 throw StateError('the burst should have lit its own flash');
84 }
85 if (frame.drawCalls < 1) {
86 throw StateError('the flash did not reach the frame');
87 }
88 }
89}