flutter3d
Showcase Changelog 38 packages API reference

The build-time converter: the source

since 0.7.0 Formats and I/O

Regions the guide quotes: source (line 22), convert (line 38), report (line 64), check (line 90).

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';
15import 'package:vector_math/vector_math.dart';
17final class BuildConvertDemo extends ShowcaseDemo {
18 late final int _sourceBytes;
19 late final int _convertedBytes;
20 late final ModelDocument _decoded;
22 // `dart run flutter3d_build:convert model.obj` reads a model by its file
23 // extension. This page hands the same source text straight to the same
24 // decoder the real tool would pick.
25 static const String _sourceObj = '''
26v 0 1 0
27v 1 -1 1
28v -1 -1 1
29v 0 -1 -1.4
30f 1 2 3
31f 1 3 4
32f 1 4 2
33f 2 4 3
34''';
36 @override
37 Future<void> prepare(DemoContext context) async {
38 // The converter's own two steps, run here by hand: decode the source
39 // with the format's ordinary reader, then write it to `.f3d` with
40 // `F3dWriter`. `dart run flutter3d_build:convert` does exactly this,
41 // for every model under a directory at once, and writes the result
42 // beside the source rather than holding it in memory.
43 final sourceBytes = utf8.encode(_sourceObj);
44 _sourceBytes = sourceBytes.length;
45 _decoded = await ObjLoader().load(sourceBytes);
46 final converted = F3dWriter(_decoded).write();
47 _convertedBytes = converted.length;
48 }
50 @override
51 Scene build(DemoContext context) => Scene()
52 ..add(
53 MeshNode(
54 DeviceMesh.upload(context.device, _decoded.surfaces.single.mesh),
55 Material(baseColor: Vector4(0.7, 0.65, 0.5, 1.0)),
56 name: 'tetra',
57 ),
58 )
59 ..add(
60 LightNode(name: 'sun', intensity: 3.0)
61 ..setLocalForward(Vector3(-0.4, -1.0, -0.3)),
62 );
64 String _report() =>
65 'source (.obj) bytes: $_sourceBytes\n'
66 'converted (.f3d) bytes: $_convertedBytes\n\n'
67 'The real command line tool:\n'
68 'dart run flutter3d_build:convert model.obj -o model.f3d\n\n'
69 'and, once per project, the build hook that runs it automatically:\n'
70 'dart run flutter3d_build:init';
72 @override
73 Widget? customBody(BuildContext buildContext, DemoContext context) =>
74 Container(
75 color: const Color(0xFF14161A),
76 padding: const EdgeInsets.all(24),
77 alignment: Alignment.topLeft,
78 child: DefaultTextStyle(
79 style: const TextStyle(
80 color: Color(0xFFE8E8EC),
81 fontSize: 16,
82 fontFamily: 'monospace',
83 ),
84 child: Text(_report()),
85 ),
86 );
88 @override
89 void verify(Scene scene, FrameResult frame) {
90 if (_decoded.surfaces.isEmpty) {
91 throw StateError('the source did not decode to a surface');
92 }
93 if (_convertedBytes == 0) {
94 throw StateError('the converter wrote nothing');
95 }
96 if (frame.drawCalls < 1) {
97 throw StateError('the tetrahedron was not drawn');
98 }
99 }