flutter3d
Showcase Changelog 62 packages API reference

Path-finding: the source

since 0.4.1 Simulation, audio and XR

Regions the guide quotes: brushes (line 46), bake (line 54), live (line 79), query (line 138).

1/// Where an agent can stand, baked once from the level's architecture into a
2/// lattice a step can query in one array lookup.
3///
4/// Quoted by `nav_grid.md` and shown whole in the Source tab.
5library;
6
7import 'dart:math' as math;
8
9import 'package:flutter/widgets.dart';
10import 'package:flutter3d/flutter3d.dart';
11import 'package:flutter3d_showcase/src/demo/demo.dart';
12import 'package:flutter3d_sim/flutter3d_sim.dart';
14/// The `nav-grid` page: slabs, a bridge and a gap baked into a walkable grid,
15/// and a probe swept over it. Its check is that the slabs and the bridge are
16/// walkable and the gap is not.
17final class NavGridDemo extends ShowcaseDemo {
18 late final NavGrid _grid;
20 /// Whether the probe sweeps the grid on its own.
21 bool sweeping = true;
22 double _clock = 0.0;
24 /// Where the probe is, in metres.
25 final Vector2 _probe = Vector2(2.0, 2.0);
27 @override
28 Scene build(DemoContext context) {
29 _grid = _bake();
30 final material = RenderMaterial(
31 name: 'floor',
32 baseColor: LinearColor.fromSrgb(0.5, 0.55, 0.6, 1.0),
33 );
34 final node = MeshNode(
35 DeviceMesh.upload(context.device, SphereShape(segments: 16).build()),
36 material,
37 );
38 return Scene()
39 ..add(node)
40 ..add(
41 LightNode(name: 'sun', intensity: 3.0 * LightNode.legacyUnit)
42 ..setLocalForward(Vector3(-0.4, -1.0, -0.3)),
43 );
44 }
46 /// Two floor slabs joined by a narrow bridge, so most of the gap between
47 /// them is not walkable but the bridge is.
48 static List<Brush> _brushes() => <Brush>[
49 Brush(center: WorldPosition(2, 0, 2), size: Vector3(4, 1, 4)),
50 Brush(center: WorldPosition(5.5, 0, 3.5), size: Vector3(3, 1, 1)),
51 Brush(center: WorldPosition(9, 0, 2), size: Vector3(4, 1, 4)),
52 ];
54 static NavGrid _bake() => NavGrid.bake(_brushes(), cellSize: 0.5);
56 @override
57 void update(DemoContext context, double dt) {
58 _clock += dt;
59 if (sweeping) {
60 // Across both slabs and the gap between, and over the edge.
61 _probe.setValues(
62 5.5 + 5.5 * math.sin(_clock * 0.5),
63 2.4 + 2.2 * math.sin(_clock * 0.83 + 1.0),
64 );
65 }
66 }
68 @override
69 List<DemoControl> controls(DemoContext context) => <DemoControl>[
70 ToggleControl(
71 'Sweep the probe',
72 value: () => sweeping,
73 onChanged: (bool v) => sweeping = v,
74 ),
75 ];
77 @override
78 Widget? customBody(BuildContext buildContext, DemoContext context) {
79 // Asking the baked grid about one point: which cell it falls in, and
80 // whether an agent could stand there.
81 final int cell = _grid.cellAtPoint(_probe.x, _probe.y);
82 final bool walkable = _grid.isWalkable(cell);
83 return ColoredBox(
84 color: const Color(0xFF14161A),
85 child: Padding(
86 padding: const EdgeInsets.all(24),
87 child: Column(
88 crossAxisAlignment: CrossAxisAlignment.start,
89 children: <Widget>[
90 Text(
91 'probe at (${_probe.x.toStringAsFixed(1)}, '
92 '${_probe.y.toStringAsFixed(1)}): '
93 '${walkable ? 'walkable' : 'not walkable'}. Tap the map to '
94 'put it somewhere.',
95 style: const TextStyle(color: Color(0xFFE8E8EC), fontSize: 15),
96 ),
97 const SizedBox(height: 16),
98 Expanded(
99 child: Center(
100 child: AspectRatio(
101 aspectRatio: _grid.columns / _grid.rows,
102 child: LayoutBuilder(
103 builder: (BuildContext context, BoxConstraints box) =>
104 GestureDetector(
105 onTapDown: (TapDownDetails d) {
106 sweeping = false;
107 _probe.setValues(
108 d.localPosition.dx /
109 box.maxWidth *
110 _grid.columns *
111 _grid.cellSize,
112 d.localPosition.dy /
113 box.maxHeight *
114 _grid.rows *
115 _grid.cellSize,
116 );
117 },
118 child: CustomPaint(
119 painter: _GridPainter(_grid, _probe, walkable),
120 child: const SizedBox.expand(),
121 ),
122 ),
123 ),
124 ),
125 ),
126 ),
127 ],
128 ),
129 ),
130 );
131 }
133 @override
134 void verify(Scene scene, FrameResult frame) {
135 if (frame.drawCalls < 1) {
136 throw StateError('the floor marker was not drawn');
137 }
138 final onFirstSlab = _grid.isWalkable(_grid.cellAtPoint(2.0, 2.0));
139 final inTheGap = _grid.isWalkable(_grid.cellAtPoint(4.5, 0.5));
140 final onBridge = _grid.isWalkable(_grid.cellAtPoint(5.0, 3.5));
141 if (!onFirstSlab || inTheGap || !onBridge) {
142 throw StateError(
143 'the slabs and the bridge should be walkable and the open gap '
144 'should not',
145 );
146 }
147 }
150/// Draws the grid from above: light for a walkable cell, dark for not.
151final class _GridPainter extends CustomPainter {
152 const _GridPainter(this.grid, this.probe, this.walkable);
154 final NavGrid grid;
155 final Vector2 probe;
156 final bool walkable;
158 @override
159 void paint(Canvas canvas, Size size) {
160 final cellW = size.width / grid.columns;
161 final cellH = size.height / grid.rows;
162 final paint = Paint();
163 for (var index = 0; index < grid.cellCount; index++) {
164 final cx = grid.cellX(index);
165 final cz = grid.cellZ(index);
166 paint.color = grid.isWalkable(index)
167 ? const Color(0xFF7FB0FF)
168 : const Color(0xFF2A2C33);
169 canvas.drawRect(
170 Rect.fromLTWH(cx * cellW, cz * cellH, cellW, cellH),
171 paint,
172 );
173 }
174 final Offset at = Offset(
175 probe.x / grid.cellSize * cellW,
176 probe.y / grid.cellSize * cellH,
177 );
178 canvas.drawCircle(
179 at,
180 cellW * 1.6,
181 Paint()
182 ..color = walkable ? const Color(0xFF6CE07C) : const Color(0xFFE5665A),
183 );
184 }
186 @override
187 bool shouldRepaint(covariant _GridPainter oldDelegate) => true;