flutter3d
Showcase Changelog 38 packages API reference

Horizon occlusion and bounced light: the source

since 0.8.0 Post-processing

Regions the guide quotes: room (line 38), light (line 55), settings (line 64), methods (line 75), ran (line 111).

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;
7
8import 'package:flutter3d/flutter3d.dart';
9import 'package:flutter3d_showcase/pages/post/post_stage.dart';
10import 'package:flutter3d_showcase/src/demo/demo.dart';
11import 'package:vector_math/vector_math.dart';
13final class HorizonOcclusionDemo extends ShowcaseDemo {
14 AmbientOcclusionMethod method = AmbientOcclusionMethod.ssil;
15 double radius = 0.6;
16 double strength = 1.0;
17 double thickness = 0.3;
19 @override
20 void configureView(DemoContext context) {
21 context.orbit
22 ..distance = 4.3
23 ..pitch = 0.62
24 ..yaw = 0.0;
25 context.orbit.target.setValues(0.0, 0.0, -0.5);
26 }
28 @override
29 Scene build(DemoContext context) {
30 final GraphicsDevice device = context.device;
31 MeshNode slab(Vector3 size, Vector3 at, Vector4 colour, String name) =>
32 MeshNode(
33 DeviceMesh.upload(device, CuboidShape(size: size).build()),
34 Material(lighting: LightingModel.lambert, baseColor: colour),
35 name: name,
36 )..setPositionFrom(at);
38 final Vector4 white = Vector4(1.0, 1.0, 1.0, 1.0);
39 final Scene scene = Scene()
40 ..add(
41 slab(Vector3(8.0, 0.1, 8.0), Vector3(0.0, -0.05, 0.0), white, 'floor'),
42 )
43 ..add(
44 slab(
45 Vector3(8.0, 2.0, 0.2),
46 Vector3(0.0, 1.0, -1.0),
47 Vector4(0.9, 0.1, 0.08, 1.0),
48 'red wall',
49 ),
50 )
51 ..add(
52 slab(Vector3(0.6, 0.6, 0.6), Vector3(0.9, 0.3, -0.5), white, 'block'),
53 );
55 scene.ambientIntensity = 0.3;
56 final LightNode sun = LightNode(name: 'sun', intensity: 1.5)
57 ..castsShadow = false
58 ..setRotationYawPitchRoll(0.0, -0.6, 0.0);
59 return scene..add(sun);
60 }
62 @override
63 RenderSettings settings(DemoContext context) => RenderSettings(
64 ambientOcclusion: AmbientOcclusionSettings(
65 enabled: true,
66 method: method,
67 radius: radius,
68 strength: strength,
69 thickness: thickness,
70 ),
71 );
73 @override
74 List<DemoControl> controls(DemoContext context) => <DemoControl>[
75 ChoiceControl(
76 'Method',
77 options: <String>[
78 for (final AmbientOcclusionMethod m in AmbientOcclusionMethod.values)
79 m.name,
80 ],
81 index: () => AmbientOcclusionMethod.values.indexOf(method),
82 onChanged: (int i) => method = AmbientOcclusionMethod.values[i],
83 ),
84 SliderControl(
85 'Radius',
86 min: 0.1,
87 max: 2,
88 value: () => radius,
89 onChanged: (double v) => radius = v,
90 format: (double v) => '${v.toStringAsFixed(2)} m',
91 ),
92 SliderControl(
93 'Strength',
94 min: 0,
95 max: 1,
96 value: () => strength,
97 onChanged: (double v) => strength = v,
98 ),
99 SliderControl(
100 'Thickness (ssil)',
101 min: 0.05,
102 max: 1,
103 value: () => thickness,
104 onChanged: (double v) => thickness = v,
105 format: (double v) => '${v.toStringAsFixed(2)} m',
106 ),
107 ];
109 @override
110 void verify(Scene scene, FrameResult frame) {
111 if (strength > 0) expectPassOrDecline(frame, 'ssao');
112 }