flutter3d
Showcase Changelog 62 packages API reference

Reflection probes: the source

since 0.4.3 Light and environment

Regions the guide quotes: ball (line 50), probe (line 57), live (line 104).

1/// A mirror finish that shows the room it stands in, not the sky: a cube
2/// captured from one point and convolved into a roughness chain on the
3/// device.
4///
5/// Quoted by `reflection_probes.md` and shown whole in the Source tab.
6library;
7
8import 'dart:math' as math;
9
10import 'package:flutter3d/flutter3d.dart';
11import 'package:flutter3d_showcase/src/demo/demo.dart';
13/// The `reflection-probes` page: a mirror ball before a red wall, reflecting the room through a probe's captured cube. Its check is
14/// that the probe finished capturing and its pass drew into the cube.
15final class ReflectionProbesDemo extends ShowcaseDemo {
16 /// How strongly the probe's cube contributes, 0 to 2; written into the
17 /// probe each frame.
18 double intensity = 1.0;
20 /// The mirror ball's roughness, 0.02 to 1: the higher, the blurrier the
21 /// level of the probe's roughness chain it reads.
22 double roughness = 0.06;
24 late final ReflectionProbeNode _probe;
25 late final RenderMaterial _ball;
27 @override
28 void configureView(DemoContext context) {
29 context.orbit
30 ..distance = 6.0
31 ..pitch = 0.18
32 ..yaw = 0.7;
33 context.orbit.target.setValues(0.0, 0.7, 0.0);
34 }
36 @override
37 Scene build(DemoContext context) {
38 final RenderMaterial floor = RenderMaterial(
39 name: 'floor',
40 baseColor: LinearColor.fromSrgb(0.5, 0.5, 0.53, 1.0),
41 roughness: 0.9,
42 doubleSided: true,
43 );
44 final RenderMaterial wall = RenderMaterial(
45 name: 'wall',
46 baseColor: LinearColor.fromSrgb(0.82, 0.18, 0.14, 1.0),
47 roughness: 0.9,
48 doubleSided: true,
49 );
50 final RenderMaterial ball = _ball = RenderMaterial(
51 name: 'mirror ball',
52 baseColor: LinearColor.fromSrgb(0.9, 0.9, 0.92, 1.0),
53 metallic: 1.0,
54 roughness: roughness,
55 );
57 _probe = ReflectionProbeNode(name: 'probe', intensity: intensity)
58 ..setPosition(0.0, 0.9, 0.0);
60 final MeshNode wallNode =
61 MeshNode(
62 DeviceMesh.upload(
63 context.device,
64 const PlaneShape(width: 3, depth: 4).build(),
65 ),
66 wall,
67 name: 'wall',
68 )
69 ..setPosition(-2.0, 1.5, 0.0)
70 ..setRotation(
71 Quaternion.axisAngle(Vector3(0.0, 0.0, 1.0), -math.pi / 2),
72 );
74 return Scene()
75 ..ambientIntensity = 0.15 * LightNode.legacyUnit
76 ..add(
77 MeshNode(
78 DeviceMesh.upload(
79 context.device,
80 const PlaneShape(width: 8, depth: 8).build(),
81 ),
82 floor,
83 name: 'floor',
84 ),
85 )
86 ..add(wallNode)
87 ..add(
88 MeshNode(
89 DeviceMesh.upload(
90 context.device,
91 SphereShape(radius: 0.9, segments: 32, rings: 16).build(),
92 ),
93 ball,
94 name: 'ball',
95 )..setPosition(0.0, 0.9, 0.0),
96 )
97 ..add(
98 LightNode(name: 'sun', intensity: 3.0 * LightNode.legacyUnit)
99 ..setLocalForward(Vector3(-0.5, -0.7, -0.35)),
100 )
101 ..add(_probe);
102 }
104 @override
105 void update(DemoContext context, double dt) {
106 _probe.intensity = intensity;
107 _ball.roughness = roughness;
108 }
110 @override
111 List<DemoControl> controls(DemoContext context) => <DemoControl>[
112 SliderControl(
113 'Probe intensity',
114 min: 0,
115 max: 2,
116 value: () => intensity,
117 onChanged: (double v) => intensity = v,
118 ),
119 SliderControl(
120 'Ball roughness',
121 min: 0.02,
122 max: 1,
123 value: () => roughness,
124 onChanged: (double v) => roughness = v,
125 ),
126 ];
128 @override
129 void verify(Scene scene, FrameResult frame) {
130 if (!_probe.isCaptured) {
131 throw StateError('the probe never finished capturing its cube');
132 }
133 final FramePass capture = frame.passes.firstWhere(
134 (FramePass p) => p.name == 'reflection probe 0',
135 orElse: () => throw StateError('no reflection probe pass ran this frame'),
136 );
137 if (capture.drawCalls == 0) {
138 throw StateError('the probe pass ran and drew nothing into its cube');
139 }
140 }