Draco meshes: the source
Open the live demo · Read the guide · View on GitHub
Regions the guide quotes: bitstream (line 17), decode (line 36), rebuild (line 39), check (line 103).
1/// Decoding a real Draco bitstream with `decodeDraco`.
2///
3/// Quoted by `draco.md` and shown whole in the Source tab.
4library;
6import 'dart:convert';
8import 'package:flutter3d/flutter3d.dart';
9import 'package:flutter3d_showcase/src/demo/demo.dart';
11/// The `draco` page: a mesh decoded from a Draco fixture. Its check is that
12/// it comes out as 60 triangles, with points, and is drawn.
13final class DracoDemo extends ShowcaseDemo {
14 late final DracoMesh _mesh;
15 late final MeshData _upload;
17 // A small edgebreaker-encoded mesh, the same fixture
18 // `flutter3d_core`'s own decoder test checks against an independently
19 // computed set of triangles. Edgebreaker connectivity carries no index
20 // buffer and no vertex order of its own: both come out however the
21 // encoder's traversal happened to walk the mesh.
22 static const String _fixtureBase64 =
23 'RFJBQ08CAgEBAAAAQDwBPAEBHx8BF2/btm3bfdu2bdv2ruu6ruu6ruu6rusC/wEz/wI1RAL/'
24 'AAAAAAABAAkDAAACAQEJAwABAwEBAQAPIwEHAQgBEAEBAQkBFxTABEaQRtBEhyBH5YsP2Cnb'
25 'LPkrs79pACDdXi8AUMmBEQBA+gYCgBrctAHA6m9DAYAfsbEGAFY/rh8AqMHMbwDgo5GqAQAA'
26 'wPwGAEAarh8AsMEaawDA9WtDAQAfsWkDANd/AwGADW4nBIDtV60BAMzAvQEADuOWBAB3O94D'
27 'AMLH6BYAyDZfeQAgfLS1AYC7jckFALodqhUAAACYXAAA3LG1AQC77ZUHAMjH6BYA0DbjPQCA'
28 'fNySAMBu/zMAEQKoACR0HgLAYSQDgAgnDQAJjQFAhEMFABpbAugdMwB4+DeAl5UAgFkjBKAM'
29 'CQFA0hECYAYaAUDU3gBgehgAMHsJwLdZAYCJMgCQUNIAiJAZAEhIVACcESYA4DADwId9A3CJ'
30 'CQBUW0IAyIgQACYtIQBjnBEAQwUOoCUAAAAA/z8AAAAgrr4AUBi/ACCuvgAwcz8OAAMBAQP/'
31 'B4E2/5+AAQGrAQP//////58BAf//////o4EBqwEBAQH/m4ATIJkSAFI2CrI77Ywk2kkIW/ru'
32 'gP8DAAD/AQAACg==';
35 Scene build(DemoContext context) {
36 final bytes = base64Decode(_fixtureBase64.replaceAll('\n', ''));
37 _mesh = decodeDraco(bytes);
39 // A decoded `DracoMesh` is not yet a `MeshData`: its attributes are
40 // keyed by Draco's own attribute id, not by name, so a caller picks
41 // out the ones it wants. This page draws positions only, with a flat
42 // normal recomputed per triangle.
43 final position = _mesh.attributes.values.firstWhere(
44 (attribute) => attribute.type == DracoAttributeType.position,
45 );
46 final builder = MeshBuilder(
47 VertexLayout.standard,
48 reserveVertices: _mesh.indices.length,
49 reserveIndices: _mesh.indices.length,
50 );
51 Vector3 pointAt(int index) => Vector3(
52 position.values[index * 3],
53 position.values[index * 3 + 1],
54 position.values[index * 3 + 2],
55 );
56 for (var t = 0; t + 2 < _mesh.indices.length; t += 3) {
57 final a = pointAt(_mesh.indices[t]);
58 final b = pointAt(_mesh.indices[t + 1]);
59 final c = pointAt(_mesh.indices[t + 2]);
60 final cross = (b - a).cross(c - a);
61 final normal = cross.length2 > 0.0
62 ? cross.normalized()
63 : Vector3(0, 0, 1);
64 final texcoord = Vector2.zero();
65 final i0 = builder.addVertex(
66 position: a,
67 normal: normal,
68 texcoord: texcoord,
69 );
70 final i1 = builder.addVertex(
71 position: b,
72 normal: normal,
73 texcoord: texcoord,
74 );
75 final i2 = builder.addVertex(
76 position: c,
77 normal: normal,
78 texcoord: texcoord,
79 );
80 builder.addTriangle(i0, i1, i2);
81 }
82 _upload = builder.build();
84 return Scene()
85 ..add(
86 MeshNode(
87 DeviceMesh.upload(context.device, _upload),
88 RenderMaterial(
89 baseColor: LinearColor.fromSrgb(0.75, 0.6, 0.5, 1.0),
90 roughness: 0.6,
91 ),
92 name: 'draco mesh',
93 ),
94 )
95 ..add(
96 LightNode(name: 'sun', intensity: 3.0 * LightNode.legacyUnit)
97 ..setLocalForward(Vector3(-0.4, -1.0, -0.3)),
98 );
99 }
102 void verify(Scene scene, FrameResult frame) {
103 if (_mesh.indices.length != 180) {
104 throw StateError(
105 'this fixture always decodes to 60 triangles (180 corners); got '
106 '${_mesh.indices.length}',
107 );
108 }
109 if (_mesh.pointCount == 0) {
110 throw StateError('the decoded mesh has no points');
111 }
112 if (frame.drawCalls < 1) {
113 throw StateError('the decoded mesh was not drawn');
114 }
115 }
116}