Horizon occlusion and bounced light: the source
Open the live demo · Read the guide · View on GitHub
Regions the guide quotes: room (line 51), light (line 68), settings (line 78), methods (line 89), ran (line 125).
1/// Horizon-based ambient occlusion and screen-space indirect light: the
2/// occlusion pass searching for the horizon, and bouncing the light of what
3/// it finds.
4///
5/// Quoted by `horizon_occlusion.md` and shown whole in the Source tab.
6library;
8import 'package:flutter3d/flutter3d.dart';
9import 'package:flutter3d_showcase/pages/post/post_stage.dart';
10import 'package:flutter3d_showcase/src/demo/demo.dart';
12/// The `horizon-occlusion` page: the post stage with horizon-based
13/// occlusion and, optionally, the indirect light it bounces. Its check is
14/// that the occlusion pass ran or was declined with a reason.
15final class HorizonOcclusionDemo extends ShowcaseDemo {
16 /// Which method the occlusion pass uses, from the "Method" choice.
17 AmbientOcclusionMethod method = AmbientOcclusionMethod.ssil;
19 /// How far the horizon search reaches, in metres, 0.1 to 2.
20 double radius = 0.6;
22 /// How strong the occlusion is, 0 to 1; 0 lets the pass off its check.
23 double strength = 1.0;
25 /// How thick a surface is assumed to be behind what the screen shows, in
26 /// metres, 0.05 to 1; used by the indirect light.
27 double thickness = 0.3;
30 void configureView(DemoContext context) {
31 context.orbit
32 ..distance = 4.3
33 ..pitch = 0.62
34 ..yaw = 0.0;
35 context.orbit.target.setValues(0.0, 0.0, -0.5);
36 }
39 Scene build(DemoContext context) {
40 final GraphicsDevice device = context.device;
41 MeshNode slab(Vector3 size, Vector3 at, Vector4 color, String name) =>
42 MeshNode(
43 DeviceMesh.upload(device, CuboidShape(size: size).build()),
44 RenderMaterial(
45 lighting: LightingModel.lambert,
46 baseColor: _fromSrgb(color),
47 ),
48 name: name,
49 )..setPositionFrom(at);
51 final Vector4 white = Vector4(1.0, 1.0, 1.0, 1.0);
52 final Scene scene = Scene()
53 ..add(
54 slab(Vector3(8.0, 0.1, 8.0), Vector3(0.0, -0.05, 0.0), white, 'floor'),
55 )
56 ..add(
57 slab(
58 Vector3(8.0, 2.0, 0.2),
59 Vector3(0.0, 1.0, -1.0),
60 Vector4(0.9, 0.1, 0.08, 1.0),
61 'red wall',
62 ),
63 )
64 ..add(
65 slab(Vector3(0.6, 0.6, 0.6), Vector3(0.9, 0.3, -0.5), white, 'block'),
66 );
68 scene.ambientIntensity = 0.3 * LightNode.legacyUnit;
69 final LightNode sun =
70 LightNode(name: 'sun', intensity: 1.5 * LightNode.legacyUnit)
71 ..castsShadow = false
72 ..setRotationYawPitchRoll(0.0, -0.6, 0.0);
73 return scene..add(sun);
74 }
77 RenderSettings settings(DemoContext context) => RenderSettings(
78 ambientOcclusion: AmbientOcclusionSettings(
79 enabled: true,
80 method: method,
81 radius: radius,
82 strength: strength,
83 thickness: thickness,
84 ),
85 );
88 List<DemoControl> controls(DemoContext context) => <DemoControl>[
89 ChoiceControl(
90 'Method',
91 options: <String>[
92 for (final AmbientOcclusionMethod m in AmbientOcclusionMethod.values)
93 m.name,
94 ],
95 index: () => AmbientOcclusionMethod.values.indexOf(method),
96 onChanged: (int i) => method = AmbientOcclusionMethod.values[i],
97 ),
98 SliderControl(
99 'Radius',
100 min: 0.1,
101 max: 2,
102 value: () => radius,
103 onChanged: (double v) => radius = v,
104 format: (double v) => '${v.toStringAsFixed(2)} m',
105 ),
106 SliderControl(
107 'Strength',
108 min: 0,
109 max: 1,
110 value: () => strength,
111 onChanged: (double v) => strength = v,
112 ),
113 SliderControl(
114 'Thickness (ssil)',
115 min: 0.05,
116 max: 1,
117 value: () => thickness,
118 onChanged: (double v) => thickness = v,
119 format: (double v) => '${v.toStringAsFixed(2)} m',
120 ),
121 ];
124 void verify(Scene scene, FrameResult frame) {
125 if (strength > 0) expectPassOrDecline(frame, 'ssao');
126 }
127}
129/// A `Vector4` holding a colour sRGB-encoded, as the linear colour it names.
130LinearColor _fromSrgb(Vector4 c) => LinearColor.fromSrgb(c.x, c.y, c.z, c.w);