/**************************************************************************/1/* xr_server.h */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#pragma once3132#include "core/object/ref_counted.h"33#include "core/os/thread_safe.h"34#include "core/variant/variant.h"35#include "rendering_server.h"3637class XRInterface;38class XRTracker;39class XRPositionalTracker;4041/**42The XR server is a singleton object that gives access to the various43objects and SDKs that are available on the system.44Because there can be multiple SDKs active this is exposed as an array45and our XR server object acts as a pass through46Also each positioning tracker is accessible from here.4748I've added some additional info into this header file that should move49into the documentation, I will do so when we're close to accepting this PR50or as a separate PR once this has been merged into the master branch.51**/5253class XRServer : public Object {54GDCLASS(XRServer, Object);55_THREAD_SAFE_CLASS_5657public:58enum XRMode {59XRMODE_DEFAULT, /* Default behavior, means we check project settings */60XRMODE_OFF, /* Ignore project settings, disable OpenXR, disable shaders */61XRMODE_ON, /* Ignore project settings, enable OpenXR, enable shaders, run editor in VR (if applicable) */62};6364enum TrackerType {65TRACKER_HEAD = 0x01, /* tracks the position of the players head (or in case of handheld AR, location of the phone) */66TRACKER_CONTROLLER = 0x02, /* tracks a controller */67TRACKER_BASESTATION = 0x04, /* tracks location of a base station */68TRACKER_ANCHOR = 0x08, /* tracks an anchor point, used in AR to track a real live location */69TRACKER_HAND = 0x10, /* tracks a hand */70TRACKER_BODY = 0x20, /* tracks a body */71TRACKER_FACE = 0x40, /* tracks a face */72TRACKER_UNKNOWN = 0x80, /* unknown tracker */7374TRACKER_ANY_KNOWN = 0x7f, /* all except unknown */75TRACKER_ANY = 0xff /* used by get_connected_trackers to return all types */76};7778enum RotationMode {79RESET_FULL_ROTATION = 0, /* we reset the full rotation, regardless of how the HMD is oriented, we're looking dead ahead */80RESET_BUT_KEEP_TILT = 1, /* reset rotation but keep tilt. */81DONT_RESET_ROTATION = 2, /* don't reset the rotation, we will only center on position */82};8384private:85static XRMode xr_mode;8687Vector<Ref<XRInterface>> interfaces;88Dictionary trackers;8990Ref<XRInterface> primary_interface; /* we'll identify one interface as primary, this will be used by our viewports */9192double world_scale = 1.0; /* scale by which we multiply our tracker positions */93Transform3D world_origin; /* our world origin point, maps a location in our virtual world to the origin point in our real world tracking volume */94Transform3D reference_frame; /* our reference frame */95bool camera_locked_to_origin = false;9697// As we may be updating our main state for our next frame while we're still rendering our previous frame,98// we need to keep copies around.99struct RenderState {100double world_scale = 1.0; /* scale by which we multiply our tracker positions */101Transform3D world_origin; /* our world origin point, maps a location in our virtual world to the origin point in our real world tracking volume */102Transform3D reference_frame; /* our reference frame */103} render_state;104105static void _set_render_world_scale(double p_world_scale);106static void _set_render_world_origin(const Transform3D &p_world_origin);107static void _set_render_reference_frame(const Transform3D &p_reference_frame);108109_FORCE_INLINE_ void set_render_world_scale(double p_world_scale) {110// If we're rendering on a separate thread, we may still be processing the last frame, don't communicate this till we're ready...111RenderingServer *rendering_server = RenderingServer::get_singleton();112ERR_FAIL_NULL(rendering_server);113114rendering_server->call_on_render_thread(callable_mp_static(&XRServer::_set_render_world_scale).bind(p_world_scale));115}116117_FORCE_INLINE_ void set_render_world_origin(const Transform3D &p_world_origin) {118// If we're rendering on a separate thread, we may still be processing the last frame, don't communicate this till we're ready...119RenderingServer *rendering_server = RenderingServer::get_singleton();120ERR_FAIL_NULL(rendering_server);121122rendering_server->call_on_render_thread(callable_mp_static(&XRServer::_set_render_world_origin).bind(p_world_origin));123}124125_FORCE_INLINE_ void set_render_reference_frame(const Transform3D &p_reference_frame) {126// If we're rendering on a separate thread, we may still be processing the last frame, don't communicate this till we're ready...127RenderingServer *rendering_server = RenderingServer::get_singleton();128ERR_FAIL_NULL(rendering_server);129130rendering_server->call_on_render_thread(callable_mp_static(&XRServer::_set_render_reference_frame).bind(p_reference_frame));131}132133protected:134static XRServer *singleton;135136static void _bind_methods();137138#ifndef DISABLE_DEPRECATED139static void _bind_compatibility_methods();140void _add_tracker_bind_compat_90645(const Ref<XRPositionalTracker> &p_tracker);141void _remove_tracker_bind_compat_90645(const Ref<XRPositionalTracker> &p_tracker);142Ref<XRPositionalTracker> _get_tracker_bind_compat_90645(const StringName &p_name) const;143#endif144145public:146static XRMode get_xr_mode();147static void set_xr_mode(XRMode p_mode);148149static XRServer *get_singleton();150151/*152World scale allows you to specify a scale factor that is applied to all positioning vectors in our VR world in essence scaling up, or scaling down the world.153For stereoscopic rendering specifically this is very important to give an accurate sense of scale.154Add controllers into the mix and an accurate mapping of real world movement to perceived virtual movement becomes very important.155156Most VR platforms, and our assumption, is that 1 unit in our virtual world equates to 1 meter in the real mode.157This scale basically effects the unit size relationship to real world size.158159I may remove access to this property in GDScript in favor of exposing it on the XROrigin3D node160*/161double get_world_scale() const;162void set_world_scale(double p_world_scale);163164/*165The world maps the 0,0,0 coordinate of our real world coordinate system for our tracking volume to a location in our166virtual world. It is this origin point that should be moved when the player is moved through the world by controller167actions be it straffing, teleporting, etc. Movement of the player by moving through the physical space is always tracked168in relation to this point.169170Note that the XROrigin3D spatial node in your scene automatically updates this property and it should be used instead of171direct access to this property and it therefore is not available in GDScript172173Note: this should not be used in AR and should be ignored by an AR based interface as it would throw what you're looking at in the real world174and in the virtual world out of sync175*/176Transform3D get_world_origin() const;177void set_world_origin(const Transform3D &p_world_origin);178179/*180center_on_hmd calculates a new reference frame. This ensures the HMD is positioned to 0,0,0 facing 0,0,-1 (need to verify this direction)181in the virtual world.182183You can ignore the tilt of the device ensuring you're looking straight forward even if the player is looking down or sideways.184You can chose to keep the height the tracking provides which is important for room scale capable tracking.185186Note: this should not be used in AR and should be ignored by an AR based interface as it would throw what you're looking at in the real world187and in the virtual world out of sync188*/189Transform3D get_reference_frame() const;190void clear_reference_frame();191void center_on_hmd(RotationMode p_rotation_mode, bool p_keep_height);192193/*194get_hmd_transform gets our hmd transform (centered between eyes) with most up to date tracking, relative to the origin195*/196Transform3D get_hmd_transform();197198void set_camera_locked_to_origin(bool p_enable);199inline bool is_camera_locked_to_origin() const { return camera_locked_to_origin; }200201/*202Interfaces are objects that 'glue' Godot to an AR or VR SDK such as the Oculus SDK, OpenVR, OpenHMD, etc.203*/204void add_interface(const Ref<XRInterface> &p_interface);205void remove_interface(const Ref<XRInterface> &p_interface);206int get_interface_count() const;207Ref<XRInterface> get_interface(int p_index) const;208Ref<XRInterface> find_interface(const String &p_name) const;209TypedArray<Dictionary> get_interfaces() const;210211/*212note, more then one interface can technically be active, especially on mobile, but only one interface is used for213rendering. This interface identifies itself by calling set_primary_interface when it is initialized214*/215Ref<XRInterface> get_primary_interface() const;216void set_primary_interface(const Ref<XRInterface> &p_primary_interface);217218/*219Our trackers are objects that expose tracked information about physical objects such as controller, anchor points, faces, hands etc.220They are created and managed by our active AR/VR interfaces.221*/222void add_tracker(const Ref<XRTracker> &p_tracker);223void remove_tracker(const Ref<XRTracker> &p_tracker);224Dictionary get_trackers(int p_tracker_types);225Ref<XRTracker> get_tracker(const StringName &p_name) const;226227/*228We don't know which trackers and actions will existing during runtime but we can request suggested names from our interfaces to help our IDE UI.229*/230PackedStringArray get_suggested_tracker_names() const;231PackedStringArray get_suggested_pose_names(const StringName &p_tracker_name) const;232// Q: Should we add get_suggested_input_names and get_suggested_haptic_names even though we don't use them for the IDE?233234// Process is called before we handle our physics process and game process. This is where our interfaces will update controller data and such.235void _process();236237// Pre-render is called right before we're rendering our viewports.238// This is where interfaces such as OpenVR and OpenXR will update positioning data.239// Many of these interfaces will also do a predictive sync which ensures we run at a steady framerate.240void pre_render();241242// End-frame is called right after Godot has finished its rendering bits.243void end_frame();244245XRServer();246~XRServer();247};248249#define XR XRServer250251VARIANT_ENUM_CAST(XRServer::TrackerType);252VARIANT_ENUM_CAST(XRServer::RotationMode);253254255