flutter3d
Showcase Changelog 62 packages API reference

Cascades that keep what stands still: the source

since 0.8.0 Shadows

Regions the guide quotes: field (line 65), ball (line 83), sun (line 97), walk (line 106), roll (line 112), flag (line 117), settings (line 127), count (line 155), check (line 170).

1/// A field of blocks that never move, kept in a cascade atlas of their own,
2/// so a walking camera and a rolling ball redraw only what changed.
3///
4/// Quoted by `static_cascades.md` and shown whole in the Source tab.
5library;
6
7import 'dart:math' as math;
8
9import 'package:flutter3d/flutter3d.dart';
10import 'package:flutter3d_showcase/src/demo/demo.dart';
12/// The `static-cascades` page: a field of blocks and a rolling ball under the
13/// sun, the blocks kept in a static atlas. Its check counts what the first
14/// frame and a frame after the ball moves draw into the sun's map.
15final class StaticCascadesDemo extends ShowcaseDemo {
16 /// Whether the blocks are marked `shadowIsStatic`.
17 bool blocksAreStatic = true;
19 /// Whether the camera walks across the field.
20 bool walk = true;
22 /// Whether the ball rolls.
23 bool roll = true;
25 /// Whether the sun's atlas is shown on screen.
26 bool showAtlas = false;
28 static const int _cascades = 3;
30 late final DemoContext _context;
31 late final List<MeshNode> _blocks;
32 late final MeshNode _ball;
33 bool _blocksWere = true;
34 double _walked = 0.0;
35 double _rolled = 0.0;
37 @override
38 void configureView(DemoContext context) {
39 context.orbit
40 ..distance = 9.0
41 ..pitch = 0.65
42 ..yaw = 0.35;
43 }
45 @override
46 Scene build(DemoContext context) {
47 _context = context;
48 final Scene scene = Scene()
49 ..add(
50 MeshNode(
51 DeviceMesh.upload(
52 context.device,
53 const PlaneShape(width: 18, depth: 12).build(),
54 ),
55 RenderMaterial(
56 name: 'ground',
57 baseColor: LinearColor.fromSrgb(0.55, 0.55, 0.53, 1.0),
58 roughness: 0.9,
59 doubleSided: true,
60 ),
61 name: 'ground',
62 )..shadowCasting = ShadowCastingMode.off,
63 );
65 final DeviceMesh block = DeviceMesh.upload(
66 context.device,
67 CuboidShape(size: Vector3(0.4, 0.8, 0.4)).build(),
68 );
69 final RenderMaterial stone = RenderMaterial(
70 name: 'stone',
71 baseColor: LinearColor.fromSrgb(0.5, 0.48, 0.45, 1.0),
72 roughness: 0.85,
73 );
74 _blocks = <MeshNode>[
75 for (var i = 0; i < 12; i++)
76 for (var j = 0; j < 5; j++)
77 MeshNode(block, stone, name: 'block $i $j')
78 ..setPosition(-6.0 + i * 1.1, 0.4, -3.0 + j * 1.2)
79 ..shadowIsStatic = true,
80 ];
81 _blocks.forEach(scene.add);
83 _ball = MeshNode(
84 DeviceMesh.upload(
85 context.device,
86 const SphereShape(radius: 0.25, segments: 24, rings: 12).build(),
87 ),
88 RenderMaterial(
89 name: 'clay',
90 baseColor: LinearColor.fromSrgb(0.6, 0.32, 0.22, 1.0),
91 roughness: 0.7,
92 ),
93 name: 'ball',
94 )..setPosition(0.05, 0.3, -0.6);
95 scene.add(_ball);
97 scene.add(
98 LightNode(name: 'sun', intensity: 1.8 * LightNode.legacyUnit)
99 ..setLocalForward(Vector3(-4.0, -5.0, -0.01).normalized()),
100 );
101 return scene;
102 }
104 @override
105 void update(DemoContext context, double dt) {
106 if (walk) {
107 _walked += dt;
108 context.orbit.target.x = 3.0 * math.sin(0.35 * _walked);
109 context.orbit.apply();
110 }
112 if (roll) {
113 _rolled += dt;
114 _ball.setPosition(0.05, 0.3, -0.6 + 2.2 * math.sin(0.8 * _rolled));
115 }
117 if (blocksAreStatic != _blocksWere) {
118 for (final MeshNode block in _blocks) {
119 block.shadowIsStatic = blocksAreStatic;
120 }
121 _blocksWere = blocksAreStatic;
122 }
123 }
125 @override
126 RenderSettings settings(DemoContext context) => RenderSettings(
127 shadows: const ShadowSettings(cascades: _cascades),
128 showShadowMap: showAtlas,
129 );
131 @override
132 List<DemoControl> controls(DemoContext context) => <DemoControl>[
133 ToggleControl(
134 'Blocks are static',
135 value: () => blocksAreStatic,
136 onChanged: (bool v) => blocksAreStatic = v,
137 ),
138 ToggleControl(
139 'Camera walks',
140 value: () => walk,
141 onChanged: (bool v) => walk = v,
142 ),
143 ToggleControl(
144 'Ball rolls',
145 value: () => roll,
146 onChanged: (bool v) => roll = v,
147 ),
148 ToggleControl(
149 'Show the sun\'s atlas',
150 value: () => showAtlas,
151 onChanged: (bool v) => showAtlas = v,
152 ),
153 ];
155 /// What the frame says the sun's atlas cost: every draw into it, casters,
156 /// copies and resets alike.
157 static int _shadowDraws(FrameResult frame) => frame.passes
158 .where((FramePass p) => p.name == 'directional shadows')
159 .fold(0, (int sum, FramePass p) => sum + p.drawCalls);
161 @override
162 void verify(Scene scene, FrameResult frame) {
163 final int first = _shadowDraws(frame);
164 if (first < _blocks.length) {
165 throw StateError(
166 'the first frame drew $first things into the sun\'s atlas, '
167 'fewer than the ${_blocks.length} blocks',
168 );
169 }
170 // The camera stays where it is and only the ball moves.
171 _ball.translate(0.0, 0.0, 0.2);
172 final int next = _shadowDraws(
173 _context.renderer.render(
174 width: 320,
175 height: 180,
176 scene: scene,
177 views: views(_context),
178 settings: settings(_context),
179 ),
180 );
181 // A copy of the kept tile and the ball, in each cascade at most.
182 if (blocksAreStatic && next > 2 * _cascades) {
183 throw StateError(
184 'moving the ball drew $next things into the sun\'s atlas, '
185 'more than a copy and the ball in each of $_cascades cascades',
186 );
187 }
188 }