Contact shadows: the source
Open the live demo · Read the guide · View on GitHub
Regions the guide quotes: props (line 45), sun (line 88), map (line 95), settings (line 100).
1/// A short march toward the sun that darkens the crease where an object meets
2/// what it stands on.
3///
4/// Quoted by `contact_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 `contact-shadows` page: a crate and a ball on the ground, darkened
11/// where they touch it. Its check is that the contact shadow pass ran and
12/// drew.
13final class ContactShadowsDemo extends ShowcaseDemo {
14 /// Whether the contact shadow pass runs.
15 bool contact = true;
17 /// Whether the sun casts its shadow map as well.
18 bool shadowMap = false;
20 /// How far each march toward the sun goes, in metres, 0.05 to 1.5.
21 double length = 0.4;
23 /// How many steps the march takes, 1 to 16, rounded.
24 double steps = 12.0;
26 /// How thick a surface is assumed to be behind the depth it shows, in
27 /// metres, 0.02 to 0.6.
28 double thickness = 0.15;
30 /// How dark the contact shadow is, 0 to 1.
31 double strength = 1.0;
33 late final LightNode _sun;
36 void configureView(DemoContext context) {
37 context.orbit
38 ..distance = 6.0
39 ..pitch = 0.5
40 ..yaw = 0.5;
41 }
44 Scene build(DemoContext context) {
45 final RenderMaterial stone = RenderMaterial(
46 name: 'stone',
47 baseColor: LinearColor.fromSrgb(0.78, 0.76, 0.72, 1.0),
48 roughness: 0.9,
49 doubleSided: true,
50 );
51 final RenderMaterial clay = RenderMaterial(
52 name: 'clay',
53 baseColor: LinearColor.fromSrgb(0.85, 0.45, 0.3, 1.0),
54 roughness: 0.7,
55 );
56 final Scene scene = Scene()
57 ..add(
58 MeshNode(
59 DeviceMesh.upload(
60 context.device,
61 const PlaneShape(width: 10, depth: 10).build(),
62 ),
63 stone,
64 name: 'ground',
65 ),
66 )
67 ..add(
68 MeshNode(
69 DeviceMesh.upload(
70 context.device,
71 CuboidShape(size: Vector3(1.4, 1.0, 1.0)).build(),
72 ),
73 clay,
74 name: 'crate',
75 )..setPosition(-1.2, 0.5, 0.0),
76 )
77 ..add(
78 MeshNode(
79 DeviceMesh.upload(
80 context.device,
81 SphereShape(radius: 0.6, segments: 32, rings: 16).build(),
82 ),
83 clay,
84 name: 'ball',
85 )..setPosition(1.3, 0.6, 0.3),
86 );
88 _sun = LightNode(name: 'sun', intensity: 3.0 * LightNode.legacyUnit)
89 ..setLocalForward(Vector3(-0.6, -0.55, -0.4));
90 return scene..add(_sun);
91 }
94 void update(DemoContext context, double dt) {
95 _sun.castsShadow = shadowMap;
96 }
99 RenderSettings settings(DemoContext context) => RenderSettings(
100 contactShadows: ContactShadowSettings(
101 enabled: contact,
102 length: length,
103 steps: steps.round(),
104 thickness: thickness,
105 strength: strength,
106 ),
107 );
110 List<DemoControl> controls(DemoContext context) => <DemoControl>[
111 ToggleControl(
112 'Contact shadows',
113 value: () => contact,
114 onChanged: (bool v) => contact = v,
115 ),
116 ToggleControl(
117 'Shadow map too',
118 value: () => shadowMap,
119 onChanged: (bool v) => shadowMap = v,
120 ),
121 SliderControl(
122 'Length',
123 min: 0.05,
124 max: 1.5,
125 value: () => length,
126 onChanged: (double v) => length = v,
127 format: (double v) => '${v.toStringAsFixed(2)} m',
128 ),
129 SliderControl(
130 'Steps',
131 min: 1,
132 max: 16,
133 divisions: 15,
134 value: () => steps,
135 onChanged: (double v) => steps = v,
136 format: (double v) => '${v.round()}',
137 ),
138 SliderControl(
139 'Thickness',
140 min: 0.02,
141 max: 0.6,
142 value: () => thickness,
143 onChanged: (double v) => thickness = v,
144 format: (double v) => '${v.toStringAsFixed(2)} m',
145 ),
146 SliderControl(
147 'Strength',
148 min: 0,
149 max: 1,
150 value: () => strength,
151 onChanged: (double v) => strength = v,
152 ),
153 ];
156 void verify(Scene scene, FrameResult frame) {
157 final FramePass? pass = frame.passes
158 .where((FramePass p) => p.name == 'contact shadows')
159 .firstOrNull;
160 if (pass == null) {
161 throw StateError('the contact shadow pass did not run');
162 }
163 if (pass.drawCalls < 1) {
164 throw StateError('the contact shadow pass ran and drew nothing');
165 }
166 }
167}