flutter3d
Showcase Changelog 62 packages API reference

Sprite-sheet animation: the source

since 0.2.0 or earlier Physics and particles

Regions the guide quotes: atlas (line 40), flipbook (line 75), check (line 127).

1/// A sprite sheet played across a particle's life instead of a single
2/// static sprite, so a spark can burn out frame by frame rather than only
3/// fade.
4///
5/// Quoted by `flipbook.md` and shown whole in the Source tab.
6library;
7
8import 'dart:math' as math;
9import 'dart:typed_data';
11import 'package:flutter3d/flutter3d.dart';
12import 'package:flutter3d_particles/flutter3d_particles.dart';
13import 'package:flutter3d_showcase/src/demo/demo.dart';
15/// The `flipbook` page: sparks that play a sprite sheet across their life.
16/// Its check is that they are drawn and that the sheet plays across a
17/// particle's life.
18final class FlipbookDemo extends ShowcaseDemo {
19 late final ParticleSystem _particles;
20 late final ParticleEffect _sparksEffect;
21 late final Flipbook _flipbook;
23 static const int _cellSize = 16;
24 static const int _frames = 4;
25 static const int _count = 40;
27 // The sheet only plays across a particle's own life, so a burst that never
28 // repeated would leave the sheet on its last, darkest frame forever.
29 static const double _pause = 1.0;
30 double _cooldown = 0.0;
32 @override
33 void configureView(DemoContext context) {
34 context.orbit
35 ..distance = 4.0
36 ..pitch = 0.2
37 ..yaw = 0.4;
38 }
40 /// Four cells in a row, each a solid colour: a stand-in for four drawn
41 /// frames of a burning-out spark, from bright to dark.
42 ByteData _atlas() {
43 const List<List<int>> colors = <List<int>>[
44 <int>[255, 255, 220],
45 <int>[255, 190, 90],
46 <int>[220, 90, 40],
47 <int>[60, 20, 10],
48 ];
49 final Uint8List bytes = Uint8List(_cellSize * _frames * _cellSize * 4);
50 final int width = _cellSize * _frames;
51 for (var y = 0; y < _cellSize; y++) {
52 for (var frame = 0; frame < _frames; frame++) {
53 final List<int> color = colors[frame];
54 for (var x = 0; x < _cellSize; x++) {
55 final int at = (y * width + frame * _cellSize + x) * 4;
56 bytes[at] = color[0];
57 bytes[at + 1] = color[1];
58 bytes[at + 2] = color[2];
59 bytes[at + 3] = 255;
60 }
61 }
62 }
63 return bytes.buffer.asByteData();
64 }
66 @override
67 Scene build(DemoContext context) {
68 final TextureHandle atlas = context.device.createTextureFromPixels(
69 width: _cellSize * _frames,
70 height: _cellSize,
71 format: TextureFormat.r8g8b8a8UNormInt,
72 pixels: _atlas(),
73 );
75 _flipbook = Flipbook(columns: _frames, rows: 1);
77 _particles = ParticleSystem(capacity: _count, seed: 606);
78 _sparksEffect = ParticleEffect(
79 count: _count,
80 emitter: const ConeEmitter(
81 speed: Range(1.0, 2.2),
82 halfAngle: 20.0 * math.pi / 180.0,
83 ),
84 lifetime: const Range(0.8, 1.2),
85 size: const Range(0.09, 0.13),
86 color: LinearColor(1.0, 1.0, 1.0, 1.0),
87 );
88 _particles.burst(
89 _sparksEffect,
90 Vector3.zero(),
91 direction: Vector3(0.0, 1.0, 0.0),
92 );
94 context.renderer.renderSteps.addContributor(
95 ParticleContributor(_particles, texture: atlas, flipbook: _flipbook),
96 );
98 return Scene()..add(
99 LightNode(name: 'sun', intensity: 2.0 * LightNode.legacyUnit)
100 ..setLocalForward(Vector3(-0.3, -0.6, -0.4)),
101 );
102 }
104 @override
105 void update(DemoContext context, double dt) {
106 _particles.update(dt);
108 if (_particles.aliveCount == 0) {
109 _cooldown -= dt;
110 if (_cooldown <= 0.0) {
111 _particles.burst(
112 _sparksEffect,
113 Vector3.zero(),
114 direction: Vector3(0.0, 1.0, 0.0),
115 );
116 _cooldown = _pause;
117 }
118 }
119 }
121 @override
122 void verify(Scene scene, FrameResult frame) {
123 if (_particles.aliveCount != _count || frame.drawCalls < 1) {
124 throw StateError('the sparks did not reach the frame');
125 }
127 // A particle just born and one about to die should be showing different
128 // cells of the sheet: the whole point of a flipbook over a static sprite.
129 final Particle newborn = Particle()
130 ..ageSeconds = 0.0
131 ..lifetimeSeconds = 1.0;
132 final Particle dying = Particle()
133 ..ageSeconds = 0.99
134 ..lifetimeSeconds = 1.0;
135 if (_flipbook.cellFor(newborn).left == _flipbook.cellFor(dying).left) {
136 throw StateError('the flipbook should play across a particle\'s life');
137 }
138 }