/
model_viewer.dart
263 lines (233 loc) · 8.75 KB
/
model_viewer.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/* This is free and unencumbered software released into the public domain. */
import 'dart:async' show Completer;
import 'dart:convert' show utf8;
import 'dart:io'
show File, HttpRequest, HttpServer, HttpStatus, InternetAddress, Platform;
import 'dart:typed_data' show Uint8List;
import 'package:flutter/material.dart';
import 'package:flutter/services.dart' show rootBundle;
import 'package:flutter_android/android_content.dart' as android_content;
import 'package:webview_flutter/platform_interface.dart';
import 'package:webview_flutter/webview_flutter.dart';
import 'html_builder.dart';
/// Flutter widget for rendering interactive 3D models.
class ModelViewer extends StatefulWidget {
ModelViewer(
{Key key,
this.backgroundColor = Colors.white,
@required this.src,
this.alt,
this.ar,
this.arModes,
this.arScale,
this.autoRotate,
this.autoRotateDelay,
this.autoPlay,
this.cameraControls,
this.iosSrc})
: super(key: key);
/// The background color for the model viewer.
///
/// The theme's [ThemeData.scaffoldBackgroundColor] by default.
final Color backgroundColor;
/// The URL or path to the 3D model. This parameter is required.
/// Only glTF/GLB models are supported.
///
/// The parameter value must conform to the following:
///
/// - `http://` and `https://` for HTTP(S) URLs
/// (for example, `https://modelviewer.dev/shared-assets/models/Astronaut.glb`)
///
/// - `file://` for local files
///
/// - a relative pathname for Flutter app assets
/// (for example, `assets/MyModel.glb`)
final String src;
/// Configures the model with custom text that will be used to describe the
/// model to viewers who use a screen reader or otherwise depend on additional
/// semantic context to understand what they are viewing.
final String alt;
/// Enable the ability to launch AR experiences on supported devices.
final bool ar;
/// A prioritized list of the types of AR experiences to enable, if available.
final List<String> arModes;
/// Controls the scaling behavior in AR mode in Scene Viewer. Set to "fixed"
/// to disable scaling of the model, which sets it to always be at 100% scale.
/// Defaults to "auto" which allows the model to be resized.
final String arScale;
/// Enables the auto-rotation of the model.
final bool autoRotate;
/// Sets the delay before auto-rotation begins. The format of the value is a
/// number in milliseconds. The default is 3000.
final int autoRotateDelay;
/// If this is true and a model has animations, an animation will
/// automatically begin to play when this attribute is set (or when the
/// property is set to true). The default is false.
final bool autoPlay;
/// Enables controls via mouse/touch when in flat view.
final bool cameraControls;
/// The URL to a USDZ model which will be used on supported iOS 12+ devices
/// via AR Quick Look.
final String iosSrc;
@override
State<ModelViewer> createState() => _ModelViewerState();
}
class _ModelViewerState extends State<ModelViewer> {
final Completer<WebViewController> _controller =
Completer<WebViewController>();
HttpServer _proxy;
@override
void initState() {
super.initState();
_initProxy();
}
@override
void dispose() {
super.dispose();
if (_proxy != null) {
_proxy.close(force: true);
_proxy = null;
}
}
@override
void didUpdateWidget(final ModelViewer oldWidget) {
super.didUpdateWidget(oldWidget);
// TODO
}
@override
Widget build(final BuildContext context) {
return WebView(
initialUrl: null,
javascriptMode: JavascriptMode.unrestricted,
initialMediaPlaybackPolicy: AutoMediaPlaybackPolicy.always_allow,
onWebViewCreated: (final WebViewController webViewController) async {
_controller.complete(webViewController);
final host = _proxy.address.address;
final port = _proxy.port;
final url = "http://$host:$port/";
print('>>>> ModelViewer initializing... <$url>'); // DEBUG
await webViewController.loadUrl(url);
},
navigationDelegate: (final NavigationRequest navigation) async {
//print('>>>> ModelViewer wants to load: <${navigation.url}>'); // DEBUG
if (!Platform.isAndroid) {
return NavigationDecision.navigate;
}
if (!navigation.url.startsWith("intent://")) {
return NavigationDecision.navigate;
}
try {
// See: https://developers.google.com/ar/develop/java/scene-viewer
final intent = android_content.Intent(
action: "android.intent.action.VIEW", // Intent.ACTION_VIEW
data: Uri.parse("https://arvr.google.com/scene-viewer/1.0").replace(
queryParameters: <String, dynamic>{
'file': widget.src,
'mode': 'ar_only',
},
),
package: "com.google.ar.core",
flags: 0x10000000, // Intent.FLAG_ACTIVITY_NEW_TASK,
);
await intent.startActivity();
} catch (error) {
print('>>>> ModelViewer failed to launch AR: $error'); // DEBUG
}
return NavigationDecision.prevent;
},
onPageStarted: (final String url) {
//print('>>>> ModelViewer began loading: <$url>'); // DEBUG
},
onPageFinished: (final String url) {
//print('>>>> ModelViewer finished loading: <$url>'); // DEBUG
},
onWebResourceError: (final WebResourceError error) {
print(
'>>>> ModelViewer failed to load: ${error.description} (${error.errorType} ${error.errorCode})'); // DEBUG
},
);
}
String _buildHTML(final String htmlTemplate) {
return HTMLBuilder.build(
htmlTemplate: htmlTemplate,
backgroundColor: widget.backgroundColor,
src: '/model',
alt: widget.alt,
ar: widget.ar,
arModes: widget.arModes,
arScale: widget.arScale,
autoRotate: widget.autoRotate,
autoRotateDelay: widget.autoRotateDelay,
autoPlay: widget.autoPlay,
cameraControls: widget.cameraControls,
iosSrc: widget.iosSrc,
);
}
Future<void> _initProxy() async {
final url = Uri.parse(widget.src);
_proxy = await HttpServer.bind(InternetAddress.loopbackIPv4, 0);
_proxy.listen((final HttpRequest request) async {
//print("${request.method} ${request.uri}"); // DEBUG
//print(request.headers); // DEBUG
final response = request.response;
switch (request.uri.path) {
case '/':
case '/index.html':
final htmlTemplate = await rootBundle
.loadString('packages/model_viewer/etc/assets/template.html');
final html = utf8.encode(_buildHTML(htmlTemplate));
response
..statusCode = HttpStatus.ok
..headers.add("Content-Type", "text/html;charset=UTF-8")
..headers.add("Content-Length", html.length.toString())
..add(html);
await response.close();
break;
case '/model-viewer.js':
final code = await _readAsset(
'packages/model_viewer/etc/assets/model-viewer.js');
response
..statusCode = HttpStatus.ok
..headers
.add("Content-Type", "application/javascript;charset=UTF-8")
..headers.add("Content-Length", code.lengthInBytes.toString())
..add(code);
await response.close();
break;
case '/model':
if (url.isAbsolute && !url.isScheme("file")) {
await response.redirect(url); // TODO: proxy the resource
} else {
final data = await (url.isScheme("file")
? _readFile(url.path)
: _readAsset(url.path));
response
..statusCode = HttpStatus.ok
..headers.add("Content-Type", "application/octet-stream")
..headers.add("Content-Length", data.lengthInBytes.toString())
..headers.add("Access-Control-Allow-Origin", "*")
..add(data);
await response.close();
}
break;
case '/favicon.ico':
default:
final text = utf8.encode("Resource '${request.uri}' not found");
response
..statusCode = HttpStatus.notFound
..headers.add("Content-Type", "text/plain;charset=UTF-8")
..headers.add("Content-Length", text.length.toString())
..add(text);
await response.close();
break;
}
});
}
Future<Uint8List> _readAsset(final String key) async {
final data = await rootBundle.load(key);
return data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);
}
Future<Uint8List> _readFile(final String path) async {
return await File(path).readAsBytes();
}
}