-
Notifications
You must be signed in to change notification settings - Fork 229
/
Copy pathload_gltf_or_glb_page.dart
101 lines (92 loc) · 3.13 KB
/
load_gltf_or_glb_page.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
import 'package:arkit_plugin/arkit_plugin.dart';
import 'package:collection/collection.dart';
import 'package:flutter/material.dart';
import 'package:vector_math/vector_math_64.dart' as vector;
class LoadGltfOrGlbFilePage extends StatefulWidget {
@override
State<LoadGltfOrGlbFilePage> createState() => _LoadGltfOrGlbFilePageState();
}
class _LoadGltfOrGlbFilePageState extends State<LoadGltfOrGlbFilePage> {
late ARKitController arkitController;
@override
void dispose() {
arkitController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(title: const Text('Load .gltf or .glb')),
body: ARKitSceneView(
showFeaturePoints: true,
enableTapRecognizer: true,
planeDetection: ARPlaneDetection.horizontalAndVertical,
onARKitViewCreated: onARKitViewCreated,
),
);
void onARKitViewCreated(ARKitController arkitController) {
this.arkitController = arkitController;
this.arkitController.onARTap = (ar) {
final point = ar.firstWhereOrNull(
(o) => o.type == ARKitHitTestResultType.featurePoint,
);
if (point != null) {
_onARTapHandler(point);
}
};
}
void _onARTapHandler(ARKitTestResult point) {
final position = vector.Vector3(
point.worldTransform.getColumn(3).x,
point.worldTransform.getColumn(3).y,
point.worldTransform.getColumn(3).z,
);
final node = _getNodeFromFlutterAsset(position);
// final node = _getNodeFromNetwork(position);
arkitController.add(node);
}
ARKitGltfNode _getNodeFromFlutterAsset(vector.Vector3 position) =>
ARKitGltfNode(
assetType: AssetType.flutterAsset,
// Box model from
// https://github.com/KhronosGroup/glTF-Sample-Models/raw/master/2.0/Box/glTF-Binary/Box.glb
url: 'assets/gltf/Box.gltf',
scale: vector.Vector3(0.05, 0.05, 0.05),
position: position,
);
}
// Future<ARKitGltfNode> _getNodeFromNetwork(vector.Vector3 position) async {
// And add dependencies to pubspec.yaml file
// path_provider: ^2.0.3
// dio: ^5.3.3
//
// Import to test file download
// import 'package:dio/dio.dart';
// import 'package:path_provider/path_provider.dart';
//
// final file = await _downloadFile(
// "https://github.com/KhronosGroup/glTF-Sample-Models/raw/master/2.0/Box/glTF-Binary/Box.glb");
// if (file.existsSync()) {
// //Load from app document folder
// return ARKitGltfNode(
// assetType: AssetType.documents,
// url: file.path.split('/').last, // filename.extension only!
// scale: vector.Vector3(0.01, 0.01, 0.01),
// position: position,
// );
// }
// throw Exception('Failed to load $file');
// }
//
// Future<File> _downloadFile(String url) async {
// try {
// final dir = await getApplicationDocumentsDirectory();
// final filePath = '${dir.path}/${url.split("/").last}';
// await Dio().download(url, filePath);
// final file = File(filePath);
// print('Download completed!! path = $filePath');
// return file;
// } catch (e) {
// print('Caught an exception: $e');
// rethrow;
// }
// }