High contrast: the source
Open the live demo · Read the guide · View on GitHub
Regions the guide quotes: level (line 40), roles (line 92), look (line 134), check (line 176).
1/// The high-contrast look: texture flattened inside each surface, the frame
2/// drained toward grey, every edge drawn, and a ring in its role's colour
3/// around each thing a player has to find.
4///
5/// Quoted by `high_contrast.md` and shown whole in the Source tab.
6library;
8import 'package:flutter3d/flutter3d.dart';
9import 'package:flutter3d_showcase/src/demo/demo.dart';
11/// The `high-contrast` page: tiles and crates in the high-contrast look,
12/// each crate outlined in its role's colour. Its check is that the look pass
13/// and the outline mask run exactly when they should.
14final class HighContrastDemo extends ShowcaseDemo {
15 /// Whether the high-contrast look is on.
16 bool look = true;
18 /// Whether each crate's outline takes its role's colour.
19 bool roles = true;
21 /// How much colour the look keeps, 0 to 1.
22 double saturation = 0.25;
24 /// The look's contrast, 1 to 3.
25 double contrast = 1.5;
27 final List<(MeshNode, Vector3)> _marked = <(MeshNode, Vector3)>[];
30 void configureView(DemoContext context) {
31 context.orbit
32 ..distance = 9.0
33 ..pitch = 0.5
34 ..yaw = 0.4;
35 context.orbit.target.setValues(0.0, 0.5, 0.0);
36 }
39 Scene build(DemoContext context) {
40 // A busy floor and some crates in colours of their own: the detail the
41 // look is there to take away.
42 const CheckerboardTexture tiles = CheckerboardTexture(
43 size: 32,
44 cell: 2,
45 light: 0x9A8F7A,
46 dark: 0x5E6B48,
47 );
48 final Scene scene = Scene()
49 ..ambientIntensity = 0.35 * LightNode.legacyUnit
50 ..add(
51 MeshNode(
52 DeviceMesh.upload(
53 context.device,
54 const PlaneShape(width: 12, depth: 12).build(),
55 ),
56 RenderMaterial(
57 name: 'tiles',
58 albedo: tiles.upload(context.device),
59 albedoSampler: SamplerDescriptor.linearRepeat,
60 roughness: 0.9,
61 ),
62 name: 'floor',
63 ),
64 )
65 ..add(
66 LightNode(name: 'sun', intensity: 2.5 * LightNode.legacyUnit)
67 ..setLocalForward(Vector3(-0.4, -0.8, -0.3)),
68 );
69 final DeviceMesh crate = DeviceMesh.upload(
70 context.device,
71 CuboidShape(size: Vector3.all(1.0)).build(),
72 );
73 final List<Vector4> paint = <Vector4>[
74 Vector4(0.7, 0.35, 0.2, 1.0),
75 Vector4(0.25, 0.45, 0.7, 1.0),
76 Vector4(0.55, 0.6, 0.25, 1.0),
77 ];
78 for (var i = 0; i < 3; i++) {
79 scene.add(
80 MeshNode(
81 crate,
82 RenderMaterial(
83 name: 'crate $i',
84 baseColor: _fromSrgb(paint[i]),
85 roughness: 0.7,
86 ),
87 name: 'crate $i',
88 )..setPosition(-3.0 + i * 1.2, 0.5, -2.5),
89 );
90 }
92 // Three things the player has to find, each in the colour its role has:
93 // a monster, a pickup and the way out. The colours are display colours,
94 // as a swatch in the game's settings gives them.
95 final DeviceMesh ball = DeviceMesh.upload(
96 context.device,
97 SphereShape(radius: 0.5, segments: 24, rings: 12).build(),
98 );
99 final List<(String, Vector3, Vector3)> things =
100 <(String, Vector3, Vector3)>[
101 ('monster', Vector3(1.0, 0.2, 0.2), Vector3(1.8, 0.5, 0.5)),
102 ('pickup', Vector3(1.0, 0.85, 0.1), Vector3(-1.2, 0.5, 1.6)),
103 ('exit', Vector3(0.2, 0.9, 0.4), Vector3(3.2, 0.5, -2.2)),
104 ];
105 for (final (String role, Vector3 color, Vector3 at) in things) {
106 final MeshNode node = MeshNode(
107 ball,
108 RenderMaterial(
109 name: role,
110 baseColor: LinearColor.fromSrgb(0.6, 0.6, 0.6, 1.0),
111 ),
112 name: role,
113 )..setPositionFrom(at);
114 // The role colours are picked by eye, so they are sRGB.
115 node.outlineColor = LinearColor.fromSrgb(color.x, color.y, color.z);
116 _marked.add((node, color));
117 scene.add(node);
118 }
119 return scene;
120 }
123 void update(DemoContext context, double dt) {
124 // A mark is per mesh; taking it off is setting it back to null.
125 for (final (MeshNode node, Vector3 color) in _marked) {
126 node.outlineColor = roles
127 ? LinearColor.fromSrgb(color.x, color.y, color.z)
128 : null;
129 }
130 }
133 RenderSettings settings(DemoContext context) => RenderSettings(
134 highContrast: HighContrastSettings(
135 enabled: look,
136 saturation: saturation,
137 contrast: contrast,
138 outlineWidth: 1.0,
139 roleWidth: 3.0,
140 roleFill: 0.3,
141 ),
142 );
145 List<DemoControl> controls(DemoContext context) => <DemoControl>[
146 ToggleControl(
147 'High contrast',
148 value: () => look,
149 onChanged: (bool v) => look = v,
150 ),
151 ToggleControl(
152 'Role outlines',
153 value: () => roles,
154 onChanged: (bool v) => roles = v,
155 ),
156 SliderControl(
157 'Saturation',
158 min: 0,
159 max: 1,
160 value: () => saturation,
161 onChanged: (double v) => saturation = v,
162 format: (double v) => v.toStringAsFixed(2),
163 ),
164 SliderControl(
165 'Contrast',
166 min: 1,
167 max: 3,
168 value: () => contrast,
169 onChanged: (double v) => contrast = v,
170 format: (double v) => v.toStringAsFixed(2),
171 ),
172 ];
175 void verify(Scene scene, FrameResult frame) {
176 // The look is one pass on the finished picture, and the rings are a mask
177 // drawn before it from the marked nodes alone. On, with marks, both run;
178 // off, neither does, which is what "an exact no-op" means here.
179 bool ran(String name) => frame.passes.any((FramePass p) => p.name == name);
180 final bool marked = scene.meshes.any(
181 (MeshNode m) => m.outlineColor != null,
182 );
183 if (ran('high contrast') != look) {
184 throw StateError(
185 'the look pass ran: ${ran('high contrast')}, look: $look',
186 );
187 }
188 if (ran('outline mask') != (look && marked)) {
189 throw StateError(
190 'the outline mask ran: ${ran('outline mask')}; '
191 'look: $look, marked nodes: $marked, skipped: ${frame.skipped}',
192 );
193 }
194 }
195}
197/// A `Vector4` holding a colour sRGB-encoded, as the linear colour it names.
198LinearColor _fromSrgb(Vector4 c) => LinearColor.fromSrgb(c.x, c.y, c.z, c.w);