flutter3d
Showcase Changelog 62 packages API reference

Tone-map curves: the source

since 0.7.0 Post-processing

Regions the guide quotes: lamps (line 43), upload (line 69), table (line 79), exposure (line 123), curve (line 124), display (line 125).

1/// The curve that squeezes light of any brightness into a picture a display can
2/// show, the curves the engine has, and a display transform baked by the page.
3///
4/// Quoted by `tone_mapping.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_showcase/pages/post/post_stage.dart';
12import 'package:flutter3d_showcase/src/demo/demo.dart';
14/// The `tone-mapping` page: a row of lamps of rising brightness through the
15/// engine's curves. Its check is that the composite pass ran at the
16/// exposure asked for.
17final class ToneMappingDemo extends ShowcaseDemo {
18 /// Which of the engine's curves the composite applies, an index into the
19 /// "Curve" choice.
20 int curve = 0;
22 /// Which display transform follows the curve: 0 none, 1 the table the page
23 /// bakes.
24 int display = 0;
25 // The engine's own default, so the page opens on the exposure every other
26 // scene is drawn at.
27 /// The frame's exposure, 0.2 to 6; starts at
28 /// `RenderSettings.defaultExposure`.
29 double exposure = RenderSettings.defaultExposure;
31 static const List<TonemapCurve> _curves = TonemapCurve.values;
33 late final TextureHandle _table;
35 @override
36 void configureView(DemoContext context) =>
37 PostStage.frame(context, distance: 7.0);
39 @override
40 Scene build(DemoContext context) {
41 final PostStage stage = PostStage.build(context, sunIntensity: 6.0);
43 // The lamps light nothing, so they hang on the side the sun comes from,
44 // where the lit faces of the shapes already look.
45 final DeviceMesh ball = DeviceMesh.upload(
46 context.device,
47 const SphereShape(radius: 0.28, segments: 24, rings: 12).build(),
48 );
49 const List<List<double>> colors = <List<double>>[
50 <double>[1.0, 0.1, 0.05],
51 <double>[0.1, 0.9, 0.2],
52 <double>[0.1, 0.25, 1.0],
53 ];
54 for (var i = 0; i < colors.length; i++) {
55 stage.scene.add(
56 MeshNode(
57 ball,
58 RenderMaterial(
59 name: 'lamp $i',
60 baseColor: LinearColor.fromSrgb(0.05, 0.05, 0.05, 1.0),
61 emissive: LinearColor(colors[i][0], colors[i][1], colors[i][2]),
62 emissiveStrength: 6.0 * RenderMaterial.defaultEmissiveStrength,
63 ),
64 name: 'lamp $i',
65 )..setPosition((i - 1) * 1.1 + 0.5, 2.3, 0.8),
66 );
67 }
69 _table = context.device.createTextureFromPixels(
70 width: _size * _size,
71 height: _size,
72 format: TextureFormat.r16g16b16a16Float,
73 pixels: _bake(),
74 );
76 return stage.scene;
77 }
79 /// Entries per axis of the page's own table.
80 static const int _size = 17;
82 /// A display transform in the strip shape the engine reads: `_size` slices
83 /// of `_size` by `_size`, blue picking the slice, red across and green down.
84 /// Entry `i` holds the output for the input `0.18 * 2^(-10 + 20 * i / 16)`.
85 ///
86 /// The curve is Reinhard on the brightest channel, with the other two
87 /// scaled by the same amount so the hue holds. Nothing here walks towards
88 /// white: a bright red stays red however bright it gets.
89 static ByteData _bake() {
90 final ByteData strip = ByteData(_size * _size * _size * 8);
91 double input(int i) =>
92 0.18 * math.pow(2.0, -10.0 + 20.0 * i / (_size - 1)).toDouble();
93 for (var b = 0; b < _size; b++) {
94 for (var g = 0; g < _size; g++) {
95 for (var r = 0; r < _size; r++) {
96 final List<double> color = <double>[input(r), input(g), input(b)];
97 final double peak = color.reduce(math.max);
98 final double scale = 1.0 / (1.0 + peak);
99 final int at = (g * _size * _size + b * _size + r) * 8;
100 for (var c = 0; c < 3; c++) {
101 strip.setUint16(at + c * 2, _half(color[c] * scale), Endian.little);
102 }
103 strip.setUint16(at + 6, _half(1.0), Endian.little);
104 }
105 }
106 }
107 return strip;
108 }
110 /// [value], from 0 to 1, as the bits of a half float.
111 ///
112 /// It truncates rather than rounds and flushes subnormals to nought, which
113 /// is harmless here: the error is under a thousandth of a display value.
114 static int _half(double value) {
115 final int bits = (ByteData(4)..setFloat32(0, value)).getUint32(0);
116 final int exponent = ((bits >> 23) & 0xff) - 127 + 15;
117 if (exponent <= 0) return 0;
118 return (exponent << 10) | ((bits >> 13) & 0x3ff);
119 }
121 @override
122 RenderSettings settings(DemoContext context) => RenderSettings(
123 exposure: exposure,
124 tonemapCurve: _curves[curve],
125 look: LookSettings(
126 displayTransform: display == 1
127 ? DisplayTransform(texture: _table, size: _size)
128 : null,
129 ),
130 );
132 @override
133 List<DemoControl> controls(DemoContext context) => <DemoControl>[
134 ChoiceControl(
135 'Curve',
136 options: <String>[for (final TonemapCurve c in _curves) c.name],
137 index: () => curve,
138 onChanged: (int i) => curve = i,
139 ),
140 ChoiceControl(
141 'Display transform',
142 options: const <String>['none', 'page table'],
143 index: () => display,
144 onChanged: (int i) => display = i,
145 ),
146 SliderControl(
147 'Exposure',
148 min: 0.2,
149 max: 6,
150 value: () => exposure,
151 onChanged: (double v) => exposure = v,
152 ),
153 ];
155 @override
156 void verify(Scene scene, FrameResult frame) {
157 if (!passRan(frame, 'composite')) {
158 throw StateError(
159 'the composite pass, which applies the curve, is absent',
160 );
161 }
162 if ((frame.exposure - exposure).abs() > 1e-6) {
163 throw StateError('the frame was exposed at ${frame.exposure}');
164 }
165 }