USDZ export: the source
Open the live demo · Read the guide · View on GitHub
Regions the guide quotes: write (line 29), unzip (line 34), report (line 58), check (line 84).
1/// Writing geometry as `.usdz`, and reading the text layer back out of its
2/// own ZIP container.
3///
4/// Quoted by `usdz.md` and shown whole in the Source tab.
5library;
7import 'dart:convert';
8import 'dart:typed_data';
10import 'package:flutter/widgets.dart';
11import 'package:flutter3d/flutter3d.dart';
12import 'package:flutter3d_showcase/src/demo/demo.dart';
14/// The `usdz` page: a cube written as `.usdz`, its text layer read back out
15/// of the ZIP. Its check is the ZIP signature, a USD ASCII layer, and a mesh
16/// prim in it.
17final class UsdzDemo extends ShowcaseDemo {
18 late final Uint8List _archive;
19 late final String _usda;
21 final _document = PlainModelDocument(
22 surfaces: <ModelSurface>[
23 ModelSurface(mesh: CuboidShape(size: Vector3.all(1.0)).build()),
24 ],
25 );
28 Scene build(DemoContext context) {
29 // Geometry only: no materials, no hierarchy past one `Mesh` prim a
30 // surface. A `.usdz` is a ZIP with its entries stored uncompressed and
31 // aligned, so what Quick Look opens is the same bytes this writes.
32 _archive = UsdzWriter(_document, name: 'cube').write();
34 // Nothing in this package reads `.usdz` back, but the archive is a
35 // plain ZIP with one uncompressed entry, so its text layer can be read
36 // straight off the local file header this writer wrote first.
37 final view = ByteData.sublistView(_archive);
38 final nameLength = view.getUint16(26, Endian.little);
39 final extraLength = view.getUint16(28, Endian.little);
40 final dataLength = view.getUint32(22, Endian.little);
41 final dataStart = 30 + nameLength + extraLength;
42 _usda = utf8.decode(_archive.sublist(dataStart, dataStart + dataLength));
44 return Scene()
45 ..add(
46 MeshNode(
47 DeviceMesh.upload(context.device, _document.surfaces.single.mesh),
48 RenderMaterial(baseColor: LinearColor.fromSrgb(0.85, 0.82, 0.7, 1.0)),
49 name: 'cube',
50 ),
51 )
52 ..add(
53 LightNode(name: 'sun', intensity: 3.0 * LightNode.legacyUnit)
54 ..setLocalForward(Vector3(-0.4, -1.0, -0.3)),
55 );
56 }
58 String _report() =>
59 'archive bytes: ${_archive.length}\n'
60 'first four bytes: '
61 '${String.fromCharCodes(_archive.take(2))}'
62 '${_archive[2]}${_archive[3]}\n'
63 '.usda bytes: ${_usda.length}\n'
64 'first line: ${_usda.split('\n').first}';
67 Widget? customBody(BuildContext buildContext, DemoContext context) =>
68 Container(
69 color: const Color(0xFF14161A),
70 padding: const EdgeInsets.all(24),
71 alignment: Alignment.topLeft,
72 child: DefaultTextStyle(
73 style: const TextStyle(
74 color: Color(0xFFE8E8EC),
75 fontSize: 16,
76 fontFamily: 'monospace',
77 ),
78 child: Text(_report()),
79 ),
80 );
83 void verify(Scene scene, FrameResult frame) {
84 if (_archive[0] != 0x50 || _archive[1] != 0x4b) {
85 throw StateError('the archive does not start with the ZIP signature');
86 }
87 if (!_usda.startsWith('#usda 1.0')) {
88 throw StateError('the text layer is not a USD ASCII file');
89 }
90 if (!_usda.contains('def Mesh')) {
91 throw StateError('the text layer has no mesh prim');
92 }
93 if (frame.drawCalls < 1) {
94 throw StateError('the cube was not drawn');
95 }
96 }
97}