Cascaded shadows: the source
Open the live demo · Read the guide · View on GitHub
Regions the guide quotes: floor (line 39), pillars (line 53), sun (line 65), settings (line 77).
1/// The sun's shadow, split into cascades so that it is sharp near you and
2/// still reaches the horizon.
3///
4/// Quoted by `cascaded_shadows.md` and shown whole in the Source tab.
5library;
7import 'package:flutter3d/flutter3d.dart';
8import 'package:flutter3d_showcase/src/demo/demo.dart';
10/// The `cascaded-shadows` page: a long row of pillars under the sun, its
11/// shadow split into cascades. Its check is that the sun got its shadow.
12final class CascadedShadowsDemo extends ShowcaseDemo {
13 /// How many cascades the sun's shadow is split into, 1 to 4.
14 int cascades = 3;
16 /// How the cascades are spaced, 0.1 to 0.95, between even and logarithmic.
17 double split = 0.7;
19 /// How far from the eye the cascades reach, in metres, 10 to 120.
20 double viewDistance = 60.0;
22 /// The size of each cascade's map: 0 for 512, 1 for 1024, 2 for 2048
23 /// texels.
24 int resolutionChoice = 1;
26 static const List<int> _resolutions = <int>[512, 1024, 2048];
29 void configureView(DemoContext context) {
30 context.orbit
31 ..distance = 9.0
32 ..pitch = 0.35
33 ..yaw = 0.5;
34 context.orbit.target.setValues(0.0, 1.0, -14.0);
35 }
38 Scene build(DemoContext context) {
39 final RenderMaterial stone = RenderMaterial(
40 name: 'stone',
41 baseColor: LinearColor.fromSrgb(0.72, 0.7, 0.66, 1.0),
42 roughness: 0.85,
43 );
44 final MeshNode floor = MeshNode(
45 DeviceMesh.upload(
46 context.device,
47 const PlaneShape(width: 80, depth: 80).build(),
48 ),
49 stone.copy()..doubleSided = true,
50 name: 'floor',
51 );
53 final Scene scene = Scene()..add(floor);
54 final DeviceMesh pillar = DeviceMesh.upload(
55 context.device,
56 CuboidShape(size: Vector3(1.0, 3.0, 1.0)).build(),
57 );
58 for (var i = 0; i < 10; i++) {
59 scene.add(
60 MeshNode(pillar, stone, name: 'pillar $i')
61 ..setPosition(i.isEven ? -3.0 : 3.0, 1.5, -i * 5.0),
62 );
63 }
65 scene.add(
66 LightNode(
67 name: 'sun',
68 intensity: 3.0 * LightNode.legacyUnit,
69 castsShadow: true,
70 )..setLocalForward(Vector3(-0.5, -0.8, -0.3)),
71 );
72 return scene;
73 }
76 RenderSettings settings(DemoContext context) => RenderSettings(
77 shadows: ShadowSettings(
78 cascades: cascades,
79 cascadeSplit: split,
80 viewDistance: viewDistance,
81 resolution: _resolutions[resolutionChoice],
82 ),
83 );
86 List<DemoControl> controls(DemoContext context) => <DemoControl>[
87 SliderControl(
88 'Cascades',
89 min: 1,
90 max: 4,
91 divisions: 3,
92 value: () => cascades.toDouble(),
93 onChanged: (double v) => cascades = v.round(),
94 format: (double v) => '${v.round()}',
95 ),
96 SliderControl(
97 'Split',
98 min: 0.1,
99 max: 0.95,
100 value: () => split,
101 onChanged: (double v) => split = v,
102 ),
103 SliderControl(
104 'Shadow distance',
105 min: 10,
106 max: 120,
107 value: () => viewDistance,
108 onChanged: (double v) => viewDistance = v,
109 format: (double v) => '${v.round()} m',
110 ),
111 ChoiceControl(
112 'Map size',
113 options: <String>[for (final int r in _resolutions) '$r'],
114 index: () => resolutionChoice,
115 onChanged: (int i) => resolutionChoice = i,
116 ),
117 ];
120 void verify(Scene scene, FrameResult frame) {
121 if (frame.shadowsDenied > 0) {
122 throw StateError('the sun was denied its shadow');
123 }
124 }
125}