flutter3d
Showcase Changelog 62 packages API reference

Rectangle area lights: the source

since 0.7.0 Shading and materials

Regions the guide quotes: window (line 47), pane (line 60), room (line 79), floor (line 96), live (line 120), check (line 159).

1/// `LightType.area`: a rectangle with extent, so a room reads as lit by a
2/// window rather than by a bright dot painted behind one, and a polished
3/// floor shows the window's shape.
4///
5/// Quoted by `area_lights.md` and shown whole in the Source tab.
6library;
7
8import 'dart:math' as math;
9
10import 'package:flutter3d/flutter3d.dart';
11import 'package:flutter3d_showcase/src/demo/demo.dart';
13/// The `area-lights` page: a room lit by a window that is an area light,
14/// over a polished floor. Its check is that the window is an area light whose
15/// size and the floor's roughness track the sliders.
16final class AreaLightsDemo extends ShowcaseDemo {
17 /// The window's width in metres, 0.5 to 4.
18 double width = 2.5;
20 /// The window's height in metres, 0.5 to 3; the light is dimmed as width
21 /// times height grows, so the room stays as bright.
22 double height = 1.5;
24 /// The floor's roughness, 0.05 to 1: low shows the window's shape in it.
25 double floorRoughness = 0.25;
27 late final LightNode _window;
28 late final MeshNode _pane;
29 late final RenderMaterial _glass;
30 late final RenderMaterial _floorMaterial;
32 /// A plane is built lying down, facing +Y; a quarter turn about X stands
33 /// it up facing +Z, into the room.
34 static Quaternion get _standUp =>
35 Quaternion.axisAngle(Vector3(1.0, 0.0, 0.0), math.pi / 2);
37 @override
38 void configureView(DemoContext context) {
39 context.orbit
40 ..distance = 7.5
41 ..yaw = 0.45
42 ..pitch = 0.4;
43 }
45 @override
46 Scene build(DemoContext context) {
47 // The panel faces the node's local -Z. Set in the back wall and aimed
48 // at a point in front of it, it shines into the room.
49 _window =
50 LightNode(
51 name: 'window',
52 type: LightType.area,
53 intensity: 8.0 * LightNode.legacyUnit,
54 )
55 ..width = width
56 ..height = height
57 ..setPosition(0.0, 0.3, -1.45);
58 _window.lookAt(Vector3(0.0, 0.3, 3.0));
60 // The light itself is not drawn. A glowing rectangle of the same size,
61 // just behind it, is what a person sees as the window.
62 _glass = RenderMaterial(
63 name: 'window glass',
64 baseColor: LinearColor.fromSrgb(0.0, 0.0, 0.0, 1.0),
65 emissive: LinearColor(3.0, 3.0, 2.8),
66 );
67 _pane =
68 MeshNode(
69 DeviceMesh.upload(
70 context.device,
71 const PlaneShape(width: 1, depth: 1).build(),
72 ),
73 _glass,
74 name: 'window pane',
75 )
76 ..setRotation(_standUp)
77 ..setPosition(0.0, 0.3, -1.48);
79 final MeshNode backWall =
80 MeshNode(
81 DeviceMesh.upload(
82 context.device,
83 const PlaneShape(width: 6, depth: 4).build(),
84 ),
85 RenderMaterial(
86 name: 'wall',
87 baseColor: LinearColor.fromSrgb(0.6, 0.58, 0.55, 1.0),
88 roughness: 0.9,
89 doubleSided: true,
90 ),
91 name: 'back wall',
92 )
93 ..setRotation(_standUp)
94 ..setPosition(0.0, 0.0, -1.5);
96 // A floor of its own, so its roughness can go down to a polish.
97 _floorMaterial = RenderMaterial(
98 name: 'floor',
99 baseColor: LinearColor.fromSrgb(0.45, 0.45, 0.47, 1.0),
100 roughness: floorRoughness,
101 );
102 final MeshNode floor = MeshNode(
103 DeviceMesh.upload(
104 context.device,
105 const PlaneShape(width: 6, depth: 5).build(),
106 ),
107 _floorMaterial,
108 name: 'floor',
109 )..setPosition(0.0, -1.8, 1.0);
111 return Scene()
112 ..add(backWall)
113 ..add(_pane)
114 ..add(floor)
115 ..add(_window);
116 }
118 @override
119 void update(DemoContext context, double dt) {
120 _window
121 ..width = width
122 ..height = height;
123 _pane.setScale(width, 1.0, height);
124 // The light's radiance is its intensity over its area, so a wider
125 // window is dimmer per square metre. The pane dims by the same ratio,
126 // measured from the 2.5 by 1.5 it starts at.
127 final double dim = (2.5 * 1.5) / (width * height);
128 _glass.emissive = LinearColor(3.0 * dim, 3.0 * dim, 2.8 * dim);
129 _floorMaterial.roughness = floorRoughness;
130 }
132 @override
133 List<DemoControl> controls(DemoContext context) => <DemoControl>[
134 SliderControl(
135 'Width',
136 min: 0.5,
137 max: 4,
138 value: () => width,
139 onChanged: (double v) => width = v,
140 ),
141 SliderControl(
142 'Height',
143 min: 0.5,
144 max: 3,
145 value: () => height,
146 onChanged: (double v) => height = v,
147 ),
148 SliderControl(
149 'Floor roughness',
150 min: 0.05,
151 max: 1,
152 value: () => floorRoughness,
153 onChanged: (double v) => floorRoughness = v,
154 ),
155 ];
157 @override
158 void verify(Scene scene, FrameResult frame) {
159 if (_window.type != LightType.area) {
160 throw StateError('the window is not an area light');
161 }
162 if (_window.width != width || _window.height != height) {
163 throw StateError('the panel size did not track the sliders');
164 }
165 final Vector3 halfWidth = _window.readHalfWidth();
166 final Vector3 halfHeight = _window.readHalfHeight();
167 if (halfWidth.length2 == 0.0 || halfHeight.length2 == 0.0) {
168 throw StateError('the panel has no extent to shade against');
169 }
170 if (_floorMaterial.roughness != floorRoughness) {
171 throw StateError('the floor roughness did not track its slider');
172 }
173 if (frame.drawCalls < 3) {
174 throw StateError('the room was not drawn');
175 }
176 }