Skip to content
0.2.0-dev Download ZIP
119 lines ·4.9 KB Raw
1/**************************************************************************/
2/* visionos_spatial_events.mm */
3/**************************************************************************/
4/* This file is part of: */
5/* GODOT ENGINE */
6/* https://godotengine.org */
7/**************************************************************************/
8/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10/* */
11/* Permission is hereby granted, free of charge, to any person obtaining */
12/* a copy of this software and associated documentation files (the */
13/* "Software"), to deal in the Software without restriction, including */
14/* without limitation the rights to use, copy, modify, merge, publish, */
15/* distribute, sublicense, and/or sell copies of the Software, and to */
16/* permit persons to whom the Software is furnished to do so, subject to */
17/* the following conditions: */
18/* */
19/* The above copyright notice and this permission notice shall be */
20/* included in all copies or substantial portions of the Software. */
21/* */
22/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29/**************************************************************************/
30
31#ifdef VISIONOS_ENABLED
32
33#include "visionos_spatial_events.h"
34
35#include "core/math/transform_3d.h"
36#include "core/math/vector3.h"
37#include "servers/xr/xr_controller_tracker.h"
38#include "servers/xr/xr_positional_tracker.h"
39#include "servers/xr/xr_server.h"
40
41#include "modules/visionos_xr/visionos_definitions.h"
42
43void VisionOSSpatialEventTracking::initialize(XRServer *p_xr_server,
44 VisionOSSharedController &p_left_hand,
45 VisionOSSharedController &p_right_hand) {
46 // Left hand
47 left_hand.controller = &p_left_hand;
48 left_hand.transform_correction = Transform3D(Vector3(-1, 0, 0), Vector3(0, 0, -1), Vector3(0, -1, 0), Vector3(0, 0, 0));
49
50 // Right hand
51 right_hand.controller = &p_right_hand;
52 right_hand.transform_correction = Transform3D(Vector3(1, 0, 0), Vector3(0, 0, -1), Vector3(0, +1, 0), Vector3(0, 0, 0));
53
54 // Eyes
55 eyes_ray.instantiate();
56 eyes_ray->set_tracker_name("/user/eyes_ext");
57 eyes_ray->set_tracker_desc("visionOS eyes selection ray");
58 p_xr_server->add_tracker(eyes_ray);
59}
60
61void VisionOSSpatialEventTracking::uninitialize(XRServer *p_xr_server) {
62 if (p_xr_server && eyes_ray.is_valid()) {
63 p_xr_server->remove_tracker(eyes_ray);
64 eyes_ray->unreference();
65 }
66}
67
68void VisionOSSpatialEventTracking::on_spatial_event(const VisionOSSpatialEvent &p_event) {
69 const bool active = (p_event.phase == VisionOSSpatialEvent::Phase::active);
70
71 // Hand
72 Hand *hand = nullptr;
73 switch (p_event.chirality) {
74 case VisionOSSpatialEvent::Chirality::left:
75 hand = &left_hand;
76 break;
77 case VisionOSSpatialEvent::Chirality::right:
78 hand = &right_hand;
79 break;
80 default:
81 break;
82 }
83
84 // Updating the pose first and sending the input second.
85 if (hand) {
86 // Updating the ray.
87 if (active) {
88 // Setting the ray pose on pinch.
89 if (p_event.has_ray && hand->ray_submitted == false) {
90 eyes_ray->set_pose("default", p_event.ray, Vector3(), Vector3());
91 hand->ray_submitted = true;
92 }
93 } else {
94 // Resetting the state on release (for the next pinch).
95 hand->ray_submitted = false;
96 }
97
98 if (active) {
99 hand->controller->source = VisionOSSharedController::Source::SpatialEvent;
100 } else {
101 hand->controller->source = VisionOSSharedController::Source::None;
102 }
103
104 // Updating the hand pose.
105 Transform3D pose = p_event.hand_pose * hand->transform_correction;
106 for (const String name : { "default", "aim" }) {
107 hand->controller->tracker->set_pose(name, pose, Vector3(), Vector3());
108 }
109 for (const String name : { "grip", "palm" }) {
110 hand->controller->tracker->invalidate_pose(name);
111 }
112
113 // Submitting input events. It will send a signal that the game
114 // can receive, to handle input.
115 hand->controller->tracker->set_input("trigger_click", active);
116 }
117}
118
119#endif // VISIONOS_ENABLED