Depth of field: the source
Open the live demo · Read the guide · View on GitHub
Regions the guide quotes: row (line 34), lens (line 60), ran (line 98).
1/// Depth of field from a thin lens: a focus distance, a focal length and an
2/// f-number, the same three numbers a photographer already knows.
3///
4/// Quoted by `depth_of_field.md` and shown whole in the Source tab.
5library;
7import 'package:flutter3d/flutter3d.dart';
8import 'package:flutter3d_showcase/src/demo/demo.dart';
10/// The `depth-of-field` page: a row of posts going back from the eye,
11/// through a thin lens. Its check is that the depth of field pass ran.
12final class DepthOfFieldDemo extends ShowcaseDemo {
13 /// How far from the eye the lens is focused, in metres, 1 to 11.
14 double focusDistance = 4.0;
16 /// The lens's f-number, 1.4 to 16: the smaller, the shallower the focus.
17 double aperture = 2.0;
19 /// The lens's focal length in metres, 0.024 to 0.135, shown as 24 to 135
20 /// mm.
21 double focalLength = 0.085;
24 void configureView(DemoContext context) {
25 context.orbit
26 ..distance = 4.0
27 ..pitch = 0.0
28 ..yaw = 0.0;
29 context.orbit.target.setValues(0.0, 0.0, -4.0);
30 }
33 Scene build(DemoContext context) {
34 final Scene scene = Scene();
35 final DeviceMesh post = DeviceMesh.upload(
36 context.device,
37 CuboidShape(size: Vector3(0.4, 1.6, 0.4)).build(),
38 );
39 for (var i = 0; i < 6; i++) {
40 final double z = -1.0 - i * 2.0;
41 scene.add(
42 MeshNode(
43 post,
44 RenderMaterial(
45 name: 'post $i',
46 baseColor: LinearColor.fromSrgb(0.85, 0.4 + i * 0.08, 0.2, 1.0),
47 ),
48 name: 'post $i',
49 )..setPosition(0.0, 0.0, z),
50 );
51 }
52 return scene..add(
53 LightNode(name: 'sun', intensity: 3.0 * LightNode.legacyUnit)
54 ..setLocalForward(Vector3(-0.3, -0.7, -0.4)),
55 );
56 }
59 RenderSettings settings(DemoContext context) => RenderSettings(
60 depthOfField: DepthOfFieldSettings(
61 enabled: true,
62 focusDistance: focusDistance,
63 focalLength: focalLength,
64 aperture: aperture,
65 ),
66 );
69 List<DemoControl> controls(DemoContext context) => <DemoControl>[
70 SliderControl(
71 'Focus distance',
72 min: 1,
73 max: 11,
74 value: () => focusDistance,
75 onChanged: (double v) => focusDistance = v,
76 format: (double v) => '${v.toStringAsFixed(1)} m',
77 ),
78 SliderControl(
79 'Aperture (f/)',
80 min: 1.4,
81 max: 16,
82 value: () => aperture,
83 onChanged: (double v) => aperture = v,
84 format: (double v) => v.toStringAsFixed(1),
85 ),
86 SliderControl(
87 'Focal length',
88 min: 0.024,
89 max: 0.135,
90 value: () => focalLength,
91 onChanged: (double v) => focalLength = v,
92 format: (double v) => '${(v * 1000).round()} mm',
93 ),
94 ];
97 void verify(Scene scene, FrameResult frame) {
98 if (!frame.passes.any((FramePass p) => p.name == 'depth of field')) {
99 throw StateError('the depth of field pass did not run: ${frame.skipped}');
100 }
101 }
102}