glTF and GLB: the source
Open the live demo · Read the guide · View on GitHub
Regions the guide quotes: write (line 25), load (line 53), upload (line 60), check (line 98).
1/// Reading a real GLB with `GltfLoader`, into meshes this page draws.
2///
3/// Quoted by `gltf_load.md` and shown whole in the Source tab.
4library;
6import 'package:flutter3d/flutter3d.dart';
7import 'package:flutter3d_showcase/src/demo/demo.dart';
9/// The `gltf-load` page: the meshes of a GLB fixture, loaded and drawn. Its
10/// check is the number of surfaces, that they have vertices, and that both
11/// are drawn.
12final class GltfLoadDemo extends ShowcaseDemo {
13 late final GltfAsset _asset;
16 void configureView(DemoContext context) {
17 context.orbit
18 ..distance = 3.0
19 ..pitch = 0.35
20 ..yaw = 0.6;
21 }
24 Future<void> prepare(DemoContext context) async {
25 // A real GLB, written by this engine's own `GltfWriter` so this page
26 // needs no bundled asset to read: two surfaces, one material each.
27 final source = PlainModelDocument(
28 surfaces: <ModelSurface>[
29 ModelSurface(
30 mesh: CuboidShape(size: Vector3.all(0.9)).build(),
31 materialIndex: 0,
32 ),
33 ModelSurface(
34 mesh: SphereShape(segments: 32, rings: 16).build(),
35 materialIndex: 1,
36 ),
37 ],
38 materials: <SurfaceMaterial>[
39 SurfaceMaterial(baseColor: LinearColor.fromSrgb(0.8, 0.4, 0.3, 1.0)),
40 SurfaceMaterial(
41 baseColor: LinearColor.fromSrgb(0.3, 0.6, 0.8, 1.0),
42 metallic: 0.8,
43 roughness: 0.3,
44 ),
45 ],
46 nodes: <ModelNode>[
47 ModelNode(surfaces: <int>[0]),
48 ModelNode(surfaces: <int>[1], translation: Vector3(1.6, 0.0, 0.0)),
49 ],
50 );
51 final bytes = GltfWriter(source).writeGlb();
53 // `GltfLoader.load` reads the container, walks its accessors and
54 // materials, and hands back a `GltfAsset`, whatever wrote the file.
55 _asset = await GltfLoader().load(bytes);
56 }
59 Scene build(DemoContext context) {
60 // `GltfAsset` is a `ModelDocument`: a list of surfaces, each with an
61 // index into the document's materials, plus a node hierarchy that
62 // places them. This page uploads each surface by hand rather than
63 // through `ModelAsset`, to keep the loader itself the whole story; the
64 // `model-asset` page shows the upload path a real application uses,
65 // textures included.
66 final scene = Scene();
67 for (final ModelNode node in _asset.nodes) {
68 for (final int surfaceIndex in node.surfaces) {
69 final ModelSurface surface = _asset.surfaces[surfaceIndex];
70 final SurfaceMaterial? material = surface.materialIndex == null
71 ? null
72 : _asset.materials[surface.materialIndex!];
73 scene.add(
74 MeshNode(
75 DeviceMesh.upload(context.device, surface.mesh),
76 RenderMaterial(
77 name: material?.name,
78 baseColor:
79 material?.baseColor ?? LinearColor.fromSrgb(0.8, 0.8, 0.8),
80 metallic: material?.metallic ?? 0.0,
81 roughness: material?.roughness ?? 0.6,
82 ),
83 name: surface.name,
84 )..setPositionFrom(node.translation),
85 );
86 }
87 }
89 scene.add(
90 LightNode(name: 'sun', intensity: 3.0 * LightNode.legacyUnit)
91 ..setLocalForward(Vector3(-0.4, -1.0, -0.3)),
92 );
93 return scene;
94 }
97 void verify(Scene scene, FrameResult frame) {
98 if (_asset.surfaces.length != 2) {
99 throw StateError('the GLB decoded to the wrong number of surfaces');
100 }
101 if (_asset.vertexCount == 0) {
102 throw StateError('the decoded mesh has no vertices');
103 }
104 if (frame.drawCalls < 2) {
105 throw StateError('both surfaces were not drawn');
106 }
107 }
108}