FXAA, SMAA and sharpen: the source
Open the live demo · Read the guide · View on GitHub
Regions the guide quotes: card (line 46), settings (line 70), method (line 73), reported (line 102).
1/// FXAA or SMAA 1x: edges smoothed on the finished picture, and a sharpen
2/// pass that shares FXAA's taps.
3///
4/// Quoted by `anti_aliasing.md` and shown whole in the Source tab.
5library;
7import 'dart:math' as math;
9import 'package:flutter3d/flutter3d.dart';
10import 'package:flutter3d_showcase/src/demo/demo.dart';
12/// The `anti-aliasing` page: a wheel of thin spokes whose edges FXAA
13/// smooths. Its check is that the frame ran FXAA exactly when it was asked
14/// to.
15final class AntiAliasingDemo extends ShowcaseDemo {
16 /// Whether edges are smoothed; the "Smooth edges" switch.
17 bool enabled = true;
19 /// How strongly the frame is sharpened with FXAA's taps, 0 to 1.
20 double sharpen = 0.0;
22 /// FXAA or SMAA 1x, as [EdgeSmoothing.fxaa] and [EdgeSmoothing.smaa].
23 EdgeSmoothing method = EdgeSmoothing.fxaa;
25 /// One diagonal card staircases at exactly one contrast step, over one
26 /// edge — too little for either pass to read as more than noise at a
27 /// glance. A dozen thin spokes cross that same centre at a dozen
28 /// different angles, so every angle a staircase can take is on screen at
29 /// once, and the same handful of pixels near the middle carry all of
30 /// them where the effect is easiest to see.
31 static const int _spokes = 14;
34 void configureView(DemoContext context) {
35 // Close enough that the spokes fill the frame — softening a single
36 // pixel-wide edge is not something a reader can see without leaning on
37 // the zoom this page never offered a reason to reach for.
38 context.orbit
39 ..distance = 1.5
40 ..pitch = 0.0
41 ..yaw = 0.0;
42 }
45 Scene build(DemoContext context) {
46 final DeviceMesh spoke = DeviceMesh.upload(
47 context.device,
48 CuboidShape(size: Vector3(0.1, 2.8, 0.1)).build(),
49 );
50 final RenderMaterial material = RenderMaterial(
51 name: 'spokes',
52 baseColor: LinearColor.fromSrgb(0.95, 0.95, 0.95, 1.0),
53 lighting: LightingModel.unlit,
54 );
55 final Scene scene = Scene();
56 for (var i = 0; i < _spokes; i++) {
57 // Only half a turn: a spoke through the origin looks the same rotated
58 // by pi, so a full turn would draw every angle twice.
59 final double angle = i / _spokes * math.pi;
60 scene.add(
61 MeshNode(spoke, material, name: 'spoke $i')
62 ..setLocalMatrix(Matrix4.rotationZ(angle)),
63 );
64 }
65 return scene;
66 }
69 RenderSettings settings(DemoContext context) => RenderSettings(
70 antiAlias: AntiAliasSettings(
71 enabled: enabled,
72 sharpen: sharpen,
73 method: method,
74 ),
75 );
78 List<DemoControl> controls(DemoContext context) => <DemoControl>[
79 ToggleControl(
80 'Smooth edges',
81 value: () => enabled,
82 onChanged: (bool v) => enabled = v,
83 ),
84 ChoiceControl(
85 'Method',
86 options: const <String>['FXAA', 'SMAA 1x'],
87 index: () => method == EdgeSmoothing.smaa ? 1 : 0,
88 onChanged: (int i) =>
89 method = i == 1 ? EdgeSmoothing.smaa : EdgeSmoothing.fxaa,
90 ),
91 SliderControl(
92 'Sharpen',
93 min: 0,
94 max: 1,
95 value: () => sharpen,
96 onChanged: (double v) => sharpen = v,
97 ),
98 ];
101 void verify(Scene scene, FrameResult frame) {
102 final bool ran = frame.antiAliasing.fxaa;
103 if (ran != enabled) {
104 throw StateError(
105 'the frame reports fxaa: $ran while the page asked '
106 'for $enabled',
107 );
108 }
109 }
110}