flutter3d
Showcase Changelog 62 packages API reference

The .f3d container: the source

since 0.1.0 Formats and I/O

Regions the guide quotes: write (line 31), read (line 37), report (line 56), check (line 81).

1/// `.f3d`: the engine's own container, built to load without parsing.
2///
3/// Quoted by `f3d.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 `f3d` page: a ball written to `.f3d` and read back. Its check is that
11/// the round trip changed nothing.
12final class F3dDemo extends ShowcaseDemo {
13 late final F3dDocument _decoded;
14 late final int _fileBytes;
15 late final int _glbBytes;
17 final _document = PlainModelDocument(
18 surfaces: <ModelSurface>[
19 ModelSurface(
20 mesh: SphereShape(segments: 32, rings: 16).build(),
21 materialIndex: 0,
22 ),
23 ],
24 materials: <SurfaceMaterial>[
25 SurfaceMaterial(baseColor: LinearColor.fromSrgb(0.4, 0.7, 0.5, 1.0)),
26 ],
27 );
29 @override
30 Scene build(DemoContext context) {
31 // `F3dWriter.write` is the offline half, run once by
32 // `dart run flutter3d_build:convert`. Nothing here is on a frame path.
33 final bytes = F3dWriter(_document).write();
34 _fileBytes = bytes.length;
35 _glbBytes = GltfWriter(_document).writeGlb().length;
37 // Parsing reads the header and the section directory; every array the
38 // engine asks for afterwards, `surfaces` included, is a view over these
39 // same bytes rather than a copy.
40 _decoded = F3dDocument.parse(bytes);
42 return Scene()
43 ..add(
44 MeshNode(
45 DeviceMesh.upload(context.device, _decoded.surfaces.single.mesh),
46 RenderMaterial(baseColor: LinearColor.fromSrgb(0.4, 0.7, 0.5, 1.0)),
47 name: 'ball',
48 ),
49 )
50 ..add(
51 LightNode(name: 'sun', intensity: 3.0 * LightNode.legacyUnit)
52 ..setLocalForward(Vector3(-0.4, -1.0, -0.3)),
53 );
54 }
56 String _report() =>
57 '.f3d bytes: $_fileBytes\n'
58 '.glb bytes (for comparison): $_glbBytes\n'
59 'surfaces: ${_decoded.surfaces.length}\n'
60 'vertices: ${_decoded.vertexCount}\n'
61 'triangles: ${_decoded.triangleCount}';
63 @override
64 Widget? customBody(BuildContext buildContext, DemoContext context) =>
65 Container(
66 color: const Color(0xFF14161A),
67 padding: const EdgeInsets.all(24),
68 alignment: Alignment.topLeft,
69 child: DefaultTextStyle(
70 style: const TextStyle(
71 color: Color(0xFFE8E8EC),
72 fontSize: 16,
73 fontFamily: 'monospace',
74 ),
75 child: Text(_report()),
76 ),
77 );
79 @override
80 void verify(Scene scene, FrameResult frame) {
81 final differences = compareModelDocuments(_document, _decoded);
82 if (differences.isNotEmpty) {
83 throw StateError(
84 'the round trip through .f3d changed something: '
85 '$differences',
86 );
87 }
88 if (_decoded.surfaces.isEmpty) {
89 throw StateError('the file decoded to no surfaces');
90 }
91 if (frame.drawCalls < 1) {
92 throw StateError('the ball was not drawn');
93 }
94 }