A decoder of your own: the source
Open the live demo · Read the guide · View on GitHub
Regions the guide quotes: decoder (line 12), request (line 72), decode (line 80), check (line 109).
1/// A decoder of your own, tried before every format this engine ships.
2///
3/// Quoted by `custom_decoder.md` and shown whole in the Source tab.
4library;
6import 'dart:convert';
7import 'dart:typed_data';
9import 'package:flutter3d/flutter3d.dart';
10import 'package:flutter3d_showcase/src/demo/demo.dart';
12/// A made-up format this engine has never heard of: one line, three
13/// numbers, a box of that size. `ModelDecoder` is the whole plugin
14/// boundary — a project's own studio format arrives exactly this way.
15final class _BoxTextDecoder extends ModelDecoder {
16 const _BoxTextDecoder();
19 bool handles(ModelProbe probe) =>
20 probe.fileName.toLowerCase().endsWith('.box');
23 Future<ModelDocument> decode(ModelDecodeContext context) async {
24 final parts = utf8.decode(context.bytes).trim().split(RegExp(r'\s+'));
25 if (parts.length != 3) {
26 throw const FormatException('a .box file is three numbers: w h d');
27 }
28 final size = Vector3(
29 double.parse(parts[0]),
30 double.parse(parts[1]),
31 double.parse(parts[2]),
32 );
33 return PlainModelDocument(
34 surfaces: <ModelSurface>[
35 ModelSurface(mesh: CuboidShape(size: size).build()),
36 ],
37 nodes: <ModelNode>[
38 ModelNode(surfaces: <int>[0]),
39 ],
40 );
41 }
42}
44final class _BytesSource extends AssetSource {
45 const _BytesSource(this._bytes, this._fileName);
47 final Uint8List _bytes;
48 final String _fileName;
51 String get key => 'bytes:$_fileName';
54 String get fileName => _fileName;
57 Future<Uint8List> read(AssetRead request) async => _bytes;
60 AssetUriResolver get resolveUri =>
61 (request) async => throw StateError('a .box file has no siblings');
62}
64/// The `custom-decoder` page: a crate read from a `.box` file by a decoder
65/// of the page's own. Its check is that the decoder made one surface the size
66/// the file named, and that it is drawn.
67final class CustomDecoderDemo extends ShowcaseDemo {
68 late final ModelDocument _decoded;
71 Future<void> prepare(DemoContext context) async {
72 // A byte-identical file this engine's own decoders would refuse:
73 // three plain numbers, no glTF, no OBJ header, nothing recognisable.
74 final bytes = Uint8List.fromList(utf8.encode('1.4 0.6 2.0'));
75 final request = ModelLoadRequest(
76 source: _BytesSource(bytes, 'crate.box'),
77 decoders: const <ModelDecoder>[_BoxTextDecoder()],
78 );
80 // An application's own decoders are tried before this package's
81 // built-in ones, by file name and by magic — so a project can also
82 // replace a built-in reader, not only add to it.
83 _decoded = await decodeModelBytes(
84 request,
85 bytes,
86 request.source.resolveUri,
87 );
88 }
91 Scene build(DemoContext context) => Scene()
92 ..add(
93 MeshNode(
94 DeviceMesh.upload(context.device, _decoded.surfaces.single.mesh),
95 RenderMaterial(
96 baseColor: LinearColor.fromSrgb(0.7, 0.55, 0.35, 1.0),
97 roughness: 0.7,
98 ),
99 name: 'crate',
100 ),
101 )
102 ..add(
103 LightNode(name: 'sun', intensity: 3.0 * LightNode.legacyUnit)
104 ..setLocalForward(Vector3(-0.4, -1.0, -0.3)),
105 );
108 void verify(Scene scene, FrameResult frame) {
109 if (_decoded.surfaces.length != 1) {
110 throw StateError('the custom decoder did not produce one surface');
111 }
112 final bounds = _decoded.computeBounds();
113 final width = bounds.max.x - bounds.min.x;
114 if ((width - 1.4).abs() > 1e-6) {
115 throw StateError('the box did not come out the size the file named');
116 }
117 if (frame.drawCalls < 1) {
118 throw StateError('the crate was not drawn');
119 }
120 }
121}