flutter3d
Showcase Changelog 62 packages API reference

Over-life curves: the source

since 0.5.0 Physics and particles

Regions the guide quotes: curve (line 41), gradient (line 50), effect (line 56), check (line 99).

1/// Size and colour described as curves over a particle's life, instead of a
2/// pair of affectors that can only ever go from one number to another.
3///
4/// Quoted by `particle_curves.md` and shown whole in the Source tab.
5library;
6
7import 'package:flutter3d/flutter3d.dart';
8import 'package:flutter3d_particles/flutter3d_particles.dart';
9import 'package:flutter3d_showcase/src/demo/demo.dart';
11/// The `particle-curves` page: smoke whose size and colour follow curves
12/// over its life. Its check is a size curve that rises then falls and a
13/// gradient that fades to nothing.
14final class ParticleCurvesDemo extends ShowcaseDemo {
15 late final ParticleSystem _particles;
16 late final ParticleEffect _smokeEffect;
17 late final ParticleContributor _contributor;
18 late final ParticleCurve _size;
19 late final ParticleGradient _color;
21 static const int _count = 60;
23 // The curve and the gradient are the whole point of this page, and both
24 // finish fading to nothing by the end of a particle's life — a burst that
25 // never repeated would leave nothing on screen to read them from.
26 static const double _pause = 1.0;
27 double _cooldown = 0.0;
29 @override
30 void configureView(DemoContext context) {
31 context.orbit
32 ..distance = 4.0
33 ..pitch = 0.2
34 ..yaw = 0.5;
35 }
37 @override
38 Scene build(DemoContext context) {
39 _particles = ParticleSystem(capacity: _count, seed: 4242);
41 // Puffs up to one and a half times its birth size by the middle of its
42 // life, then shrinks away, easing smoothly through the peak rather than
43 // arriving at it with a corner.
44 _size = ParticleCurve(<CurveKey>[
45 CurveKey(0.0, 0.4, ease: KeyEase.smooth),
46 CurveKey(0.5, 1.5, ease: KeyEase.smooth),
47 CurveKey(1.0, 0.0),
48 ]);
50 _color = ParticleGradient(<GradientKey>[
51 GradientKey(0.0, LinearColor(1.0, 0.95, 0.6, 1.0)),
52 GradientKey(0.4, LinearColor(1.0, 0.4, 0.1, 1.0), ease: KeyEase.smooth),
53 GradientKey(1.0, LinearColor(0.2, 0.05, 0.05, 0.0)),
54 ]);
56 _smokeEffect = ParticleEffect(
57 count: _count,
58 emitter: const DriftEmitter(speed: Range(0.4, 0.9)),
59 lifetime: const Range(1.2, 2.0),
60 size: const Range(0.08, 0.12),
61 color: LinearColor(1.0, 1.0, 1.0, 1.0),
62 affectors: <ParticleAffector>[
63 ParticleSizeCurve(_size),
64 ParticleColorGradient(_color),
65 ],
66 );
68 _particles.burst(_smokeEffect, Vector3.zero());
69 _contributor = ParticleContributor(_particles);
70 context.renderer.renderSteps.addContributor(_contributor);
72 return Scene()..add(
73 LightNode(name: 'sun', intensity: 2.0 * LightNode.legacyUnit)
74 ..setLocalForward(Vector3(-0.3, -0.6, -0.4)),
75 );
76 }
78 @override
79 void update(DemoContext context, double dt) {
80 _particles.update(dt);
82 if (_particles.aliveCount == 0) {
83 _cooldown -= dt;
84 if (_cooldown <= 0.0) {
85 _particles.burst(_smokeEffect, Vector3.zero());
86 _cooldown = _pause;
87 }
88 }
89 }
91 @override
92 void verify(Scene scene, FrameResult frame) {
93 if (_particles.aliveCount != _count ||
94 !_contributor.isActive ||
95 frame.drawCalls < 1) {
96 throw StateError('the smoke did not reach the frame');
97 }
99 // The curve holds its end values outside its keys and rises then falls
100 // in between, which is what "puffs up, then shrinks" means as numbers.
101 if (_size.sample(0.0) >= _size.sample(0.5) ||
102 _size.sample(0.5) <= _size.sample(1.0)) {
103 throw StateError('the size curve should rise then fall');
104 }
105 // The gradient fades to nothing: alpha at the last key is zero.
106 final Vector4 end = Vector4.zero();
107 _color.sampleInto(end, 1.0);
108 if (end.w != 0.0) {
109 throw StateError('the gradient should fade to nothing');
110 }
111 }