flutter3d
Showcase Changelog 62 packages API reference

Baked visibility: the source

since 0.7.0 Simulation, audio and XR

Regions the guide quotes: level (line 44), bake (line 55), cull (line 72), live (line 89).

1/// Which parts of a level can be seen from where, decided before the game
2/// runs, and applied every frame by hiding the batches a camera cannot see.
3///
4/// Quoted by `baked_visibility.md` and shown whole in the Source tab.
5library;
6
7import 'dart:math' as math;
8
9import 'package:flutter3d/flutter3d.dart';
10import 'package:flutter3d/flutter3d.dart' as f3d show RenderMaterial;
11import 'package:flutter3d_app/flutter3d_app.dart';
12import 'package:flutter3d_showcase/src/demo/demo.dart';
13import 'package:flutter3d_showcase/src/demo/scene_kit.dart';
15/// The `baked-visibility` page: a corridor and a room, with an eye whose
16/// side of a solid wall stays visible. Its check is that the near side shows
17/// and the far side behind a wall with no doorway is hidden.
18final class BakedVisibilityDemo extends ShowcaseDemo {
19 late final String _report;
21 /// Where the eye stands along x, in metres, -1 to 10.9; written each frame
22 /// while [walking].
23 double eyeX = 0.0;
25 /// Whether the eye walks back and forth on its own; dragging the slider
26 /// stops it.
27 bool walking = true;
29 late final VisibilityCuller _live;
30 late final MeshNode _eye;
31 double _clock = 0.0;
33 @override
34 void configureView(DemoContext context) {
35 context.orbit
36 ..distance = 15.0
37 ..pitch = 0.85
38 ..yaw = 0.0;
39 context.orbit.target.setValues(5.0, 0.0, 2.0);
40 }
42 @override
43 Scene build(DemoContext context) {
44 // A floor the whole way along, and one solid wall crossing it a third of
45 // the way down: nothing on the near side of the wall can be seen from
46 // the far side.
47 final level = Level(
48 name: 'corridor',
49 brushes: <Brush>[
50 Brush(center: WorldPosition(5, -2, 2), size: Vector3(14, 4, 4)),
51 Brush(center: WorldPosition(5, 1, 2), size: Vector3(1.0, 8, 4)),
52 ],
53 );
55 final visibility = LevelVisibility.bake(level, cellSize: 1.0);
57 final material = f3d.RenderMaterial(
58 name: 'room',
59 baseColor: LinearColor.fromSrgb(0.6, 0.6, 0.8, 1.0),
60 );
61 final nearMarker = MeshNode(
62 DeviceMesh.upload(context.device, CuboidShape().build()),
63 material,
64 name: 'near',
65 )..setPositionFrom(Vector3(0, 0.5, 2));
66 final farMarker = MeshNode(
67 DeviceMesh.upload(context.device, CuboidShape().build()),
68 material,
69 name: 'far',
70 )..setPositionFrom(Vector3(10, 0.5, 2));
72 final culler = VisibilityCuller(visibility, <VisibilityBatch>[
73 VisibilityBatch(
74 node: nearMarker,
75 bounds: Aabb3.minMax(Vector3(-1, 0, 1), Vector3(1, 2, 3)),
76 ),
77 VisibilityBatch(
78 node: farMarker,
79 bounds: Aabb3.minMax(Vector3(9, 0, 1), Vector3(11, 2, 3)),
80 ),
81 ]);
82 final hiddenFromNearSide = culler.apply(Vector3(0, 0.2, 2));
84 _report =
85 'standing on the near side, $hiddenFromNearSide of 2 batches are '
86 'hidden; near marker visible: ${nearMarker.isVisible}, far marker '
87 'visible: ${farMarker.isVisible}';
89 // The same culler, over four markers, asked again from wherever the eye
90 // is each frame: a marker is on when the eye's own cell can see it.
91 const List<double> stands = <double>[0.0, 1.5, 8.5, 10.0];
92 final List<MeshNode> markers = <MeshNode>[
93 for (final double x in stands)
94 blockNode(
95 context,
96 'marker $x',
97 Vector3(1.4, 1.4, 1.4),
98 Vector4(0.85, 0.6, 0.35, 1.0),
99 at: Vector3(x, 0.7, 2.0),
100 ),
101 ];
102 _live = VisibilityCuller(visibility, <VisibilityBatch>[
103 for (final MeshNode marker in markers)
104 VisibilityBatch(
105 node: marker,
106 bounds: Aabb3.minMax(
107 marker.readPosition() - Vector3.all(0.7),
108 marker.readPosition() + Vector3.all(0.7),
109 ),
110 ),
111 ]);
112 _eye = ballNode(
113 context,
114 'eye',
115 0.35,
116 Vector4(0.45, 0.65, 0.95, 1.0),
117 at: Vector3(0.0, 0.35, 2.0),
118 );
119 return sceneOf(<SceneNode>[
120 blockNode(
121 context,
122 'floor',
123 Vector3(14.0, 0.1, 4.0),
124 Vector4(0.34, 0.38, 0.36, 1.0),
125 at: Vector3(5.0, -0.05, 2.0),
126 ),
127 blockNode(
128 context,
129 'wall',
130 Vector3(1.0, 3.0, 4.0),
131 Vector4(0.55, 0.5, 0.5, 1.0),
132 at: Vector3(5.0, 1.5, 2.0),
133 ),
134 _eye,
135 ...markers,
136 ]);
137 }
139 @override
140 void update(DemoContext context, double dt) {
141 _clock += dt;
142 if (walking) eyeX = 5.0 + 5.8 * math.sin(_clock * 0.4);
143 _eye.setPosition(eyeX, 0.35, 2.0);
144 _live.apply(Vector3(eyeX, 0.2, 2.0));
145 }
147 @override
148 List<DemoControl> controls(DemoContext context) => <DemoControl>[
149 SliderControl(
150 'Eye at',
151 min: -1.0,
152 max: 10.9,
153 value: () => eyeX,
154 onChanged: (double v) {
155 walking = false;
156 eyeX = v;
157 },
158 format: (double v) => '${v.toStringAsFixed(1)} m',
159 ),
160 ToggleControl(
161 'Walk about',
162 value: () => walking,
163 onChanged: (bool v) => walking = v,
164 ),
165 ];
167 @override
168 void verify(Scene scene, FrameResult frame) {
169 if (frame.drawCalls < 1) {
170 throw StateError('a marker was not drawn');
171 }
172 if (!_report.contains('near marker visible: true')) {
173 throw StateError('the side the eye stands on should stay visible');
174 }
175 if (!_report.contains('far marker visible: false')) {
176 throw StateError(
177 'the far side of a solid wall with no doorway should be hidden',
178 );
179 }
180 }