flutter3d
Showcase Changelog 62 packages API reference

Point and spot shadows: the source

since 0.2.0 Shadows

Regions the guide quotes: lamp (line 95), spot (line 105), settings (line 123).

1/// Shadows from a lamp and from a spotlight, drawn into a cube map.
2///
3/// Quoted by `point_light_shadows.md` and shown whole in the Source tab.
4library;
5
6import 'package:flutter3d/flutter3d.dart';
7import 'package:flutter3d_showcase/src/demo/demo.dart';
8
9/// The `point-light-shadows` page: a column and a ball between a lamp and a
10/// spot, both casting into cube maps. Its check is that a cube shadow pass
11/// ran and no light was refused its shadow.
12final class PointLightShadowsDemo extends ShowcaseDemo {
13 /// How soft the cube shadows' edges are, in texels, 0 to 16.
14 double softness = 4.0;
16 /// How far the cube shadows are pushed back, in metres, 0 to 0.5.
17 double bias = 0.08;
19 /// The size of each cube face: 0 for 128, 1 for 256, 2 for 512 texels.
20 int sizeChoice = 1;
22 /// Which faces of a caster draw into the cube: 0 front, 1 back, 2 both.
23 int facesChoice = 1;
25 static const List<int> _sizes = <int>[128, 256, 512];
26 static const List<ShadowCasterFaces> _faces = <ShadowCasterFaces>[
27 ShadowCasterFaces.front,
28 ShadowCasterFaces.back,
29 ShadowCasterFaces.both,
30 ];
32 @override
33 void configureView(DemoContext context) {
34 context.orbit
35 ..distance = 9.0
36 ..pitch = 0.55
37 ..yaw = 0.7;
38 }
40 @override
41 Scene build(DemoContext context) {
42 final RenderMaterial stone = RenderMaterial(
43 name: 'stone',
44 baseColor: LinearColor.fromSrgb(0.78, 0.76, 0.72, 1.0),
45 roughness: 0.9,
46 );
47 final RenderMaterial floorStone = stone.copy()..doubleSided = true;
48 final RenderMaterial clay = RenderMaterial(
49 name: 'clay',
50 baseColor: LinearColor.fromSrgb(0.85, 0.45, 0.3, 1.0),
51 roughness: 0.7,
52 );
53 final Scene scene = Scene()
54 ..add(
55 MeshNode(
56 DeviceMesh.upload(
57 context.device,
58 const PlaneShape(width: 12, depth: 12).build(),
59 ),
60 floorStone,
61 name: 'floor',
62 ),
63 )
64 ..add(
65 MeshNode(
66 DeviceMesh.upload(
67 context.device,
68 CuboidShape(size: Vector3(12.0, 4.0, 0.3)).build(),
69 ),
70 stone,
71 name: 'wall',
72 )..setPosition(0.0, 2.0, -5.0),
73 )
74 ..add(
75 MeshNode(
76 DeviceMesh.upload(
77 context.device,
78 CuboidShape(size: Vector3(1.0, 2.0, 1.0)).build(),
79 ),
80 clay,
81 name: 'column',
82 )..setPosition(-1.0, 1.0, -1.0),
83 )
84 ..add(
85 MeshNode(
86 DeviceMesh.upload(
87 context.device,
88 SphereShape(radius: 0.7, segments: 24, rings: 12).build(),
89 ),
90 clay,
91 name: 'ball',
92 )..setPosition(1.8, 0.7, -2.0),
93 );
95 scene.add(
96 LightNode(
97 name: 'lamp',
98 type: LightType.point,
99 intensity: 50.0 * LightNode.legacyUnit,
100 range: 16.0,
101 castsShadow: true,
102 )..setPosition(1.5, 3.2, 1.0),
103 );
105 scene.add(
106 LightNode(
107 name: 'spot',
108 type: LightType.spot,
109 intensity: 90.0 * LightNode.legacyUnit,
110 range: 16.0,
111 innerConeAngle: 0.2,
112 outerConeAngle: 0.5,
113 castsShadow: true,
114 )
115 ..setPosition(-4.0, 3.5, 3.5)
116 ..lookAt(Vector3(-1.0, 0.5, -1.5)),
117 );
118 return scene;
119 }
121 @override
122 RenderSettings settings(DemoContext context) => RenderSettings(
123 shadows: ShadowSettings(
124 cubeResolution: _sizes[sizeChoice],
125 pointBias: bias,
126 pointSoftness: softness,
127 casterFaces: _faces[facesChoice],
128 ),
129 );
131 @override
132 List<DemoControl> controls(DemoContext context) => <DemoControl>[
133 SliderControl(
134 'Softness',
135 min: 0,
136 max: 16,
137 value: () => softness,
138 onChanged: (double v) => softness = v,
139 format: (double v) => '${v.toStringAsFixed(1)} texels',
140 ),
141 SliderControl(
142 'Bias',
143 min: 0,
144 max: 0.5,
145 value: () => bias,
146 onChanged: (double v) => bias = v,
147 format: (double v) => '${v.toStringAsFixed(2)} m',
148 ),
149 ChoiceControl(
150 'Face size',
151 options: <String>[for (final int s in _sizes) '$s'],
152 index: () => sizeChoice,
153 onChanged: (int i) => sizeChoice = i,
154 ),
155 ChoiceControl(
156 'Casters draw',
157 options: const <String>['Front', 'Back', 'Both'],
158 index: () => facesChoice,
159 onChanged: (int i) => facesChoice = i,
160 ),
161 ];
163 @override
164 void verify(Scene scene, FrameResult frame) {
165 if (!frame.passes.any((FramePass p) => p.name == 'point shadows')) {
166 throw StateError('no cube shadow pass ran');
167 }
168 if (frame.shadowsDenied > 0) {
169 throw StateError(
170 '${frame.shadowsDenied} light(s) asked for a shadow and were refused',
171 );
172 }
173 }