-
Notifications
You must be signed in to change notification settings - Fork 228
/
widget_projection.dart
160 lines (141 loc) · 4.51 KB
/
widget_projection.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
import 'dart:math' as math;
import 'package:arkit_plugin/arkit_plugin.dart';
import 'package:flutter/material.dart';
import 'package:vector_math/vector_math_64.dart' as vector;
class WidgetProjectionPage extends StatefulWidget {
@override
_WidgetProjectionPageState createState() => _WidgetProjectionPageState();
}
class _WidgetProjectionPageState extends State<WidgetProjectionPage> {
late ARKitController arkitController;
String anchorId = '';
double x = 0, y = 0;
double width = 1, height = 1;
Matrix4 transform = Matrix4.identity();
@override
void dispose() {
arkitController.onAddNodeForAnchor = null;
arkitController.onUpdateNodeForAnchor = null;
arkitController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(
title: const Text('Widget Projection'),
),
body: Stack(
children: [
ARKitSceneView(
trackingImagesGroupName: 'AR Resources',
onARKitViewCreated: onARKitViewCreated,
worldAlignment: ARWorldAlignment.camera,
configuration: ARKitConfiguration.imageTracking,
),
Positioned(
left: x,
top: y,
child: Container(
transform: transform,
width: width,
height: height,
child: const MyHomePage(
title: 'Widgets in AR',
),
),
),
],
));
void onARKitViewCreated(ARKitController arkitController) {
this.arkitController = arkitController;
this.arkitController.onAddNodeForAnchor = _handleAddAnchor;
this.arkitController.onUpdateNodeForAnchor = _handleUpdateAnchor;
}
void _handleAddAnchor(ARKitAnchor anchor) {
if (anchor is ARKitImageAnchor) {
anchorId = anchor.identifier;
_updatePosition(anchor);
_updateRotation(anchor);
}
}
void _handleUpdateAnchor(ARKitAnchor anchor) {
if (anchor.identifier == anchorId && anchor is ARKitImageAnchor) {
_updatePosition(anchor);
_updateRotation(anchor);
}
}
Future _updateRotation(ARKitAnchor anchor) async {
final t = anchor.transform.clone();
t.invertRotation();
t.rotateZ(math.pi / 2);
t.rotateX(math.pi / 2);
setState(() {
transform = t;
});
}
Future _updatePosition(ARKitImageAnchor anchor) async {
final transform = anchor.transform;
final width = anchor.referenceImagePhysicalSize.x / 2;
final height = anchor.referenceImagePhysicalSize.y / 2;
final topRight = vector.Vector4(width, 0, -height, 1)
..applyMatrix4(transform);
final bottomRight = vector.Vector4(width, 0, height, 1)
..applyMatrix4(transform);
final bottomLeft = vector.Vector4(-width, 0, -height, 1)
..applyMatrix4(transform);
final topLeft = vector.Vector4(-width, 0, height, 1)
..applyMatrix4(transform);
final pointsWorldSpace = [topRight, bottomRight, bottomLeft, topLeft];
final pointsViewportSpace = pointsWorldSpace.map(
(p) => arkitController.projectPoint(vector.Vector3(p.x, p.y, p.z)));
final pointsViewportSpaceResults = await Future.wait(pointsViewportSpace);
setState(() {
x = pointsViewportSpaceResults[2]!.x;
y = pointsViewportSpaceResults[2]!.y;
this.width = pointsViewportSpaceResults[0]!
.distanceTo(pointsViewportSpaceResults[3]!);
this.height = pointsViewportSpaceResults[1]!
.distanceTo(pointsViewportSpaceResults[2]!);
});
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({
Key? key,
required this.title,
}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() => setState(() => _counter++);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headlineMedium,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}