flutter3d
Showcase Changelog 62 packages API reference

Automatic multisampling: the source

since 0.7.0 Post-processing

Regions the guide quotes: ask (line 26), compete (line 34), declined (line 48).

1/// Automatic multisampling: smoothed edges from the scene pass itself, with
2/// no extra pass and no texture read back.
3///
4/// Quoted by `msaa.md` and shown whole in the Source tab.
5library;
6
7import 'package:flutter3d/flutter3d.dart';
8import 'package:flutter3d_showcase/pages/post/post_stage.dart';
9import 'package:flutter3d_showcase/src/demo/demo.dart';
11/// The `msaa` page: the post stage multisampled by the scene pass. Its check
12/// is that the frame multisampled unless the device cannot or the surface
13/// buffer is in use, and said which.
14final class MsaaDemo extends ShowcaseDemo {
15 /// Whether ambient occlusion is on, which reads the surface buffer and so
16 /// costs the scene pass its multisampling.
17 bool readSurface = false;
19 late final bool _deviceCanMsaa;
21 @override
22 void configureView(DemoContext context) => PostStage.frame(context);
24 @override
25 Scene build(DemoContext context) {
26 _deviceCanMsaa = context.device.features.has(
27 DeviceFeature.offscreenMultisample,
28 );
29 return PostStage.build(context).scene;
30 }
32 @override
33 RenderSettings settings(DemoContext context) => RenderSettings(
34 ambientOcclusion: AmbientOcclusionSettings(enabled: readSurface),
35 );
37 @override
38 List<DemoControl> controls(DemoContext context) => <DemoControl>[
39 ToggleControl(
40 'Ambient occlusion (reads the surface buffer)',
41 value: () => readSurface,
42 onChanged: (bool v) => readSurface = v,
43 ),
44 ];
46 @override
47 void verify(Scene scene, FrameResult frame) {
48 final String? declined = frame.antiAliasing.msaaDeclined;
49 if (!_deviceCanMsaa) {
50 if (declined == null) {
51 throw StateError(
52 'this device cannot multisample and the frame '
53 'did not say so',
54 );
55 }
56 } else if (readSurface) {
57 if (declined == null || frame.antiAliasing.msaaSamples > 1) {
58 throw StateError(
59 'the surface buffer is in use and the scene pass '
60 'still multisampled',
61 );
62 }
63 } else {
64 if (declined != null || frame.antiAliasing.msaaSamples <= 1) {
65 throw StateError(
66 'nothing stopped multisampling and the scene pass '
67 'did not use it',
68 );
69 }
70 }
71 }