Local exposure: the source
Open the live demo · Read the guide · View on GitHub
Regions the guide quotes: room (line 44), outside (line 103), settings (line 124), ran (line 166).
1/// Local exposure: each part of the frame given the exposure that shows it
2/// best, so a dark room and its bright window can both be seen.
3///
4/// Quoted by `local_exposure.md` and shown whole in the Source tab.
5library;
7import 'package:flutter3d/flutter3d.dart';
8import 'package:flutter3d_showcase/pages/post/post_stage.dart';
9import 'package:flutter3d_showcase/src/demo/demo.dart';
11/// The `local-exposure` page: a dark plastered room with a bright window of
12/// sky. Its check is that the pass does not run while it is off.
13final class LocalExposureDemo extends ShowcaseDemo {
14 /// Whether local exposure is on.
15 bool local = true;
17 /// How much of the local exposure is applied, 0 to 1.
18 double strength = 1.0;
20 /// How many stops the shadows may be lifted, 0 to 4.
21 double shadowStops = 2.0;
23 /// How many stops the highlights may be pulled down, 0 to 4.
24 double highlightStops = 2.0;
27 void configureView(DemoContext context) {
28 context.orbit
29 ..distance = 5.7
30 ..pitch = 0.07
31 ..yaw = 0.18;
32 context.orbit.target.setValues(-0.2, 1.1, -3.0);
33 }
36 Scene build(DemoContext context) {
37 final GraphicsDevice device = context.device;
38 MeshNode slab(Vector3 size, Vector3 at, RenderMaterial material) =>
39 MeshNode(
40 DeviceMesh.upload(device, CuboidShape(size: size).build()),
41 material,
42 )..setPositionFrom(at);
44 final RenderMaterial plaster = RenderMaterial(
45 name: 'plaster',
46 baseColor: LinearColor.fromSrgb(0.75, 0.72, 0.68, 1.0),
47 roughness: 0.9,
48 );
49 // A room six metres square and three high, with walls half a metre
50 // thick. Every slab runs past the ones it meets, so no corner is a seam.
51 const double t = 0.5;
52 const double outer = 6 + 2 * t;
53 const double side = 3 + t / 2;
54 // The window, a metre and a half wide, from 0.9 m to 2.3 m up.
55 const double half = 0.75, sill = 0.9, head = 2.3;
56 const double jamb = 3 + t - half;
57 final Scene scene = Scene()
58 ..add(slab(Vector3(outer, t, outer), Vector3(0, -t / 2, 0), plaster))
59 ..add(slab(Vector3(outer, t, outer), Vector3(0, 3 + t / 2, 0), plaster))
60 ..add(slab(Vector3(t, 3 + 2 * t, outer), Vector3(-side, 1.5, 0), plaster))
61 ..add(slab(Vector3(t, 3 + 2 * t, outer), Vector3(side, 1.5, 0), plaster))
62 // The back wall, in four pieces around the window.
63 ..add(
64 slab(
65 Vector3(jamb, 3 + 2 * t, t),
66 Vector3(-(half + jamb / 2), 1.5, -side),
67 plaster,
68 ),
69 )
70 ..add(
71 slab(
72 Vector3(jamb, 3 + 2 * t, t),
73 Vector3(half + jamb / 2, 1.5, -side),
74 plaster,
75 ),
76 )
77 ..add(
78 slab(
79 Vector3(2 * half, sill + t, t),
80 Vector3(0, (sill - t) / 2, -side),
81 plaster,
82 ),
83 )
84 ..add(
85 slab(
86 Vector3(2 * half, 3 + t - head, t),
87 Vector3(0, (head + 3 + t) / 2, -side),
88 plaster,
89 ),
90 )
91 // A crate in the dark half of the room.
92 ..add(
93 slab(
94 Vector3(0.8, 0.8, 0.8),
95 Vector3(-1.8, 0.4, -1.2),
96 RenderMaterial(
97 name: 'crate',
98 baseColor: LinearColor.fromSrgb(0.6, 0.25, 0.15, 1.0),
99 ),
100 ),
101 );
103 final MeshNode sky = slab(
104 Vector3(8.0, 8.0, 0.1),
105 Vector3(0.0, 1.5, -5.0),
106 RenderMaterial(
107 name: 'sky',
108 baseColor: LinearColor.fromSrgb(0.0, 0.0, 0.0, 1.0),
109 emissive: LinearColor(5.0, 6.0, 8.0),
110 ),
111 )..shadowCasting = ShadowCastingMode.off;
112 // No lamp and no sun: the room has only a weak ambient light, and the
113 // engine is told not to add a light of its own to a scene that has none.
114 scene
115 ..ambientIntensity = 0.12 * LightNode.legacyUnit
116 ..defaultLightWhenUnlit = false;
117 return scene..add(sky);
118 }
121 RenderSettings settings(DemoContext context) => RenderSettings(
122 exposure: 1.0,
123 bloom: const BloomSettings(enabled: false),
124 localExposure: LocalExposureSettings(
125 enabled: local,
126 strength: strength,
127 shadowStops: shadowStops,
128 highlightStops: highlightStops,
129 ),
130 );
133 List<DemoControl> controls(DemoContext context) => <DemoControl>[
134 ToggleControl(
135 'Local exposure',
136 value: () => local,
137 onChanged: (bool v) => local = v,
138 ),
139 SliderControl(
140 'Strength',
141 min: 0,
142 max: 1,
143 value: () => strength,
144 onChanged: (double v) => strength = v,
145 ),
146 SliderControl(
147 'Shadow stops',
148 min: 0,
149 max: 4,
150 value: () => shadowStops,
151 onChanged: (double v) => shadowStops = v,
152 format: (double v) => '+${v.toStringAsFixed(1)}',
153 ),
154 SliderControl(
155 'Highlight stops',
156 min: 0,
157 max: 4,
158 value: () => highlightStops,
159 onChanged: (double v) => highlightStops = v,
160 format: (double v) => '-${v.toStringAsFixed(1)}',
161 ),
162 ];
165 void verify(Scene scene, FrameResult frame) {
166 if (local && strength > 0) {
167 expectPassOrDecline(frame, 'local exposure');
168 } else if (passRan(frame, 'local exposure')) {
169 throw StateError('local exposure is off and its pass ran');
170 }
171 }
172}