Shadows of cut-out leaves: the source
Open the live demo · Read the guide · View on GitHub
Regions the guide quotes: texture (line 12), leaves (line 76), sun (line 93), live (line 116).
1/// A cut-out caster throws a cut-out shadow: the holes in a leaf's texture are
2/// holes in its shadow too.
3///
4/// Quoted by `masked_shadow_casters.md` and shown whole in the Source tab.
5library;
7import 'dart:typed_data';
9import 'package:flutter3d/flutter3d.dart';
10import 'package:flutter3d_showcase/src/demo/demo.dart';
12/// A grid of soft discs on a clear background: opaque in the middle of each
13/// disc, fading to nothing at its rim.
14final class DiscsTexture extends ProceduralTexture {
15 /// A texture [size] texels square, cut into [cells] discs a side.
16 const DiscsTexture({this.size = 64, this.cells = 4});
19 final int size;
21 /// How many discs fit along each side.
22 final int cells;
25 ByteData encode() {
26 final Uint8List bytes = Uint8List(size * size * 4);
27 final double cell = size / cells;
28 for (var y = 0; y < size; y++) {
29 for (var x = 0; x < size; x++) {
30 final double dx = (x + 0.5) % cell - cell / 2;
31 final double dy = (y + 0.5) % cell - cell / 2;
32 final double radius = Vector2(dx, dy).length / cell;
33 final double alpha = ((0.5 - radius) / 0.35).clamp(0.0, 1.0);
34 final int at = (y * size + x) * 4;
35 bytes[at] = 70;
36 bytes[at + 1] = 150;
37 bytes[at + 2] = 70;
38 bytes[at + 3] = (alpha * 255).round();
39 }
40 }
41 return bytes.buffer.asByteData();
42 }
43}
45/// The `masked-shadow-casters` page: a canopy of leaves cut out of a disc
46/// texture, over the ground in the sun. Its check is that the canopy is a
47/// masked caster and the sun drew a map.
48final class MaskedShadowCastersDemo extends ShowcaseDemo {
49 /// Whether the canopy is cut out by its alpha, rather than opaque.
50 bool cutOut = true;
52 /// The alpha below which the canopy is cut away, in its picture and its
53 /// shadow, 0.05 to 0.95.
54 double cutoff = 0.5;
56 late final RenderMaterial _leaves;
59 void configureView(DemoContext context) {
60 context.orbit
61 ..distance = 8.0
62 ..pitch = 0.75
63 ..yaw = 0.4;
64 context.orbit.target.setValues(0.5, 0.5, 0.0);
65 }
68 Scene build(DemoContext context) {
69 final RenderMaterial stone = RenderMaterial(
70 name: 'stone',
71 baseColor: LinearColor.fromSrgb(0.78, 0.76, 0.72, 1.0),
72 roughness: 0.9,
73 doubleSided: true,
74 );
76 _leaves = RenderMaterial(
77 name: 'leaves',
78 albedo: const DiscsTexture().upload(context.device),
79 alphaMode: MaterialAlphaMode.mask,
80 alphaCutoff: cutoff,
81 doubleSided: true,
82 roughness: 0.8,
83 );
84 final MeshNode canopy = MeshNode(
85 DeviceMesh.upload(
86 context.device,
87 const PlaneShape(width: 3, depth: 3).build(),
88 ),
89 _leaves,
90 name: 'canopy',
91 )..setPosition(-0.5, 1.6, 0.0);
93 final LightNode sun = LightNode(
94 name: 'sun',
95 intensity: 3.0 * LightNode.legacyUnit,
96 castsShadow: true,
97 )..setLocalForward(Vector3(-0.5, -0.8, -0.3));
99 return Scene()
100 ..add(
101 MeshNode(
102 DeviceMesh.upload(
103 context.device,
104 const PlaneShape(width: 10, depth: 10).build(),
105 ),
106 stone,
107 name: 'ground',
108 ),
109 )
110 ..add(canopy)
111 ..add(sun);
112 }
115 void update(DemoContext context, double dt) {
116 _leaves
117 ..alphaMode = cutOut ? MaterialAlphaMode.mask : MaterialAlphaMode.opaque
118 ..alphaCutoff = cutoff;
119 }
122 RenderSettings settings(DemoContext context) => const RenderSettings(
123 shadows: ShadowSettings(cascades: 1, resolution: 1024),
124 );
127 List<DemoControl> controls(DemoContext context) => <DemoControl>[
128 ToggleControl(
129 'Cut out',
130 value: () => cutOut,
131 onChanged: (bool v) => cutOut = v,
132 ),
133 SliderControl(
134 'Cutoff',
135 min: 0.05,
136 max: 0.95,
137 value: () => cutoff,
138 onChanged: (double v) => cutoff = v,
139 ),
140 ];
143 void verify(Scene scene, FrameResult frame) {
144 if (_leaves.alphaMode != MaterialAlphaMode.mask || _leaves.albedo == null) {
145 throw StateError('the canopy is not a masked caster');
146 }
147 if (!frame.passes.any((FramePass p) => p.name == 'directional shadows')) {
148 throw StateError('the sun drew no shadow map');
149 }
150 if (frame.shadowCasters < 2) {
151 throw StateError('the canopy and the ground were not both drawn');
152 }
153 }
154}