flutter3d
Showcase Changelog 62 packages API reference

Shadow quality: the source

since 0.1.0 Shadows

Regions the guide quotes: ground (line 37), casters (line 55), sun (line 82), settings (line 94).

1/// The knobs that decide how good the sun's shadow looks: the size of its map,
2/// the bias that keeps it clean, how dark it is and how far it is fitted for.
3///
4/// Quoted by `shadow_settings.md` and shown whole in the Source tab.
5library;
6
7import 'package:flutter3d/flutter3d.dart';
8import 'package:flutter3d_showcase/src/demo/demo.dart';
9
10/// The `shadow-settings` page: a column and a ball on the ground under the
11/// sun. Its check is that the sun drew a map holding both.
12final class ShadowSettingsDemo extends ShowcaseDemo {
13 /// The size of the sun's map: 0 for 256, up to 3 for 2048 texels.
14 int resolutionChoice = 0;
16 /// The depth bias that keeps a surface from shadowing itself, 0 to 0.02.
17 double bias = 0.0015;
19 /// How dark the shadow is, 0 to 1.
20 double strength = 1.0;
22 /// How far from the eye the map is fitted for, in metres, 3 to 40.
23 double viewDistance = 40.0;
25 static const List<int> _resolutions = <int>[256, 512, 1024, 2048];
27 @override
28 void configureView(DemoContext context) {
29 context.orbit
30 ..distance = 9.0
31 ..pitch = 0.6
32 ..yaw = 0.6;
33 }
35 @override
36 Scene build(DemoContext context) {
37 final RenderMaterial stone = RenderMaterial(
38 name: 'stone',
39 baseColor: LinearColor.fromSrgb(0.75, 0.73, 0.68, 1.0),
40 roughness: 0.9,
41 doubleSided: true,
42 );
43 final Scene scene = Scene()
44 ..add(
45 MeshNode(
46 DeviceMesh.upload(
47 context.device,
48 const PlaneShape(width: 14, depth: 14).build(),
49 ),
50 stone,
51 name: 'ground',
52 ),
53 );
55 final RenderMaterial clay = RenderMaterial(
56 name: 'clay',
57 baseColor: LinearColor.fromSrgb(0.85, 0.45, 0.3, 1.0),
58 roughness: 0.7,
59 );
60 scene
61 ..add(
62 MeshNode(
63 DeviceMesh.upload(
64 context.device,
65 CuboidShape(size: Vector3(1.2, 2.4, 1.2)).build(),
66 ),
67 clay,
68 name: 'column',
69 )..setPosition(-1.5, 1.2, 0.0),
70 )
71 ..add(
72 MeshNode(
73 DeviceMesh.upload(
74 context.device,
75 SphereShape(radius: 0.8, segments: 32, rings: 16).build(),
76 ),
77 clay,
78 name: 'ball',
79 )..setPosition(1.5, 0.8, 0.5),
80 );
82 scene.add(
83 LightNode(
84 name: 'sun',
85 intensity: 3.0 * LightNode.legacyUnit,
86 castsShadow: true,
87 )..setLocalForward(Vector3(-0.5, -0.9, -0.4)),
88 );
89 return scene;
90 }
92 @override
93 RenderSettings settings(DemoContext context) => RenderSettings(
94 shadows: ShadowSettings(
95 cascades: 2,
96 resolution: _resolutions[resolutionChoice],
97 bias: bias,
98 strength: strength,
99 viewDistance: viewDistance,
100 ),
101 );
103 @override
104 List<DemoControl> controls(DemoContext context) => <DemoControl>[
105 ChoiceControl(
106 'Map size',
107 options: <String>[for (final int r in _resolutions) '$r'],
108 index: () => resolutionChoice,
109 onChanged: (int i) => resolutionChoice = i,
110 ),
111 SliderControl(
112 'Bias',
113 min: 0,
114 max: 0.02,
115 value: () => bias,
116 onChanged: (double v) => bias = v,
117 format: (double v) => v.toStringAsFixed(4),
118 ),
119 SliderControl(
120 'Strength',
121 min: 0,
122 max: 1,
123 value: () => strength,
124 onChanged: (double v) => strength = v,
125 ),
126 SliderControl(
127 'Distance',
128 min: 3,
129 max: 40,
130 value: () => viewDistance,
131 onChanged: (double v) => viewDistance = v,
132 format: (double v) => '${v.round()} m',
133 ),
134 ];
136 @override
137 void verify(Scene scene, FrameResult frame) {
138 if (!frame.passes.any((FramePass p) => p.name == 'directional shadows')) {
139 throw StateError('the sun drew no shadow map');
140 }
141 if (frame.shadowCasters < 2) {
142 throw StateError(
143 'expected the column and the ball in the map, '
144 'got ${frame.shadowCasters}',
145 );
146 }
147 }