One bounce of diffuse light: the source
Open the live demo · Read the guide · View on GitHub
Regions the guide quotes: field (line 78), gather (line 86), live (line 91), shadow (line 97).
1/// One bounce of diffuse light: a grid of probes, each filled by casting rays
2/// out from it and recording what colour comes back.
3///
4/// Quoted by `irradiance_field.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-field` page: a floor between a red wall and a white one
11/// under a sun, lit a second time by a grid of probes. Its check is that the
12/// probe facing the red wall came out redder than the one facing away.
13final class IrradianceFieldDemo extends ShowcaseDemo {
14 /// Whether the scene reads the baked field; off leaves only the flat
15 /// ambient term.
16 bool baked = true;
18 /// The scene's ambient intensity as a multiple of
19 /// `LightNode.legacyUnit`, 0 to 2 on the "Ambient strength" slider.
20 double ambient = 0.5;
22 late final IrradianceField _field;
23 late final Scene _scene;
26 void configureView(DemoContext context) {
27 context.orbit
28 ..distance = 6.0
29 ..pitch = 0.35
30 ..yaw = 0.5;
31 context.orbit.target.setValues(-0.5, 1.0, 0.0);
32 }
35 Scene build(DemoContext context) {
36 final RenderMaterial floor = RenderMaterial(
37 name: 'floor',
38 baseColor: LinearColor.fromSrgb(0.55, 0.55, 0.55, 1.0),
39 roughness: 0.95,
40 doubleSided: true,
41 );
42 final RenderMaterial wall = RenderMaterial(
43 name: 'red wall',
44 baseColor: LinearColor.fromSrgb(0.85, 0.08, 0.08, 1.0),
45 roughness: 0.95,
46 doubleSided: true,
47 );
49 final MeshNode wallNode =
50 MeshNode(
51 DeviceMesh.upload(
52 context.device,
53 const PlaneShape(width: 3, depth: 4).build(),
54 ),
55 wall,
56 name: 'wall',
57 )
58 ..setPosition(-2.0, 1.5, 0.0)
59 ..setRotation(Quaternion.axisAngle(Vector3(0.0, 0.0, 1.0), -1.5708));
61 final Scene scene = _scene = Scene()
62 ..add(
63 MeshNode(
64 DeviceMesh.upload(
65 context.device,
66 const PlaneShape(width: 6, depth: 6).build(),
67 ),
68 floor,
69 name: 'floor',
70 ),
71 )
72 ..add(wallNode)
73 ..add(
74 LightNode(name: 'sun', intensity: 4.0 * LightNode.legacyUnit)
75 ..setLocalForward(Vector3(-0.6, -0.7, 0.15)),
76 );
78 _field = IrradianceField(
79 origin: Vector3(-1.0, 0.5, -1.0),
80 spacing: Vector3(1.0, 1.0, 1.0),
81 countX: 2,
82 countY: 2,
83 countZ: 2,
84 );
86 gather(_field, scene, rays: 96);
88 return scene;
89 }
92 void update(DemoContext context, double dt) {
93 _scene.irradianceField = baked ? _field : null;
94 _scene.ambientIntensity = ambient * LightNode.legacyUnit;
95 }
97 // A one-sided wall, not a closed brush. A pass that records only back
98 // faces sees straight through a plane like this one, so the floor beside
99 // the wall would never fall into shadow and the field's bounce light would
100 // have nothing but full sun to compete with. The sun records both sides by
101 // default since 0.8.2; named here because the shadow depends on it.
103 RenderSettings settings(DemoContext context) => const RenderSettings(
104 shadows: ShadowSettings(directionalCasterFaces: ShadowCasterFaces.both),
105 );
108 List<DemoControl> controls(DemoContext context) => <DemoControl>[
109 ToggleControl(
110 'Field on',
111 value: () => baked,
112 onChanged: (bool v) => baked = v,
113 ),
114 SliderControl(
115 'Ambient strength',
116 min: 0,
117 max: 2,
118 value: () => ambient,
119 onChanged: (double v) => ambient = v,
120 ),
121 ];
124 void verify(Scene scene, FrameResult frame) {
125 final LinearColor towardWall = _field.sample(
126 Vector3(-0.5, 1.0, -0.5),
127 Vector3(-1.0, 0.0, 0.0),
128 );
129 final LinearColor awayFromWall = _field.sample(
130 Vector3(-0.5, 1.0, -0.5),
131 Vector3(1.0, 0.0, 0.0),
132 );
133 if (towardWall.r < 0.01 || towardWall.r <= awayFromWall.r * 1.5) {
134 throw StateError(
135 'the probe facing the red wall ($towardWall) is not redder than the '
136 'one facing away from it ($awayFromWall); the bounce never arrived',
137 );
138 }
139 }
140}