Skip to content

armgit / godot

Public Imported from GitHub
0

Godot Engine – Multi-platform 2D and 3D game engine

Add support for Spatial pinch input on visionOS

Olivier Pinone90b68ddab36
diff --git a/modules/visionos_xr/visionos_controller_tracking.h b/modules/visionos_xr/visionos_controller_tracking.h
index e28f4d8a4b8..7ee29fa3459 100644
--- a/modules/visionos_xr/visionos_controller_tracking.h
+++ b/modules/visionos_xr/visionos_controller_tracking.h
@@ -34,6 +34,8 @@
#include "visionos_definitions.h"
+#include "servers/xr/xr_controller_tracker.h"
+
#ifdef __OBJC__
#define Key GodotKey
#import <CoreHaptics/CoreHaptics.h>
@@ -60,8 +62,8 @@ struct VisionOSControllerTracking {
ar_accessory_anchor_t right_controller_anchor = nullptr;
// Controller state
- Ref<XRControllerTracker> left_controller_tracker;
- Ref<XRControllerTracker> right_controller_tracker;
+ VisionOSSharedController *left_shared_controller = nullptr;
+ VisionOSSharedController *right_shared_controller = nullptr;
GCController *left_gc_controller = nullptr;
GCController *right_gc_controller = nullptr;
CHHapticEngine *left_haptic_engine = nullptr;
@@ -71,7 +73,9 @@ struct VisionOSControllerTracking {
id controller_observer = nullptr;
id controller_disconnect_observer = nullptr;
- void initialize(XRServer *p_xr_server, VisionOSXRInterface *p_xr_interface);
+ void initialize(XRServer *p_xr_server, VisionOSXRInterface *p_xr_interface,
+ VisionOSSharedController &p_left_hand,
+ VisionOSSharedController &p_right_hand);
void uninitialize(XRServer *p_xr_server);
void init_for_controller(GCController *p_controller);
diff --git a/modules/visionos_xr/visionos_controller_tracking.mm b/modules/visionos_xr/visionos_controller_tracking.mm
index d8e8162bd7e..a36939cf96c 100644
--- a/modules/visionos_xr/visionos_controller_tracking.mm
+++ b/modules/visionos_xr/visionos_controller_tracking.mm
@@ -35,7 +35,11 @@
#include "visionos_simd_helpers.h"
#include "visionos_xr_interface.h"
-void VisionOSControllerTracking::initialize(XRServer *p_xr_server, VisionOSXRInterface *p_xr_interface) {
+#include "modules/visionos_xr/visionos_definitions.h"
+
+void VisionOSControllerTracking::initialize(XRServer *p_xr_server, VisionOSXRInterface *p_xr_interface,
+ VisionOSSharedController &p_left_hand,
+ VisionOSSharedController &p_right_hand) {
accessories = ar_accessories_create();
xr_interface = p_xr_interface;
@@ -47,17 +51,8 @@ void VisionOSControllerTracking::initialize(XRServer *p_xr_server, VisionOSXRInt
setup_controller_notifications();
- left_controller_tracker.instantiate();
- left_controller_tracker->set_tracker_hand(XRPositionalTracker::TRACKER_HAND_LEFT);
- left_controller_tracker->set_tracker_name("left_hand");
- left_controller_tracker->set_tracker_desc("visionOS Left Controller");
- p_xr_server->add_tracker(left_controller_tracker);
-
- right_controller_tracker.instantiate();
- right_controller_tracker->set_tracker_hand(XRPositionalTracker::TRACKER_HAND_RIGHT);
- right_controller_tracker->set_tracker_name("right_hand");
- right_controller_tracker->set_tracker_desc("visionOS Right Controller");
- p_xr_server->add_tracker(right_controller_tracker);
+ left_shared_controller = &p_left_hand;
+ right_shared_controller = &p_right_hand;
ar_accessory_tracking_configuration_t accessory_tracking_configuration = ar_accessory_tracking_configuration_create();
ar_accessory_tracking_configuration_set_accessories(accessory_tracking_configuration, accessories);
@@ -69,17 +64,6 @@ void VisionOSControllerTracking::uninitialize(XRServer *p_xr_server) {
accessory_tracking_provider = nullptr;
}
- if (p_xr_server != nullptr) {
- if (left_controller_tracker.is_valid()) {
- p_xr_server->remove_tracker(left_controller_tracker);
- left_controller_tracker.unref();
- }
- if (right_controller_tracker.is_valid()) {
- p_xr_server->remove_tracker(right_controller_tracker);
- right_controller_tracker.unref();
- }
- }
-
left_controller_anchor = nullptr;
right_controller_anchor = nullptr;
left_gc_controller = nullptr;
@@ -295,18 +279,30 @@ void VisionOSControllerTracking::update_controller_trackers_from_arkit(CFTimeInt
ar_accessory_t accessory = ar_accessory_anchor_get_accessory(accessory_anchor);
ar_accessory_chirality_t chirality = ar_accessory_get_inherent_chirality(accessory);
- if (chirality == ar_accessory_chirality_left && !left_found) {
+ if (chirality == ar_accessory_chirality_left && !left_found &&
+ left_shared_controller->source != VisionOSSharedController::Source::SpatialEvent) {
+ left_shared_controller->source = VisionOSSharedController::Source::Controller;
left_controller_anchor = accessory_anchor;
- update_controller_from_anchor(left_controller_tracker, left_controller_anchor, left_gc_controller);
+ update_controller_from_anchor(left_shared_controller->tracker, left_controller_anchor, left_gc_controller);
left_found = true;
- } else if (chirality == ar_accessory_chirality_right && !right_found) {
+ } else if (chirality == ar_accessory_chirality_right && !right_found &&
+ right_shared_controller->source != VisionOSSharedController::Source::SpatialEvent) {
+ right_shared_controller->source = VisionOSSharedController::Source::Controller;
right_controller_anchor = accessory_anchor;
- update_controller_from_anchor(right_controller_tracker, right_controller_anchor, right_gc_controller);
+ update_controller_from_anchor(right_shared_controller->tracker, right_controller_anchor, right_gc_controller);
right_found = true;
}
return true;
});
}
+
+ if (!left_found && left_shared_controller->source == VisionOSSharedController::Source::Controller) {
+ left_shared_controller->source = VisionOSSharedController::Source::None;
+ }
+
+ if (!right_found && right_shared_controller->source == VisionOSSharedController::Source::Controller) {
+ right_shared_controller->source = VisionOSSharedController::Source::None;
+ }
}
}
@@ -314,10 +310,10 @@ void VisionOSControllerTracking::trigger_haptic_pulse(const String &p_action_nam
GCController *target_controller = nullptr;
CHHapticEngine *target_engine = nullptr;
- if (p_tracker_name == left_controller_tracker->get_tracker_name()) {
+ if (left_shared_controller && p_tracker_name == left_shared_controller->tracker->get_tracker_name()) {
target_controller = left_gc_controller;
target_engine = left_haptic_engine;
- } else if (p_tracker_name == right_controller_tracker->get_tracker_name()) {
+ } else if (right_shared_controller && p_tracker_name == right_shared_controller->tracker->get_tracker_name()) {
target_controller = right_gc_controller;
target_engine = right_haptic_engine;
}
diff --git a/modules/visionos_xr/visionos_definitions.h b/modules/visionos_xr/visionos_definitions.h
index 2d73f351ea1..33d73761e02 100644
--- a/modules/visionos_xr/visionos_definitions.h
+++ b/modules/visionos_xr/visionos_definitions.h
@@ -71,4 +71,18 @@ enum class VisionOSAuthorizationStatus {
DENIED,
};
+// Trackers used by both VisionOSControllerTracking and VisionOSSpatialEventTracking.
+struct VisionOSSharedController {
+ Ref<XRControllerTracker> tracker;
+
+ enum class Source {
+ None,
+ SpatialEvent,
+ Controller
+ };
+
+ // Spatial Events override controllers, when active.
+ Source source = Source::None;
+};
+
#endif // VISIONOS_ENABLED
diff --git a/modules/visionos_xr/visionos_spatial_events.h b/modules/visionos_xr/visionos_spatial_events.h
new file mode 100644
index 00000000000..02ce9e88240
--- /dev/null
+++ b/modules/visionos_xr/visionos_spatial_events.h
@@ -0,0 +1,95 @@
+/**************************************************************************/
+/* visionos_spatial_events.h */
+/**************************************************************************/
+/* This file is part of: */
+/* GODOT ENGINE */
+/* https://godotengine.org */
+/**************************************************************************/
+/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
+/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
+/* */
+/* Permission is hereby granted, free of charge, to any person obtaining */
+/* a copy of this software and associated documentation files (the */
+/* "Software"), to deal in the Software without restriction, including */
+/* without limitation the rights to use, copy, modify, merge, publish, */
+/* distribute, sublicense, and/or sell copies of the Software, and to */
+/* permit persons to whom the Software is furnished to do so, subject to */
+/* the following conditions: */
+/* */
+/* The above copyright notice and this permission notice shall be */
+/* included in all copies or substantial portions of the Software. */
+/* */
+/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
+/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
+/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
+/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
+/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
+/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
+/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
+/**************************************************************************/
+
+#pragma once
+
+#ifdef VISIONOS_ENABLED
+
+#include "visionos_definitions.h"
+
+#include "core/math/transform_3d.h"
+#include "servers/xr/xr_controller_tracker.h"
+
+// Equivalent to https://developer.apple.com/documentation/swiftui/spatialeventcollection/event
+struct VisionOSSpatialEvent {
+ // Ray
+ bool has_ray;
+ Transform3D ray;
+
+ // Hand
+ enum class Chirality : int {
+ none = 0,
+ left = 1,
+ right = 2
+ };
+ Chirality chirality;
+ Transform3D hand_pose;
+
+ // Phase
+ enum class Phase : int {
+ unknown = 0,
+ active = 1,
+ cancelled = 2,
+ ended = 3
+ };
+ Phase phase;
+};
+
+// Godot representation of visionOS spatial events.
+struct VisionOSSpatialEventTracking {
+ // Ray from center of the head to
+ // the direction of the eyes, when a
+ // pinch gesture begins.
+ Ref<XRControllerTracker> eyes_ray;
+
+ struct Hand {
+ // Hand pose when pinching and dragging.
+ VisionOSSharedController *controller = nullptr;
+
+ // Update the ray only once per gesture.
+ bool ray_submitted = false;
+
+ // Correcting the transforms from each hand to
+ // map to the Godot and OpenXR convention:
+ // https://registry.khronos.org/OpenXR/specs/1.1/html/xrspec.html#XR_EXT_hand_interaction
+ Transform3D transform_correction;
+ };
+
+ // Left and right hands.
+ Hand left_hand, right_hand;
+
+ void initialize(XRServer *p_xr_server, VisionOSSharedController &p_left_hand,
+ VisionOSSharedController &p_right_hand);
+ void uninitialize(XRServer *p_xr_server);
+
+ void on_spatial_event(const VisionOSSpatialEvent &);
+};
+
+#endif // VISIONOS_ENABLED
diff --git a/modules/visionos_xr/visionos_spatial_events.mm b/modules/visionos_xr/visionos_spatial_events.mm
new file mode 100644
index 00000000000..ea249999497
--- /dev/null
+++ b/modules/visionos_xr/visionos_spatial_events.mm
@@ -0,0 +1,119 @@
+/**************************************************************************/
+/* visionos_spatial_events.mm */
+/**************************************************************************/
+/* This file is part of: */
+/* GODOT ENGINE */
+/* https://godotengine.org */
+/**************************************************************************/
+/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
+/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
+/* */
+/* Permission is hereby granted, free of charge, to any person obtaining */
+/* a copy of this software and associated documentation files (the */
+/* "Software"), to deal in the Software without restriction, including */
+/* without limitation the rights to use, copy, modify, merge, publish, */
+/* distribute, sublicense, and/or sell copies of the Software, and to */
+/* permit persons to whom the Software is furnished to do so, subject to */
+/* the following conditions: */
+/* */
+/* The above copyright notice and this permission notice shall be */
+/* included in all copies or substantial portions of the Software. */
+/* */
+/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
+/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
+/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
+/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
+/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
+/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
+/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
+/**************************************************************************/
+
+#ifdef VISIONOS_ENABLED
+
+#include "visionos_spatial_events.h"
+
+#include "core/math/transform_3d.h"
+#include "core/math/vector3.h"
+#include "servers/xr/xr_controller_tracker.h"
+#include "servers/xr/xr_positional_tracker.h"
+#include "servers/xr/xr_server.h"
+
+#include "modules/visionos_xr/visionos_definitions.h"
+
+void VisionOSSpatialEventTracking::initialize(XRServer *p_xr_server,
+ VisionOSSharedController &p_left_hand,
+ VisionOSSharedController &p_right_hand) {
+ // Left hand
+ left_hand.controller = &p_left_hand;
+ left_hand.transform_correction = Transform3D(Vector3(-1, 0, 0), Vector3(0, 0, -1), Vector3(0, -1, 0), Vector3(0, 0, 0));
+
+ // Right hand
+ right_hand.controller = &p_right_hand;
+ right_hand.transform_correction = Transform3D(Vector3(1, 0, 0), Vector3(0, 0, -1), Vector3(0, +1, 0), Vector3(0, 0, 0));
+
+ // Eyes
+ eyes_ray.instantiate();
+ eyes_ray->set_tracker_name("/user/eyes_ext");
+ eyes_ray->set_tracker_desc("visionOS eyes selection ray");
+ p_xr_server->add_tracker(eyes_ray);
+}
+
+void VisionOSSpatialEventTracking::uninitialize(XRServer *p_xr_server) {
+ if (p_xr_server && eyes_ray.is_valid()) {
+ p_xr_server->remove_tracker(eyes_ray);
+ eyes_ray->unreference();
+ }
+}
+
+void VisionOSSpatialEventTracking::on_spatial_event(const VisionOSSpatialEvent &p_event) {
+ const bool active = (p_event.phase == VisionOSSpatialEvent::Phase::active);
+
+ // Hand
+ Hand *hand = nullptr;
+ switch (p_event.chirality) {
+ case VisionOSSpatialEvent::Chirality::left:
+ hand = &left_hand;
+ break;
+ case VisionOSSpatialEvent::Chirality::right:
+ hand = &right_hand;
+ break;
+ default:
+ break;
+ }
+
+ // Updating the pose first and sending the input second.
+ if (hand) {
+ // Updating the ray.
+ if (active) {
+ // Setting the ray pose on pinch.
+ if (p_event.has_ray && hand->ray_submitted == false) {
+ eyes_ray->set_pose("default", p_event.ray, Vector3(), Vector3());
+ hand->ray_submitted = true;
+ }
+ } else {
+ // Resetting the state on release (for the next pinch).
+ hand->ray_submitted = false;
+ }
+
+ if (active) {
+ hand->controller->source = VisionOSSharedController::Source::SpatialEvent;
+ } else {
+ hand->controller->source = VisionOSSharedController::Source::None;
+ }
+
+ // Updating the hand pose.
+ Transform3D pose = p_event.hand_pose * hand->transform_correction;
+ for (const String name : { "default", "aim" }) {
+ hand->controller->tracker->set_pose(name, pose, Vector3(), Vector3());
+ }
+ for (const String name : { "grip", "palm" }) {
+ hand->controller->tracker->invalidate_pose(name);
+ }
+
+ // Submitting input events. It will send a signal that the game
+ // can receive, to handle input.
+ hand->controller->tracker->set_input("trigger_click", active);
+ }
+}
+
+#endif // VISIONOS_ENABLED
diff --git a/modules/visionos_xr/visionos_xr_interface.h b/modules/visionos_xr/visionos_xr_interface.h
index 5c7b4ac9761..955ee125fb0 100644
--- a/modules/visionos_xr/visionos_xr_interface.h
+++ b/modules/visionos_xr/visionos_xr_interface.h
@@ -35,6 +35,9 @@
#include "visionos_controller_tracking.h"
#include "visionos_definitions.h"
#include "visionos_hand_tracking.h"
+#include "visionos_spatial_events.h"
+
+#include "servers/xr/xr_controller_tracker.h"
#ifdef __OBJC__
#import <CompositorServices/CompositorServices.h>
@@ -118,9 +121,14 @@ private:
// Hand tracking
VisionOSHandTracking hands;
+ VisionOSSharedController left_hand, right_hand;
+
// Controller tracking
VisionOSControllerTracking controllers;
+ // Pinching events
+ VisionOSSpatialEventTracking spatial_events;
+
// Data and functions only accessible from the rendering thread
class RenderThread : public Object {
// Inherit from Object to use callable_mp(), so we declare it as GDCLASS,
@@ -225,6 +233,8 @@ public:
// or when a new controller was connected (on visionOS 26 and earlier).
void run_ar_session();
+ void on_spatial_event(const VisionOSSpatialEvent &);
+
// The LayerRenderer and Capabilities are polled from the app delegate when initializing the VisionOSXRInterface,
// but they need to be updated when the app backgrounds and foregrounds because they are recreated by visionOS
void update_layer_renderer(cp_layer_renderer_t p_layer_renderer, cp_layer_renderer_capabilities_t p_layer_renderer_capabilities);
diff --git a/modules/visionos_xr/visionos_xr_interface.mm b/modules/visionos_xr/visionos_xr_interface.mm
index ebf9ace33cb..419d2159043 100644
--- a/modules/visionos_xr/visionos_xr_interface.mm
+++ b/modules/visionos_xr/visionos_xr_interface.mm
@@ -38,6 +38,7 @@
#include "core/error/error_macros.h"
#include "core/input/input.h"
#include "core/math/transform_3d.h"
+#include "core/math/vector3.h"
#include "core/object/callable_mp.h"
#include "core/object/class_db.h"
#include "core/os/os.h"
@@ -48,6 +49,7 @@
#include "servers/rendering/rendering_server.h" // ERR_NOT_ON_RENDER_THREAD_V
#include "servers/rendering/rendering_server_globals.h"
#include "servers/rendering/rendering_server_types.h"
+#include "servers/xr/xr_controller_tracker.h"
#include "servers/xr/xr_server.h"
#include "platform/visionos/godot_app_delegate_service_visionos.h"
@@ -150,6 +152,18 @@ bool VisionOSXRInterface::initialize() {
hands.enabled = GLOBAL_GET("xr/visionos/enable_hand_tracking");
controllers.enabled = GLOBAL_GET("xr/visionos/enable_controller_tracking");
+ // Shared trackers
+ left_hand.tracker.instantiate();
+ left_hand.tracker->set_tracker_hand(XRPositionalTracker::TRACKER_HAND_LEFT);
+ left_hand.tracker->set_tracker_name("left_hand");
+ left_hand.tracker->set_tracker_desc("visionOS left controller and spatial event");
+ xr_server->add_tracker(left_hand.tracker);
+ right_hand.tracker.instantiate();
+ right_hand.tracker->set_tracker_hand(XRPositionalTracker::TRACKER_HAND_RIGHT);
+ right_hand.tracker->set_tracker_name("right_hand");
+ right_hand.tracker->set_tracker_desc("visionOS right controller and spatial event");
+ xr_server->add_tracker(right_hand.tracker);
+
// ARKit session
ar_session = ar_session_create();
@@ -179,9 +193,11 @@ bool VisionOSXRInterface::initialize() {
// Controllers
if (controllers.enabled) {
- controllers.initialize(xr_server, this);
+ controllers.initialize(xr_server, this, left_hand, right_hand);
}
+ spatial_events.initialize(xr_server, left_hand, right_hand);
+
// Running the ARKit session for head tracking, at first
run_ar_session();
@@ -221,6 +237,18 @@ bool VisionOSXRInterface::CompositorServicesData::initialize(XRServer *p_xr_serv
return true;
}
+namespace {
+
+template <typename TrackerType>
+void uninitialize_tracker(Ref<TrackerType> &p_tracker, XRServer *p_xr_server) {
+ if (p_tracker.is_valid()) {
+ p_xr_server->remove_tracker(p_tracker);
+ p_tracker.unref();
+ }
+}
+
+} // namespace
+
void VisionOSXRInterface::uninitialize() {
if (!initialized) {
return;
@@ -232,26 +260,20 @@ void VisionOSXRInterface::uninitialize() {
XRServer *xr_server = XRServer::get_singleton();
if (xr_server != nullptr) {
+ uninitialize_tracker(left_hand.tracker, xr_server);
+ uninitialize_tracker(right_hand.tracker, xr_server);
+
if (controllers.enabled) {
controllers.uninitialize(xr_server);
}
if (hands.enabled) {
- if (hands.left_hand_tracker.is_valid()) {
- xr_server->remove_tracker(hands.left_hand_tracker);
- hands.left_hand_tracker.unref();
- }
- if (hands.right_hand_tracker.is_valid()) {
- xr_server->remove_tracker(hands.right_hand_tracker);
- hands.right_hand_tracker.unref();
- }
+ uninitialize_tracker(hands.left_hand_tracker, xr_server);
+ uninitialize_tracker(hands.right_hand_tracker, xr_server);
}
if (cs.enabled) {
- if (cs.head_tracker.is_valid()) {
- xr_server->remove_tracker(cs.head_tracker);
- cs.head_tracker.unref();
- }
+ uninitialize_tracker(cs.head_tracker, xr_server);
if (xr_server->get_primary_interface() == this) {
// no longer our primary interface
@@ -259,6 +281,8 @@ void VisionOSXRInterface::uninitialize() {
}
}
+ spatial_events.uninitialize(xr_server);
+
initialized = false;
}
@@ -541,6 +565,10 @@ void VisionOSXRInterface::run_ar_session() {
ar_session_run(ar_session, ar_data_providers);
}
+void VisionOSXRInterface::on_spatial_event(const VisionOSSpatialEvent &p_event) {
+ spatial_events.on_spatial_event(p_event);
+}
+
CFTimeInterval VisionOSXRInterface::get_trackable_anchor_time() {
// Computing the time to use for pose prediction
CFTimeInterval trackable_anchor_time = 0;
@@ -597,6 +625,15 @@ void VisionOSXRInterface::process() {
controllers.update_controller_trackers_from_arkit(trackable_anchor_time);
}
}
+
+ // Invalidating poses on controllers when not tracked.
+ for (const VisionOSSharedController &hand : { left_hand, right_hand }) {
+ if (hand.source == VisionOSSharedController::Source::None) {
+ for (const String name : { "default", "aim", "grip", "palm" }) {
+ hand.tracker->invalidate_pose(name);
+ }
+ }
+ }
}
Size2 VisionOSXRInterface::get_render_target_size() {
diff --git a/platform/visionos/app_visionos.swift b/platform/visionos/app_visionos.swift
index 1675d4ecf52..72d51e7dbe1 100644
--- a/platform/visionos/app_visionos.swift
+++ b/platform/visionos/app_visionos.swift
@@ -116,6 +116,59 @@ public final class GDTSwiftBridge: NSObject {
}
}
+/// ObjC-accessible version of SwiftUI's "SpatialEventCollection.Event".
+/// https://developer.apple.com/documentation/swiftui/spatialeventcollection/event
+@MainActor
+@objc
+public final class SpatialEventObjC: NSObject {
+
+ // Ray
+ @objc var hasRay: Bool = false
+ @objc var rayOrigin: simd_double3 = .init()
+ @objc var rayDirection: simd_double3 = .init()
+
+ // Hand
+ @objc enum Chirality: Int {
+ case none, left, right
+ }
+ @objc var chirality: Chirality
+ @objc var handPose: simd_double4x4
+
+ // Phase
+ @objc enum Phase: Int {
+ case unknown, active, cancelled, ended
+ }
+ @objc var phase: Phase
+
+ init(_ event: SpatialEventCollection.Event) {
+ if let ray = event.selectionRay {
+ hasRay = true
+ rayOrigin = ray.origin.vector
+ rayDirection = ray.direction.vector
+ }
+
+ chirality = event.chirality.map {
+ switch $0 {
+ case .left: return .left
+ case .right: return .right
+ }
+ } ?? .none
+
+ handPose = event.inputDevicePose.map {
+ return $0.pose3D.matrix
+ } ?? .init(diagonal: SIMD4<Double>(repeating: 1.0))
+
+ phase = {
+ switch event.phase {
+ case .active: return .active
+ case .cancelled: return .cancelled
+ case .ended: return .ended
+ default: return .unknown
+ }
+ }()
+ }
+}
+
// MARK: Compositor Services Scene
struct ContentStageConfiguration: CompositorLayerConfiguration {
@@ -206,6 +259,12 @@ struct CompositorServicesImmersiveSpace: Scene {
renderer = GDTCompositorServicesRenderer(layerRenderer: layerRenderer,
capabilities: GDTAppDelegateServiceVisionOS.layerRendererCapabilities)
+ layerRenderer.onSpatialEvent = { events in
+ for event in events {
+ renderer.onSpatialEvent(.init(event))
+ }
+ }
+
let signposter = OSSignposter(subsystem: "org.godotengine.godot.compositorservices", category: "loading")
let signpostID = signposter.makeSignpostID()
diff --git a/platform/visionos/godot_compositor_services_renderer.h b/platform/visionos/godot_compositor_services_renderer.h
index 47504dfe793..b474febbd78 100644
--- a/platform/visionos/godot_compositor_services_renderer.h
+++ b/platform/visionos/godot_compositor_services_renderer.h
@@ -32,8 +32,12 @@
#import "drivers/apple_embedded/godot_renderer.h"
+#import "modules/visionos_xr/visionos_definitions.h"
+
#import <CompositorServices/CompositorServices.h>
+@class SpatialEventObjC;
+
@interface GDTCompositorServicesRenderer : GDTRenderer
- (instancetype)initWithLayerRenderer:(cp_layer_renderer_t)layer_renderer
@@ -45,4 +49,6 @@
- (void)renderFrame;
- (void)worldRecentered;
+- (void)onSpatialEvent:(SpatialEventObjC *)event;
+
@end
diff --git a/platform/visionos/godot_compositor_services_renderer.mm b/platform/visionos/godot_compositor_services_renderer.mm
index a48eefd6caa..439fd44dac3 100644
--- a/platform/visionos/godot_compositor_services_renderer.mm
+++ b/platform/visionos/godot_compositor_services_renderer.mm
@@ -30,6 +30,11 @@
#import "godot_compositor_services_renderer.h"
+#import "godot_swift_module-Swift.gen.h"
+
+#include "core/math/transform_3d.h"
+#include "core/math/vector3.h"
+#include "core/templates/vector.h"
#import "drivers/apple_embedded/os_apple_embedded.h"
#include "modules/modules_enabled.gen.h"
@@ -43,6 +48,22 @@ extern void apple_embedded_finish();
#if defined(MODULE_VISIONOS_XR_ENABLED)
+namespace {
+
+inline Vector3 convert(simd_double3 p_v) {
+ return Vector3(p_v.x, p_v.y, p_v.z);
+}
+
+inline Vector3 xyz(simd_double4 p_v) {
+ return Vector3(p_v.x, p_v.y, p_v.z);
+}
+
+inline Transform3D convert(simd_double4x4 p_v) {
+ return Transform3D(xyz(p_v.columns[0]), xyz(p_v.columns[1]), xyz(p_v.columns[2]), xyz(p_v.columns[3]));
+}
+
+} // namespace
+
@implementation GDTCompositorServicesRenderer {
cp_layer_renderer_t _layer_renderer;
cp_layer_renderer_capabilities_t _layer_renderer_capabilities;
@@ -124,6 +145,32 @@ extern void apple_embedded_finish();
}
}
+- (void)onSpatialEvent:(SpatialEventObjC *)p_event {
+ Ref<VisionOSXRInterface> visionos_xr_interface = VisionOSXRInterface::find_interface();
+ if (visionos_xr_interface.is_valid()) {
+ VisionOSSpatialEvent event;
+
+ // Convert from the ObjC/Swift type to the C++ type.
+ {
+ // Ray
+ event.has_ray = p_event.hasRay;
+ if (p_event.hasRay) {
+ event.ray.set_look_at(convert(p_event.rayOrigin), convert(p_event.rayOrigin + p_event.rayDirection));
+ }
+ // Hand
+ static_assert((int)VisionOSSpatialEvent::Chirality::right == (int)ChiralityRight);
+ event.chirality = (VisionOSSpatialEvent::Chirality)p_event.chirality;
+ event.hand_pose = convert(p_event.handPose);
+ // Phase
+ static_assert((int)VisionOSSpatialEvent::Phase::ended == (int)PhaseEnded);
+ event.phase = (VisionOSSpatialEvent::Phase)p_event.phase;
+ }
+
+ // Send the event to the VisionOSXRInterface.
+ visionos_xr_interface->on_spatial_event(event);
+ }
+}
+
@end
#else