The field kept current: the source
Open the live demo · Read the guide · View on GitHub
Regions the guide quotes: room (line 45), field (line 90), live (line 106).
1/// The irradiance field kept current by the renderer: a few probes a frame
2/// look at the room again, so the bounce follows a wall that changes colour.
3///
4/// Quoted by `irradiance_updates.md` and shown whole in the Source tab.
5library;
7import 'package:flutter3d/flutter3d.dart';
8import 'package:flutter3d_showcase/src/demo/demo.dart';
10/// The `irradiance-updates` page: a room with one wall whose colour can be
11/// changed and a lamp, its irradiance field refreshed a few probes a frame.
12/// Its check is that the update pass ran and refreshed at least one probe.
13final class IrradianceUpdatesDemo extends ShowcaseDemo {
14 /// The painted wall's colour: 0 red, 1 green, 2 blue.
15 int wallColour = 0;
17 /// How many probes the renderer refreshes each frame, 1 to 8 in whole
18 /// steps; rounded into the field's `gpuUpdates`.
19 double probesPerFrame = 2;
21 /// How much of a probe's old value survives each refresh, 0 to 0.99: higher
22 /// is steadier and slower to follow the wall.
23 double hysteresis = 0.9;
25 late final RenderMaterial _paint;
26 late final IrradianceField _field;
28 static final List<Vector4> _colors = <Vector4>[
29 Vector4(0.85, 0.08, 0.08, 1.0),
30 Vector4(0.08, 0.7, 0.12, 1.0),
31 Vector4(0.1, 0.2, 0.85, 1.0),
32 ];
35 void configureView(DemoContext context) {
36 context.orbit
37 ..distance = 6.0
38 ..pitch = 0.35
39 ..yaw = 0.5;
40 context.orbit.target.setValues(-0.5, 1.0, 0.0);
41 }
44 Scene build(DemoContext context) {
45 final RenderMaterial floor = RenderMaterial(
46 name: 'floor',
47 baseColor: LinearColor.fromSrgb(0.55, 0.55, 0.55, 1.0),
48 roughness: 0.95,
49 doubleSided: true,
50 );
51 _paint = RenderMaterial(
52 name: 'painted wall',
53 baseColor: _fromSrgb(_colors[wallColour].clone()),
54 roughness: 0.95,
55 doubleSided: true,
56 );
57 final Scene scene = Scene()
58 ..ambientIntensity = 1.0 * LightNode.legacyUnit
59 ..add(
60 MeshNode(
61 DeviceMesh.upload(
62 context.device,
63 const PlaneShape(width: 6, depth: 6).build(),
64 ),
65 floor,
66 name: 'floor',
67 ),
68 )
69 ..add(
70 MeshNode(
71 DeviceMesh.upload(
72 context.device,
73 const PlaneShape(width: 3, depth: 4).build(),
74 ),
75 _paint,
76 name: 'wall',
77 )
78 ..setPosition(-2.0, 1.5, 0.0)
79 ..setRotation(Quaternion.axisAngle(Vector3(0.0, 0.0, 1.0), -1.5708)),
80 )
81 ..add(
82 LightNode(
83 name: 'lamp',
84 type: LightType.point,
85 intensity: 30.0 * LightNode.legacyUnit,
86 range: 12.0,
87 )..setPosition(-0.8, 2.5, 0.0),
88 );
90 _field =
91 IrradianceField(
92 origin: Vector3(-1.0, 0.5, -1.0),
93 spacing: Vector3(1.0, 1.0, 1.0),
94 countX: 2,
95 countY: 2,
96 countZ: 2,
97 )
98 ..gpuUpdates = probesPerFrame.round()
99 ..fillGutters();
100 scene.irradianceField = _field;
101 return scene;
102 }
105 void update(DemoContext context, double dt) {
106 _paint.baseColor = _fromSrgb(_colors[wallColour]);
107 _field
108 ..gpuUpdates = probesPerFrame.round()
109 ..hysteresis = hysteresis;
110 }
113 List<DemoControl> controls(DemoContext context) => <DemoControl>[
114 ChoiceControl(
115 'Wall colour',
116 options: const <String>['Red', 'Green', 'Blue'],
117 index: () => wallColour,
118 onChanged: (int i) => wallColour = i,
119 ),
120 SliderControl(
121 'Probes a frame',
122 min: 1,
123 max: 8,
124 divisions: 7,
125 value: () => probesPerFrame,
126 onChanged: (double v) => probesPerFrame = v,
127 format: (double v) => '${v.round()}',
128 ),
129 SliderControl(
130 'Hysteresis',
131 min: 0,
132 max: 0.99,
133 value: () => hysteresis,
134 onChanged: (double v) => hysteresis = v,
135 ),
136 ];
139 void verify(Scene scene, FrameResult frame) {
140 final PassSkip? skipped = frame.skipReasonOf('irradiance update');
141 if (skipped != null) {
142 throw StateError('the probe update was skipped: $skipped');
143 }
144 if (!frame.passes.any((FramePass p) => p.name == 'irradiance update')) {
145 throw StateError('no probe was updated this frame');
146 }
147 }
148}
150/// A `Vector4` holding a colour sRGB-encoded, as the linear colour it names.
151LinearColor _fromSrgb(Vector4 c) => LinearColor.fromSrgb(c.x, c.y, c.z, c.w);