flutter3d
Showcase Changelog 62 packages API reference

Planar reflections: the source

since 1.0.0-rc.1 Light and environment

Regions the guide quotes: floor (line 37), objects (line 60), mirror (line 104), switch (line 126), check (line 156).

1/// Planar reflections: the scene drawn again by a camera mirrored in a floor,
2/// and laid over the floor in proportion to its reflectance and the angle.
3///
4/// Quoted by `planar_reflections.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 `planar-reflections` page: a polished floor with a ball, a block and
11/// a post standing on it. Its check is that the mirrored camera drew the
12/// three meshes above the floor, and that the pass does not run when off.
13final class PlanarReflectionsDemo extends ShowcaseDemo {
14 /// Whether the mirrored pass runs; the "Planar reflections" switch.
15 bool reflections = true;
17 /// How much of the reflection the floor shows, 0.02 to 1, written into the
18 /// floor's material and the plane's settings.
19 double reflectance = 0.3;
21 /// The reflection's size as a fraction of the frame's, 0.25 to 1.
22 double resolution = 0.5;
24 late final PlanarReflectorNode _mirror;
26 @override
27 void configureView(DemoContext context) {
28 context.orbit
29 ..distance = 8.0
30 ..pitch = 0.3
31 ..yaw = 0.5;
32 context.orbit.target.setValues(0.0, 0.8, 0.0);
33 }
35 @override
36 Scene build(DemoContext context) {
37 // A dark polished floor: the surface the reflection is laid over. It keeps
38 // its own material, lit and shadowed as any floor is.
39 final MeshNode floor = MeshNode(
40 DeviceMesh.upload(
41 context.device,
42 const PlaneShape(width: 14, depth: 14).build(),
43 ),
44 RenderMaterial(
45 name: 'polished floor',
46 baseColor: LinearColor.fromSrgb(0.08, 0.08, 0.09, 1.0),
47 roughness: 0.2,
48 ),
49 name: 'floor',
50 );
52 final Scene scene = Scene()
53 ..ambientIntensity = 0.3 * LightNode.legacyUnit
54 ..add(floor)
55 ..add(
56 LightNode(name: 'sun', intensity: 3.0 * LightNode.legacyUnit)
57 ..setLocalForward(Vector3(-0.5, -0.7, -0.4)),
58 );
60 // Something to see in it: a red ball, a gold block and a tall blue post.
61 scene
62 ..add(
63 MeshNode(
64 DeviceMesh.upload(
65 context.device,
66 SphereShape(radius: 0.8, segments: 32, rings: 16).build(),
67 ),
68 RenderMaterial(
69 name: 'red',
70 baseColor: LinearColor.fromSrgb(0.8, 0.12, 0.1, 1.0),
71 ),
72 name: 'ball',
73 )..setPosition(-1.2, 0.8, 0.4),
74 )
75 ..add(
76 MeshNode(
77 DeviceMesh.upload(
78 context.device,
79 CuboidShape(size: Vector3(1.0, 1.0, 1.0)).build(),
80 ),
81 RenderMaterial(
82 name: 'gold',
83 baseColor: LinearColor.fromSrgb(1.0, 0.75, 0.3, 1.0),
84 metallic: 1.0,
85 roughness: 0.3,
86 ),
87 name: 'block',
88 )..setPosition(1.3, 0.5, -0.3),
89 )
90 ..add(
91 MeshNode(
92 DeviceMesh.upload(
93 context.device,
94 CuboidShape(size: Vector3(0.4, 3.0, 0.4)).build(),
95 ),
96 RenderMaterial(
97 name: 'blue',
98 baseColor: LinearColor.fromSrgb(0.15, 0.3, 0.8, 1.0),
99 ),
100 name: 'post',
101 )..setPosition(0.2, 1.5, -2.0),
102 );
104 // The plane is the node's own: through its origin, seen from its +Y. The
105 // floor lies in it, so the floor is the surface the picture goes on.
106 _mirror = PlanarReflectorNode(
107 surfaces: <MeshNode>[floor],
108 reflectance: reflectance,
109 resolution: resolution,
110 name: 'mirror',
111 );
112 scene.add(_mirror);
113 return scene;
114 }
116 @override
117 void update(DemoContext context, double dt) {
118 _mirror
119 ..reflectance = reflectance
120 ..resolution = resolution;
121 }
123 @override
124 RenderSettings settings(DemoContext context) => RenderSettings(
125 sky: const SkySettings(enabled: true),
126 planarReflections: PlanarReflectionSettings(enabled: reflections),
127 );
129 @override
130 List<DemoControl> controls(DemoContext context) => <DemoControl>[
131 ToggleControl(
132 'Planar reflections',
133 value: () => reflections,
134 onChanged: (bool v) => reflections = v,
135 ),
136 SliderControl(
137 'Reflectance',
138 min: 0.02,
139 max: 1,
140 value: () => reflectance,
141 onChanged: (double v) => reflectance = v,
142 format: (double v) => v.toStringAsFixed(2),
143 ),
144 SliderControl(
145 'Resolution',
146 min: 0.25,
147 max: 1,
148 value: () => resolution,
149 onChanged: (double v) => resolution = v,
150 format: (double v) => '${(v * 100).round()} %',
151 ),
152 ];
154 @override
155 void verify(Scene scene, FrameResult frame) {
156 // On, the mirrored camera draws the scene before the scene pass does: the
157 // three objects, not the floor, which stands in the plane.
158 final FramePass? pass = frame.passes
159 .where((FramePass p) => p.name == 'planar reflections')
160 .firstOrNull;
161 if (!reflections) {
162 if (pass != null) throw StateError('off should not run the pass');
163 return;
164 }
165 if (pass == null) {
166 throw StateError('the reflection was not drawn: ${frame.skipped}');
167 }
168 if (pass.drawCalls < 3) {
169 throw StateError(
170 'the mirrored camera drew ${pass.drawCalls} meshes; three stand '
171 'above the floor',
172 );
173 }
174 }