flutter3d
Showcase Changelog 62 packages API reference

Writing GLB: the source

since 0.7.0 Formats and I/O

Regions the guide quotes: document (line 22), write (line 43), report (line 70), check (line 99).

1/// Writing a GLB with `GltfWriter`, plain and with `compressGeometry`.
2///
3/// Quoted by `gltf_write.md` and shown whole in the Source tab.
4library;
5
6import 'package:flutter/widgets.dart';
7import 'package:flutter3d/flutter3d.dart';
8import 'package:flutter3d_showcase/src/demo/demo.dart';
9
10/// The `gltf-write` page: a sphere written as GLB, once plain and once with
11/// `compressGeometry`. Its check is the GLB magic and that compression made
12/// the file smaller.
13final class GltfWriteDemo extends ShowcaseDemo {
14 late final int _plainBytes;
15 late final int _compressedBytes;
16 late final bool _usedQuantization;
17 late final bool _usedReordering;
18 late final List<int> _magic;
20 @override
21 Scene build(DemoContext context) {
22 // A sphere with enough vertices that quantizing its attributes and
23 // reordering its triangles actually has something to save on.
24 final document = PlainModelDocument(
25 surfaces: <ModelSurface>[
26 ModelSurface(
27 mesh: SphereShape(segments: 48, rings: 24).build(),
28 materialIndex: 0,
29 ),
30 ],
31 materials: <SurfaceMaterial>[
32 SurfaceMaterial(
33 name: 'shell',
34 baseColor: LinearColor.fromSrgb(0.8, 0.5, 0.2, 1.0),
35 roughness: 0.5,
36 ),
37 ],
38 nodes: <ModelNode>[
39 ModelNode(surfaces: <int>[0]),
40 ],
41 );
43 final plain = GltfWriter(document).writeGlb();
44 final compressor = GltfWriter(document, compressGeometry: true);
45 final compressed = compressor.writeGlb();
47 _plainBytes = plain.length;
48 _compressedBytes = compressed.length;
49 _usedQuantization = compressor.didQuantizeGeometry;
50 _usedReordering = compressor.didReorderVertexCache;
51 _magic = plain.take(4).toList();
53 return Scene()
54 ..add(
55 MeshNode(
56 DeviceMesh.upload(context.device, document.surfaces.single.mesh),
57 RenderMaterial(
58 baseColor: LinearColor.fromSrgb(0.8, 0.5, 0.2, 1.0),
59 roughness: 0.5,
60 ),
61 name: 'shell',
62 ),
63 )
64 ..add(
65 LightNode(name: 'sun', intensity: 3.0 * LightNode.legacyUnit)
66 ..setLocalForward(Vector3(-0.4, -1.0, -0.3)),
67 );
68 }
70 String _report() {
71 final magicText = String.fromCharCodes(_magic);
72 final ratio = _plainBytes / _compressedBytes;
73 return 'GLB magic: "$magicText"\n'
74 'plain: $_plainBytes bytes\n'
75 'compressed: $_compressedBytes bytes '
76 '(${ratio.toStringAsFixed(2)}x smaller)\n'
77 'used quantization: $_usedQuantization\n'
78 'used vertex-cache reordering: $_usedReordering';
79 }
81 @override
82 Widget? customBody(BuildContext buildContext, DemoContext context) =>
83 Container(
84 color: const Color(0xFF14161A),
85 padding: const EdgeInsets.all(24),
86 alignment: Alignment.topLeft,
87 child: DefaultTextStyle(
88 style: const TextStyle(
89 color: Color(0xFFE8E8EC),
90 fontSize: 16,
91 fontFamily: 'monospace',
92 ),
93 child: Text(_report()),
94 ),
95 );
97 @override
98 void verify(Scene scene, FrameResult frame) {
99 if (_magic.first != 0x67) {
100 throw StateError('the file does not start with the GLB magic');
101 }
102 if (_compressedBytes >= _plainBytes) {
103 throw StateError('compressGeometry did not shrink the file');
104 }
105 if (frame.drawCalls < 1) {
106 throw StateError('the sphere was not drawn');
107 }
108 }