Clustered lights: the source
Open the live demo · Read the guide · View on GitHub
Regions the guide quotes: floor (line 43), lights (line 56), settings (line 81).
1/// Clustered lights: every fragment is lit by the lights that reach its part
2/// of the view, so a floor under sixty-four lamps is lit by all of them.
3///
4/// Quoted by `clustered_lights.md` and shown whole in the Source tab.
5library;
7import 'dart:math' as math;
9import 'package:flutter3d/flutter3d.dart';
10import 'package:flutter3d_showcase/src/demo/demo.dart';
12/// The `clustered-lights` page: an eight by eight grid of coloured point
13/// lamps over a white floor. Its check is that a per-draw light list would
14/// leave 32 of the 64 out, which is the loss the cells take back.
15final class ClusteredLightsDemo extends ShowcaseDemo {
16 /// Whether the frame sorts lights into clusters; the "Clustered lights"
17 /// switch, read into `RenderSettings.clusteredLights` every frame.
18 bool clustered = true;
20 /// How far each lamp reaches, in metres, from 0.4 to 3.0 on the "Light
21 /// range" slider; written into every lamp's `range` each frame.
22 double range = 0.8;
24 final List<LightNode> _lights = <LightNode>[];
27 void configureView(DemoContext context) {
28 context.orbit
29 ..distance = 9.8
30 ..pitch = 1.15
31 ..yaw = 0.0;
32 }
34 /// A saturated colour at [t] of the way round the hue circle.
35 static Vector3 _hue(double t) {
36 double channel(double offset) =>
37 (0.5 + 0.5 * math.cos(2 * math.pi * (t + offset))).clamp(0.0, 1.0);
38 return Vector3(channel(0.0), channel(2 / 3), channel(1 / 3));
39 }
42 Scene build(DemoContext context) {
43 final Scene scene = Scene()
44 ..ambientIntensity = 0.03 * LightNode.legacyUnit
45 ..add(
46 MeshNode(
47 DeviceMesh.upload(
48 context.device,
49 CuboidShape(size: Vector3(12.0, 0.1, 12.0)).build(),
50 ),
51 RenderMaterial(name: 'floor', lighting: LightingModel.lambert),
52 name: 'floor',
53 )..setPosition(0.0, -0.05, 0.0),
54 );
56 for (var i = 0; i < 8; i++) {
57 for (var j = 0; j < 8; j++) {
58 final LightNode light = LightNode(
59 name: 'lamp $i $j',
60 type: LightType.point,
61 intensity: 1.5 * LightNode.legacyUnit,
62 range: range,
63 color: _hue((i * 8 + j) / 64.0).toLinearColor(),
64 )..setPosition(i - 3.5, 0.3, j - 3.5);
65 _lights.add(light);
66 scene.add(light);
67 }
68 }
69 return scene;
70 }
73 void update(DemoContext context, double dt) {
74 for (final LightNode light in _lights) {
75 light.range = range;
76 }
77 }
80 RenderSettings settings(DemoContext context) => RenderSettings(
81 clusteredLights: clustered,
82 );
85 List<DemoControl> controls(DemoContext context) => <DemoControl>[
86 ToggleControl(
87 'Clustered lights',
88 value: () => clustered,
89 onChanged: (bool v) => clustered = v,
90 ),
91 SliderControl(
92 'Light range',
93 min: 0.4,
94 max: 3.0,
95 value: () => range,
96 onChanged: (double v) => range = v,
97 format: (double v) => '${v.toStringAsFixed(1)} m',
98 ),
99 ];
102 void verify(Scene scene, FrameResult frame) {
103 if (scene.lights.length != 64) {
104 throw StateError('the grid should hold 64 lights');
105 }
106 if (!clustered) {
107 throw StateError('the page should start with clusteredLights on');
108 }
109 // The per-draw list reports the same loss with the cells on or off: 64
110 // lights less eight slots and a tail of twenty-four. Those 32 are the
111 // lamps the floor loses without the cells.
112 if (frame.lightsDropped != 32) {
113 throw StateError(
114 'a per-draw list should leave 32 of the 64 lamps out, '
115 'the frame reports ${frame.lightsDropped}',
116 );
117 }
118 }
119}