Lens flare, distortion and .cube tables: the source
Open the live demo · Read the guide · View on GitHub
Regions the guide quotes: tables (line 12), load (line 65), lamp (line 72), lens (line 92), check (line 134).
1/// What the glass does to a picture: a bright lamp throws ghosts and a halo
2/// across the frame, the frame bows outwards or pinches in, and the colours
3/// go through a table written by a grading tool.
4///
5/// Quoted by `lens_effects.md` and shown whole in the Source tab.
6library;
8import 'package:flutter3d/flutter3d.dart';
9import 'package:flutter3d_showcase/pages/post/post_stage.dart';
10import 'package:flutter3d_showcase/src/demo/demo.dart';
12/// A warm grade as a `.cube` file: the eight corners of the colour cube,
13/// red running fastest. Black is lifted a little towards blue, white is
14/// pulled towards amber, and everything between is blended from the corners.
15const String _warmCube = '''
16TITLE "Warm highlights"
17LUT_3D_SIZE 2
180.04 0.02 0.06
191.00 0.10 0.02
200.06 0.92 0.04
211.00 0.96 0.10
220.02 0.06 0.80
230.95 0.10 0.78
240.06 0.95 0.86
251.00 0.97 0.88
26''';
28/// A contrast curve as a 1D `.cube`: the same S on each channel, darker
29/// shadows and brighter highlights with the middle left where it was.
30const String _curveCube = '''
31TITLE "S curve"
32LUT_1D_SIZE 5
330.00 0.00 0.00
340.15 0.15 0.15
350.50 0.50 0.50
360.85 0.85 0.85
371.00 1.00 1.00
38''';
40/// The `lens-effects` page: a bright lamp throwing a flare, a distorted
41/// frame, and a colour table written by a grading tool. Its check is that the
42/// flare ran and the table maps white and warms mid grey as the file says.
43final class LensEffectsDemo extends ShowcaseDemo {
44 /// Whether the lamp throws a lens flare.
45 bool flare = true;
47 /// How strong the flare's halo is, 0 to 1.
48 double halo = 0.5;
50 /// How far the frame bows, -0.4 (pincushion) to 0.4 (barrel).
51 double distortion = 0.15;
53 /// 0 for no table, 1 for the warm 3D one, 2 for the 1D curve.
54 int table = 1;
56 late final CubeLut _warm;
57 late final List<TextureHandle?> _strips;
58 late LookSettings _look;
61 void configureView(DemoContext context) => PostStage.frame(context);
64 Scene build(DemoContext context) {
65 _warm = CubeLut.parse(_warmCube);
66 _strips = <TextureHandle?>[
67 null,
68 _warm.upload(context.device),
69 CubeLut.parse(_curveCube).upload(context.device),
70 ];
72 // A lamp far brighter than the display can show, up and to the left, so
73 // its glow is strong enough to flare and its ghosts cross the middle.
74 final MeshNode lamp = MeshNode(
75 DeviceMesh.upload(
76 context.device,
77 const SphereShape(radius: 0.25).build(),
78 ),
79 RenderMaterial(
80 name: 'lamp',
81 baseColor: LinearColor.fromSrgb(0.1, 0.1, 0.1, 1.0),
82 emissive: LinearColor(1.0, 0.85, 0.6),
83 emissiveStrength: 40.0 * RenderMaterial.defaultEmissiveStrength,
84 ),
85 name: 'lamp',
86 )..setPosition(-3.2, 2.8, -2.0);
87 return PostStage.build(context).scene..add(lamp);
88 }
91 RenderSettings settings(DemoContext context) {
92 final BloomSettings bloom = BloomSettings(
93 // Three levels rather than five keep the glow, and so each ghost,
94 // tight enough to read as a disc rather than a wash.
95 levels: 3,
96 lensFlare: LensFlareSettings(enabled: flare, halo: halo),
97 );
98 _look = LookSettings(distortion: distortion, lut: _strips[table]);
99 return RenderSettings(bloom: bloom, look: _look);
100 }
103 List<DemoControl> controls(DemoContext context) => <DemoControl>[
104 ToggleControl(
105 'Lens flare',
106 value: () => flare,
107 onChanged: (bool v) => flare = v,
108 ),
109 SliderControl(
110 'Halo',
111 min: 0,
112 max: 1,
113 value: () => halo,
114 onChanged: (double v) => halo = v,
115 ),
116 SliderControl(
117 'Distortion (pincushion to barrel)',
118 min: -0.4,
119 max: 0.4,
120 value: () => distortion,
121 onChanged: (double v) => distortion = v,
122 format: (double v) => v.toStringAsFixed(2),
123 ),
124 ChoiceControl(
125 'Table',
126 options: const <String>['None', 'Warm (3D)', 'S curve (1D)'],
127 index: () => table,
128 onChanged: (int i) => table = i,
129 ),
130 ];
133 void verify(Scene scene, FrameResult frame) {
134 // The flare is a pass of its own, drawn from the glow.
135 if (flare && !passRan(frame, 'lens flare')) {
136 throw StateError('the lens flare did not run: ${frame.skipped}');
137 }
138 // The table reads back what the file says at a corner, and a middle grey
139 // comes out of it warmer than it went in: more red than blue.
140 final RgbTriple(:r, :g, :b) = _warm.lookup(1.0, 1.0, 1.0);
141 // Held as 32-bit floats, so compared to within a millionth.
142 if ((r - 1.0).abs() > 1e-6 ||
143 (g - 0.97).abs() > 1e-6 ||
144 (b - 0.88).abs() > 1e-6) {
145 throw StateError('white maps to ($r, $g, $b), not the file\'s corner');
146 }
147 final RgbTriple(r: gr, b: gb) = _warm.lookup(0.5, 0.5, 0.5);
148 if (gr <= gb + 0.05) {
149 throw StateError('mid grey is not warmer: red $gr, blue $gb');
150 }
151 if (table != 0 && (_strips[table] == null || !_look.gradesThroughLut)) {
152 throw StateError('the table did not reach the device');
153 }
154 // The composite bends the frame and reads the table.
155 if (!passRan(frame, 'composite')) {
156 throw StateError('the composite pass is absent');
157 }
158 }
159}