diff options
Diffstat (limited to 'servers/xr')
-rw-r--r-- | servers/xr/xr_interface.cpp | 95 | ||||
-rw-r--r-- | servers/xr/xr_interface.h | 30 | ||||
-rw-r--r-- | servers/xr/xr_interface_extension.cpp | 67 | ||||
-rw-r--r-- | servers/xr/xr_interface_extension.h | 21 | ||||
-rw-r--r-- | servers/xr/xr_pose.cpp | 22 | ||||
-rw-r--r-- | servers/xr/xr_pose.h | 19 | ||||
-rw-r--r-- | servers/xr/xr_positional_tracker.cpp | 53 | ||||
-rw-r--r-- | servers/xr/xr_positional_tracker.h | 27 |
8 files changed, 243 insertions, 91 deletions
diff --git a/servers/xr/xr_interface.cpp b/servers/xr/xr_interface.cpp index ca11df439c..4b9ea40223 100644 --- a/servers/xr/xr_interface.cpp +++ b/servers/xr/xr_interface.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -29,7 +29,7 @@ /*************************************************************************/ #include "xr_interface.h" -// #include "servers/rendering/renderer_compositor.h" +#include "servers/rendering/renderer_compositor.h" void XRInterface::_bind_methods() { ADD_SIGNAL(MethodInfo("play_area_changed", PropertyInfo(Variant::INT, "mode"))); @@ -114,7 +114,12 @@ void XRInterface::set_primary(bool p_primary) { XRInterface::XRInterface() {} -XRInterface::~XRInterface() {} +XRInterface::~XRInterface() { + if (vrs.vrs_texture.is_valid()) { + RS::get_singleton()->free(vrs.vrs_texture); + vrs.vrs_texture = RID(); + } +} // query if this interface supports this play area mode bool XRInterface::supports_play_area_mode(XRInterface::PlayAreaMode p_mode) { @@ -151,6 +156,85 @@ int XRInterface::get_camera_feed_id() { return 0; } +RID XRInterface::get_vrs_texture() { + // Default logic will return a standard VRS image based on our target size and default projections. + // Note that this only gets called if VRS is supported on the hardware. + + Size2 texel_size = Size2(16.0, 16.0); // For now we assume we always use 16x16 texels, seems to be the standard. + int view_count = get_view_count(); + Size2 target_size = get_render_target_size(); + real_t aspect = target_size.x / target_size.y; // is this y/x ? + Size2 vrs_size = Size2(round(0.5 + target_size.x / texel_size.x), round(0.5 + target_size.y / texel_size.y)); + real_t radius = vrs_size.length() * 0.5; + Size2 vrs_sizei = vrs_size; + + if (vrs.size != vrs_sizei) { + const uint8_t densities[] = { + 0, // 1x1 + 1, // 1x2 + // 4, // 2x1 + 5, // 2x2 + 6, // 2x4 + // 9, // 4x2 + 10, // 4x4 + }; + + // out with the old + if (vrs.vrs_texture.is_valid()) { + RS::get_singleton()->free(vrs.vrs_texture); + vrs.vrs_texture = RID(); + } + + // in with the new + Vector<Ref<Image>> images; + vrs.size = vrs_sizei; + + for (int i = 0; i < view_count && i < 2; i++) { + PackedByteArray data; + data.resize(vrs_sizei.x * vrs_sizei.y); + uint8_t *data_ptr = data.ptrw(); + + // Our near and far don't matter much for what we're doing here, but there are some interfaces that will remember this as the near and far and may fail as a result... + Projection cm = get_projection_for_view(i, aspect, 0.1, 1000.0); + Vector3 center = cm.xform(Vector3(0.0, 0.0, 999.0)); + + Vector2i view_center; + view_center.x = int(vrs_size.x * (center.x + 1.0) * 0.5); + view_center.y = int(vrs_size.y * (center.y + 1.0) * 0.5); + + int d = 0; + for (int y = 0; y < vrs_sizei.y; y++) { + for (int x = 0; x < vrs_sizei.x; x++) { + Vector2 offset = Vector2(x - view_center.x, y - view_center.y); + offset.y *= aspect; + real_t distance = offset.length(); + int idx = round(5.0 * distance / radius); + if (idx > 4) { + idx = 4; + } + uint8_t density = densities[idx]; + + data_ptr[d++] = density; + } + } + + Ref<Image> image; + image.instantiate(); + image->create_from_data(vrs_sizei.x, vrs_sizei.y, false, Image::FORMAT_R8, data); + + images.push_back(image); + } + + if (images.size() == 1) { + vrs.vrs_texture = RS::get_singleton()->texture_2d_create(images[0]); + } else { + vrs.vrs_texture = RS::get_singleton()->texture_2d_layered_create(images, RS::TEXTURE_LAYERED_2D_ARRAY); + } + } + + return vrs.vrs_texture; +} + /** these are optional, so we want dummies **/ PackedStringArray XRInterface::get_suggested_tracker_names() const { PackedStringArray arr; @@ -168,8 +252,5 @@ XRInterface::TrackingStatus XRInterface::get_tracking_status() const { return XR_UNKNOWN_TRACKING; } -void XRInterface::notification(int p_what) { -} - void XRInterface::trigger_haptic_pulse(const String &p_action_name, const StringName &p_tracker_name, double p_frequency, double p_amplitude, double p_duration_sec, double p_delay_sec) { } diff --git a/servers/xr/xr_interface.h b/servers/xr/xr_interface.h index b489481f75..f11458f1cc 100644 --- a/servers/xr/xr_interface.h +++ b/servers/xr/xr_interface.h @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -31,7 +31,7 @@ #ifndef XR_INTERFACE_H #define XR_INTERFACE_H -#include "core/math/camera_matrix.h" +#include "core/math/projection.h" #include "core/os/thread_safe.h" #include "servers/xr_server.h" @@ -39,15 +39,13 @@ struct BlitToScreen; /** - @author Bastiaan Olij <mux213@gmail.com> - The XR interface is a template class on top of which we build interface to different AR, VR and tracking SDKs. The idea is that we subclass this class, implement the logic, and then instantiate a singleton of each interface when Godot starts. These instances do not initialize themselves but register themselves with the AR/VR server. If the user wants to enable AR/VR the choose the interface they want to use and initialize it. - Note that we may make this into a fully instantiable class for GDNative support. + Note that we may make this into a fully instantiable class for GDExtension support. */ class XRInterface : public RefCounted { @@ -121,21 +119,31 @@ public: virtual uint32_t get_view_count() = 0; /* returns the view count we need (1 is monoscopic, 2 is stereoscopic but can be more) */ virtual Transform3D get_camera_transform() = 0; /* returns the position of our camera for updating our camera node. For monoscopic this is equal to the views transform, for stereoscopic this should be an average */ virtual Transform3D get_transform_for_view(uint32_t p_view, const Transform3D &p_cam_transform) = 0; /* get each views transform */ - virtual CameraMatrix get_projection_for_view(uint32_t p_view, double p_aspect, double p_z_near, double p_z_far) = 0; /* get each view projection matrix */ + virtual Projection get_projection_for_view(uint32_t p_view, double p_aspect, double p_z_near, double p_z_far) = 0; /* get each view projection matrix */ + virtual RID get_vrs_texture(); /* obtain VRS texture */ // note, external color/depth/vrs texture support will be added here soon. - virtual Vector<BlitToScreen> commit_views(RID p_render_target, const Rect2 &p_screen_rect) = 0; /* commit rendered views to the XR interface */ - virtual void process() = 0; - virtual void notification(int p_what); + virtual void pre_render(){}; + virtual bool pre_draw_viewport(RID p_render_target) { return true; }; /* inform XR interface we are about to start our viewport draw process */ + virtual Vector<BlitToScreen> post_draw_viewport(RID p_render_target, const Rect2 &p_screen_rect) = 0; /* inform XR interface we finished our viewport draw process */ + virtual void end_frame(){}; + + virtual void notification(int p_what){}; XRInterface(); ~XRInterface(); + +private: + struct VRSData { + RID vrs_texture; + Size2i size; + } vrs; }; VARIANT_ENUM_CAST(XRInterface::Capabilities); VARIANT_ENUM_CAST(XRInterface::TrackingStatus); VARIANT_ENUM_CAST(XRInterface::PlayAreaMode); -#endif // !XR_INTERFACE_H +#endif // XR_INTERFACE_H diff --git a/servers/xr/xr_interface_extension.cpp b/servers/xr/xr_interface_extension.cpp index 80576ac607..7395cd5ad4 100644 --- a/servers/xr/xr_interface_extension.cpp +++ b/servers/xr/xr_interface_extension.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -29,8 +29,7 @@ /*************************************************************************/ #include "xr_interface_extension.h" -#include "servers/rendering/renderer_rd/renderer_storage_rd.h" -#include "servers/rendering/renderer_storage.h" +#include "servers/rendering/renderer_rd/storage_rd/texture_storage.h" #include "servers/rendering/rendering_server_globals.h" void XRInterfaceExtension::_bind_methods() { @@ -51,10 +50,14 @@ void XRInterfaceExtension::_bind_methods() { GDVIRTUAL_BIND(_get_camera_transform); GDVIRTUAL_BIND(_get_transform_for_view, "view", "cam_transform"); GDVIRTUAL_BIND(_get_projection_for_view, "view", "aspect", "z_near", "z_far"); - - GDVIRTUAL_BIND(_commit_views, "render_target", "screen_rect"); + GDVIRTUAL_BIND(_get_vrs_texture); GDVIRTUAL_BIND(_process); + GDVIRTUAL_BIND(_pre_render); + GDVIRTUAL_BIND(_pre_draw_viewport, "render_target"); + GDVIRTUAL_BIND(_post_draw_viewport, "render_target", "screen_rect"); + GDVIRTUAL_BIND(_end_frame); + GDVIRTUAL_BIND(_notification, "what"); /** input and output **/ @@ -255,12 +258,12 @@ Transform3D XRInterfaceExtension::get_transform_for_view(uint32_t p_view, const return Transform3D(); } -CameraMatrix XRInterfaceExtension::get_projection_for_view(uint32_t p_view, double p_aspect, double p_z_near, double p_z_far) { - CameraMatrix cm; +Projection XRInterfaceExtension::get_projection_for_view(uint32_t p_view, double p_aspect, double p_z_near, double p_z_far) { + Projection cm; PackedFloat64Array arr; if (GDVIRTUAL_CALL(_get_projection_for_view, p_view, p_aspect, p_z_near, p_z_far, arr)) { - ERR_FAIL_COND_V_MSG(arr.size() != 16, CameraMatrix(), "Projection matrix must contain 16 floats"); + ERR_FAIL_COND_V_MSG(arr.size() != 16, Projection(), "Projection matrix must contain 16 floats"); real_t *m = (real_t *)cm.matrix; for (int i = 0; i < 16; i++) { m[i] = arr[i]; @@ -268,13 +271,22 @@ CameraMatrix XRInterfaceExtension::get_projection_for_view(uint32_t p_view, doub return cm; } - return CameraMatrix(); + return Projection(); +} + +RID XRInterfaceExtension::get_vrs_texture() { + RID vrs_texture; + if (GDVIRTUAL_CALL(_get_vrs_texture, vrs_texture)) { + return vrs_texture; + } else { + return XRInterface::get_vrs_texture(); + } } void XRInterfaceExtension::add_blit(RID p_render_target, Rect2 p_src_rect, Rect2i p_dst_rect, bool p_use_layer, uint32_t p_layer, bool p_apply_lens_distortion, Vector2 p_eye_center, double p_k1, double p_k2, double p_upscale, double p_aspect_ratio) { BlitToScreen blit; - ERR_FAIL_COND_MSG(!can_add_blits, "add_blit can only be called from an XR plugin from within _commit_views!"); + ERR_FAIL_COND_MSG(!can_add_blits, "add_blit can only be called from an XR plugin from within _post_draw_viewport!"); blit.render_target = p_render_target; blit.src_rect = p_src_rect; @@ -293,12 +305,31 @@ void XRInterfaceExtension::add_blit(RID p_render_target, Rect2 p_src_rect, Rect2 blits.push_back(blit); } -Vector<BlitToScreen> XRInterfaceExtension::commit_views(RID p_render_target, const Rect2 &p_screen_rect) { +void XRInterfaceExtension::process() { + GDVIRTUAL_CALL(_process); +} + +void XRInterfaceExtension::pre_render() { + GDVIRTUAL_CALL(_pre_render); +} + +bool XRInterfaceExtension::pre_draw_viewport(RID p_render_target) { + bool do_render = true; + + if (GDVIRTUAL_CALL(_pre_draw_viewport, p_render_target, do_render)) { + return do_render; + } else { + // if not implemented we're returning true + return true; + } +} + +Vector<BlitToScreen> XRInterfaceExtension::post_draw_viewport(RID p_render_target, const Rect2 &p_screen_rect) { // This is just so our XR plugin can add blits... blits.clear(); can_add_blits = true; - if (GDVIRTUAL_CALL(_commit_views, p_render_target, p_screen_rect)) { + if (GDVIRTUAL_CALL(_post_draw_viewport, p_render_target, p_screen_rect)) { return blits; } @@ -306,8 +337,8 @@ Vector<BlitToScreen> XRInterfaceExtension::commit_views(RID p_render_target, con return blits; } -void XRInterfaceExtension::process() { - GDVIRTUAL_CALL(_process); +void XRInterfaceExtension::end_frame() { + GDVIRTUAL_CALL(_end_frame); } void XRInterfaceExtension::notification(int p_what) { @@ -317,10 +348,10 @@ void XRInterfaceExtension::notification(int p_what) { RID XRInterfaceExtension::get_render_target_texture(RID p_render_target) { // In due time this will need to be enhance to return the correct INTERNAL RID for the chosen rendering engine. // So once a GLES driver is implemented we'll return that and the implemented plugin needs to handle this correctly too. - RendererStorageRD *rd_storage = RendererStorageRD::base_singleton; - ERR_FAIL_NULL_V_MSG(rd_storage, RID(), "Renderer storage not setup"); + RendererRD::TextureStorage *texture_storage = RendererRD::TextureStorage::get_singleton(); + ERR_FAIL_NULL_V_MSG(texture_storage, RID(), "Texture storage not setup"); - return rd_storage->render_target_get_rd_texture(p_render_target); + return texture_storage->render_target_get_rd_texture(p_render_target); } /* diff --git a/servers/xr/xr_interface_extension.h b/servers/xr/xr_interface_extension.h index 763526de96..65b474425e 100644 --- a/servers/xr/xr_interface_extension.h +++ b/servers/xr/xr_interface_extension.h @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -100,22 +100,31 @@ public: virtual uint32_t get_view_count() override; virtual Transform3D get_camera_transform() override; virtual Transform3D get_transform_for_view(uint32_t p_view, const Transform3D &p_cam_transform) override; - virtual CameraMatrix get_projection_for_view(uint32_t p_view, double p_aspect, double p_z_near, double p_z_far) override; + virtual Projection get_projection_for_view(uint32_t p_view, double p_aspect, double p_z_near, double p_z_far) override; + virtual RID get_vrs_texture() override; GDVIRTUAL0R(Size2, _get_render_target_size); GDVIRTUAL0R(uint32_t, _get_view_count); GDVIRTUAL0R(Transform3D, _get_camera_transform); GDVIRTUAL2R(Transform3D, _get_transform_for_view, uint32_t, const Transform3D &); GDVIRTUAL4R(PackedFloat64Array, _get_projection_for_view, uint32_t, double, double, double); + GDVIRTUAL0R(RID, _get_vrs_texture); void add_blit(RID p_render_target, Rect2 p_src_rect, Rect2i p_dst_rect, bool p_use_layer = false, uint32_t p_layer = 0, bool p_apply_lens_distortion = false, Vector2 p_eye_center = Vector2(), double p_k1 = 0.0, double p_k2 = 0.0, double p_upscale = 1.0, double p_aspect_ratio = 1.0); - virtual Vector<BlitToScreen> commit_views(RID p_render_target, const Rect2 &p_screen_rect) override; - GDVIRTUAL2(_commit_views, RID, const Rect2 &); virtual void process() override; + virtual void pre_render() override; + virtual bool pre_draw_viewport(RID p_render_target) override; + virtual Vector<BlitToScreen> post_draw_viewport(RID p_render_target, const Rect2 &p_screen_rect) override; + virtual void end_frame() override; virtual void notification(int p_what) override; GDVIRTUAL0(_process); + GDVIRTUAL0(_pre_render); + GDVIRTUAL1R(bool, _pre_draw_viewport, RID); + GDVIRTUAL2(_post_draw_viewport, RID, const Rect2 &); + GDVIRTUAL0(_end_frame); + GDVIRTUAL1(_notification, int); /* access to some internals we need */ @@ -123,4 +132,4 @@ public: // RID get_render_target_depth(RID p_render_target); }; -#endif // !XR_INTERFACE_EXTENSION_H +#endif // XR_INTERFACE_EXTENSION_H diff --git a/servers/xr/xr_pose.cpp b/servers/xr/xr_pose.cpp index 0d05e62b46..400f13b9c2 100644 --- a/servers/xr/xr_pose.cpp +++ b/servers/xr/xr_pose.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -33,9 +33,13 @@ #include "servers/xr_server.h" void XRPose::_bind_methods() { + BIND_ENUM_CONSTANT(XR_TRACKING_CONFIDENCE_NONE); + BIND_ENUM_CONSTANT(XR_TRACKING_CONFIDENCE_LOW); + BIND_ENUM_CONSTANT(XR_TRACKING_CONFIDENCE_HIGH); + ClassDB::bind_method(D_METHOD("set_has_tracking_data", "has_tracking_data"), &XRPose::set_has_tracking_data); ClassDB::bind_method(D_METHOD("get_has_tracking_data"), &XRPose::get_has_tracking_data); - ADD_PROPERTY(PropertyInfo(Variant::STRING, "has_tracking_data"), "set_has_tracking_data", "get_has_tracking_data"); + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "has_tracking_data"), "set_has_tracking_data", "get_has_tracking_data"); ClassDB::bind_method(D_METHOD("set_name", "name"), &XRPose::set_name); ClassDB::bind_method(D_METHOD("get_name"), &XRPose::get_name); @@ -53,6 +57,10 @@ void XRPose::_bind_methods() { ClassDB::bind_method(D_METHOD("set_angular_velocity", "velocity"), &XRPose::set_angular_velocity); ClassDB::bind_method(D_METHOD("get_angular_velocity"), &XRPose::get_angular_velocity); ADD_PROPERTY(PropertyInfo(Variant::STRING, "angular_velocity"), "set_angular_velocity", "get_angular_velocity"); + + ClassDB::bind_method(D_METHOD("set_tracking_confidence", "tracking_confidence"), &XRPose::set_tracking_confidence); + ClassDB::bind_method(D_METHOD("get_tracking_confidence"), &XRPose::get_tracking_confidence); + ADD_PROPERTY(PropertyInfo(Variant::INT, "tracking_confidence"), "set_tracking_confidence", "get_tracking_confidence"); } void XRPose::set_has_tracking_data(const bool p_has_tracking_data) { @@ -108,3 +116,11 @@ void XRPose::set_angular_velocity(const Vector3 p_velocity) { Vector3 XRPose::get_angular_velocity() const { return angular_velocity; } + +void XRPose::set_tracking_confidence(const XRPose::TrackingConfidence p_tracking_confidence) { + tracking_confidence = p_tracking_confidence; +} + +XRPose::TrackingConfidence XRPose::get_tracking_confidence() const { + return tracking_confidence; +} diff --git a/servers/xr/xr_pose.h b/servers/xr/xr_pose.h index 223e95ddfe..e7d363764b 100644 --- a/servers/xr/xr_pose.h +++ b/servers/xr/xr_pose.h @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -37,12 +37,20 @@ class XRPose : public RefCounted { GDCLASS(XRPose, RefCounted); public: + // TrackingConfidence gives an indication of how reliable our transform data is. + enum TrackingConfidence { + XR_TRACKING_CONFIDENCE_NONE, // No tracking information is available for this pose. + XR_TRACKING_CONFIDENCE_LOW, // Tracking information may be inaccurate or estimated. + XR_TRACKING_CONFIDENCE_HIGH // Tracking information is deemed accurate and up to date. + }; + private: bool has_tracking_data = false; StringName name; Transform3D transform; Vector3 linear_velocity; Vector3 angular_velocity; + TrackingConfidence tracking_confidence = XR_TRACKING_CONFIDENCE_NONE; protected: static void _bind_methods(); @@ -63,6 +71,11 @@ public: void set_angular_velocity(const Vector3 p_velocity); Vector3 get_angular_velocity() const; + + void set_tracking_confidence(const TrackingConfidence p_tracking_confidence); + TrackingConfidence get_tracking_confidence() const; }; -#endif +VARIANT_ENUM_CAST(XRPose::TrackingConfidence); + +#endif // XR_POSE_H diff --git a/servers/xr/xr_positional_tracker.cpp b/servers/xr/xr_positional_tracker.cpp index 1313a91172..4857167a8e 100644 --- a/servers/xr/xr_positional_tracker.cpp +++ b/servers/xr/xr_positional_tracker.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -49,6 +49,10 @@ void XRPositionalTracker::_bind_methods() { ClassDB::bind_method(D_METHOD("set_tracker_desc", "description"), &XRPositionalTracker::set_tracker_desc); ADD_PROPERTY(PropertyInfo(Variant::STRING, "description"), "set_tracker_desc", "get_tracker_desc"); + ClassDB::bind_method(D_METHOD("get_tracker_profile"), &XRPositionalTracker::get_tracker_profile); + ClassDB::bind_method(D_METHOD("set_tracker_profile", "profile"), &XRPositionalTracker::set_tracker_profile); + ADD_PROPERTY(PropertyInfo(Variant::STRING, "profile"), "set_tracker_profile", "get_tracker_profile"); + ClassDB::bind_method(D_METHOD("get_tracker_hand"), &XRPositionalTracker::get_tracker_hand); ClassDB::bind_method(D_METHOD("set_tracker_hand", "hand"), &XRPositionalTracker::set_tracker_hand); ADD_PROPERTY(PropertyInfo(Variant::INT, "hand", PROPERTY_HINT_ENUM, "Unknown,Left,Right"), "set_tracker_hand", "get_tracker_hand"); @@ -56,7 +60,7 @@ void XRPositionalTracker::_bind_methods() { ClassDB::bind_method(D_METHOD("has_pose", "name"), &XRPositionalTracker::has_pose); ClassDB::bind_method(D_METHOD("get_pose", "name"), &XRPositionalTracker::get_pose); ClassDB::bind_method(D_METHOD("invalidate_pose", "name"), &XRPositionalTracker::invalidate_pose); - ClassDB::bind_method(D_METHOD("set_pose", "name", "transform", "linear_velocity", "angular_velocity"), &XRPositionalTracker::set_pose); + ClassDB::bind_method(D_METHOD("set_pose", "name", "transform", "linear_velocity", "angular_velocity", "tracking_confidence"), &XRPositionalTracker::set_pose); ADD_SIGNAL(MethodInfo("pose_changed", PropertyInfo(Variant::OBJECT, "pose", PROPERTY_HINT_RESOURCE_TYPE, "XRPose"))); ClassDB::bind_method(D_METHOD("get_input", "name"), &XRPositionalTracker::get_input); @@ -65,10 +69,7 @@ void XRPositionalTracker::_bind_methods() { ADD_SIGNAL(MethodInfo("button_released", PropertyInfo(Variant::STRING, "name"))); ADD_SIGNAL(MethodInfo("input_value_changed", PropertyInfo(Variant::STRING, "name"), PropertyInfo(Variant::FLOAT, "value"))); ADD_SIGNAL(MethodInfo("input_axis_changed", PropertyInfo(Variant::STRING, "name"), PropertyInfo(Variant::VECTOR2, "vector"))); - - ClassDB::bind_method(D_METHOD("get_rumble"), &XRPositionalTracker::get_rumble); - ClassDB::bind_method(D_METHOD("set_rumble", "rumble"), &XRPositionalTracker::set_rumble); - ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "rumble"), "set_rumble", "get_rumble"); + ADD_SIGNAL(MethodInfo("profile_changed", PropertyInfo(Variant::STRING, "role"))); }; void XRPositionalTracker::set_tracker_type(XRServer::TrackerType p_type) { @@ -99,6 +100,18 @@ String XRPositionalTracker::get_tracker_desc() const { return description; } +void XRPositionalTracker::set_tracker_profile(const String &p_profile) { + if (profile != p_profile) { + profile = p_profile; + + emit_signal("profile_changed", profile); + } +} + +String XRPositionalTracker::get_tracker_profile() const { + return profile; +} + XRPositionalTracker::TrackerHand XRPositionalTracker::get_tracker_hand() const { return hand; }; @@ -137,7 +150,7 @@ void XRPositionalTracker::invalidate_pose(const StringName &p_action_name) { } } -void XRPositionalTracker::set_pose(const StringName &p_action_name, const Transform3D &p_transform, const Vector3 &p_linear_velocity, const Vector3 &p_angular_velocity) { +void XRPositionalTracker::set_pose(const StringName &p_action_name, const Transform3D &p_transform, const Vector3 &p_linear_velocity, const Vector3 &p_angular_velocity, const XRPose::TrackingConfidence p_tracking_confidence) { Ref<XRPose> new_pose; new_pose.instantiate(); @@ -146,9 +159,10 @@ void XRPositionalTracker::set_pose(const StringName &p_action_name, const Transf new_pose->set_transform(p_transform); new_pose->set_linear_velocity(p_linear_velocity); new_pose->set_angular_velocity(p_angular_velocity); + new_pose->set_tracking_confidence(p_tracking_confidence); poses[p_action_name] = new_pose; - emit_signal("pose_changed", new_pose); + emit_signal(SNAME("pose_changed"), new_pose); // TODO discuss whether we also want to create and emit an InputEventXRPose event } @@ -181,20 +195,20 @@ void XRPositionalTracker::set_input(const StringName &p_action_name, const Varia case Variant::BOOL: { bool pressed = p_value; if (pressed) { - emit_signal("button_pressed", p_action_name); + emit_signal(SNAME("button_pressed"), p_action_name); } else { - emit_signal("button_released", p_action_name); + emit_signal(SNAME("button_released"), p_action_name); } // TODO discuss whether we also want to create and emit an InputEventXRButton event } break; case Variant::FLOAT: { - emit_signal("input_value_changed", p_action_name, p_value); + emit_signal(SNAME("input_value_changed"), p_action_name, p_value); // TODO discuss whether we also want to create and emit an InputEventXRValue event } break; case Variant::VECTOR2: { - emit_signal("input_axis_changed", p_action_name, p_value); + emit_signal(SNAME("input_axis_changed"), p_action_name, p_value); // TODO discuss whether we also want to create and emit an InputEventXRAxis event } break; @@ -205,21 +219,8 @@ void XRPositionalTracker::set_input(const StringName &p_action_name, const Varia } } -real_t XRPositionalTracker::get_rumble() const { - return rumble; -}; - -void XRPositionalTracker::set_rumble(real_t p_rumble) { - if (p_rumble > 0.0) { - rumble = p_rumble; - } else { - rumble = 0.0; - }; -}; - XRPositionalTracker::XRPositionalTracker() { type = XRServer::TRACKER_UNKNOWN; name = "Unknown"; hand = TRACKER_HAND_UNKNOWN; - rumble = 0.0; }; diff --git a/servers/xr/xr_positional_tracker.h b/servers/xr/xr_positional_tracker.h index 69eb105b5a..8371dfcbb5 100644 --- a/servers/xr/xr_positional_tracker.h +++ b/servers/xr/xr_positional_tracker.h @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -37,8 +37,6 @@ #include "servers/xr_server.h" /** - @author Bastiaan Olij <mux213@gmail.com> - The positional tracker object as an object that represents the position and orientation of a tracked object like a controller or headset. An AR/VR Interface will registered the trackers it manages with our AR/VR server and update its position and orientation. This is where potentially additional AR/VR interfaces may be active as there are AR/VR SDKs that solely deal with positional tracking. @@ -58,15 +56,12 @@ public: private: XRServer::TrackerType type; // type of tracker StringName name; // (unique) name of the tracker - String description; // description of the tracker, this is interface dependent, for OpenXR this will be the interaction profile bound for to the tracker + String description; // description of the tracker + String profile; // this is interface dependent, for OpenXR this will be the interaction profile bound for to the tracker TrackerHand hand; // if known, the hand this tracker is held in - Map<StringName, Ref<XRPose>> poses; - Map<StringName, Variant> inputs; - - int joy_id; // if we also have a related joystick entity, the id of the joystick - Ref<Mesh> mesh; // when available, a mesh that can be used to render this tracker - real_t rumble; // rumble strength, 0.0 is off, 1.0 is maximum, note that we only record here, xr_interface is responsible for execution + HashMap<StringName, Ref<XRPose>> poses; + HashMap<StringName, Variant> inputs; protected: static void _bind_methods(); @@ -78,25 +73,23 @@ public: StringName get_tracker_name() const; void set_tracker_desc(const String &p_desc); String get_tracker_desc() const; + void set_tracker_profile(const String &p_profile); + String get_tracker_profile() const; XRPositionalTracker::TrackerHand get_tracker_hand() const; void set_tracker_hand(const XRPositionalTracker::TrackerHand p_hand); bool has_pose(const StringName &p_action_name) const; Ref<XRPose> get_pose(const StringName &p_action_name) const; void invalidate_pose(const StringName &p_action_name); - void set_pose(const StringName &p_action_name, const Transform3D &p_transform, const Vector3 &p_linear_velocity, const Vector3 &p_angular_velocity); + void set_pose(const StringName &p_action_name, const Transform3D &p_transform, const Vector3 &p_linear_velocity, const Vector3 &p_angular_velocity, const XRPose::TrackingConfidence p_tracking_confidence = XRPose::XR_TRACKING_CONFIDENCE_HIGH); Variant get_input(const StringName &p_action_name) const; void set_input(const StringName &p_action_name, const Variant &p_value); - // TODO replace by new implementation - real_t get_rumble() const; - void set_rumble(real_t p_rumble); - XRPositionalTracker(); ~XRPositionalTracker() {} }; VARIANT_ENUM_CAST(XRPositionalTracker::TrackerHand); -#endif +#endif // XR_POSITIONAL_TRACKER_H |