-
Notifications
You must be signed in to change notification settings - Fork 240
Expand file tree
/
Copy pathmanipulation_page.dart
More file actions
93 lines (82 loc) · 2.8 KB
/
manipulation_page.dart
File metadata and controls
93 lines (82 loc) · 2.8 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
84
85
86
87
88
89
90
91
92
93
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;
import 'package:collection/collection.dart';
class ManipulationPage extends StatefulWidget {
const ManipulationPage({super.key});
@override
State<ManipulationPage> createState() => _ManipulationPageState();
}
class _ManipulationPageState extends State<ManipulationPage> {
late ARKitController arkitController;
ARKitNode? boxNode;
@override
void dispose() {
arkitController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(title: const Text('Manipulation Sample')),
body: ARKitSceneView(
enablePinchRecognizer: true,
enablePanRecognizer: true,
enableRotationRecognizer: true,
onARKitViewCreated: onARKitViewCreated,
),
);
void onARKitViewCreated(ARKitController arkitController) {
this.arkitController = arkitController;
this.arkitController.onNodePinch = (pinch) => _onPinchHandler(pinch);
this.arkitController.onNodePan = (pan) => _onPanHandler(pan);
this.arkitController.onNodeRotation =
(rotation) => _onRotationHandler(rotation);
addNode();
}
void addNode() {
final material = ARKitMaterial(
lightingModelName: ARKitLightingModel.physicallyBased,
diffuse: ARKitMaterialProperty.color(
Color((math.Random().nextDouble() * 0xFFFFFF).toInt() << 0)
.withValues(alpha: 1.0),
),
);
final box =
ARKitBox(materials: [material], width: 0.1, height: 0.1, length: 0.1);
final node = ARKitNode(
geometry: box,
position: vector.Vector3(0, 0, -0.5),
);
arkitController.add(node);
boxNode = node;
}
void _onPinchHandler(List<ARKitNodePinchResult> pinch) {
final pinchNode = pinch.firstWhereOrNull(
(e) => e.nodeName == boxNode?.name,
);
if (pinchNode != null) {
final scale = vector.Vector3.all(pinchNode.scale);
boxNode?.scale = scale;
}
}
void _onPanHandler(List<ARKitNodePanResult> pan) {
final panNode = pan.firstWhereOrNull((e) => e.nodeName == boxNode?.name);
if (panNode != null) {
final old = boxNode?.eulerAngles;
final newAngleY = panNode.translation.x * math.pi / 180;
boxNode?.eulerAngles =
vector.Vector3(old?.x ?? 0, newAngleY, old?.z ?? 0);
}
}
void _onRotationHandler(List<ARKitNodeRotationResult> rotation) {
final rotationNode = rotation.firstWhereOrNull(
(e) => e.nodeName == boxNode?.name,
);
if (rotationNode != null) {
final rotation = boxNode?.eulerAngles ??
vector.Vector3.zero() + vector.Vector3.all(rotationNode.rotation);
boxNode?.eulerAngles = rotation;
}
}
}