2017-04-23 14:10:41 +02:00
/*************************************************************************/
2020-04-08 16:47:36 +02:00
/* xr_interface.cpp */
2017-04-23 14:10:41 +02:00
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
2017-08-27 14:16:55 +02:00
/* https://godotengine.org */
2017-04-23 14:10:41 +02:00
/*************************************************************************/
2022-01-03 21:27:34 +01:00
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
2017-04-23 14:10:41 +02:00
/* */
/* 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. */
/*************************************************************************/
2018-01-05 00:50:27 +01:00
2020-04-08 16:47:36 +02:00
# include "xr_interface.h"
2022-02-11 12:33:54 +01:00
# include "servers/rendering/renderer_compositor.h"
2017-04-23 14:10:41 +02:00
2020-04-08 16:47:36 +02:00
void XRInterface : : _bind_methods ( ) {
2021-06-27 13:51:30 +02:00
ADD_SIGNAL ( MethodInfo ( " play_area_changed " , PropertyInfo ( Variant : : INT , " mode " ) ) ) ;
2020-04-08 16:47:36 +02:00
ClassDB : : bind_method ( D_METHOD ( " get_name " ) , & XRInterface : : get_name ) ;
ClassDB : : bind_method ( D_METHOD ( " get_capabilities " ) , & XRInterface : : get_capabilities ) ;
2017-04-23 14:10:41 +02:00
2020-04-08 16:47:36 +02:00
ClassDB : : bind_method ( D_METHOD ( " is_primary " ) , & XRInterface : : is_primary ) ;
2021-08-23 07:23:16 +02:00
ClassDB : : bind_method ( D_METHOD ( " set_primary " , " primary " ) , & XRInterface : : set_primary ) ;
2017-04-23 14:10:41 +02:00
2020-04-08 16:47:36 +02:00
ClassDB : : bind_method ( D_METHOD ( " is_initialized " ) , & XRInterface : : is_initialized ) ;
ClassDB : : bind_method ( D_METHOD ( " initialize " ) , & XRInterface : : initialize ) ;
ClassDB : : bind_method ( D_METHOD ( " uninitialize " ) , & XRInterface : : uninitialize ) ;
2017-04-23 14:10:41 +02:00
2020-04-08 16:47:36 +02:00
ClassDB : : bind_method ( D_METHOD ( " get_tracking_status " ) , & XRInterface : : get_tracking_status ) ;
2017-09-29 13:36:27 +02:00
2021-08-23 07:23:16 +02:00
ClassDB : : bind_method ( D_METHOD ( " get_render_target_size " ) , & XRInterface : : get_render_target_size ) ;
2021-05-07 15:19:04 +02:00
ClassDB : : bind_method ( D_METHOD ( " get_view_count " ) , & XRInterface : : get_view_count ) ;
2017-09-29 13:36:27 +02:00
2021-08-29 08:05:11 +02:00
ClassDB : : bind_method ( D_METHOD ( " trigger_haptic_pulse " , " action_name " , " tracker_name " , " frequency " , " amplitude " , " duration_sec " , " delay_sec " ) , & XRInterface : : trigger_haptic_pulse ) ;
2017-09-29 13:36:27 +02:00
ADD_GROUP ( " Interface " , " interface_ " ) ;
2021-08-23 07:23:16 +02:00
ADD_PROPERTY ( PropertyInfo ( Variant : : BOOL , " interface_is_primary " ) , " set_primary " , " is_primary " ) ;
2017-09-29 13:36:27 +02:00
2021-06-27 13:51:30 +02:00
// methods and properties specific to VR...
ClassDB : : bind_method ( D_METHOD ( " supports_play_area_mode " , " mode " ) , & XRInterface : : supports_play_area_mode ) ;
ClassDB : : bind_method ( D_METHOD ( " get_play_area_mode " ) , & XRInterface : : get_play_area_mode ) ;
ClassDB : : bind_method ( D_METHOD ( " set_play_area_mode " , " mode " ) , & XRInterface : : set_play_area_mode ) ;
ClassDB : : bind_method ( D_METHOD ( " get_play_area " ) , & XRInterface : : get_play_area ) ;
ADD_GROUP ( " XR " , " xr_ " ) ;
ADD_PROPERTY ( PropertyInfo ( Variant : : INT , " xr_play_area_mode " , PROPERTY_HINT_ENUM , " Unknown,3DOF,Sitting,Roomscale,Stage " ) , " set_play_area_mode " , " get_play_area_mode " ) ;
2017-04-23 14:10:41 +02:00
2021-06-27 13:51:30 +02:00
// methods and properties specific to AR....
2020-04-08 16:47:36 +02:00
ClassDB : : bind_method ( D_METHOD ( " get_anchor_detection_is_enabled " ) , & XRInterface : : get_anchor_detection_is_enabled ) ;
ClassDB : : bind_method ( D_METHOD ( " set_anchor_detection_is_enabled " , " enable " ) , & XRInterface : : set_anchor_detection_is_enabled ) ;
ClassDB : : bind_method ( D_METHOD ( " get_camera_feed_id " ) , & XRInterface : : get_camera_feed_id ) ;
2017-04-23 14:10:41 +02:00
2022-09-26 09:04:12 +02:00
ClassDB : : bind_method ( D_METHOD ( " is_passthrough_supported " ) , & XRInterface : : is_passthrough_supported ) ;
ClassDB : : bind_method ( D_METHOD ( " is_passthrough_enabled " ) , & XRInterface : : is_passthrough_enabled ) ;
ClassDB : : bind_method ( D_METHOD ( " start_passthrough " ) , & XRInterface : : start_passthrough ) ;
ClassDB : : bind_method ( D_METHOD ( " stop_passthrough " ) , & XRInterface : : stop_passthrough ) ;
2022-11-07 23:31:39 +01:00
ClassDB : : bind_method ( D_METHOD ( " get_transform_for_view " , " view " , " cam_transform " ) , & XRInterface : : get_transform_for_view ) ;
ClassDB : : bind_method ( D_METHOD ( " get_projection_for_view " , " view " , " aspect " , " near " , " far " ) , & XRInterface : : get_projection_for_view ) ;
2022-09-26 09:04:12 +02:00
2017-09-29 13:36:27 +02:00
ADD_GROUP ( " AR " , " ar_ " ) ;
ADD_PROPERTY ( PropertyInfo ( Variant : : BOOL , " ar_is_anchor_detection_enabled " ) , " set_anchor_detection_is_enabled " , " get_anchor_detection_is_enabled " ) ;
2020-04-08 16:47:36 +02:00
BIND_ENUM_CONSTANT ( XR_NONE ) ;
BIND_ENUM_CONSTANT ( XR_MONO ) ;
BIND_ENUM_CONSTANT ( XR_STEREO ) ;
2021-08-29 08:05:11 +02:00
BIND_ENUM_CONSTANT ( XR_QUAD ) ;
BIND_ENUM_CONSTANT ( XR_VR ) ;
2020-04-08 16:47:36 +02:00
BIND_ENUM_CONSTANT ( XR_AR ) ;
BIND_ENUM_CONSTANT ( XR_EXTERNAL ) ;
2017-04-23 14:10:41 +02:00
2020-04-08 16:47:36 +02:00
BIND_ENUM_CONSTANT ( XR_NORMAL_TRACKING ) ;
BIND_ENUM_CONSTANT ( XR_EXCESSIVE_MOTION ) ;
BIND_ENUM_CONSTANT ( XR_INSUFFICIENT_FEATURES ) ;
BIND_ENUM_CONSTANT ( XR_UNKNOWN_TRACKING ) ;
BIND_ENUM_CONSTANT ( XR_NOT_TRACKING ) ;
2021-06-27 13:51:30 +02:00
BIND_ENUM_CONSTANT ( XR_PLAY_AREA_UNKNOWN ) ;
BIND_ENUM_CONSTANT ( XR_PLAY_AREA_3DOF ) ;
BIND_ENUM_CONSTANT ( XR_PLAY_AREA_SITTING ) ;
BIND_ENUM_CONSTANT ( XR_PLAY_AREA_ROOMSCALE ) ;
BIND_ENUM_CONSTANT ( XR_PLAY_AREA_STAGE ) ;
} ;
2017-04-23 14:10:41 +02:00
2020-04-08 16:47:36 +02:00
bool XRInterface : : is_primary ( ) {
XRServer * xr_server = XRServer : : get_singleton ( ) ;
ERR_FAIL_NULL_V ( xr_server , false ) ;
2017-04-23 14:10:41 +02:00
2020-04-08 16:47:36 +02:00
return xr_server - > get_primary_interface ( ) = = this ;
2021-08-23 07:23:16 +02:00
}
2017-04-23 14:10:41 +02:00
2021-08-23 07:23:16 +02:00
void XRInterface : : set_primary ( bool p_primary ) {
2020-04-08 16:47:36 +02:00
XRServer * xr_server = XRServer : : get_singleton ( ) ;
ERR_FAIL_NULL ( xr_server ) ;
2017-04-23 14:10:41 +02:00
2021-08-23 07:23:16 +02:00
if ( p_primary ) {
2017-04-23 14:10:41 +02:00
ERR_FAIL_COND ( ! is_initialized ( ) ) ;
2020-04-08 16:47:36 +02:00
xr_server - > set_primary_interface ( this ) ;
2021-08-23 07:23:16 +02:00
} else if ( xr_server - > get_primary_interface ( ) = = this ) {
xr_server - > set_primary_interface ( nullptr ) ;
}
}
2017-09-29 13:36:27 +02:00
2021-08-23 07:23:16 +02:00
XRInterface : : XRInterface ( ) { }
2017-09-29 13:36:27 +02:00
2022-02-11 12:33:54 +01:00
XRInterface : : ~ XRInterface ( ) {
if ( vrs . vrs_texture . is_valid ( ) ) {
2022-12-12 18:42:37 +01:00
ERR_FAIL_NULL ( RenderingServer : : get_singleton ( ) ) ;
2022-02-11 12:33:54 +01:00
RS : : get_singleton ( ) - > free ( vrs . vrs_texture ) ;
vrs . vrs_texture = RID ( ) ;
}
}
2019-03-30 14:03:49 +01:00
2021-06-27 13:51:30 +02:00
// query if this interface supports this play area mode
bool XRInterface : : supports_play_area_mode ( XRInterface : : PlayAreaMode p_mode ) {
return p_mode = = XR_PLAY_AREA_UNKNOWN ;
}
// get the current play area mode
XRInterface : : PlayAreaMode XRInterface : : get_play_area_mode ( ) const {
return XR_PLAY_AREA_UNKNOWN ;
}
// change the play area mode, note that this should return false if the mode is not available
bool XRInterface : : set_play_area_mode ( XRInterface : : PlayAreaMode p_mode ) {
return p_mode = = XR_PLAY_AREA_UNKNOWN ;
}
// if available, returns an array of vectors denoting the play area the player can move around in
PackedVector3Array XRInterface : : get_play_area ( ) const {
// Return an empty array by default.
// Note implementation is responsible for applying our reference frame and world scale to the raw data.
// `play_area_changed` should be emitted if play area data is available and either the reference frame or world scale changes.
return PackedVector3Array ( ) ;
} ;
2017-09-29 13:36:27 +02:00
/** these will only be implemented on AR interfaces, so we want dummies for VR **/
2020-04-08 16:47:36 +02:00
bool XRInterface : : get_anchor_detection_is_enabled ( ) const {
2017-09-29 13:36:27 +02:00
return false ;
2021-08-23 07:23:16 +02:00
}
2017-09-29 13:36:27 +02:00
2020-05-19 11:24:58 +02:00
void XRInterface : : set_anchor_detection_is_enabled ( bool p_enable ) {
}
2017-08-20 16:17:24 +02:00
2020-04-08 16:47:36 +02:00
int XRInterface : : get_camera_feed_id ( ) {
2017-08-20 16:17:24 +02:00
return 0 ;
2021-08-23 07:23:16 +02:00
}
2022-02-11 12:33:54 +01:00
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.
2022-11-16 03:32:42 +01:00
int32_t texel_width = RD : : get_singleton ( ) - > limit_get ( RD : : LIMIT_VRS_TEXEL_WIDTH ) ;
int32_t texel_height = RD : : get_singleton ( ) - > limit_get ( RD : : LIMIT_VRS_TEXEL_HEIGHT ) ;
2022-02-11 12:33:54 +01:00
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 ?
2022-11-16 03:32:42 +01:00
Size2 vrs_size = Size2 ( round ( 0.5 + target_size . x / texel_width ) , round ( 0.5 + target_size . y / texel_height ) ) ;
2022-02-11 12:33:54 +01:00
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
2022-11-16 03:32:42 +01:00
// 2, // 1x4 - not supported
// 3, // 1x8 - not supported
2022-02-11 12:33:54 +01:00
// 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...
Implement Vector4, Vector4i, Projection
Implement built-in classes Vector4, Vector4i and Projection.
* Two versions of Vector4 (float and integer).
* A Projection class, which is a 4x4 matrix specialized in projection types.
These types have been requested for a long time, but given they were very corner case they were not added before.
Because in Godot 4, reimplementing parts of the rendering engine is now possible, access to these types (heavily used by the rendering code) becomes a necessity.
**Q**: Why Projection and not Matrix4?
**A**: Godot does not use Matrix2, Matrix3, Matrix4x3, etc. naming convention because, within the engine, these types always have a *purpose*. As such, Godot names them: Transform2D, Transform3D or Basis. In this case, this 4x4 matrix is _always_ used as a _Projection_, hence the naming.
2022-07-20 01:11:13 +02:00
Projection cm = get_projection_for_view ( i , aspect , 0.1 , 1000.0 ) ;
2022-02-11 12:33:54 +01:00
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 ;
}
}
2022-07-22 20:06:19 +02:00
images . push_back ( Image : : create_from_data ( vrs_sizei . x , vrs_sizei . y , false , Image : : FORMAT_R8 , data ) ) ;
2022-02-11 12:33:54 +01:00
}
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 ;
}
2021-08-23 07:23:16 +02:00
/** these are optional, so we want dummies **/
2022-09-01 10:10:53 +02:00
RID XRInterface : : get_color_texture ( ) {
return RID ( ) ;
}
RID XRInterface : : get_depth_texture ( ) {
return RID ( ) ;
}
RID XRInterface : : get_velocity_texture ( ) {
return RID ( ) ;
}
2021-08-29 08:05:11 +02:00
PackedStringArray XRInterface : : get_suggested_tracker_names ( ) const {
PackedStringArray arr ;
return arr ;
}
PackedStringArray XRInterface : : get_suggested_pose_names ( const StringName & p_tracker_name ) const {
PackedStringArray arr ;
return arr ;
}
2021-08-23 07:23:16 +02:00
XRInterface : : TrackingStatus XRInterface : : get_tracking_status ( ) const {
return XR_UNKNOWN_TRACKING ;
}
2021-08-29 08:05:11 +02:00
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 ) {
}