.fmat material files: the source
Open the live demo · Read the guide · View on GitHub
Regions the guide quotes: document (line 35), roundtrip (line 44), typo (line 50), report (line 83), check (line 139).
1/// `.fmat`: a material as a file of its own, read and written by
2/// `readFmat`/`writeFmat`, round-tripping through `MaterialDocument`.
3///
4/// Quoted by `fmat_files.md` and shown whole in the Source tab.
5library;
7import 'dart:convert';
8import 'dart:typed_data';
10import 'package:flutter/gestures.dart';
11import 'package:flutter/widgets.dart';
12import 'package:flutter3d/flutter3d.dart';
13import 'package:flutter3d/flutter3d.dart' as f3d show RenderMaterial;
14import 'package:flutter3d_app/flutter3d_app.dart';
15import 'package:flutter3d_showcase/src/demo/demo.dart';
17/// The `fmat-files` page: a hull plate whose material went through `.fmat`
18/// and back. Its check is that the round trip changed nothing, a clean file
19/// does not warn, and a file with an unknown key does.
20final class FmatFilesDemo extends ShowcaseDemo {
21 late final String _text;
22 late final MaterialDocument _roundTripped;
23 late final List<String> _typoWarnings;
24 late final Scene _scene;
27 void configureView(DemoContext context) {
28 context.orbit
29 ..distance = 2.4
30 ..pitch = 0.2;
31 }
34 Scene build(DemoContext context) {
35 final MaterialDocument document = MaterialDocument(
36 surface: SurfaceMaterial(
37 name: 'hull-plate',
38 baseColor: LinearColor.fromSrgb(0.55, 0.6, 0.65, 1.0),
39 metallic: 0.9,
40 roughness: 0.28,
41 ),
42 );
44 _text = writeFmat(document);
45 _roundTripped = readFmat(
46 Uint8List.fromList(utf8.encode(_text)),
47 name: 'hull-plate.fmat',
48 );
50 // An unknown key does not fail the file: it is recorded as a warning and
51 // everything this reader does know still loads.
52 const String typo =
53 '{"fmat": 1, "name": "typo", "roughnesss": 0.2, "baseColor": '
54 '[1, 1, 1, 1]}';
55 _typoWarnings = readFmat(
56 Uint8List.fromList(utf8.encode(typo)),
57 name: 'typo.fmat',
58 ).warnings;
60 final SurfaceMaterial surface = _roundTripped.surface;
61 return _scene = Scene()
62 ..add(
63 MeshNode(
64 DeviceMesh.upload(
65 context.device,
66 SphereShape(segments: 40, rings: 20).build(),
67 ),
68 f3d.RenderMaterial(
69 name: surface.name,
70 baseColor: surface.baseColor,
71 metallic: surface.metallic,
72 roughness: surface.roughness,
73 ),
74 name: 'plate',
75 ),
76 )
77 ..add(
78 LightNode(name: 'sun', intensity: 3.0 * LightNode.legacyUnit)
79 ..setLocalForward(Vector3(-0.4, -0.7, -0.6)),
80 );
81 }
83 String _report() =>
84 '$_text\n'
85 'round trip warnings: ${_roundTripped.warnings.isEmpty ? "none" : _roundTripped.warnings}\n'
86 'a file with an unknown key warns: $_typoWarnings';
88 // A material file is text first — Step 4 of the guide says so, and the
89 // report stays the whole story on the left. But a reader who cannot see
90 // what "metallic: 0.9, roughness: 0.28" looks like is trusting the
91 // numbers on faith, so the sphere those numbers actually shade sits
92 // beside the report rather than instead of it, and drags like every
93 // other page's viewport does.
95 Widget? customBody(BuildContext buildContext, DemoContext context) => Row(
96 children: <Widget>[
97 Expanded(
98 child: Container(
99 color: const Color(0xFF14161A),
100 padding: const EdgeInsets.all(24),
101 alignment: Alignment.topLeft,
102 child: SingleChildScrollView(
103 child: DefaultTextStyle(
104 style: const TextStyle(
105 color: Color(0xFFE8E8EC),
106 fontSize: 14,
107 fontFamily: 'monospace',
108 ),
109 child: Text(_report()),
110 ),
111 ),
112 ),
113 ),
114 Expanded(
115 child: Listener(
116 onPointerMove: (PointerMoveEvent event) =>
117 context.orbit.rotate(event.delta.dx, event.delta.dy),
118 onPointerSignal: (PointerSignalEvent event) {
119 if (event is PointerScrollEvent) {
120 context.orbit.zoom(event.scrollDelta.dy > 0.0 ? 1.1 : 1.0 / 1.1);
121 }
122 },
123 child: SceneSurface(
124 renderer: context.renderer,
125 scene: _scene,
126 view: context.view,
127 settings: () => const RenderSettings(),
128 onBeforeDraw: () =>
129 context.orbit.syncProjectionDepth(context.camera),
130 presentFrame: presentFrame,
131 ),
132 ),
133 ),
134 ],
135 );
138 void verify(Scene scene, FrameResult frame) {
139 final SurfaceMaterial original = SurfaceMaterial(
140 metallic: 0.9,
141 roughness: 0.28,
142 );
143 final SurfaceMaterial read = _roundTripped.surface;
144 if (read.metallic != original.metallic ||
145 read.roughness != original.roughness) {
146 throw StateError('the round trip changed the material');
147 }
148 if (_roundTripped.warnings.isNotEmpty) {
149 throw StateError(
150 'a clean file should not warn: ${_roundTripped.warnings}',
151 );
152 }
153 if (_typoWarnings.isEmpty) {
154 throw StateError('an unknown key should have warned and did not');
155 }
156 if (frame.drawCalls < 1) {
157 throw StateError('the plate was not drawn');
158 }
159 }
160}