-
Notifications
You must be signed in to change notification settings - Fork 240
Expand file tree
/
Copy pathreal_time_updates.dart
More file actions
83 lines (73 loc) · 2.29 KB
/
real_time_updates.dart
File metadata and controls
83 lines (73 loc) · 2.29 KB
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
import 'package:arkit_plugin/arkit_plugin.dart';
import 'package:flutter/material.dart';
import 'package:vector_math/vector_math_64.dart' as vector;
import 'package:collection/collection.dart';
class RealTimeUpdatesPage extends StatefulWidget {
const RealTimeUpdatesPage({super.key});
@override
State<RealTimeUpdatesPage> createState() => _RealTimeUpdatesPageState();
}
class _RealTimeUpdatesPageState extends State<RealTimeUpdatesPage> {
late ARKitController arkitController;
ARKitNode? movingNode;
bool busy = false;
@override
void dispose() {
arkitController.updateAtTime = null;
arkitController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Real Time Updates Sample'),
),
body: ARKitSceneView(
onARKitViewCreated: _onARKitViewCreated,
),
);
}
void _onARKitViewCreated(ARKitController arkitController) {
final material = ARKitMaterial(
diffuse: ARKitMaterialProperty.color(Colors.white),
);
final sphere = ARKitSphere(
materials: [material],
radius: 0.01,
);
movingNode =
ARKitNode(geometry: sphere, position: vector.Vector3(0, 0, -0.25));
this.arkitController = arkitController;
this.arkitController.updateAtTime = (time) {
if (busy == false) {
busy = true;
this.arkitController.performHitTest(x: 0.25, y: 0.75).then((results) {
if (results.isNotEmpty) {
final point = results.firstWhereOrNull(
(o) => o.type == ARKitHitTestResultType.featurePoint,
);
if (point == null) {
return;
}
final position = vector.Vector3(
point.worldTransform.getColumn(3).x,
point.worldTransform.getColumn(3).y,
point.worldTransform.getColumn(3).z,
);
final newNode = ARKitNode(
geometry: sphere,
position: position,
);
this.arkitController.remove(movingNode!.name);
movingNode = null;
this.arkitController.add(newNode);
movingNode = newNode;
}
busy = false;
});
}
};
this.arkitController.add(movingNode!);
}
}