Post effects on your own image: the source
Open the live demo · Read the guide · View on GitHub
Regions the guide quotes: source (line 44), screen (line 68), first (line 93), post (line 107), answer (line 152).
1/// Post effects on an image of your own: one render, then `renderPost` over the
2/// result, then the answer shown on a screen in the scene.
3///
4/// Quoted by `render_post.md` and shown whole in the Source tab.
5library;
7import 'package:flutter3d/flutter3d.dart';
8import 'package:flutter3d_showcase/src/demo/demo.dart';
10/// The `render-post` page: a bar rendered once by a second eye, put through
11/// `renderPost`, and shown on a screen in the scene. Its check is that
12/// `renderPost` was called and drew into its target.
13final class RenderPostDemo extends ShowcaseDemo {
14 /// The brightness above which `renderPost` blooms the image, 0.1 to 1.5.
15 double threshold = 0.6;
17 /// How much bloom `renderPost` adds back, 0 to 1.5.
18 double intensity = 0.6;
20 /// The exposure `renderPost` applies to the image, 0.3 to 3.
21 double exposure = 1.0;
23 static const int _width = 320;
24 static const int _height = 180;
26 late final Scene _source;
27 late final CameraView _sourceView;
28 late final TextureHandle _output;
30 /// What the last call to `renderPost` handed back.
31 PostFrameResult? result;
34 void configureView(DemoContext context) {
35 context.orbit
36 ..distance = 2.3
37 ..pitch = 0.0
38 ..yaw = 0.0;
39 context.orbit.target.setValues(0.0, 0.0, 0.0);
40 }
43 Scene build(DemoContext context) {
44 final CameraNode eye = CameraNode(name: 'source eye')
45 ..setPosition(0.0, 0.0, 4.0);
46 _source = Scene()
47 ..add(
48 MeshNode(
49 DeviceMesh.upload(
50 context.device,
51 CuboidShape(size: Vector3(2.4, 0.5, 0.2)).build(),
52 ),
53 RenderMaterial(
54 name: 'bar',
55 baseColor: LinearColor.fromSrgb(0.0, 0.0, 0.0, 1.0),
56 emissive: LinearColor(1.0, 0.55, 0.2),
57 emissiveStrength: 6.0 * RenderMaterial.defaultEmissiveStrength,
58 ),
59 name: 'bar',
60 ),
61 )
62 ..add(eye);
63 _sourceView = CameraView(
64 camera: eye,
65 clearColor: LinearColor.fromSrgb(0.0, 0.0, 0.0, 1.0),
66 );
68 _output = context.device.createTexture(
69 RenderTargetDescriptor(
70 width: _width,
71 height: _height,
72 format: context.device.defaultColorFormat,
73 ),
74 );
75 final MeshNode screen = MeshNode(
76 DeviceMesh.upload(
77 context.device,
78 CuboidShape(size: Vector3(3.2, 1.8, 0.02)).build(),
79 ),
80 RenderMaterial(
81 name: 'screen',
82 lighting: LightingModel.unlit,
83 albedo: _output,
84 ),
85 name: 'screen',
86 );
88 return Scene()..add(screen);
89 }
92 void update(DemoContext context, double dt) {
93 final TextureHandle picture = context.renderer
94 .render(
95 width: _width,
96 height: _height,
97 scene: _source,
98 views: <CameraView>[_sourceView],
99 settings: const RenderSettings(
100 bloom: BloomSettings(enabled: false),
101 shadows: ShadowSettings(enabled: false),
102 tonemap: false,
103 ),
104 )
105 .frame;
107 result = context.renderer.renderPost(
108 hdr: picture,
109 target: _output,
110 settings: RenderSettings(
111 exposure: exposure,
112 bloom: BloomSettings(threshold: threshold, intensity: intensity),
113 ),
114 );
115 }
118 RenderSettings settings(DemoContext context) => const RenderSettings(
119 bloom: BloomSettings(enabled: false),
120 shadows: ShadowSettings(enabled: false),
121 tonemap: false,
122 exposure: 1.0,
123 );
126 List<DemoControl> controls(DemoContext context) => <DemoControl>[
127 SliderControl(
128 'Threshold',
129 min: 0.1,
130 max: 1.5,
131 value: () => threshold,
132 onChanged: (double v) => threshold = v,
133 ),
134 SliderControl(
135 'Intensity',
136 min: 0,
137 max: 1.5,
138 value: () => intensity,
139 onChanged: (double v) => intensity = v,
140 ),
141 SliderControl(
142 'Exposure',
143 min: 0.3,
144 max: 3,
145 value: () => exposure,
146 onChanged: (double v) => exposure = v,
147 ),
148 ];
151 void verify(Scene scene, FrameResult frame) {
152 final PostFrameResult? post = result;
153 if (post == null) {
154 throw StateError('renderPost was never called');
155 }
156 if (!identical(post.frame, _output)) {
157 throw StateError('renderPost drew somewhere other than the target');
158 }
159 }
160}