Meshopt compression: the source
Open the live demo · Read the guide · View on GitHub
Regions the guide quotes: write (line 35), used (line 43), decode (line 49), check (line 81).
1/// `EXT_meshopt_compression`, written by `GltfWriter(compressGeometry:
2/// true)` and decoded again by `GltfLoader`.
3///
4/// Quoted by `meshopt.md` and shown whole in the Source tab.
5library;
7import 'dart:convert';
9import 'package:flutter3d/flutter3d.dart';
10import 'package:flutter3d_showcase/src/demo/demo.dart';
12/// The `meshopt` page: a ball written with `EXT_meshopt_compression` and
13/// read back. Its check is that meshopt was used, the triangles came back
14/// the same, and the file is smaller.
15final class MeshoptDemo extends ShowcaseDemo {
16 late final bool _usedMeshopt;
17 late final List<DocumentDifference> _differences;
18 late final int _plainBytes;
19 late final int _compressedBytes;
20 late final ModelDocument _readBack;
22 final _document = PlainModelDocument(
23 surfaces: <ModelSurface>[
24 ModelSurface(mesh: SphereShape(segments: 32, rings: 16).build()),
25 ],
26 // `GltfWriter` reads placement from the node graph alone, so a document
27 // headed for glTF needs a node naming its surface.
28 nodes: <ModelNode>[
29 ModelNode(surfaces: <int>[0]),
30 ],
31 );
34 Future<void> prepare(DemoContext context) async {
35 // The vertex and index bitstreams themselves live in
36 // `meshopt_vertex_codec.dart` and `meshopt_index_codec.dart`; this is
37 // the writer that reaches for them.
38 final plain = GltfWriter(_document).writeGlb();
39 final compressed = GltfWriter(_document, compressGeometry: true).writeGlb();
40 _plainBytes = plain.length;
41 _compressedBytes = compressed.length;
43 // A GLB's JSON chunk is plain UTF-8 text at the front of the file, so
44 // whether the extension was actually reached for is a byte search away.
45 _usedMeshopt = latin1
46 .decode(compressed, allowInvalid: true)
47 .contains('EXT_meshopt_compression');
49 // Reordering moves which byte offset a vertex or an index lands at
50 // without changing what the mesh draws, so the comparison asks whether
51 // the same triangles came back, not whether they are in the same
52 // order.
53 _readBack = await GltfLoader().load(compressed);
54 _differences = compareModelDocuments(
55 _document,
56 _readBack,
57 tolerance: 1e-2,
58 allowVertexReorder: true,
59 );
60 }
63 Scene build(DemoContext context) => Scene()
64 ..add(
65 MeshNode(
66 DeviceMesh.upload(context.device, _readBack.surfaces.single.mesh),
67 RenderMaterial(
68 baseColor: LinearColor.fromSrgb(0.6, 0.7, 0.85, 1.0),
69 roughness: 0.5,
70 ),
71 name: 'ball',
72 ),
73 )
74 ..add(
75 LightNode(name: 'sun', intensity: 3.0 * LightNode.legacyUnit)
76 ..setLocalForward(Vector3(-0.4, -1.0, -0.3)),
77 );
80 void verify(Scene scene, FrameResult frame) {
81 if (!_usedMeshopt) {
82 throw StateError('compressGeometry did not reach for meshopt at all');
83 }
84 if (_differences.isNotEmpty) {
85 throw StateError('the compressed mesh drew different triangles back');
86 }
87 if (_compressedBytes >= _plainBytes) {
88 throw StateError('the compressed file was not smaller');
89 }
90 if (frame.drawCalls < 1) {
91 throw StateError('the ball was not drawn');
92 }
93 }
94}