flutter3d
Showcase Changelog 62 packages API reference

Screen-space reflections: the source

since 0.2.0 Post-processing

Regions the guide quotes: floor (line 33), beacon (line 47), settings (line 77), ran (line 100).

1/// Screen-space reflections: a ray marched through the picture the scene
2/// already drew, so a polished floor shows what stands on it.
3///
4/// Quoted by `screen_space_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 `screen-space-reflections` page: a polished floor with a beacon on
11/// it. Its check is that the reflections pass ran.
12final class ScreenSpaceReflectionsDemo extends ShowcaseDemo {
13 /// The floor's roughness, 0.02 to 0.4: the rougher, the blurrier the
14 /// reflection.
15 double roughness = 0.05;
17 /// How strong the reflections are, 0 to 1.5.
18 double intensity = 1.0;
20 late final RenderMaterial _floor;
22 @override
23 void configureView(DemoContext context) {
24 context.orbit
25 ..distance = 5.0
26 ..pitch = 0.28
27 ..yaw = 0.5;
28 context.orbit.target.setValues(0.0, 0.3, 0.0);
29 }
31 @override
32 Scene build(DemoContext context) {
33 _floor = RenderMaterial(
34 name: 'floor',
35 baseColor: LinearColor.fromSrgb(0.06, 0.06, 0.07, 1.0),
36 roughness: roughness,
37 );
38 final MeshNode floorMesh = MeshNode(
39 DeviceMesh.upload(
40 context.device,
41 CuboidShape(size: Vector3(10.0, 0.3, 10.0)).build(),
42 ),
43 _floor,
44 name: 'floor',
45 );
47 final MeshNode beacon = MeshNode(
48 DeviceMesh.upload(
49 context.device,
50 CuboidShape(size: Vector3.all(1.0)).build(),
51 ),
52 RenderMaterial(
53 name: 'beacon',
54 baseColor: LinearColor.fromSrgb(0.0, 0.0, 0.0, 1.0),
55 emissive: LinearColor(1.0, 0.4, 0.1),
56 emissiveStrength: 2.0 * RenderMaterial.defaultEmissiveStrength,
57 ),
58 name: 'beacon',
59 )..setPosition(0.0, 0.65, 0.0);
61 return Scene()
62 ..add(floorMesh)
63 ..add(beacon)
64 ..add(
65 LightNode(name: 'sun', intensity: 1.0 * LightNode.legacyUnit)
66 ..setLocalForward(Vector3(-0.4, -1.0, -0.3)),
67 );
68 }
70 @override
71 void update(DemoContext context, double dt) {
72 _floor.roughness = roughness;
73 }
75 @override
76 RenderSettings settings(DemoContext context) => RenderSettings(
77 reflections: ReflectionSettings(enabled: true, intensity: intensity),
78 );
80 @override
81 List<DemoControl> controls(DemoContext context) => <DemoControl>[
82 SliderControl(
83 'Roughness',
84 min: 0.02,
85 max: 0.4,
86 value: () => roughness,
87 onChanged: (double v) => roughness = v,
88 ),
89 SliderControl(
90 'Intensity',
91 min: 0,
92 max: 1.5,
93 value: () => intensity,
94 onChanged: (double v) => intensity = v,
95 ),
96 ];
98 @override
99 void verify(Scene scene, FrameResult frame) {
100 if (!frame.passes.any((FramePass p) => p.name == 'reflections')) {
101 throw StateError('the reflections pass did not run: ${frame.skipped}');
102 }
103 }