EVSM shadows: the source
Open the live demo · Read the guide · View on GitHub
Regions the guide quotes: filters (line 23), casters (line 50), sun (line 71), settings (line 80).
1/// The sun's shadow map turned into exponential moments, blurred once, and
2/// read back with one filtered tap per pixel.
3///
4/// Quoted by `evsm_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 `evsm-shadows` page: a floor and grey shapes under the sun, its
11/// shadow filtered as exponential moments. Its check is that the sun drew a
12/// map and, with EVSM picked, that the map was turned into moments.
13final class EvsmShadowsDemo extends ShowcaseDemo {
14 /// Which filter reads the shadow: 0 PCF, 1 PCSS, 2 EVSM.
15 int filterChoice = 2;
17 /// The radius of the blur over the moments, in texels, 0 to 8, rounded.
18 double blurRadius = 4;
20 /// How much light bleeding is cut away, 0 to 0.95.
21 double bleedReduction = 0.2;
23 static const List<ShadowFilter> _filters = <ShadowFilter>[
24 ShadowFilter.pcf,
25 ShadowFilter.pcss,
26 ShadowFilter.evsm,
27 ];
30 void configureView(DemoContext context) {
31 context.orbit
32 ..distance = 8.3
33 ..pitch = 0.81
34 ..yaw = 0.27;
35 }
38 Scene build(DemoContext context) {
39 MeshNode slab(
40 String name,
41 Vector3 size,
42 Vector3 at,
43 RenderMaterial material,
44 ) => MeshNode(
45 DeviceMesh.upload(context.device, CuboidShape(size: size).build()),
46 material,
47 name: name,
48 )..setPosition(at.x, at.y, at.z);
50 final RenderMaterial grey = RenderMaterial(
51 name: 'grey',
52 baseColor: LinearColor.fromSrgb(0.6, 0.6, 0.62, 1.0),
53 );
54 final Scene scene = Scene()
55 ..add(
56 slab(
57 'floor',
58 Vector3(14.0, 0.2, 14.0),
59 Vector3(0.0, -0.1, 0.0),
60 RenderMaterial(
61 name: 'floor',
62 baseColor: LinearColor.fromSrgb(0.5, 0.5, 0.52, 1.0),
63 ),
64 ),
65 )
66 ..add(slab('box', Vector3(1.6, 0.2, 1.6), Vector3(0.0, 0.6, 0.0), grey))
67 ..add(
68 slab('post', Vector3(0.2, 1.5, 0.2), Vector3(-2.0, 0.75, 1.0), grey),
69 );
71 final LightNode sun = LightNode(
72 name: 'sun',
73 intensity: 1.5 * LightNode.legacyUnit,
74 )..setLocalForward(Vector3(-0.85, -1.0, -0.2).normalized());
75 return scene..add(sun);
76 }
79 RenderSettings settings(DemoContext context) => RenderSettings(
80 shadows: ShadowSettings(
81 cascades: 1,
82 viewDistance: 20.0,
83 filter: _filters[filterChoice],
84 evsmBlurRadius: blurRadius.round(),
85 evsmBleedReduction: bleedReduction,
86 directionalLightRadius: 0.02,
87 ),
88 );
91 List<DemoControl> controls(DemoContext context) => <DemoControl>[
92 ChoiceControl(
93 'Filter',
94 options: const <String>['PCF', 'PCSS', 'EVSM'],
95 index: () => filterChoice,
96 onChanged: (int i) => filterChoice = i,
97 ),
98 SliderControl(
99 'Blur radius',
100 min: 0,
101 max: 8,
102 divisions: 8,
103 value: () => blurRadius,
104 onChanged: (double v) => blurRadius = v,
105 format: (double v) => '${v.round()} texels',
106 ),
107 SliderControl(
108 'Bleed reduction',
109 min: 0,
110 max: 0.95,
111 value: () => bleedReduction,
112 onChanged: (double v) => bleedReduction = v,
113 ),
114 ];
117 void verify(Scene scene, FrameResult frame) {
118 if (!frame.passes.any((FramePass p) => p.name == 'directional shadows')) {
119 throw StateError('the sun drew no shadow map to filter');
120 }
121 if (_filters[filterChoice] != ShadowFilter.evsm) return;
122 final PassSkip? skipped = frame.skipReasonOf('shadow moments');
123 if (skipped != null) {
124 throw StateError('the moments were refused: $skipped');
125 }
126 if (!frame.passes.any((FramePass p) => p.name == 'shadow moments')) {
127 throw StateError('the shadow map was never turned into moments');
128 }
129 }
130}