flutter3d
Showcase Changelog 62 packages API reference

Export, read back, compare: the source

since 0.7.0 Formats and I/O

Regions the guide quotes: checked (line 39), validate (line 47), report (line 69), check (line 98).

1/// Write, read back, and compare: `ExportReport` and `validateGltfExport`.
2///
3/// Quoted by `export_validate.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 `export-validate` page: a cube written as GLB and validated on the
11/// way back. Its check is that the round trip was clean and the bounds
12/// declared right.
13final class ExportValidateDemo extends ShowcaseDemo {
14 late final ExportReport _glb;
15 late final ExportReport _obj;
16 late final ExportReport _stl;
17 late final List<String> _boundsProblems;
19 final _document = PlainModelDocument(
20 surfaces: <ModelSurface>[
21 ModelSurface(
22 mesh: CuboidShape(size: Vector3.all(1.0)).build(),
23 materialIndex: 0,
24 ),
25 ],
26 materials: <SurfaceMaterial>[
27 SurfaceMaterial(baseColor: LinearColor.fromSrgb(0.5, 0.7, 0.9, 1.0)),
28 ],
29 // `GltfWriter` places a surface through the node graph alone, never
30 // through its own transform, so a document meant to be written as glTF
31 // needs at least one node naming it.
32 nodes: <ModelNode>[
33 ModelNode(surfaces: <int>[0]),
34 ],
35 );
37 @override
38 Future<void> prepare(DemoContext context) async {
39 // `exportChecked` writes, reads the result back through the same writer
40 // and compares. A binary format like GLB is held to a tolerance of
41 // `0.0`; OBJ, a decimal text format, rounds on the way out by design and
42 // is checked against `ObjWriter.decimals` own precision instead.
43 _glb = await exportToGlb(_document);
44 _obj = await exportToObj(_document);
45 _stl = await exportToStl(_document);
47 // `compareModelDocuments` proves the geometry round-tripped; it says
48 // nothing about whether the file's own declared bounds match the data,
49 // which is a writer's own bookkeeping and not part of a document at
50 // all. `validateGltfExport` checks that separately, straight off the
51 // bytes.
52 _boundsProblems = await validateGltfExport(_glb.files.values.first);
53 }
55 @override
56 Scene build(DemoContext context) => Scene()
57 ..add(
58 MeshNode(
59 DeviceMesh.upload(context.device, _document.surfaces.single.mesh),
60 RenderMaterial(baseColor: LinearColor.fromSrgb(0.5, 0.7, 0.9, 1.0)),
61 name: 'cube',
62 ),
63 )
64 ..add(
65 LightNode(name: 'sun', intensity: 3.0 * LightNode.legacyUnit)
66 ..setLocalForward(Vector3(-0.4, -1.0, -0.3)),
67 );
69 String _line(String name, ExportReport report) =>
70 '$name: ${report.isClean ? 'clean' : 'not clean'}, '
71 '${report.writerWarnings.length} warning(s), '
72 '${report.differences.length} difference(s)';
74 String _report() =>
75 '${_line('glb', _glb)}\n'
76 '${_line('obj', _obj)}\n'
77 '${_line('stl', _stl)}\n'
78 'accessor bounds: ${_boundsProblems.isEmpty ? 'match the data' : _boundsProblems.join('\n')}';
80 @override
81 Widget? customBody(BuildContext buildContext, DemoContext context) =>
82 Container(
83 color: const Color(0xFF14161A),
84 padding: const EdgeInsets.all(24),
85 alignment: Alignment.topLeft,
86 child: DefaultTextStyle(
87 style: const TextStyle(
88 color: Color(0xFFE8E8EC),
89 fontSize: 16,
90 fontFamily: 'monospace',
91 ),
92 child: Text(_report()),
93 ),
94 );
96 @override
97 void verify(Scene scene, FrameResult frame) {
98 if (!_glb.isClean) {
99 throw StateError('the GLB round trip was not clean: $_glb');
100 }
101 if (_boundsProblems.isNotEmpty) {
102 throw StateError('the GLB declared the wrong bounds: $_boundsProblems');
103 }
104 if (frame.drawCalls < 1) {
105 throw StateError('the cube was not drawn');
106 }
107 }