Loading a level: the source
Open the live demo · Read the guide · View on GitHub
Regions the guide quotes: document (line 19), build (line 35), shared (line 41).
1/// Reading a level document into something playable: brushes become mesh
2/// nodes, lights become light nodes, and a shared mesh cache uploads each
3/// repeated shape once.
4///
5/// Quoted by `level_loader.md` and shown whole in the Source tab.
6library;
8import 'package:flutter3d/flutter3d.dart';
9import 'package:flutter3d_app/flutter3d_app.dart';
10import 'package:flutter3d_showcase/src/demo/demo.dart';
12/// The `level-loader` page: a level document loaded into nodes. Its check is
13/// that its one brush became one mesh node and that a clean document loads
14/// with no issues.
15final class LevelLoaderDemo extends ShowcaseDemo {
16 late LoadedLevel _loaded;
17 late SharedMeshes _shared;
19 static Level _document() => Level(
20 name: 'room',
21 brushes: <Brush>[
22 Brush(center: WorldPosition(0, -0.5, 0), size: Vector3(4, 1, 4)),
23 ],
24 lights: <LevelLight>[
25 LevelLight(
26 position: WorldPosition(0, 3, 0),
27 intensity: 5.0 * LevelLight.absentIntensity,
28 range: 10.0,
29 ),
30 ],
31 );
34 Future<void> prepare(DemoContext context) async {
35 _loaded = await const LevelLoader().build(
36 _document(),
37 device: context.device,
38 registry: EntityRegistry(const <EntityKind>[]),
39 );
41 // Three torch posts that all ask the cache for the same box, uploaded
42 // once and reused by all three nodes.
43 _shared = SharedMeshes(context.device);
44 for (var i = 0; i < 3; i++) {
45 final mesh = _shared.box(Vector3(0.2, 1.0, 0.2));
46 final post = MeshNode(
47 mesh,
48 RenderMaterial(
49 name: 'post',
50 baseColor: LinearColor.fromSrgb(0.4, 0.3, 0.2, 1.0),
51 ),
52 )..setPosition(-1.0 + i.toDouble(), 0.0, 1.5);
53 _loaded.scene.add(post);
54 }
55 }
58 Scene build(DemoContext context) => _loaded.scene;
61 void verify(Scene scene, FrameResult frame) {
62 if (frame.drawCalls < 1) {
63 throw StateError('the loaded level was not drawn');
64 }
65 if (_loaded.brushNodes.length != 1) {
66 throw StateError(
67 'the one brush in the document should become one '
68 'mesh node',
69 );
70 }
71 if (_loaded.issues.isNotEmpty) {
72 throw StateError(
73 'a clean document should load with no issues, got '
74 '${_loaded.issues}',
75 );
76 }
77 }
78}