X-ray silhouettes: the source
Open the live demo · Read the guide · View on GitHub
Regions the guide quotes: layer (line 19), hidden (line 71), settings (line 98), draws (line 121).
1/// X-ray silhouettes: the outline of what a wall hides, painted over the wall.
2///
3/// Quoted by `xray.md` and shown whole in the Source tab.
4library;
6import 'package:flutter3d/flutter3d.dart';
7import 'package:flutter3d_showcase/src/demo/demo.dart';
9/// The `xray` page: a monster behind a wall whose silhouette is painted over
10/// the wall. Its check is that the scene pass made the x-ray draws on top of
11/// the meshes when stencil is there.
12final class XrayDemo extends ShowcaseDemo {
13 /// Whether the monster's silhouette is drawn through the wall.
14 bool enabled = true;
16 /// The silhouette's colour: 0 orange, 1 green, 2 cyan.
17 int colorChoice = 0;
19 /// One bit of `layerMask`, picked so it does not collide with the default.
20 static const int _watched = 1 << 2;
22 static const List<String> _colourNames = <String>['Orange', 'Green', 'Cyan'];
24 Vector3 get _color => switch (colorChoice) {
25 1 => Vector3(0.1, 1.0, 0.2),
26 2 => Vector3(0.1, 0.9, 1.0),
27 _ => Vector3(1.0, 0.32, 0.08),
28 };
30 late final int _meshCount;
31 late final bool _stencil;
34 void configureView(DemoContext context) {
35 context.orbit
36 ..distance = 7.0
37 ..pitch = 0.2
38 ..yaw = 0.0;
39 context.orbit.target.setValues(0.0, 1.0, 0.0);
40 }
43 Scene build(DemoContext context) {
44 final GraphicsDevice device = context.device;
45 _stencil = device.features.has(DeviceFeature.stencil);
47 final MeshNode floor = MeshNode(
48 DeviceMesh.upload(device, const PlaneShape(width: 12, depth: 12).build()),
49 RenderMaterial(
50 name: 'floor',
51 baseColor: LinearColor.fromSrgb(0.5, 0.5, 0.52, 1.0),
52 roughness: 0.9,
53 doubleSided: true,
54 ),
55 name: 'floor',
56 );
58 final MeshNode wall = MeshNode(
59 DeviceMesh.upload(
60 device,
61 CuboidShape(size: Vector3(4.0, 2.4, 0.3)).build(),
62 ),
63 RenderMaterial(
64 name: 'wall',
65 baseColor: LinearColor.fromSrgb(0.65, 0.6, 0.55, 1.0),
66 roughness: 0.9,
67 ),
68 name: 'wall',
69 )..setPosition(0.0, 1.2, 0.0);
71 final MeshNode monster = MeshNode(
72 DeviceMesh.upload(
73 device,
74 const CapsuleShape(radius: 0.4, height: 0.9).build(),
75 ),
76 RenderMaterial(
77 name: 'monster',
78 baseColor: LinearColor.fromSrgb(0.3, 0.7, 0.35, 1.0),
79 roughness: 0.5,
80 ),
81 name: 'monster',
82 )..setPosition(0.0, 0.85, -2.0);
83 monster.layerMask = 1 | _watched;
85 _meshCount = 3;
86 return Scene()
87 ..add(floor)
88 ..add(wall)
89 ..add(monster)
90 ..add(
91 LightNode(name: 'sun', intensity: 3.0 * LightNode.legacyUnit)
92 ..setLocalForward(Vector3(-0.4, -0.8, -0.6)),
93 );
94 }
97 RenderSettings settings(DemoContext context) => RenderSettings(
98 xray: XraySettings(
99 layerMask: enabled ? _watched : 0,
100 color: _color.toLinearColor(),
101 ),
102 );
105 List<DemoControl> controls(DemoContext context) => <DemoControl>[
106 ToggleControl(
107 'X-ray',
108 value: () => enabled,
109 onChanged: (bool v) => enabled = v,
110 ),
111 ChoiceControl(
112 'Colour',
113 options: _colourNames,
114 index: () => colorChoice,
115 onChanged: (int i) => colorChoice = i,
116 ),
117 ];
120 void verify(Scene scene, FrameResult frame) {
121 final int drawn = frame.passes
122 .firstWhere((FramePass p) => p.name == 'scene')
123 .drawCalls;
124 final int extra = enabled && _stencil ? 2 : 0;
125 if (drawn != _meshCount + extra) {
126 throw StateError(
127 'the scene pass made $drawn draws; $_meshCount meshes and '
128 '$extra x-ray draws were expected',
129 );
130 }
131 }
132}