Screen-space reflections: the source
Open the live demo · Read the guide · View on GitHub
Regions the guide quotes: floor (line 28), beacon (line 42), settings (line 72), ran (line 95).
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;
7import 'package:flutter3d/flutter3d.dart';
8import 'package:flutter3d_showcase/src/demo/demo.dart';
9import 'package:vector_math/vector_math.dart';
11final class ScreenSpaceReflectionsDemo extends ShowcaseDemo {
12 double roughness = 0.05;
13 double intensity = 1.0;
15 late final Material _floor;
18 void configureView(DemoContext context) {
19 context.orbit
20 ..distance = 5.0
21 ..pitch = 0.28
22 ..yaw = 0.5;
23 context.orbit.target.setValues(0.0, 0.3, 0.0);
24 }
27 Scene build(DemoContext context) {
28 _floor = Material(
29 name: 'floor',
30 baseColor: Vector4(0.06, 0.06, 0.07, 1.0),
31 roughness: roughness,
32 );
33 final MeshNode floorMesh = MeshNode(
34 DeviceMesh.upload(
35 context.device,
36 CuboidShape(size: Vector3(10.0, 0.3, 10.0)).build(),
37 ),
38 _floor,
39 name: 'floor',
40 );
42 final MeshNode beacon = MeshNode(
43 DeviceMesh.upload(
44 context.device,
45 CuboidShape(size: Vector3.all(1.0)).build(),
46 ),
47 Material(
48 name: 'beacon',
49 baseColor: Vector4(0.0, 0.0, 0.0, 1.0),
50 emissive: Vector3(1.0, 0.4, 0.1),
51 emissiveStrength: 2.0,
52 ),
53 name: 'beacon',
54 )..setPosition(0.0, 0.65, 0.0);
56 return Scene()
57 ..add(floorMesh)
58 ..add(beacon)
59 ..add(
60 LightNode(name: 'sun', intensity: 1.0)
61 ..setLocalForward(Vector3(-0.4, -1.0, -0.3)),
62 );
63 }
66 void update(DemoContext context, double dt) {
67 _floor.roughness = roughness;
68 }
71 RenderSettings settings(DemoContext context) => RenderSettings(
72 reflections: ReflectionSettings(enabled: true, intensity: intensity),
73 );
76 List<DemoControl> controls(DemoContext context) => <DemoControl>[
77 SliderControl(
78 'Roughness',
79 min: 0.02,
80 max: 0.4,
81 value: () => roughness,
82 onChanged: (double v) => roughness = v,
83 ),
84 SliderControl(
85 'Intensity',
86 min: 0,
87 max: 1.5,
88 value: () => intensity,
89 onChanged: (double v) => intensity = v,
90 ),
91 ];
94 void verify(Scene scene, FrameResult frame) {
95 if (!frame.passes.any((FramePass p) => p.name == 'reflections')) {
96 throw StateError('the reflections pass did not run: ${frame.skipped}');
97 }
98 }
99}