flutter3d
Showcase Changelog 62 packages API reference

OBJ and MTL: the source

since 0.1.0 Formats and I/O

Regions the guide quotes: source (line 21), load (line 36), write (line 41), report (line 62), check (line 91).

1/// Reading and writing Wavefront OBJ, with `ObjNormals` filling in what a
2/// file left out.
3///
4/// Quoted by `obj.md` and shown whole in the Source tab.
5library;
6
7import 'dart:convert';
8import 'dart:typed_data';
9
10import 'package:flutter/widgets.dart';
11import 'package:flutter3d/flutter3d.dart';
12import 'package:flutter3d_showcase/src/demo/demo.dart';
14/// The `obj` page: a tetrahedron read from OBJ text with no normals and
15/// written back. Its check is four triangles, generated normals, and a
16/// writer that produced output.
17final class ObjDemo extends ShowcaseDemo {
18 late final ModelDocument _decoded;
19 late final Uint8List _written;
21 // A tetrahedron with no `vn` records at all — real files omit normals
22 // often enough that `ObjLoader` has to have an answer for it.
23 static const String _sourceObj = '''
24v 0 1 0
25v 1 -1 1
26v -1 -1 1
27v 0 -1 -1.4
28f 1 2 3
29f 1 3 4
30f 1 4 2
31f 2 4 3
32''';
34 @override
35 Future<void> prepare(DemoContext context) async {
36 // `ObjNormals.smooth`, the loader's default, averages the face normals
37 // meeting at each vertex rather than leaving them at zero.
38 final bytes = Uint8List.fromList(utf8.encode(_sourceObj));
39 _decoded = await ObjLoader(normals: ObjNormals.smooth).load(bytes);
41 final writer = ObjWriter(_decoded, name: 'tetra');
42 _written = writer.write();
43 }
45 @override
46 Scene build(DemoContext context) => Scene()
47 ..add(
48 MeshNode(
49 DeviceMesh.upload(context.device, _decoded.surfaces.single.mesh),
50 RenderMaterial(
51 baseColor: LinearColor.fromSrgb(0.8, 0.6, 0.3, 1.0),
52 roughness: 0.6,
53 ),
54 name: 'tetra',
55 ),
56 )
57 ..add(
58 LightNode(name: 'sun', intensity: 3.0 * LightNode.legacyUnit)
59 ..setLocalForward(Vector3(-0.4, -1.0, -0.3)),
60 );
62 String _report() {
63 final mesh = _decoded.surfaces.single.mesh;
64 return 'source bytes: ${_sourceObj.length}\n'
65 'written bytes: ${_written.length}\n'
66 'vertices: ${mesh.vertexCount}\n'
67 'triangles: ${mesh.triangleCount}\n'
68 'a generated normal: ${mesh.vertices[3].toStringAsFixed(3)}, '
69 '${mesh.vertices[4].toStringAsFixed(3)}, '
70 '${mesh.vertices[5].toStringAsFixed(3)}';
71 }
73 @override
74 Widget? customBody(BuildContext buildContext, DemoContext context) =>
75 Container(
76 color: const Color(0xFF14161A),
77 padding: const EdgeInsets.all(24),
78 alignment: Alignment.topLeft,
79 child: DefaultTextStyle(
80 style: const TextStyle(
81 color: Color(0xFFE8E8EC),
82 fontSize: 16,
83 fontFamily: 'monospace',
84 ),
85 child: Text(_report()),
86 ),
87 );
89 @override
90 void verify(Scene scene, FrameResult frame) {
91 final mesh = _decoded.surfaces.single.mesh;
92 if (mesh.triangleCount != 4) {
93 throw StateError('the tetrahedron did not decode to four triangles');
94 }
95 // A generated normal is never the zero vector `ObjNormals.none` would
96 // have left behind.
97 final nx = mesh.vertices[3];
98 final ny = mesh.vertices[4];
99 final nz = mesh.vertices[5];
100 if (nx == 0.0 && ny == 0.0 && nz == 0.0) {
101 throw StateError('no normal was generated for a file that had none');
102 }
103 if (_written.isEmpty) {
104 throw StateError('the writer produced nothing');
105 }
106 if (frame.drawCalls < 1) {
107 throw StateError('the tetrahedron was not drawn');
108 }
109 }