flutter3d
Showcase Changelog 62 packages API reference

Grade through a LUT: the source

since 0.7.0 Post-processing

Regions the guide quotes: table (line 33), apply (line 80).

1/// A colour grade held in a table: every colour goes in, the colour the table
2/// says comes out.
3///
4/// Quoted by `lut_grading.md` and shown whole in the Source tab.
5library;
6
7import 'dart:typed_data';
8
9import 'package:flutter3d/flutter3d.dart';
10import 'package:flutter3d_core/flutter3d_core.dart' show buildIdentityLut;
11import 'package:flutter3d_showcase/pages/post/post_stage.dart';
12import 'package:flutter3d_showcase/src/demo/demo.dart';
14/// The `lut-grading` page: the post stage graded through a colour table.
15/// Its check is that the table will be sampled and the composite ran.
16final class LutGradingDemo extends ShowcaseDemo {
17 /// Which table grades the frame: 0 sepia, 1 red and blue swapped, 2 night.
18 int table = 0;
20 /// How much of the table's colour replaces the original, 0 to 1.
21 double strength = 1.0;
23 late final List<TextureHandle> _tables;
24 late LookSettings _look;
26 static const int _size = 17;
28 @override
29 void configureView(DemoContext context) => PostStage.frame(context);
31 @override
32 Scene build(DemoContext context) {
33 final Uint8List identity = buildIdentityLut(size: _size);
34 final List<Uint8List> looks = <Uint8List>[
35 // Sepia: every colour becomes a brown of the same brightness.
36 _remap(identity, (double r, double g, double b) {
37 final double l = 0.3 * r + 0.59 * g + 0.11 * b;
38 return <double>[l * 1.15, l * 0.95, l * 0.7];
39 }),
40 // Red and blue swapped, which nobody would mistake for the original.
41 _remap(identity, (double r, double g, double b) => <double>[b, g, r]),
42 // Night: dark, with the red taken out.
43 _remap(identity, (double r, double g, double b) {
44 return <double>[r * 0.3, g * 0.55, b * 0.9];
45 }),
46 ];
47 _tables = <TextureHandle>[
48 for (final Uint8List pixels in looks)
49 context.device.createTextureFromPixels(
50 width: _size * _size,
51 height: _size,
52 format: TextureFormat.r8g8b8a8UNormInt,
53 pixels: ByteData.sublistView(pixels),
54 ),
55 ];
56 return PostStage.build(context).scene;
57 }
59 /// A copy of [source] with every entry passed through [change].
60 static Uint8List _remap(
61 Uint8List source,
62 List<double> Function(double r, double g, double b) change,
63 ) {
64 final Uint8List out = Uint8List.fromList(source);
65 for (var i = 0; i < out.length; i += 4) {
66 final List<double> mapped = change(
67 out[i] / 255.0,
68 out[i + 1] / 255.0,
69 out[i + 2] / 255.0,
70 );
71 for (var c = 0; c < 3; c++) {
72 out[i + c] = (mapped[c].clamp(0.0, 1.0) * 255).round();
73 }
74 }
75 return out;
76 }
78 @override
79 RenderSettings settings(DemoContext context) {
80 _look = LookSettings(lut: _tables[table], lutStrength: strength);
81 return RenderSettings(look: _look);
82 }
84 @override
85 List<DemoControl> controls(DemoContext context) => <DemoControl>[
86 ChoiceControl(
87 'Table',
88 options: const <String>['Sepia', 'Red and blue swapped', 'Night'],
89 index: () => table,
90 onChanged: (int i) => table = i,
91 ),
92 SliderControl(
93 'Strength',
94 min: 0,
95 max: 1,
96 value: () => strength,
97 onChanged: (double v) => strength = v,
98 ),
99 ];
101 @override
102 void verify(Scene scene, FrameResult frame) {
103 if (!_look.gradesThroughLut) {
104 throw StateError('the table would not be sampled this frame');
105 }
106 if (!passRan(frame, 'composite')) {
107 throw StateError('the composite pass, which reads the table, is absent');
108 }
109 }