flutter3d
Showcase Changelog 62 packages API reference

Picking by pixel: the source

since 0.4.3 Views, picking and input

Regions the guide quotes: pick (line 44).

1/// Asking the renderer which mesh is drawn at a point, by drawing the scene
2/// once more as ids and reading one pixel back.
3///
4/// Quoted by `pixel_picking.md` and shown whole in the Source tab.
5library;
6
7import 'package:flutter3d/flutter3d.dart';
8import 'package:flutter3d_showcase/src/demo/demo.dart';
9
10/// The `pixel-picking` page: a ball picked by drawing ids and reading one
11/// pixel. Its check is that the pixel asked about is over the ball.
12final class PixelPickingDemo extends ShowcaseDemo {
13 late final CameraNode _camera;
14 final Raycaster _raycaster = Raycaster();
16 /// What the last question answered, once the frame that drew the ids has
17 /// come back. Null until then, and null again for a click on nothing.
18 MeshNode? lastAnswer;
20 @override
21 Scene build(DemoContext context) {
22 _camera = context.camera;
23 final RenderMaterial stone = RenderMaterial(
24 name: 'stone',
25 baseColor: LinearColor.fromSrgb(0.45, 0.6, 0.7, 1.0),
26 roughness: 0.6,
27 );
28 final MeshNode ball = MeshNode(
29 DeviceMesh.upload(
30 context.device,
31 SphereShape(segments: 32, rings: 16).build(),
32 ),
33 stone,
34 name: 'ball',
35 );
36 return Scene()
37 ..add(ball)
38 ..add(
39 LightNode(name: 'sun', intensity: 3.0 * LightNode.legacyUnit)
40 ..setLocalForward(Vector3(-0.4, -1.0, -0.3)),
41 );
42 }
44 void _pickCentre(DemoContext context) {
45 context.renderer
46 .pickPixel(0.5, 0.5)
47 .then((MeshNode? node) => lastAnswer = node);
48 }
50 @override
51 void update(DemoContext context, double dt) => _pickCentre(context);
53 @override
54 void verify(Scene scene, FrameResult frame) {
55 if (frame.drawCalls < 1) {
56 throw StateError('the ball was not drawn');
57 }
58 // pickPixel answers a frame or two after it is asked, on a completer this
59 // page does not block on. The honest, synchronous half of the claim is
60 // that (0.5, 0.5), the middle of the frame, really is over the mesh the
61 // id pass would find there. A raycaster through the same point checks
62 // exactly that, with no frame involved.
63 final HitResult? hit = _raycaster
64 .setFromNdc(_camera, 0.0, 0.0, aspect: 16 / 9)
65 .intersectScene(scene);
66 if (hit?.node?.name != 'ball') {
67 throw StateError('the pixel asked about is not over the ball');
68 }
69 }