flutter3d
Showcase Changelog 62 packages API reference

STL: the source

since 0.7.0 Formats and I/O

Regions the guide quotes: source (line 20), load (line 57), write (line 64), report (line 84), check (line 116).

1/// Reading ASCII STL and writing it back as the binary form.
2///
3/// Quoted by `stl.md` and shown whole in the Source tab.
4library;
5
6import 'dart:convert';
7import 'dart:typed_data';
8
9import 'package:flutter/widgets.dart';
10import 'package:flutter3d/flutter3d.dart';
11import 'package:flutter3d_showcase/src/demo/demo.dart';
13/// The `stl` page: a tetrahedron read from ASCII STL and written as binary.
14/// Its check is four facets, a degenerate normal recomputed, and a binary
15/// file of the size four facets make.
16final class StlDemo extends ShowcaseDemo {
17 late final ModelDocument _decoded;
18 late final Uint8List _binary;
20 // STL has no shared vertices: each of its four facets repeats its own
21 // three corners in full, with the facet's own normal ahead of them.
22 static const String _sourceStl = '''
23solid tetra
24 facet normal 0 0 0
25 outer loop
26 vertex 0 1 0
27 vertex 1 -1 1
28 vertex -1 -1 1
29 endloop
30 endfacet
31 facet normal 0 0 0
32 outer loop
33 vertex 0 1 0
34 vertex -1 -1 1
35 vertex 0 -1 -1.4
36 endloop
37 endfacet
38 facet normal 0 0 0
39 outer loop
40 vertex 0 1 0
41 vertex 0 -1 -1.4
42 vertex 1 -1 1
43 endloop
44 endfacet
45 facet normal 0 0 0
46 outer loop
47 vertex 1 -1 1
48 vertex 0 -1 -1.4
49 vertex -1 -1 1
50 endloop
51 endfacet
52endsolid tetra
53''';
55 @override
56 Future<void> prepare(DemoContext context) async {
57 // Every facet's own normal is the zero vector here, on purpose: a
58 // surprising number of real exporters write exactly that, and
59 // `StlNormals.fromFile`, the default, falls back to the triangle's own
60 // cross product rather than trusting a degenerate normal.
61 final bytes = Uint8List.fromList(utf8.encode(_sourceStl));
62 _decoded = await StlLoader().load(bytes);
64 _binary = StlWriter(_decoded, name: 'tetra').write();
65 }
67 @override
68 Scene build(DemoContext context) => Scene()
69 ..add(
70 MeshNode(
71 DeviceMesh.upload(context.device, _decoded.surfaces.single.mesh),
72 RenderMaterial(
73 baseColor: LinearColor.fromSrgb(0.75, 0.75, 0.8, 1.0),
74 roughness: 0.5,
75 ),
76 name: 'tetra',
77 ),
78 )
79 ..add(
80 LightNode(name: 'sun', intensity: 3.0 * LightNode.legacyUnit)
81 ..setLocalForward(Vector3(-0.4, -1.0, -0.3)),
82 );
84 String _report() {
85 final headerBytes = _binary.sublist(0, 20);
86 final header = String.fromCharCodes(headerBytes.where((byte) => byte != 0));
87 final facetCount = ByteData.sublistView(
88 _binary,
89 80,
90 84,
91 ).getUint32(0, Endian.little);
92 return 'source (ASCII) bytes: ${_sourceStl.length}\n'
93 'written (binary) bytes: ${_binary.length}\n'
94 'binary header starts: "$header"\n'
95 'facet count in binary header: $facetCount';
96 }
98 @override
99 Widget? customBody(BuildContext buildContext, DemoContext context) =>
100 Container(
101 color: const Color(0xFF14161A),
102 padding: const EdgeInsets.all(24),
103 alignment: Alignment.topLeft,
104 child: DefaultTextStyle(
105 style: const TextStyle(
106 color: Color(0xFFE8E8EC),
107 fontSize: 16,
108 fontFamily: 'monospace',
109 ),
110 child: Text(_report()),
111 ),
112 );
114 @override
115 void verify(Scene scene, FrameResult frame) {
116 final mesh = _decoded.surfaces.single.mesh;
117 if (mesh.triangleCount != 4) {
118 throw StateError('the tetrahedron did not decode to four facets');
119 }
120 // A facet whose file normal was degenerate must have been recomputed,
121 // never left at zero.
122 final nx = mesh.vertices[3];
123 final ny = mesh.vertices[4];
124 final nz = mesh.vertices[5];
125 if (nx == 0.0 && ny == 0.0 && nz == 0.0) {
126 throw StateError('a degenerate normal was not recomputed');
127 }
128 if (_binary.length != 84 + 50 * 4) {
129 throw StateError('the binary file is not the size four facets makes it');
130 }
131 if (frame.drawCalls < 1) {
132 throw StateError('the tetrahedron was not drawn');
133 }
134 }