flutter3d
Showcase Changelog 62 packages API reference

Thirty-two lights and the fade band: the source

since 0.7.0 Shading and materials

Regions the guide quotes: floor (line 49), torches (line 82), settings (line 99), check (line 115).

1/// Thirty-two lights on one draw: eight in the shader's own slots and
2/// twenty-four more through the light list, with `lightFadeBand` softening
3/// the edge between them. Tiled rather than one giant plane, so which
4/// torches reach a given tile is a real question — see `#region floor`.
5///
6/// Quoted by `many_lights.md` and shown whole in the Source tab.
7library;
8
9import 'dart:math' as math;
11import 'package:flutter3d/flutter3d.dart';
12import 'package:flutter3d_showcase/src/demo/demo.dart';
14/// The `many-lights` page: a tiled floor under more torches than one draw
15/// can take. Its check is the number of torches past the eight slots and the
16/// twenty-four-light tail, and that every tile is drawn.
17final class ManyLightsDemo extends ShowcaseDemo {
18 /// The frame's `lightFadeBand`, 0 to 1: how softly a light fades out at the
19 /// edge between the slots and the dropped.
20 double fadeBand = 0.5;
22 /// Eight direct slots plus the twenty-four-light tail is thirty-two; eight
23 /// more than that is what a floor this crowded actually has to drop.
24 static const int _torchCount = 40;
26 /// The floor as a grid of tiles rather than one plane — see `#region
27 /// floor` for why.
28 static const int _tilesPerSide = 5;
29 static const double _floorSize = 20.0;
30 static const double _tileSize = _floorSize / _tilesPerSide;
32 /// Wide enough that every tile, edge or centre, has more than the
33 /// thirty-two the list can carry within reach — the case the fade band
34 /// needs, and past a torch's own visible glow long before its falloff
35 /// actually reaches this far.
36 static const double _torchRange = 12.0;
38 @override
39 void configureView(DemoContext context) {
40 context.orbit
41 ..distance = 12.0
42 ..pitch = 0.7;
43 }
45 @override
46 Scene build(DemoContext context) {
47 final Scene scene = Scene();
49 // A grid of tiles, not one plane. A single 20x20 draw's own bounding
50 // sphere (radius about 14) reaches every torch on the ring below, six
51 // units out — every one of them lands *inside* it, and `_relevanceIn`
52 // scores a light inside an object's sphere at that object's ceiling
53 // regardless of exactly where inside it sits. Selection then cannot
54 // tell one torch from another, and `lightFadeBand` has nothing to
55 // soften: every candidate ties, so the torch that finally overflows the
56 // list is exactly as strong as the thirty-two that did not. Twenty-five
57 // four-by-four tiles each carry a bounding sphere small enough, next to
58 // a torch's own wide range, that how far a tile sits from the ring is a
59 // real question with a real answer — the tile at the very centre is
60 // furthest from every torch at once, and is where the band has
61 // something to soften.
62 final DeviceMesh tile = DeviceMesh.upload(
63 context.device,
64 PlaneShape(width: _tileSize, depth: _tileSize).build(),
65 );
66 final RenderMaterial floorMaterial = RenderMaterial(
67 baseColor: LinearColor.fromSrgb(0.5, 0.5, 0.55, 1.0),
68 roughness: 0.85,
69 doubleSided: true,
70 );
71 for (var row = 0; row < _tilesPerSide; row++) {
72 for (var col = 0; col < _tilesPerSide; col++) {
73 final double x = (col - (_tilesPerSide - 1) / 2) * _tileSize;
74 final double z = (row - (_tilesPerSide - 1) / 2) * _tileSize;
75 scene.add(
76 MeshNode(tile, floorMaterial, name: 'floor $row,$col')
77 ..setPosition(x, 0.0, z),
78 );
79 }
80 }
82 for (var i = 0; i < _torchCount; i++) {
83 final double angle = i / _torchCount * 2.0 * math.pi;
84 scene.add(
85 LightNode(
86 name: 'torch $i',
87 type: LightType.point,
88 intensity: 2.5 * LightNode.legacyUnit,
89 range: _torchRange,
90 )..setPosition(math.cos(angle) * 6.0, 0.6, math.sin(angle) * 6.0),
91 );
92 }
94 return scene;
95 }
97 @override
98 RenderSettings settings(DemoContext context) => RenderSettings(
99 lightFadeBand: fadeBand,
100 );
102 @override
103 List<DemoControl> controls(DemoContext context) => <DemoControl>[
104 SliderControl(
105 'Fade band',
106 min: 0,
107 max: 1,
108 value: () => fadeBand,
109 onChanged: (double v) => fadeBand = v,
110 ),
111 ];
113 @override
114 void verify(Scene scene, FrameResult frame) {
115 if (scene.lights.length != _torchCount) {
116 throw StateError(
117 'expected $_torchCount torches, found '
118 '${scene.lights.length}',
119 );
120 }
121 final int expectedDropped = math.max(
122 _torchCount - LightNode.maxLights - LightNode.maxExtraLights,
123 0,
124 );
125 if (expectedDropped == 0) {
126 throw StateError(
127 'this scene needs more torches than the cap to prove '
128 'anything is actually dropped',
129 );
130 }
131 if (frame.lightsDropped != expectedDropped) {
132 throw StateError(
133 'expected $expectedDropped lights past the eight slots and the '
134 'twenty-four-light tail, the frame reports ${frame.lightsDropped}',
135 );
136 }
137 final int tileCount = _tilesPerSide * _tilesPerSide;
138 if (scene.meshes.length != tileCount) {
139 throw StateError(
140 'expected $tileCount floor tiles, found ${scene.meshes.length} — a '
141 'floor back to one plane gives every torch the same bounding-sphere '
142 'ceiling and the fade band nothing to soften',
143 );
144 }
145 if (frame.drawCalls < tileCount) {
146 throw StateError('fewer than $tileCount tiles were drawn');
147 }
148 }