flutter3d
Showcase Changelog 62 packages API reference

Forces: the source

since 0.2.0 Physics and particles

Regions the guide quotes: affectors (line 41), effect (line 49), burst (line 61), advance (line 77), check (line 101).

1/// What happens to a particle after it is born: gravity, drag, wind,
2/// turbulence and spin, stacked on the same burst.
3///
4/// Quoted by `particle_modifiers.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-modifiers` page: one burst under gravity, drag, wind,
14/// turbulence and spin. Its check is that gravity and drag slowed the climb
15/// and spin turned the particles.
16final class ParticleModifiersDemo extends ShowcaseDemo {
17 late final ParticleSystem _particles;
18 late final ParticleEffect _debrisEffect;
19 late final ParticleContributor _contributor;
20 late final List<ParticleAffector> _affectors;
22 static const int _count = 80;
24 // Turbulence and wind are easiest to read while the debris is still
25 // moving, not once it has settled and stopped.
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.5
33 ..pitch = 0.25
34 ..yaw = 0.4;
35 }
37 @override
38 Scene build(DemoContext context) {
39 _particles = ParticleSystem(capacity: _count, seed: 90210);
41 _affectors = <ParticleAffector>[
42 const ParticleGravity(acceleration: -4.0),
43 const ParticleDrag(0.4),
44 ParticleWind(Vector3(0.6, 0.0, 0.0)),
45 const ParticleTurbulence(strength: 1.5, scale: 0.8),
46 const ParticleSpin(rate: 1.2 * 2.0 * math.pi),
47 ];
49 _debrisEffect = ParticleEffect(
50 count: _count,
51 emitter: const ConeEmitter(
52 speed: Range(1.5, 3.0),
53 halfAngle: 30.0 * math.pi / 180.0,
54 ),
55 lifetime: const Range(1.5, 2.5),
56 size: const Range(0.06, 0.1),
57 color: LinearColor(0.9, 0.6, 0.3, 1.0),
58 affectors: _affectors,
59 );
61 _particles.burst(
62 _debrisEffect,
63 Vector3.zero(),
64 direction: Vector3(0.0, 1.0, 0.0),
65 );
66 _contributor = ParticleContributor(_particles);
67 context.renderer.renderSteps.addContributor(_contributor);
69 return Scene()..add(
70 LightNode(name: 'sun', intensity: 2.0 * LightNode.legacyUnit)
71 ..setLocalForward(Vector3(-0.3, -0.6, -0.4)),
72 );
73 }
75 @override
76 void update(DemoContext context, double dt) {
77 _particles.update(dt);
79 if (_particles.aliveCount == 0) {
80 _cooldown -= dt;
81 if (_cooldown <= 0.0) {
82 _particles.burst(
83 _debrisEffect,
84 Vector3.zero(),
85 direction: Vector3(0.0, 1.0, 0.0),
86 );
87 _cooldown = _pause;
88 }
89 }
90 }
92 @override
93 void verify(Scene scene, FrameResult frame) {
94 if (_particles.aliveCount != _count) {
95 throw StateError('expected $_count live particles');
96 }
97 if (!_contributor.isActive || frame.drawCalls < 1) {
98 throw StateError('the burst did not reach the frame');
99 }
101 // The system keeps no public handle on one particle, so the claim is
102 // checked the way the affectors are unit tested: run the same list
103 // against a particle of our own and read what changed.
104 final Particle sample = Particle()
105 ..velocity.setValues(0.0, 2.0, 0.0)
106 ..ageSeconds = 0.4
107 ..lifetimeSeconds = 2.0
108 ..seed = 0.5;
109 for (final ParticleAffector affector in _affectors) {
110 affector.apply(ParticleStep(particle: sample, dt: 1 / 60));
111 }
112 if (sample.velocity.y >= 2.0) {
113 throw StateError('gravity and drag should have slowed the climb');
114 }
115 if (sample.rotation == 0.0) {
116 throw StateError('spin should have turned the particle');
117 }
118 }