Glass that shows the scene: the source
Open the live demo · Read the guide · View on GitHub
Regions the guide quotes: wall (line 50), glass (line 78), live (line 125).
1/// A pane of glass in front of a striped wall, showing the wall through it.
2///
3/// Quoted by `transmission.md` and shown whole in the Source tab.
4library;
6import 'dart:math' as math;
8import 'package:flutter3d/flutter3d.dart';
9import 'package:flutter3d_showcase/src/demo/demo.dart';
11/// The `transmission` page: a glass pane and a glass ball in front of a
12/// striped wall. Its check is that the scene was copied for the glass and the
13/// pass that draws the glass drew it.
14final class TransmissionDemo extends ShowcaseDemo {
15 /// How much light passes through the glass, 0 to 1.
16 double transmission = 1.0;
18 /// The glass's roughness, 0 to 1.
19 double roughness = 0.0;
21 /// The ball's thickness in the mesh's own units, as glTF measures it, 0 to
22 /// 1.5; the pane stays thin.
23 double thickness = 0.9;
25 /// The glass's index of refraction, 1.0 to 2.4.
26 double ior = 1.5;
28 late final RenderMaterial _pane;
29 late final RenderMaterial _ball;
32 void configureView(DemoContext context) {
33 context.orbit
34 ..distance = 2.6
35 ..yaw = 0.0
36 ..pitch = 0.0;
37 }
40 Scene build(DemoContext context) {
41 final Quaternion upright = Quaternion.axisAngle(
42 Vector3(1.0, 0.0, 0.0),
43 math.pi / 2,
44 );
45 RenderMaterial flat(Vector4 color) => RenderMaterial(
46 lighting: LightingModel.unlit,
47 baseColor: _fromSrgb(color),
48 );
50 final DeviceMesh half = DeviceMesh.upload(
51 context.device,
52 const PlaneShape(width: 3.0, depth: 4.0).build(),
53 );
54 final DeviceMesh stripe = DeviceMesh.upload(
55 context.device,
56 const PlaneShape(width: 0.08, depth: 4.0).build(),
57 );
58 final Scene scene = Scene()
59 ..ambientIntensity = 0.2 * LightNode.legacyUnit
60 ..add(
61 MeshNode(half, flat(Vector4(0.8, 0.1, 0.1, 1.0)), name: 'red wall')
62 ..setRotation(upright)
63 ..setPosition(-1.5, 0.0, -2.0),
64 )
65 ..add(
66 MeshNode(half, flat(Vector4(0.1, 0.2, 0.9, 1.0)), name: 'blue wall')
67 ..setRotation(upright)
68 ..setPosition(1.5, 0.0, -2.0),
69 );
70 for (var i = 0; i < 9; i++) {
71 scene.add(
72 MeshNode(stripe, flat(Vector4(0.95, 0.95, 0.9, 1.0)), name: 'stripe')
73 ..setRotation(upright)
74 ..setPosition(-2.0 + i * 0.5, 0.0, -1.98),
75 );
76 }
78 RenderMaterial glass(String name, {required double thickness}) =>
79 RenderMaterial(
80 name: name,
81 lighting: LightingModel.pbrLayered,
82 baseColor: LinearColor.fromSrgb(1.0, 1.0, 1.0, 1.0),
83 roughness: roughness,
84 doubleSided: true,
85 extensions: MaterialExtensions(
86 transmission: transmission,
87 thickness: thickness,
88 ior: ior,
89 ),
90 );
91 _pane = glass('pane', thickness: 0.0);
92 _ball = glass('ball', thickness: thickness);
93 scene
94 ..add(
95 MeshNode(
96 DeviceMesh.upload(
97 context.device,
98 const PlaneShape(width: 0.9, depth: 1.4).build(),
99 ),
100 _pane,
101 name: 'pane',
102 )
103 ..setRotation(upright)
104 ..setPosition(-0.6, 0.0, 0.0),
105 )
106 ..add(
107 MeshNode(
108 DeviceMesh.upload(
109 context.device,
110 SphereShape(radius: 0.45, segments: 48, rings: 24).build(),
111 ),
112 _ball,
113 name: 'ball',
114 )..setPosition(0.6, 0.0, 0.0),
115 )
116 ..add(
117 LightNode(name: 'sun', intensity: 3.0 * LightNode.legacyUnit)
118 ..setLocalForward(Vector3(-0.3, -0.6, -1.0).normalized()),
119 );
120 return scene;
121 }
124 void update(DemoContext context, double dt) {
125 for (final (RenderMaterial material, double depth)
126 in <(RenderMaterial, double)>[(_pane, 0.0), (_ball, thickness)]) {
127 material
128 ..roughness = roughness
129 ..extensions = MaterialExtensions(
130 transmission: transmission,
131 thickness: depth,
132 ior: ior,
133 );
134 }
135 }
138 List<DemoControl> controls(DemoContext context) => <DemoControl>[
139 SliderControl(
140 'Transmission',
141 min: 0,
142 max: 1,
143 value: () => transmission,
144 onChanged: (double v) => transmission = v,
145 ),
146 SliderControl(
147 'Roughness',
148 min: 0,
149 max: 1,
150 value: () => roughness,
151 onChanged: (double v) => roughness = v,
152 ),
153 SliderControl(
154 'Thickness',
155 min: 0,
156 max: 1.5,
157 value: () => thickness,
158 onChanged: (double v) => thickness = v,
159 // In the mesh's own units, as glTF measures it, not metres.
160 format: (double v) => '${v.toStringAsFixed(2)} units',
161 ),
162 SliderControl(
163 'Index of refraction',
164 min: 1.0,
165 max: 2.4,
166 value: () => ior,
167 onChanged: (double v) => ior = v,
168 format: (double v) => v.toStringAsFixed(2),
169 ),
170 ];
173 void verify(Scene scene, FrameResult frame) {
174 // A pane with no transmission is opaque, drawn in one pass as it always
175 // was; at an index of 1.5 it is ordinary metal-rough, and at another
176 // index it keeps that index's reflectance.
177 if (transmission <= 0.0) return;
178 final Set<String> ran = <String>{
179 for (final FramePass pass in frame.passes) pass.name,
180 };
181 if (!ran.contains('scene color copy')) {
182 throw StateError(
183 'the glass is in the frame but the scene was not copied',
184 );
185 }
186 final FramePass? glass = frame.passes
187 .where((FramePass p) => p.name == 'transparent')
188 .firstOrNull;
189 if (glass == null || glass.drawCalls < 1) {
190 throw StateError('the pass that draws the glass drew nothing');
191 }
192 }
193}
195/// A `Vector4` holding a colour sRGB-encoded, as the linear colour it names.
196LinearColor _fromSrgb(Vector4 c) => LinearColor.fromSrgb(c.x, c.y, c.z, c.w);