The cached shadow map: the source
Open the live demo · Read the guide · View on GitHub
Regions the guide quotes: walls (line 69), mover (line 92), orbit (line 116), invalidate (line 119), atlas (line 130).
1/// Walls that never move are drawn into a lamp's shadow atlas once and kept;
2/// only the things that move are redrawn.
3///
4/// Quoted by `static_shadow_cache.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 `static-shadow-cache` page: two walls and a mover around a lamp, the
13/// walls cached in a static atlas. Its check is that the atlas was baked and
14/// the first frame drew the walls into it.
15final class StaticShadowCacheDemo extends ShowcaseDemo {
16 /// Whether the walls are marked `shadowIsStatic`.
17 bool wallsAreStatic = true;
19 /// Whether the mover circles the lamp, a radian a second.
20 bool moverOrbits = true;
22 /// Whether the static atlas is shown on screen; turning it on hides the
23 /// moving one.
24 bool showStatic = false;
26 /// Whether the moving atlas is shown on screen; turning it on hides the
27 /// static one.
28 bool showMoving = false;
30 late final Scene _scene;
31 late final List<MeshNode> _walls;
32 late final MeshNode _mover;
33 bool _wallsWere = true;
34 double _angle = 0.0;
37 void configureView(DemoContext context) {
38 context.orbit
39 ..distance = 9.0
40 ..pitch = 0.7
41 ..yaw = 0.8;
42 }
45 Scene build(DemoContext context) {
46 final RenderMaterial stone = RenderMaterial(
47 name: 'stone',
48 baseColor: LinearColor.fromSrgb(0.78, 0.76, 0.72, 1.0),
49 roughness: 0.9,
50 );
51 final RenderMaterial floorStone = stone.copy()..doubleSided = true;
52 final RenderMaterial clay = RenderMaterial(
53 name: 'clay',
54 baseColor: LinearColor.fromSrgb(0.85, 0.45, 0.3, 1.0),
55 roughness: 0.7,
56 );
57 _scene = Scene()
58 ..add(
59 MeshNode(
60 DeviceMesh.upload(
61 context.device,
62 const PlaneShape(width: 12, depth: 12).build(),
63 ),
64 floorStone,
65 name: 'floor',
66 ),
67 );
69 _walls = <MeshNode>[
70 MeshNode(
71 DeviceMesh.upload(
72 context.device,
73 CuboidShape(size: Vector3(0.3, 3.0, 4.0)).build(),
74 ),
75 stone,
76 name: 'west wall',
77 )..setPosition(-2.5, 1.5, 0.0),
78 MeshNode(
79 DeviceMesh.upload(
80 context.device,
81 CuboidShape(size: Vector3(4.0, 3.0, 0.3)).build(),
82 ),
83 stone,
84 name: 'north wall',
85 )..setPosition(0.0, 1.5, -2.5),
86 ];
87 for (final MeshNode wall in _walls) {
88 wall.shadowIsStatic = true;
89 _scene.add(wall);
90 }
92 _mover = MeshNode(
93 DeviceMesh.upload(
94 context.device,
95 SphereShape(radius: 0.4, segments: 24, rings: 12).build(),
96 ),
97 clay,
98 name: 'mover',
99 );
100 _scene.add(_mover);
102 _scene.add(
103 LightNode(
104 name: 'lamp',
105 type: LightType.point,
106 intensity: 50.0 * LightNode.legacyUnit,
107 range: 14.0,
108 castsShadow: true,
109 )..setPosition(0.0, 3.6, 0.0),
110 );
111 return _scene;
112 }
115 void update(DemoContext context, double dt) {
116 if (moverOrbits) _angle += dt;
117 _mover.setPosition(1.4 * math.cos(_angle), 0.6, 1.4 * math.sin(_angle));
119 if (wallsAreStatic != _wallsWere) {
120 for (final MeshNode wall in _walls) {
121 wall.shadowIsStatic = wallsAreStatic;
122 }
123 _scene.invalidateStaticShadows();
124 _wallsWere = wallsAreStatic;
125 }
126 }
129 RenderSettings settings(DemoContext context) => RenderSettings(
130 shadows: const ShadowSettings(cubeResolution: 256),
131 showStaticShadowMap: showStatic,
132 showShadowMap: showMoving,
133 );
136 List<DemoControl> controls(DemoContext context) => <DemoControl>[
137 ToggleControl(
138 'Walls are static',
139 value: () => wallsAreStatic,
140 onChanged: (bool v) => wallsAreStatic = v,
141 ),
142 ToggleControl(
143 'Mover circles',
144 value: () => moverOrbits,
145 onChanged: (bool v) => moverOrbits = v,
146 ),
147 ToggleControl(
148 'Show static atlas',
149 value: () => showStatic,
150 onChanged: (bool v) {
151 showStatic = v;
152 if (v) showMoving = false;
153 },
154 ),
155 ToggleControl(
156 'Show moving atlas',
157 value: () => showMoving,
158 onChanged: (bool v) {
159 showMoving = v;
160 if (v) showStatic = false;
161 },
162 ),
163 ];
166 void verify(Scene scene, FrameResult frame) {
167 final FramePass? bake = frame.passes
168 .where((FramePass p) => p.name == 'point shadows (static)')
169 .firstOrNull;
170 if (bake == null) {
171 throw StateError('the static shadow atlas was never baked');
172 }
173 if (bake.drawCalls < 1) {
174 throw StateError('the first frame did not draw the walls into the atlas');
175 }
176 }
177}