flutter3d
Showcase Changelog 62 packages API reference

A camera into a texture: the source

since 1.0.0-rc.1 Scene and geometry

Regions the guide quotes: watcher (line 83), texture (line 95), screen (line 103), add (line 132), refresh (line 143), check (line 173).

1/// A camera that draws into a texture: a monitor on a stand shows what a
2/// second camera in the room sees, in the same frame it was taken.
3///
4/// Quoted by `render_textures.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/src/demo/demo.dart';
12/// The `render-textures` page: a monitor showing what a watcher camera in
13/// the room sees. Its check is that the pass ran and drew into the texture.
14final class RenderTexturesDemo extends ShowcaseDemo {
15 /// Whether the watcher redraws its texture every frame rather than once.
16 bool everyFrame = true;
17 bool _retakeAsked = false;
19 late final CameraNode _watcher;
20 late final CameraView _picture;
21 late final MeshNode _spinner;
22 late final Renderer _renderer;
23 double _age = 0.0;
25 @override
26 void configureView(DemoContext context) {
27 context.orbit
28 ..distance = 6.5
29 ..pitch = 0.35
30 ..yaw = 0.3;
31 context.orbit.target.setValues(0.4, 0.8, 0.0);
32 }
34 MeshNode _slab(
35 DemoContext context,
36 String name,
37 Vector3 size,
38 Vector3 at,
39 Vector4 color,
40 ) => MeshNode(
41 DeviceMesh.upload(context.device, CuboidShape(size: size).build()),
42 RenderMaterial(name: name, baseColor: _fromSrgb(color), roughness: 0.7),
43 name: name,
44 )..setPositionFrom(at);
46 @override
47 Scene build(DemoContext context) {
48 _renderer = context.renderer;
49 final Scene scene = Scene()
50 ..ambientColor = LinearColor(0.55, 0.6, 0.7)
51 ..ambientIntensity = 0.3 * LightNode.legacyUnit
52 ..add(
53 _slab(
54 context,
55 'floor',
56 Vector3(6.0, 0.1, 6.0),
57 Vector3(0.0, -0.05, 0.0),
58 Vector4(0.6, 0.62, 0.6, 1.0),
59 ),
60 )
61 ..add(
62 _slab(
63 context,
64 'red block',
65 Vector3(0.6, 0.6, 0.6),
66 Vector3(-1.4, 0.3, -0.6),
67 Vector4(0.85, 0.2, 0.15, 1.0),
68 ),
69 )
70 ..add(
71 LightNode(name: 'sun', intensity: 2.0 * LightNode.legacyUnit)
72 ..setLocalForward(Vector3(-0.6, -1.0, -0.4)),
73 );
74 _spinner = _slab(
75 context,
76 'spinner',
77 Vector3(0.5, 0.9, 0.5),
78 Vector3(-0.6, 0.45, 0.6),
79 Vector4(0.2, 0.45, 0.9, 1.0),
80 );
81 scene.add(_spinner);
83 // An ordinary camera, put in the scene like any node, with a projection
84 // of its own. The texture sets its aspect.
85 _watcher = CameraNode(name: 'watcher')
86 ..projection = const PerspectiveProjection(
87 fieldOfView: math.pi / 4,
88 near: 0.1,
89 far: 50.0,
90 )
91 ..setPosition(-3.0, 1.6, 2.4)
92 ..lookAt(Vector3(-0.9, 0.4, 0.0));
93 scene.add(_watcher);
95 _picture = CameraView.texture(
96 context.device,
97 camera: _watcher,
98 width: 160,
99 height: 120,
100 clearColor: LinearColor.fromSrgb(0.35, 0.45, 0.6, 1.0),
101 );
103 // An unlit plane shows the picture exactly as the camera took it. A
104 // plane stood up has v running down it, the way a picture's rows do.
105 final MeshNode screen = MeshNode(
106 DeviceMesh.upload(
107 context.device,
108 const PlaneShape(width: 1.6, depth: 1.2).build(),
109 ),
110 RenderMaterial(
111 name: 'screen',
112 lighting: LightingModel.unlit,
113 albedo: _picture.texture,
114 ),
115 name: 'screen',
116 )..setPosition(1.4, 1.3, -0.6);
117 screen.setRotation(
118 Quaternion.axisAngle(Vector3(0.0, 1.0, 0.0), -0.4) *
119 Quaternion.axisAngle(Vector3(1.0, 0.0, 0.0), math.pi / 2),
120 );
121 final MeshNode stand = _slab(
122 context,
123 'stand',
124 Vector3(0.12, 0.7, 0.12),
125 Vector3(1.4, 0.35, -0.6),
126 Vector4(0.2, 0.2, 0.22, 1.0),
127 );
128 scene
129 ..add(screen)
130 ..add(stand);
132 // The monitor is left out of its own picture, and the texture joins the
133 // scene, which draws it before the frame's own meshes.
134 _picture.excluded.addAll(<MeshNode>[screen, stand]);
135 scene.addTextureView(_picture);
136 return scene;
137 }
139 @override
140 void update(DemoContext context, double dt) {
141 _age += dt;
142 _spinner.setRotation(Quaternion.axisAngle(Vector3(0.0, 1.0, 0.0), _age));
143 // Every frame, or once and then only when asked: a picture of a room
144 // nobody changes does not need taking sixty times a second.
145 _picture.settings = _picture.settings.copyWith(
146 refreshEveryFrame: everyFrame,
147 );
148 if (_retakeAsked) {
149 _retakeAsked = false;
150 _picture.invalidate();
151 }
152 }
154 @override
155 void dispose() => _renderer.releaseTextureAfterFrame(_picture.texture!);
157 @override
158 List<DemoControl> controls(DemoContext context) => <DemoControl>[
159 ToggleControl(
160 'Every frame',
161 value: () => everyFrame,
162 onChanged: (bool v) => everyFrame = v,
163 ),
164 ToggleControl(
165 'Take the picture again',
166 value: () => _retakeAsked,
167 onChanged: (bool v) => _retakeAsked = v,
168 ),
169 ];
171 @override
172 void verify(Scene scene, FrameResult frame) {
173 // The pass that draws the scene's textures ran, and the texture holds a
174 // picture rather than whatever its allocation held.
175 final PassSkip? skipped = frame.skipReasonOf('render textures');
176 if (skipped != null) {
177 throw StateError('the render textures pass was skipped: $skipped');
178 }
179 if (!frame.passes.any((FramePass p) => p.name == 'render textures')) {
180 throw StateError('the render textures pass did not run');
181 }
182 if (!_picture.isDrawn) {
183 throw StateError('the camera never drew into its texture');
184 }
185 }
188/// A `Vector4` holding a colour sRGB-encoded, as the linear colour it names.
189LinearColor _fromSrgb(Vector4 c) => LinearColor.fromSrgb(c.x, c.y, c.z, c.w);