flutter3d
Showcase Changelog 62 packages API reference

Temporal anti-aliasing: the source

since 0.8.0 Post-processing

Regions the guide quotes: railing (line 49), motion (line 79), settings (line 93), clips (line 115), reported (line 141).

1/// Temporal anti-aliasing: the scene drawn a fraction of a pixel off each
2/// frame and blended into a history of the frames before it.
3///
4/// Quoted by `temporal_anti_aliasing.md` and shown whole in the Source tab.
5library;
6
7import 'dart:math' as math;
8
9import 'package:flutter3d/flutter3d.dart';
10import 'package:flutter3d_showcase/pages/post/post_stage.dart';
11import 'package:flutter3d_showcase/src/demo/demo.dart';
13/// The `temporal-anti-aliasing` page: a railing of thin bars in front of a
14/// wall that changes colour, resolved over frames.
15final class TemporalAntiAliasingDemo extends ShowcaseDemo {
16 /// Whether the temporal resolve runs.
17 bool temporal = true;
19 /// Whether the wall turns yellow for two seconds out of every four, to show
20 /// what the history clip does with a change.
21 bool swapWall = true;
23 /// How much of the history each frame keeps, 0 to 0.98.
24 double historyWeight = 0.9;
26 /// How much the resolve sharpens, 0 to 1.
27 double sharpen = 0.25;
29 /// How the history is clipped to the new frame's neighbourhood.
30 TemporalClip clip = TemporalClip.aabb;
32 /// Seconds since the page opened, which is what moves the railing.
33 double _time = 0.0;
35 late final SceneNode _railing;
36 late final RenderMaterial _wallPaint;
38 @override
39 void configureView(DemoContext context) {
40 context.orbit
41 ..distance = 3.5
42 ..pitch = 0.0
43 ..yaw = 0.0;
44 context.orbit.target.setValues(0.0, 0.0, 0.0);
45 }
47 @override
48 Scene build(DemoContext context) {
49 RenderMaterial flat(Vector4 color) => RenderMaterial(
50 lighting: LightingModel.unlit,
51 baseColor: _fromSrgb(color),
52 );
53 final DeviceMesh cube = DeviceMesh.upload(
54 context.device,
55 CuboidShape().build(),
56 );
57 _wallPaint = flat(Vector4(0.75, 0.25, 0.5, 1.0));
58 final RenderMaterial barPaint = flat(Vector4(0.0, 0.0, 1.0, 1.0));
60 _railing = SceneNode(name: 'railing');
61 for (int i = -6; i <= 6; i++) {
62 _railing.add(
63 MeshNode(cube, barPaint, name: 'bar $i')
64 ..setPosition(i * 0.45, 0.0, 0.0)
65 ..setRotationYawPitchRoll(0.0, 0.0, 0.3)
66 ..setScale(0.04, 6.0, 0.04),
67 );
68 }
69 final MeshNode wall = MeshNode(cube, _wallPaint, name: 'wall')
70 ..setPosition(0.0, 0.0, -0.3)
71 ..setScale(20.0, 20.0, 0.1);
72 return Scene()
73 ..add(wall)
74 ..add(_railing);
75 }
77 @override
78 void update(DemoContext context, double dt) {
79 _time += dt;
80 _railing.setPosition(0.25 * math.sin(_time * 1.5), 0.0, 0.0);
81 // Pink and yellow. Pink lies inside the YCoCg box that yellow and the
82 // blue bars span, so the box keeps it as history.
83 final bool yellow = swapWall && (_time ~/ 2).isOdd;
84 if (yellow) {
85 _wallPaint.baseColor = LinearColor.fromSrgb(1.0, 1.0, 0.0, 1.0);
86 } else {
87 _wallPaint.baseColor = LinearColor.fromSrgb(0.75, 0.25, 0.5, 1.0);
88 }
89 }
91 @override
92 RenderSettings settings(DemoContext context) => RenderSettings(
93 antiAlias: AntiAliasSettings(
94 temporal: TemporalSettings(
95 enabled: temporal,
96 historyWeight: historyWeight,
97 sharpen: sharpen,
98 clip: clip,
99 ),
100 ),
101 );
103 @override
104 List<DemoControl> controls(DemoContext context) => <DemoControl>[
105 ToggleControl(
106 'Temporal resolve',
107 value: () => temporal,
108 onChanged: (bool v) => temporal = v,
109 ),
110 ToggleControl(
111 'Swap the wall colour',
112 value: () => swapWall,
113 onChanged: (bool v) => swapWall = v,
114 ),
115 ChoiceControl(
116 'History clip',
117 options: <String>[
118 for (final TemporalClip c in TemporalClip.values) c.name,
119 ],
120 index: () => clip.index,
121 onChanged: (int i) => clip = TemporalClip.values[i],
122 ),
123 SliderControl(
124 'History weight',
125 min: 0,
126 max: 0.98,
127 value: () => historyWeight,
128 onChanged: (double v) => historyWeight = v,
129 ),
130 SliderControl(
131 'Sharpen',
132 min: 0,
133 max: 1,
134 value: () => sharpen,
135 onChanged: (double v) => sharpen = v,
136 ),
137 ];
139 @override
140 void verify(Scene scene, FrameResult frame) {
141 expectPassOrDecline(frame, 'temporal resolve');
142 }
145/// A `Vector4` holding a colour sRGB-encoded, as the linear colour it names.
146LinearColor _fromSrgb(Vector4 c) => LinearColor.fromSrgb(c.x, c.y, c.z, c.w);