flutter3d
Showcase Changelog 62 packages API reference

The build-time converter: the source

since 0.7.0 Formats and I/O

Regions the guide quotes: source (line 24), convert (line 40), report (line 66), check (line 92).

1/// `flutter3d_build`'s converter, described rather than run: it is a
2/// command line tool with no Flutter SDK on its own dependency list, and
3/// this app cannot depend on it without pulling the tool into every build.
4/// This page instead runs the same decode-then-write path by hand, on a
5/// document built in memory, to show what the real command does.
6///
7/// Quoted by `build_convert.md` and shown whole in the Source tab.
8library;
9
10import 'dart:convert';
12import 'package:flutter/widgets.dart';
13import 'package:flutter3d/flutter3d.dart';
14import 'package:flutter3d_showcase/src/demo/demo.dart';
16/// The `build-convert` page: a tetrahedron that went through the converter's
17/// decode-then-write path in memory. Its check is that the source decoded,
18/// the writer wrote something, and the result is drawn.
19final class BuildConvertDemo extends ShowcaseDemo {
20 late final int _sourceBytes;
21 late final int _convertedBytes;
22 late final ModelDocument _decoded;
24 // `dart run flutter3d_build:convert model.obj` reads a model by its file
25 // extension. This page hands the same source text straight to the same
26 // decoder the real tool would pick.
27 static const String _sourceObj = '''
28v 0 1 0
29v 1 -1 1
30v -1 -1 1
31v 0 -1 -1.4
32f 1 2 3
33f 1 3 4
34f 1 4 2
35f 2 4 3
36''';
38 @override
39 Future<void> prepare(DemoContext context) async {
40 // The converter's own two steps, run here by hand: decode the source
41 // with the format's ordinary reader, then write it to `.f3d` with
42 // `F3dWriter`. `dart run flutter3d_build:convert` does exactly this,
43 // for every model under a directory at once, and writes the result
44 // beside the source rather than holding it in memory.
45 final sourceBytes = utf8.encode(_sourceObj);
46 _sourceBytes = sourceBytes.length;
47 _decoded = await ObjLoader().load(sourceBytes);
48 final converted = F3dWriter(_decoded).write();
49 _convertedBytes = converted.length;
50 }
52 @override
53 Scene build(DemoContext context) => Scene()
54 ..add(
55 MeshNode(
56 DeviceMesh.upload(context.device, _decoded.surfaces.single.mesh),
57 RenderMaterial(baseColor: LinearColor.fromSrgb(0.7, 0.65, 0.5, 1.0)),
58 name: 'tetra',
59 ),
60 )
61 ..add(
62 LightNode(name: 'sun', intensity: 3.0 * LightNode.legacyUnit)
63 ..setLocalForward(Vector3(-0.4, -1.0, -0.3)),
64 );
66 String _report() =>
67 'source (.obj) bytes: $_sourceBytes\n'
68 'converted (.f3d) bytes: $_convertedBytes\n\n'
69 'The real command line tool:\n'
70 'flutter3d convert model.obj -o .\n\n'
71 'and, once per project, the build hook that runs it automatically:\n'
72 'dart run flutter3d_build:init';
74 @override
75 Widget? customBody(BuildContext buildContext, DemoContext context) =>
76 Container(
77 color: const Color(0xFF14161A),
78 padding: const EdgeInsets.all(24),
79 alignment: Alignment.topLeft,
80 child: DefaultTextStyle(
81 style: const TextStyle(
82 color: Color(0xFFE8E8EC),
83 fontSize: 16,
84 fontFamily: 'monospace',
85 ),
86 child: Text(_report()),
87 ),
88 );
90 @override
91 void verify(Scene scene, FrameResult frame) {
92 if (_decoded.surfaces.isEmpty) {
93 throw StateError('the source did not decode to a surface');
94 }
95 if (_convertedBytes == 0) {
96 throw StateError('the converter wrote nothing');
97 }
98 if (frame.drawCalls < 1) {
99 throw StateError('the tetrahedron was not drawn');
100 }
101 }