flutter3d
Showcase Changelog 62 packages API reference

Textured billboards: the source

since 0.2.0 Physics and particles

Regions the guide quotes: sprite (line 37), texture (line 60), contributor (line 77), check (line 101).

1/// A sprite on every billboard instead of the procedural disc, which is what
2/// turns a particle into any shape you can paint.
3///
4/// Quoted by `textured_particles.md` and shown whole in the Source tab.
5library;
6
7import 'dart:math' as math;
8import 'dart:typed_data';
9
10import 'package:flutter3d/flutter3d.dart';
11import 'package:flutter3d_particles/flutter3d_particles.dart';
12import 'package:flutter3d_showcase/src/demo/demo.dart';
14/// The `textured-particles` page: sparks drawn with a painted sprite. Its
15/// check is that the contributor draws the sprite.
16final class TexturedParticlesDemo extends ShowcaseDemo {
17 late final ParticleSystem _particles;
18 late final ParticleEffect _sparksEffect;
19 late final ParticleContributor _contributor;
21 static const int _size = 32;
22 static const int _count = 50;
24 // The sprite is the whole point of the page; a burst that never repeated
25 // would leave nothing wearing it after the first second.
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.4;
35 }
37 /// A four-pointed spark: bright along the axes, dark at the corners. Not a
38 /// shape the procedural disc every untextured particle uses can make.
39 ByteData _sparkSprite() {
40 final Uint8List bytes = Uint8List(_size * _size * 4);
41 for (var y = 0; y < _size; y++) {
42 for (var x = 0; x < _size; x++) {
43 final double dx = (x + 0.5) / _size * 2.0 - 1.0;
44 final double dy = (y + 0.5) / _size * 2.0 - 1.0;
45 final double along = math.max(dx.abs(), dy.abs());
46 final double across = math.min(dx.abs(), dy.abs());
47 final double alpha = (1.0 - along) * (1.0 - across * 3.0);
48 final int at = (y * _size + x) * 4;
49 bytes[at] = 255;
50 bytes[at + 1] = 220;
51 bytes[at + 2] = 160;
52 bytes[at + 3] = (alpha.clamp(0.0, 1.0) * 255).round();
53 }
54 }
55 return bytes.buffer.asByteData();
56 }
58 @override
59 Scene build(DemoContext context) {
60 final TextureHandle sprite = context.device.createTextureFromPixels(
61 width: _size,
62 height: _size,
63 format: TextureFormat.r8g8b8a8UNormInt,
64 pixels: _sparkSprite(),
65 );
67 _particles = ParticleSystem(capacity: _count, seed: 909);
68 _sparksEffect = ParticleEffect(
69 count: _count,
70 emitter: const SphereEmitter(speed: Range(1.0, 2.0)),
71 lifetime: const Range(0.8, 1.4),
72 size: const Range(0.1, 0.16),
73 color: LinearColor(1.0, 1.0, 1.0, 1.0),
74 );
75 _particles.burst(_sparksEffect, Vector3.zero());
77 _contributor = ParticleContributor(_particles, texture: sprite);
78 context.renderer.renderSteps.addContributor(_contributor);
80 return Scene()..add(
81 LightNode(name: 'sun', intensity: 2.0 * LightNode.legacyUnit)
82 ..setLocalForward(Vector3(-0.3, -0.6, -0.4)),
83 );
84 }
86 @override
87 void update(DemoContext context, double dt) {
88 _particles.update(dt);
90 if (_particles.aliveCount == 0) {
91 _cooldown -= dt;
92 if (_cooldown <= 0.0) {
93 _particles.burst(_sparksEffect, Vector3.zero());
94 _cooldown = _pause;
95 }
96 }
97 }
99 @override
100 void verify(Scene scene, FrameResult frame) {
101 if (_contributor.texture == null) {
102 throw StateError('the contributor should be drawing a sprite');
103 }
104 if (_particles.aliveCount != _count || frame.drawCalls < 1) {
105 throw StateError('the sparks did not reach the frame');
106 }
107 }