flutter3d
Showcase Changelog 62 packages API reference

Emitter shapes: the source

since 0.2.0 Physics and particles

Regions the guide quotes: shapes (line 58), along (line 73), burst (line 82), check (line 107).

1/// Four emitter shapes, each bursting from its own spot so the difference
2/// between them is something you can see rather than something you are told.
3///
4/// Quoted by `particle_emitters.md` and shown whole in the Source tab.
5library;
6
7import 'dart:math' as math;
8
9import 'package:flutter3d/flutter3d.dart';
10import 'package:flutter3d_particles/flutter3d_particles.dart';
11import 'package:flutter3d_showcase/src/demo/demo.dart';
13/// The `particle-emitters` page: four emitter shapes bursting side by side.
14/// Its check is that all four bursts are alive.
15final class ParticleEmittersDemo extends ShowcaseDemo {
16 late final ParticleSystem _particles;
17 late final ParticleContributor _contributor;
18 late final List<(Vector3, ParticleEmitter)> _shapes;
20 static const int _perBurst = 40;
22 // Four separate bursts that each ran once would leave three of the four
23 // shapes dead within a couple of seconds while the smoke of the fourth
24 // drifted on alone.
25 static const double _pause = 1.0;
26 double _cooldown = 0.0;
28 @override
29 void configureView(DemoContext context) {
30 context.orbit
31 ..distance = 6.0
32 ..pitch = 0.25
33 ..yaw = 0.5;
34 }
36 ParticleEffect _effect(ParticleEmitter emitter) => ParticleEffect(
37 count: _perBurst,
38 emitter: emitter,
39 lifetime: const Range(0.6, 1.2),
40 size: const Range(0.05, 0.09),
41 color: LinearColor(0.8, 0.85, 1.0, 1.0),
42 );
44 void _burstAll() {
45 for (final (Vector3 origin, ParticleEmitter emitter) in _shapes) {
46 _particles.burst(
47 _effect(emitter),
48 origin,
49 direction: Vector3(0.0, 1.0, 0.0),
50 );
51 }
52 }
54 @override
55 Scene build(DemoContext context) {
56 _particles = ParticleSystem(capacity: _perBurst * 4, seed: 314159);
58 // A sphere throws particles outwards in every direction: an explosion.
59 // A cone narrows that spread around one axis: a muzzle flash, sparks off
60 // a wall. A box fills a volume rather than starting at a point: rain over
61 // an area. A drift barely moves outward at all: smoke.
62 _shapes = <(Vector3, ParticleEmitter)>[
63 (Vector3(-3.0, 0.0, 0.0), const SphereEmitter(speed: Range(1.0, 2.5))),
64 (
65 Vector3(-1.0, 0.0, 0.0),
66 const ConeEmitter(
67 speed: Range(2.0, 4.0),
68 halfAngle: 18.0 * math.pi / 180.0,
69 ),
70 ),
71 (
72 Vector3(1.0, 0.0, 0.0),
73 BoxEmitter(
74 halfExtents: Vector3(0.6, 0.05, 0.6),
75 speed: const Range(0.3, 0.6),
76 along: Vector3(0, 1, 0),
77 ),
78 ),
79 (Vector3(3.0, 0.0, 0.0), const DriftEmitter()),
80 ];
82 _burstAll();
83 _contributor = ParticleContributor(_particles);
84 context.renderer.renderSteps.addContributor(_contributor);
86 return Scene()..add(
87 LightNode(name: 'sun', intensity: 2.0 * LightNode.legacyUnit)
88 ..setLocalForward(Vector3(-0.3, -0.6, -0.4)),
89 );
90 }
92 @override
93 void update(DemoContext context, double dt) {
94 _particles.update(dt);
96 if (_particles.aliveCount == 0) {
97 _cooldown -= dt;
98 if (_cooldown <= 0.0) {
99 _burstAll();
100 _cooldown = _pause;
101 }
102 }
103 }
105 @override
106 void verify(Scene scene, FrameResult frame) {
107 if (_particles.aliveCount != _perBurst * 4) {
108 throw StateError(
109 'expected ${_perBurst * 4} particles from four emitters, '
110 'got ${_particles.aliveCount}',
111 );
112 }
113 if (!_contributor.isActive || frame.drawCalls < 1) {
114 throw StateError('the emitters did not reach the frame');
115 }
116 }