Soft shadows: the source
Open the live demo · Read the guide · View on GitHub
Regions the guide quotes: casters (line 47), lights (line 84), settings (line 114).
1/// A penumbra that widens with distance: sharp where a shadow starts, soft
2/// where it has travelled.
3///
4/// Quoted by `soft_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 `soft-shadows` page: a pole and a slab on the ground, lit by a sun or
11/// a lamp of a size. Its check is that the light that is on drew its shadow.
12final class SoftShadowsDemo extends ShowcaseDemo {
13 /// The sun's angular radius in radians, 0 to 0.2, shown in degrees.
14 double sunRadius = 0.05;
16 /// The lamp's radius in metres, 0 to 0.6.
17 double lampRadius = 0.3;
19 /// Which light casts: 0 the sun, 1 the lamp.
20 int lightChoice = 0;
22 late final LightNode _sun;
23 late final LightNode _lamp;
26 void configureView(DemoContext context) {
27 context.orbit
28 ..distance = 8.0
29 ..pitch = 0.75
30 ..yaw = 0.9;
31 }
34 Scene build(DemoContext context) {
35 final RenderMaterial stone = RenderMaterial(
36 name: 'stone',
37 baseColor: LinearColor.fromSrgb(0.78, 0.76, 0.72, 1.0),
38 roughness: 0.9,
39 doubleSided: true,
40 );
41 final RenderMaterial clay = RenderMaterial(
42 name: 'clay',
43 baseColor: LinearColor.fromSrgb(0.85, 0.45, 0.3, 1.0),
44 roughness: 0.7,
45 );
47 final Scene scene = Scene()
48 ..add(
49 MeshNode(
50 DeviceMesh.upload(
51 context.device,
52 const PlaneShape(width: 12, depth: 12).build(),
53 ),
54 stone,
55 name: 'ground',
56 ),
57 )
58 ..add(
59 MeshNode(
60 DeviceMesh.upload(
61 context.device,
62 const CylinderShape(
63 radiusTop: 0.12,
64 radiusBottom: 0.12,
65 height: 3.0,
66 segments: 16,
67 ).build(),
68 ),
69 clay,
70 name: 'pole',
71 )..setPosition(-1.5, 1.5, 0.0),
72 )
73 ..add(
74 MeshNode(
75 DeviceMesh.upload(
76 context.device,
77 CuboidShape(size: Vector3(1.6, 0.15, 1.6)).build(),
78 ),
79 clay,
80 name: 'slab',
81 )..setPosition(1.5, 1.6, 0.0),
82 );
84 _sun = LightNode(
85 name: 'sun',
86 intensity: 3.0 * LightNode.legacyUnit,
87 castsShadow: true,
88 )..setLocalForward(Vector3(-0.55, -0.7, -0.35));
89 _lamp = LightNode(
90 name: 'lamp',
91 type: LightType.point,
92 intensity: 60.0 * LightNode.legacyUnit,
93 range: 20.0,
94 )..setPosition(2.5, 3.2, 2.5);
95 scene
96 ..add(_sun)
97 ..add(_lamp);
98 return scene;
99 }
102 void update(DemoContext context, double dt) {
103 final bool lamp = lightChoice == 1;
104 _sun
105 ..castsShadow = !lamp
106 ..intensity = lamp ? 0.0 : 3.0 * LightNode.legacyUnit;
107 _lamp
108 ..castsShadow = lamp
109 ..intensity = lamp ? 60.0 * LightNode.legacyUnit : 0.0;
110 }
113 RenderSettings settings(DemoContext context) => RenderSettings(
114 shadows: ShadowSettings(
115 cascades: 1,
116 resolution: 1024,
117 cubeResolution: 256,
118 directionalLightRadius: sunRadius,
119 pointLightRadius: lampRadius,
120 ),
121 );
124 List<DemoControl> controls(DemoContext context) => <DemoControl>[
125 ChoiceControl(
126 'Light',
127 options: const <String>['Sun', 'Lamp'],
128 index: () => lightChoice,
129 onChanged: (int i) => lightChoice = i,
130 ),
131 SliderControl(
132 'Sun size',
133 min: 0,
134 max: 0.2,
135 value: () => sunRadius,
136 onChanged: (double v) => sunRadius = v,
137 format: (double v) => '${(v * 57.29578).toStringAsFixed(1)}°',
138 ),
139 SliderControl(
140 'Lamp size',
141 min: 0,
142 max: 0.6,
143 value: () => lampRadius,
144 onChanged: (double v) => lampRadius = v,
145 format: (double v) => '${v.toStringAsFixed(2)} m',
146 ),
147 ];
150 void verify(Scene scene, FrameResult frame) {
151 if (!frame.passes.any((FramePass p) => p.name == 'directional shadows')) {
152 throw StateError('the sun drew no shadow map to soften');
153 }
154 if (frame.shadowsDenied > 0) {
155 throw StateError('the light was denied its shadow');
156 }
157 }
158}