flutter3d
Showcase Changelog 62 packages API reference

Motion blur: the source

since 0.8.0 Post-processing

Regions the guide quotes: wheel (line 46), turn (line 103), settings (line 109), ran (line 151).

1/// Motion blur: what moved while the shutter was open, smeared along the way
2/// it moved.
3///
4/// Quoted by `motion_blur.md` and shown whole in the Source tab.
5library;
6
7import 'dart:math' as math;
8
9import 'package:flutter3d/flutter3d.dart';
10import 'package:flutter3d_showcase/pages/post/post_stage.dart';
11import 'package:flutter3d_showcase/src/demo/demo.dart';
13/// The `motion-blur` page: a spinning wheel of spokes in front of a
14/// backdrop. Its check is that the pass does not run while it is off.
15final class MotionBlurDemo extends ShowcaseDemo {
16 /// Whether motion blur is on.
17 bool blur = true;
19 /// How long the shutter is open, as a share of a frame, 0 to 1, shown as a
20 /// shutter angle of 0 to 360 degrees.
21 double shutter = 0.5;
23 /// The longest a streak may reach each side, in pixels, 1 to 64.
24 double maxRadius = 20.0;
26 /// How fast the wheel spins, in radians a second, 0 to 20.
27 double speed = 8.0;
29 /// The wheel's angle, in radians, which [update] advances.
30 double _angle = 0.0;
32 late final SceneNode _wheel;
34 @override
35 void configureView(DemoContext context) {
36 context.orbit
37 ..distance = 5.0
38 ..pitch = 0.0
39 ..yaw = 0.0;
40 context.orbit.target.setValues(0.0, 0.0, 0.0);
41 }
43 @override
44 Scene build(DemoContext context) {
45 final GraphicsDevice device = context.device;
46 final DeviceMesh spoke = DeviceMesh.upload(
47 device,
48 CuboidShape(size: Vector3(3.0, 0.3, 0.3)).build(),
49 );
50 final List<Vector4> colors = <Vector4>[
51 Vector4(0.9, 0.8, 0.2, 1.0),
52 Vector4(0.2, 0.7, 0.9, 1.0),
53 Vector4(0.9, 0.3, 0.3, 1.0),
54 ];
55 _wheel = SceneNode(name: 'wheel');
56 for (int i = 0; i < 3; i++) {
57 _wheel.add(
58 MeshNode(
59 spoke,
60 RenderMaterial(baseColor: _fromSrgb(colors[i])),
61 name: 'spoke $i',
62 )..setRotationYawPitchRoll(0.0, 0.0, i * math.pi / 3),
63 );
64 }
65 _wheel.add(
66 MeshNode(
67 DeviceMesh.upload(
68 device,
69 const CylinderShape(
70 radiusTop: 0.35,
71 radiusBottom: 0.35,
72 height: 0.5,
73 ).build(),
74 ),
75 RenderMaterial(baseColor: LinearColor.fromSrgb(0.5, 0.5, 0.5, 1.0)),
76 name: 'hub',
77 )..setRotationYawPitchRoll(0.0, math.pi / 2, 0.0),
78 );
80 final MeshNode backdrop = MeshNode(
81 DeviceMesh.upload(
82 device,
83 CuboidShape(size: Vector3(14.0, 9.0, 0.1)).build(),
84 ),
85 RenderMaterial(
86 baseColor: LinearColor.fromSrgb(0.35, 0.35, 0.38, 1.0),
87 roughness: 0.9,
88 ),
89 name: 'backdrop',
90 )..setPosition(0.0, 0.0, -1.0);
91 final LightNode sun =
92 LightNode(name: 'sun', intensity: 3.0 * LightNode.legacyUnit)
93 ..castsShadow = false
94 ..setLocalForward(Vector3(-2.0, -3.0, -4.0).normalized());
95 return Scene()
96 ..add(backdrop)
97 ..add(_wheel)
98 ..add(sun);
99 }
101 @override
102 void update(DemoContext context, double dt) {
103 _angle += speed * dt;
104 _wheel.setRotationYawPitchRoll(0.0, 0.0, _angle);
105 }
107 @override
108 RenderSettings settings(DemoContext context) => RenderSettings(
109 motionBlur: MotionBlurSettings(
110 enabled: blur,
111 shutterFraction: shutter,
112 maxRadius: maxRadius,
113 ),
114 );
116 @override
117 List<DemoControl> controls(DemoContext context) => <DemoControl>[
118 ToggleControl(
119 'Motion blur',
120 value: () => blur,
121 onChanged: (bool v) => blur = v,
122 ),
123 SliderControl(
124 'Shutter',
125 min: 0,
126 max: 1,
127 value: () => shutter,
128 onChanged: (double v) => shutter = v,
129 format: (double v) => '${(v * 360).round()}°',
130 ),
131 SliderControl(
132 'Streak each side',
133 min: 1,
134 max: 64,
135 value: () => maxRadius,
136 onChanged: (double v) => maxRadius = v,
137 format: (double v) => '${v.round()} px',
138 ),
139 SliderControl(
140 'Speed',
141 min: 0,
142 max: 20,
143 value: () => speed,
144 onChanged: (double v) => speed = v,
145 format: (double v) => '${v.toStringAsFixed(1)} rad/s',
146 ),
147 ];
149 @override
150 void verify(Scene scene, FrameResult frame) {
151 if (blur) {
152 expectPassOrDecline(frame, 'motion blur');
153 } else if (passRan(frame, 'motion blur')) {
154 throw StateError('motion blur is off and its pass ran');
155 }
156 }
159/// A `Vector4` holding a colour sRGB-encoded, as the linear colour it names.
160LinearColor _fromSrgb(Vector4 c) => LinearColor.fromSrgb(c.x, c.y, c.z, c.w);