2017-04-23 12:10:41 +00:00
|
|
|
/*************************************************************************/
|
2020-04-08 14:47:36 +00:00
|
|
|
/* xr_nodes.cpp */
|
2017-04-23 12:10:41 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 12:16:55 +00:00
|
|
|
/* https://godotengine.org */
|
2017-04-23 12:10:41 +00:00
|
|
|
/*************************************************************************/
|
2021-01-01 19:13:46 +00:00
|
|
|
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
|
2017-04-23 12:10:41 +00: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. */
|
|
|
|
/*************************************************************************/
|
|
|
|
|
2020-04-08 14:47:36 +00:00
|
|
|
#include "xr_nodes.h"
|
2020-05-19 09:24:58 +00:00
|
|
|
|
2021-08-12 23:05:59 +00:00
|
|
|
#include "scene/main/viewport.h"
|
2020-04-08 14:47:36 +00:00
|
|
|
#include "servers/xr/xr_interface.h"
|
2017-04-23 12:10:41 +00:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2020-04-08 14:47:36 +00:00
|
|
|
void XRCamera3D::_notification(int p_what) {
|
2017-04-23 12:10:41 +00:00
|
|
|
switch (p_what) {
|
|
|
|
case NOTIFICATION_ENTER_TREE: {
|
2020-04-08 14:47:36 +00:00
|
|
|
// need to find our XROrigin3D parent and let it know we're its camera!
|
|
|
|
XROrigin3D *origin = Object::cast_to<XROrigin3D>(get_parent());
|
2020-04-01 23:20:12 +00:00
|
|
|
if (origin != nullptr) {
|
2017-04-23 12:10:41 +00:00
|
|
|
origin->set_tracked_camera(this);
|
|
|
|
}
|
|
|
|
}; break;
|
|
|
|
case NOTIFICATION_EXIT_TREE: {
|
2020-04-08 14:47:36 +00:00
|
|
|
// need to find our XROrigin3D parent and let it know we're no longer its camera!
|
|
|
|
XROrigin3D *origin = Object::cast_to<XROrigin3D>(get_parent());
|
2020-04-01 23:20:12 +00:00
|
|
|
if (origin != nullptr) {
|
2017-04-23 12:10:41 +00:00
|
|
|
origin->clear_tracked_camera_if(this);
|
|
|
|
}
|
|
|
|
}; break;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2020-10-29 10:01:28 +00:00
|
|
|
TypedArray<String> XRCamera3D::get_configuration_warnings() const {
|
|
|
|
TypedArray<String> warnings = Node::get_configuration_warnings();
|
|
|
|
|
|
|
|
if (is_visible() && is_inside_tree()) {
|
|
|
|
// must be child node of XROrigin3D!
|
|
|
|
XROrigin3D *origin = Object::cast_to<XROrigin3D>(get_parent());
|
|
|
|
if (origin == nullptr) {
|
|
|
|
warnings.push_back(TTR("XRCamera3D must have an XROrigin3D node as its parent."));
|
|
|
|
};
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2017-04-23 12:10:41 +00:00
|
|
|
|
2020-10-29 10:01:28 +00:00
|
|
|
return warnings;
|
2017-04-23 12:10:41 +00:00
|
|
|
};
|
|
|
|
|
2020-04-08 14:47:36 +00:00
|
|
|
Vector3 XRCamera3D::project_local_ray_normal(const Point2 &p_pos) const {
|
|
|
|
// get our XRServer
|
|
|
|
XRServer *xr_server = XRServer::get_singleton();
|
|
|
|
ERR_FAIL_NULL_V(xr_server, Vector3());
|
2017-09-29 11:36:27 +00:00
|
|
|
|
2020-04-08 14:47:36 +00:00
|
|
|
Ref<XRInterface> xr_interface = xr_server->get_primary_interface();
|
|
|
|
if (xr_interface.is_null()) {
|
2018-06-24 01:54:08 +00:00
|
|
|
// we might be in the editor or have VR turned off, just call superclass
|
2020-03-26 21:49:16 +00:00
|
|
|
return Camera3D::project_local_ray_normal(p_pos);
|
2018-06-24 01:54:08 +00:00
|
|
|
}
|
2017-09-29 11:36:27 +00:00
|
|
|
|
2019-08-08 20:11:48 +00:00
|
|
|
ERR_FAIL_COND_V_MSG(!is_inside_tree(), Vector3(), "Camera is not inside scene.");
|
2017-09-29 11:36:27 +00:00
|
|
|
|
|
|
|
Size2 viewport_size = get_viewport()->get_camera_rect_size();
|
|
|
|
Vector2 cpos = get_viewport()->get_camera_coords(p_pos);
|
|
|
|
Vector3 ray;
|
|
|
|
|
2021-05-07 13:19:04 +00:00
|
|
|
// Just use the first view, if multiple views are supported this function has no good result
|
|
|
|
CameraMatrix cm = xr_interface->get_projection_for_view(0, viewport_size.aspect(), get_near(), get_far());
|
2020-01-21 18:39:16 +00:00
|
|
|
Vector2 screen_he = cm.get_viewport_half_extents();
|
2020-12-16 12:40:42 +00:00
|
|
|
ray = Vector3(((cpos.x / viewport_size.width) * 2.0 - 1.0) * screen_he.x, ((1.0 - (cpos.y / viewport_size.height)) * 2.0 - 1.0) * screen_he.y, -get_near()).normalized();
|
2017-09-29 11:36:27 +00:00
|
|
|
|
|
|
|
return ray;
|
|
|
|
};
|
|
|
|
|
2020-04-08 14:47:36 +00:00
|
|
|
Point2 XRCamera3D::unproject_position(const Vector3 &p_pos) const {
|
|
|
|
// get our XRServer
|
|
|
|
XRServer *xr_server = XRServer::get_singleton();
|
|
|
|
ERR_FAIL_NULL_V(xr_server, Vector2());
|
2017-09-29 11:36:27 +00:00
|
|
|
|
2020-04-08 14:47:36 +00:00
|
|
|
Ref<XRInterface> xr_interface = xr_server->get_primary_interface();
|
|
|
|
if (xr_interface.is_null()) {
|
2018-06-24 01:54:08 +00:00
|
|
|
// we might be in the editor or have VR turned off, just call superclass
|
2020-03-26 21:49:16 +00:00
|
|
|
return Camera3D::unproject_position(p_pos);
|
2018-06-24 01:54:08 +00:00
|
|
|
}
|
2017-09-29 11:36:27 +00:00
|
|
|
|
2019-08-08 20:11:48 +00:00
|
|
|
ERR_FAIL_COND_V_MSG(!is_inside_tree(), Vector2(), "Camera is not inside scene.");
|
2017-09-29 11:36:27 +00:00
|
|
|
|
|
|
|
Size2 viewport_size = get_viewport()->get_visible_rect().size;
|
|
|
|
|
2021-05-07 13:19:04 +00:00
|
|
|
// Just use the first view, if multiple views are supported this function has no good result
|
|
|
|
CameraMatrix cm = xr_interface->get_projection_for_view(0, viewport_size.aspect(), get_near(), get_far());
|
2017-09-29 11:36:27 +00:00
|
|
|
|
|
|
|
Plane p(get_camera_transform().xform_inv(p_pos), 1.0);
|
|
|
|
|
|
|
|
p = cm.xform4(p);
|
2020-05-10 14:47:11 +00:00
|
|
|
p.normal /= p.d;
|
2017-09-29 11:36:27 +00:00
|
|
|
|
|
|
|
Point2 res;
|
|
|
|
res.x = (p.normal.x * 0.5 + 0.5) * viewport_size.x;
|
|
|
|
res.y = (-p.normal.y * 0.5 + 0.5) * viewport_size.y;
|
|
|
|
|
|
|
|
return res;
|
|
|
|
};
|
|
|
|
|
2021-01-30 00:55:54 +00:00
|
|
|
Vector3 XRCamera3D::project_position(const Point2 &p_point, real_t p_z_depth) const {
|
2020-04-08 14:47:36 +00:00
|
|
|
// get our XRServer
|
|
|
|
XRServer *xr_server = XRServer::get_singleton();
|
|
|
|
ERR_FAIL_NULL_V(xr_server, Vector3());
|
2017-09-29 11:36:27 +00:00
|
|
|
|
2020-04-08 14:47:36 +00:00
|
|
|
Ref<XRInterface> xr_interface = xr_server->get_primary_interface();
|
|
|
|
if (xr_interface.is_null()) {
|
2018-06-24 01:54:08 +00:00
|
|
|
// we might be in the editor or have VR turned off, just call superclass
|
2020-03-26 21:49:16 +00:00
|
|
|
return Camera3D::project_position(p_point, p_z_depth);
|
2018-06-24 01:54:08 +00:00
|
|
|
}
|
2017-09-29 11:36:27 +00:00
|
|
|
|
2019-08-08 20:11:48 +00:00
|
|
|
ERR_FAIL_COND_V_MSG(!is_inside_tree(), Vector3(), "Camera is not inside scene.");
|
2017-09-29 11:36:27 +00:00
|
|
|
|
|
|
|
Size2 viewport_size = get_viewport()->get_visible_rect().size;
|
|
|
|
|
2021-05-07 13:19:04 +00:00
|
|
|
// Just use the first view, if multiple views are supported this function has no good result
|
|
|
|
CameraMatrix cm = xr_interface->get_projection_for_view(0, viewport_size.aspect(), get_near(), get_far());
|
2017-09-29 11:36:27 +00:00
|
|
|
|
2020-01-21 18:39:16 +00:00
|
|
|
Vector2 vp_he = cm.get_viewport_half_extents();
|
2017-09-29 11:36:27 +00:00
|
|
|
|
|
|
|
Vector2 point;
|
|
|
|
point.x = (p_point.x / viewport_size.x) * 2.0 - 1.0;
|
|
|
|
point.y = (1.0 - (p_point.y / viewport_size.y)) * 2.0 - 1.0;
|
2020-01-21 18:39:16 +00:00
|
|
|
point *= vp_he;
|
2017-09-29 11:36:27 +00:00
|
|
|
|
2019-05-28 13:14:13 +00:00
|
|
|
Vector3 p(point.x, point.y, -p_z_depth);
|
2017-09-29 11:36:27 +00:00
|
|
|
|
|
|
|
return get_camera_transform().xform(p);
|
|
|
|
};
|
|
|
|
|
2020-04-08 14:47:36 +00:00
|
|
|
Vector<Plane> XRCamera3D::get_frustum() const {
|
|
|
|
// get our XRServer
|
|
|
|
XRServer *xr_server = XRServer::get_singleton();
|
|
|
|
ERR_FAIL_NULL_V(xr_server, Vector<Plane>());
|
2017-09-29 11:36:27 +00:00
|
|
|
|
2020-04-08 14:47:36 +00:00
|
|
|
Ref<XRInterface> xr_interface = xr_server->get_primary_interface();
|
|
|
|
if (xr_interface.is_null()) {
|
2018-06-24 01:54:08 +00:00
|
|
|
// we might be in the editor or have VR turned off, just call superclass
|
2020-03-26 21:49:16 +00:00
|
|
|
return Camera3D::get_frustum();
|
2018-06-24 01:54:08 +00:00
|
|
|
}
|
2017-09-29 11:36:27 +00:00
|
|
|
|
|
|
|
ERR_FAIL_COND_V(!is_inside_world(), Vector<Plane>());
|
|
|
|
|
|
|
|
Size2 viewport_size = get_viewport()->get_visible_rect().size;
|
2021-05-07 13:19:04 +00:00
|
|
|
// TODO Just use the first view for now, this is mostly for debugging so we may look into using our combined projection here.
|
|
|
|
CameraMatrix cm = xr_interface->get_projection_for_view(0, viewport_size.aspect(), get_near(), get_far());
|
2017-09-29 11:36:27 +00:00
|
|
|
return cm.get_projection_planes(get_camera_transform());
|
|
|
|
};
|
|
|
|
|
2017-04-23 12:10:41 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2020-04-08 14:47:36 +00:00
|
|
|
void XRController3D::_notification(int p_what) {
|
2017-04-23 12:10:41 +00:00
|
|
|
switch (p_what) {
|
|
|
|
case NOTIFICATION_ENTER_TREE: {
|
|
|
|
set_process_internal(true);
|
|
|
|
}; break;
|
|
|
|
case NOTIFICATION_EXIT_TREE: {
|
|
|
|
set_process_internal(false);
|
|
|
|
}; break;
|
|
|
|
case NOTIFICATION_INTERNAL_PROCESS: {
|
2020-04-08 14:47:36 +00:00
|
|
|
// get our XRServer
|
|
|
|
XRServer *xr_server = XRServer::get_singleton();
|
|
|
|
ERR_FAIL_NULL(xr_server);
|
2017-04-23 12:10:41 +00:00
|
|
|
|
|
|
|
// find the tracker for our controller
|
2021-03-29 10:15:53 +00:00
|
|
|
Ref<XRPositionalTracker> tracker = xr_server->find_by_type_and_id(XRServer::TRACKER_CONTROLLER, controller_id);
|
|
|
|
if (!tracker.is_valid()) {
|
2017-04-23 12:10:41 +00:00
|
|
|
// this controller is currently turned off
|
|
|
|
is_active = false;
|
|
|
|
button_states = 0;
|
|
|
|
} else {
|
2017-08-03 08:58:05 +00:00
|
|
|
is_active = true;
|
2017-04-23 12:10:41 +00:00
|
|
|
set_transform(tracker->get_transform(true));
|
|
|
|
|
|
|
|
int joy_id = tracker->get_joy_id();
|
|
|
|
if (joy_id >= 0) {
|
|
|
|
int mask = 1;
|
|
|
|
// check button states
|
|
|
|
for (int i = 0; i < 16; i++) {
|
2017-10-19 12:35:44 +00:00
|
|
|
bool was_pressed = (button_states & mask) == mask;
|
2021-03-25 20:56:12 +00:00
|
|
|
bool is_pressed = Input::get_singleton()->is_joy_button_pressed(joy_id, (JoyButton)i);
|
2017-04-23 12:10:41 +00:00
|
|
|
|
|
|
|
if (!was_pressed && is_pressed) {
|
2021-07-17 21:22:52 +00:00
|
|
|
emit_signal(SNAME("button_pressed"), i);
|
2017-04-23 12:10:41 +00:00
|
|
|
button_states += mask;
|
|
|
|
} else if (was_pressed && !is_pressed) {
|
2021-07-17 21:22:52 +00:00
|
|
|
emit_signal(SNAME("button_released"), i);
|
2017-04-23 12:10:41 +00:00
|
|
|
button_states -= mask;
|
|
|
|
};
|
|
|
|
|
|
|
|
mask = mask << 1;
|
|
|
|
};
|
|
|
|
|
|
|
|
} else {
|
|
|
|
button_states = 0;
|
|
|
|
};
|
2019-02-05 10:02:13 +00:00
|
|
|
|
|
|
|
// check for an updated mesh
|
|
|
|
Ref<Mesh> trackerMesh = tracker->get_mesh();
|
|
|
|
if (mesh != trackerMesh) {
|
|
|
|
mesh = trackerMesh;
|
2021-07-17 21:22:52 +00:00
|
|
|
emit_signal(SNAME("mesh_updated"), mesh);
|
2019-02-05 10:02:13 +00:00
|
|
|
}
|
2017-04-23 12:10:41 +00:00
|
|
|
};
|
|
|
|
}; break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2020-04-08 14:47:36 +00:00
|
|
|
void XRController3D::_bind_methods() {
|
|
|
|
ClassDB::bind_method(D_METHOD("set_controller_id", "controller_id"), &XRController3D::set_controller_id);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_controller_id"), &XRController3D::get_controller_id);
|
2017-12-21 12:10:44 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "controller_id", PROPERTY_HINT_RANGE, "0,32,1"), "set_controller_id", "get_controller_id");
|
2020-04-08 14:47:36 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("get_controller_name"), &XRController3D::get_controller_name);
|
2017-04-23 12:10:41 +00:00
|
|
|
|
|
|
|
// passthroughs to information about our related joystick
|
2020-04-08 14:47:36 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("get_joystick_id"), &XRController3D::get_joystick_id);
|
|
|
|
ClassDB::bind_method(D_METHOD("is_button_pressed", "button"), &XRController3D::is_button_pressed);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_joystick_axis", "axis"), &XRController3D::get_joystick_axis);
|
2017-04-23 12:10:41 +00:00
|
|
|
|
2020-04-08 14:47:36 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("get_is_active"), &XRController3D::get_is_active);
|
2020-12-10 11:16:14 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("get_tracker_hand"), &XRController3D::get_tracker_hand);
|
2017-04-23 12:10:41 +00:00
|
|
|
|
2020-04-08 14:47:36 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("get_rumble"), &XRController3D::get_rumble);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_rumble", "rumble"), &XRController3D::set_rumble);
|
Variant: Added 64-bit packed arrays, renamed Variant::REAL to FLOAT.
- Renames PackedIntArray to PackedInt32Array.
- Renames PackedFloatArray to PackedFloat32Array.
- Adds PackedInt64Array and PackedFloat64Array.
- Renames Variant::REAL to Variant::FLOAT for consistency.
Packed arrays are for storing large amount of data and creating stuff like
meshes, buffers. textures, etc. Forcing them to be 64 is a huge waste of
memory. That said, many users requested the ability to have 64 bits packed
arrays for their games, so this is just an optional added type.
For Variant, the float datatype is always 64 bits, and exposed as `float`.
We still have `real_t` which is the datatype that can change from 32 to 64
bits depending on a compile flag (not entirely working right now, but that's
the idea). It affects math related datatypes and code only.
Neither Variant nor PackedArray make use of real_t, which is only intended
for math precision, so the term is removed from there to keep only float.
2020-02-24 18:20:53 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "rumble", PROPERTY_HINT_RANGE, "0.0,1.0,0.01"), "set_rumble", "get_rumble");
|
2019-06-01 13:42:22 +00:00
|
|
|
ADD_PROPERTY_DEFAULT("rumble", 0.0);
|
2017-11-01 10:46:37 +00:00
|
|
|
|
2020-04-08 14:47:36 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("get_mesh"), &XRController3D::get_mesh);
|
2019-02-05 10:02:13 +00:00
|
|
|
|
2017-04-23 12:10:41 +00:00
|
|
|
ADD_SIGNAL(MethodInfo("button_pressed", PropertyInfo(Variant::INT, "button")));
|
2020-12-08 08:37:48 +00:00
|
|
|
ADD_SIGNAL(MethodInfo("button_released", PropertyInfo(Variant::INT, "button")));
|
2019-02-05 10:02:13 +00:00
|
|
|
ADD_SIGNAL(MethodInfo("mesh_updated", PropertyInfo(Variant::OBJECT, "mesh", PROPERTY_HINT_RESOURCE_TYPE, "Mesh")));
|
2017-04-23 12:10:41 +00:00
|
|
|
};
|
|
|
|
|
2020-04-08 14:47:36 +00:00
|
|
|
void XRController3D::set_controller_id(int p_controller_id) {
|
2017-12-21 12:10:44 +00:00
|
|
|
// We don't check any bounds here, this controller may not yet be active and just be a place holder until it is.
|
|
|
|
// Note that setting this to 0 means this node is not bound to a controller yet.
|
2017-04-23 12:10:41 +00:00
|
|
|
controller_id = p_controller_id;
|
2020-10-29 10:01:28 +00:00
|
|
|
update_configuration_warnings();
|
2017-04-23 12:10:41 +00:00
|
|
|
};
|
|
|
|
|
2020-05-14 09:11:27 +00:00
|
|
|
int XRController3D::get_controller_id() const {
|
2017-04-23 12:10:41 +00:00
|
|
|
return controller_id;
|
|
|
|
};
|
|
|
|
|
2020-05-14 09:11:27 +00:00
|
|
|
String XRController3D::get_controller_name() const {
|
2020-04-08 14:47:36 +00:00
|
|
|
// get our XRServer
|
|
|
|
XRServer *xr_server = XRServer::get_singleton();
|
|
|
|
ERR_FAIL_NULL_V(xr_server, String());
|
2017-04-23 12:10:41 +00:00
|
|
|
|
2021-03-29 10:15:53 +00:00
|
|
|
Ref<XRPositionalTracker> tracker = xr_server->find_by_type_and_id(XRServer::TRACKER_CONTROLLER, controller_id);
|
|
|
|
if (!tracker.is_valid()) {
|
2017-04-23 12:10:41 +00:00
|
|
|
return String("Not connected");
|
|
|
|
};
|
|
|
|
|
2020-04-15 19:22:14 +00:00
|
|
|
return tracker->get_tracker_name();
|
2017-04-23 12:10:41 +00:00
|
|
|
};
|
|
|
|
|
2020-04-08 14:47:36 +00:00
|
|
|
int XRController3D::get_joystick_id() const {
|
|
|
|
// get our XRServer
|
|
|
|
XRServer *xr_server = XRServer::get_singleton();
|
|
|
|
ERR_FAIL_NULL_V(xr_server, 0);
|
2017-04-23 12:10:41 +00:00
|
|
|
|
2021-03-29 10:15:53 +00:00
|
|
|
Ref<XRPositionalTracker> tracker = xr_server->find_by_type_and_id(XRServer::TRACKER_CONTROLLER, controller_id);
|
|
|
|
if (!tracker.is_valid()) {
|
2020-01-22 07:27:32 +00:00
|
|
|
// No tracker? no joystick id... (0 is our first joystick)
|
|
|
|
return -1;
|
2017-04-23 12:10:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return tracker->get_joy_id();
|
|
|
|
};
|
|
|
|
|
2020-04-08 14:47:36 +00:00
|
|
|
bool XRController3D::is_button_pressed(int p_button) const {
|
2017-04-23 12:10:41 +00:00
|
|
|
int joy_id = get_joystick_id();
|
2017-09-14 23:53:59 +00:00
|
|
|
if (joy_id == -1) {
|
2017-04-23 12:10:41 +00:00
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2021-03-25 20:56:12 +00:00
|
|
|
return Input::get_singleton()->is_joy_button_pressed(joy_id, (JoyButton)p_button);
|
2017-04-23 12:10:41 +00:00
|
|
|
};
|
|
|
|
|
2020-04-08 14:47:36 +00:00
|
|
|
float XRController3D::get_joystick_axis(int p_axis) const {
|
2017-04-23 12:10:41 +00:00
|
|
|
int joy_id = get_joystick_id();
|
2017-09-14 23:53:59 +00:00
|
|
|
if (joy_id == -1) {
|
2017-04-23 12:10:41 +00:00
|
|
|
return 0.0;
|
|
|
|
};
|
|
|
|
|
2021-03-25 20:56:12 +00:00
|
|
|
return Input::get_singleton()->get_joy_axis(joy_id, (JoyAxis)p_axis);
|
2017-04-23 12:10:41 +00:00
|
|
|
};
|
|
|
|
|
2020-04-08 14:47:36 +00:00
|
|
|
real_t XRController3D::get_rumble() const {
|
|
|
|
// get our XRServer
|
|
|
|
XRServer *xr_server = XRServer::get_singleton();
|
|
|
|
ERR_FAIL_NULL_V(xr_server, 0.0);
|
2017-11-01 10:46:37 +00:00
|
|
|
|
2021-03-29 10:15:53 +00:00
|
|
|
Ref<XRPositionalTracker> tracker = xr_server->find_by_type_and_id(XRServer::TRACKER_CONTROLLER, controller_id);
|
|
|
|
if (!tracker.is_valid()) {
|
2017-11-01 10:46:37 +00:00
|
|
|
return 0.0;
|
|
|
|
};
|
|
|
|
|
|
|
|
return tracker->get_rumble();
|
|
|
|
};
|
|
|
|
|
2020-04-08 14:47:36 +00:00
|
|
|
void XRController3D::set_rumble(real_t p_rumble) {
|
|
|
|
// get our XRServer
|
|
|
|
XRServer *xr_server = XRServer::get_singleton();
|
|
|
|
ERR_FAIL_NULL(xr_server);
|
2017-11-01 10:46:37 +00:00
|
|
|
|
2021-03-29 10:15:53 +00:00
|
|
|
Ref<XRPositionalTracker> tracker = xr_server->find_by_type_and_id(XRServer::TRACKER_CONTROLLER, controller_id);
|
|
|
|
if (tracker.is_valid()) {
|
2017-11-01 10:46:37 +00:00
|
|
|
tracker->set_rumble(p_rumble);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2020-04-08 14:47:36 +00:00
|
|
|
Ref<Mesh> XRController3D::get_mesh() const {
|
2019-02-05 10:02:13 +00:00
|
|
|
return mesh;
|
|
|
|
}
|
|
|
|
|
2020-04-08 14:47:36 +00:00
|
|
|
bool XRController3D::get_is_active() const {
|
2017-04-23 12:10:41 +00:00
|
|
|
return is_active;
|
|
|
|
};
|
|
|
|
|
2020-12-10 11:16:14 +00:00
|
|
|
XRPositionalTracker::TrackerHand XRController3D::get_tracker_hand() const {
|
2020-04-08 14:47:36 +00:00
|
|
|
// get our XRServer
|
|
|
|
XRServer *xr_server = XRServer::get_singleton();
|
|
|
|
ERR_FAIL_NULL_V(xr_server, XRPositionalTracker::TRACKER_HAND_UNKNOWN);
|
2017-09-10 06:15:11 +00:00
|
|
|
|
2021-03-29 10:15:53 +00:00
|
|
|
Ref<XRPositionalTracker> tracker = xr_server->find_by_type_and_id(XRServer::TRACKER_CONTROLLER, controller_id);
|
|
|
|
if (!tracker.is_valid()) {
|
2020-04-08 14:47:36 +00:00
|
|
|
return XRPositionalTracker::TRACKER_HAND_UNKNOWN;
|
2017-09-10 06:15:11 +00:00
|
|
|
};
|
|
|
|
|
2020-12-10 11:16:14 +00:00
|
|
|
return tracker->get_tracker_hand();
|
2017-09-10 06:15:11 +00:00
|
|
|
};
|
|
|
|
|
2020-10-29 10:01:28 +00:00
|
|
|
TypedArray<String> XRController3D::get_configuration_warnings() const {
|
|
|
|
TypedArray<String> warnings = Node::get_configuration_warnings();
|
2020-05-14 20:59:27 +00:00
|
|
|
|
2020-10-29 10:01:28 +00:00
|
|
|
if (is_visible() && is_inside_tree()) {
|
|
|
|
// must be child node of XROrigin!
|
|
|
|
XROrigin3D *origin = Object::cast_to<XROrigin3D>(get_parent());
|
|
|
|
if (origin == nullptr) {
|
|
|
|
warnings.push_back(TTR("XRController3D must have an XROrigin3D node as its parent."));
|
2020-05-14 20:59:27 +00:00
|
|
|
}
|
2017-04-23 12:10:41 +00:00
|
|
|
|
2020-10-29 10:01:28 +00:00
|
|
|
if (controller_id == 0) {
|
|
|
|
warnings.push_back(TTR("The controller ID must not be 0 or this controller won't be bound to an actual controller."));
|
2020-05-14 20:59:27 +00:00
|
|
|
}
|
2020-10-29 10:01:28 +00:00
|
|
|
}
|
2017-04-23 12:10:41 +00:00
|
|
|
|
2020-10-29 10:01:28 +00:00
|
|
|
return warnings;
|
2017-04-23 12:10:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2020-04-08 14:47:36 +00:00
|
|
|
void XRAnchor3D::_notification(int p_what) {
|
2017-08-03 08:58:05 +00:00
|
|
|
switch (p_what) {
|
|
|
|
case NOTIFICATION_ENTER_TREE: {
|
|
|
|
set_process_internal(true);
|
|
|
|
}; break;
|
|
|
|
case NOTIFICATION_EXIT_TREE: {
|
|
|
|
set_process_internal(false);
|
|
|
|
}; break;
|
|
|
|
case NOTIFICATION_INTERNAL_PROCESS: {
|
2020-04-08 14:47:36 +00:00
|
|
|
// get our XRServer
|
|
|
|
XRServer *xr_server = XRServer::get_singleton();
|
|
|
|
ERR_FAIL_NULL(xr_server);
|
2017-08-03 08:58:05 +00:00
|
|
|
|
|
|
|
// find the tracker for our anchor
|
2021-03-29 10:15:53 +00:00
|
|
|
Ref<XRPositionalTracker> tracker = xr_server->find_by_type_and_id(XRServer::TRACKER_ANCHOR, anchor_id);
|
|
|
|
if (!tracker.is_valid()) {
|
2017-08-03 08:58:05 +00:00
|
|
|
// this anchor is currently not available
|
|
|
|
is_active = false;
|
|
|
|
} else {
|
|
|
|
is_active = true;
|
2020-10-17 05:08:21 +00:00
|
|
|
Transform3D transform;
|
2017-08-03 08:58:05 +00:00
|
|
|
|
|
|
|
// we'll need our world_scale
|
2020-04-08 14:47:36 +00:00
|
|
|
real_t world_scale = xr_server->get_world_scale();
|
2017-08-03 08:58:05 +00:00
|
|
|
|
|
|
|
// get our info from our tracker
|
|
|
|
transform.basis = tracker->get_orientation();
|
|
|
|
transform.origin = tracker->get_position(); // <-- already adjusted to world scale
|
|
|
|
|
|
|
|
// our basis is scaled to the size of the plane the anchor is tracking
|
|
|
|
// extract the size from our basis and reset the scale
|
|
|
|
size = transform.basis.get_scale() * world_scale;
|
2017-08-30 01:47:59 +00:00
|
|
|
transform.basis.orthonormalize();
|
2017-08-03 08:58:05 +00:00
|
|
|
|
|
|
|
// apply our reference frame and set our transform
|
2020-04-08 14:47:36 +00:00
|
|
|
set_transform(xr_server->get_reference_frame() * transform);
|
2019-02-05 10:02:13 +00:00
|
|
|
|
|
|
|
// check for an updated mesh
|
|
|
|
Ref<Mesh> trackerMesh = tracker->get_mesh();
|
|
|
|
if (mesh != trackerMesh) {
|
|
|
|
mesh = trackerMesh;
|
2021-07-17 21:22:52 +00:00
|
|
|
emit_signal(SNAME("mesh_updated"), mesh);
|
2019-02-05 10:02:13 +00:00
|
|
|
}
|
2017-08-03 08:58:05 +00:00
|
|
|
};
|
|
|
|
}; break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2020-04-08 14:47:36 +00:00
|
|
|
void XRAnchor3D::_bind_methods() {
|
|
|
|
ClassDB::bind_method(D_METHOD("set_anchor_id", "anchor_id"), &XRAnchor3D::set_anchor_id);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_anchor_id"), &XRAnchor3D::get_anchor_id);
|
2017-12-21 12:10:44 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "anchor_id", PROPERTY_HINT_RANGE, "0,32,1"), "set_anchor_id", "get_anchor_id");
|
2020-04-08 14:47:36 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("get_anchor_name"), &XRAnchor3D::get_anchor_name);
|
2017-08-03 08:58:05 +00:00
|
|
|
|
2020-04-08 14:47:36 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("get_is_active"), &XRAnchor3D::get_is_active);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_size"), &XRAnchor3D::get_size);
|
2017-09-29 11:36:27 +00:00
|
|
|
|
2020-04-08 14:47:36 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("get_plane"), &XRAnchor3D::get_plane);
|
2019-02-05 10:02:13 +00:00
|
|
|
|
2020-04-08 14:47:36 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("get_mesh"), &XRAnchor3D::get_mesh);
|
2019-02-05 10:02:13 +00:00
|
|
|
ADD_SIGNAL(MethodInfo("mesh_updated", PropertyInfo(Variant::OBJECT, "mesh", PROPERTY_HINT_RESOURCE_TYPE, "Mesh")));
|
2017-08-03 08:58:05 +00:00
|
|
|
};
|
|
|
|
|
2020-04-08 14:47:36 +00:00
|
|
|
void XRAnchor3D::set_anchor_id(int p_anchor_id) {
|
2017-12-21 12:10:44 +00:00
|
|
|
// We don't check any bounds here, this anchor may not yet be active and just be a place holder until it is.
|
|
|
|
// Note that setting this to 0 means this node is not bound to an anchor yet.
|
2017-08-03 08:58:05 +00:00
|
|
|
anchor_id = p_anchor_id;
|
2020-10-29 10:01:28 +00:00
|
|
|
update_configuration_warnings();
|
2017-08-03 08:58:05 +00:00
|
|
|
};
|
|
|
|
|
2020-05-14 09:11:27 +00:00
|
|
|
int XRAnchor3D::get_anchor_id() const {
|
2017-08-03 08:58:05 +00:00
|
|
|
return anchor_id;
|
|
|
|
};
|
|
|
|
|
2020-04-08 14:47:36 +00:00
|
|
|
Vector3 XRAnchor3D::get_size() const {
|
2017-08-03 08:58:05 +00:00
|
|
|
return size;
|
|
|
|
};
|
|
|
|
|
2020-05-14 09:11:27 +00:00
|
|
|
String XRAnchor3D::get_anchor_name() const {
|
2020-04-08 14:47:36 +00:00
|
|
|
// get our XRServer
|
|
|
|
XRServer *xr_server = XRServer::get_singleton();
|
|
|
|
ERR_FAIL_NULL_V(xr_server, String());
|
2017-08-03 08:58:05 +00:00
|
|
|
|
2021-03-29 10:15:53 +00:00
|
|
|
Ref<XRPositionalTracker> tracker = xr_server->find_by_type_and_id(XRServer::TRACKER_ANCHOR, anchor_id);
|
|
|
|
if (!tracker.is_valid()) {
|
2017-08-03 08:58:05 +00:00
|
|
|
return String("Not connected");
|
|
|
|
};
|
|
|
|
|
2020-04-15 19:22:14 +00:00
|
|
|
return tracker->get_tracker_name();
|
2017-08-03 08:58:05 +00:00
|
|
|
};
|
|
|
|
|
2020-04-08 14:47:36 +00:00
|
|
|
bool XRAnchor3D::get_is_active() const {
|
2017-08-03 08:58:05 +00:00
|
|
|
return is_active;
|
|
|
|
};
|
|
|
|
|
2020-10-29 10:01:28 +00:00
|
|
|
TypedArray<String> XRAnchor3D::get_configuration_warnings() const {
|
|
|
|
TypedArray<String> warnings = Node::get_configuration_warnings();
|
2020-05-14 20:59:27 +00:00
|
|
|
|
2020-10-29 10:01:28 +00:00
|
|
|
if (is_visible() && is_inside_tree()) {
|
|
|
|
// must be child node of XROrigin3D!
|
|
|
|
XROrigin3D *origin = Object::cast_to<XROrigin3D>(get_parent());
|
|
|
|
if (origin == nullptr) {
|
|
|
|
warnings.push_back(TTR("XRAnchor3D must have an XROrigin3D node as its parent."));
|
2020-05-14 20:59:27 +00:00
|
|
|
}
|
2017-08-03 08:58:05 +00:00
|
|
|
|
2020-10-29 10:01:28 +00:00
|
|
|
if (anchor_id == 0) {
|
|
|
|
warnings.push_back(TTR("The anchor ID must not be 0 or this anchor won't be bound to an actual anchor."));
|
2020-05-14 20:59:27 +00:00
|
|
|
}
|
2020-10-29 10:01:28 +00:00
|
|
|
}
|
2017-08-03 08:58:05 +00:00
|
|
|
|
2020-10-29 10:01:28 +00:00
|
|
|
return warnings;
|
2017-08-03 08:58:05 +00:00
|
|
|
};
|
|
|
|
|
2020-04-08 14:47:36 +00:00
|
|
|
Plane XRAnchor3D::get_plane() const {
|
2020-12-08 17:35:30 +00:00
|
|
|
Vector3 location = get_position();
|
2017-09-29 11:36:27 +00:00
|
|
|
Basis orientation = get_transform().basis;
|
|
|
|
|
|
|
|
Plane plane(location, orientation.get_axis(1).normalized());
|
|
|
|
|
|
|
|
return plane;
|
|
|
|
};
|
|
|
|
|
2020-04-08 14:47:36 +00:00
|
|
|
Ref<Mesh> XRAnchor3D::get_mesh() const {
|
2019-02-05 10:02:13 +00:00
|
|
|
return mesh;
|
|
|
|
}
|
|
|
|
|
2017-08-03 08:58:05 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2020-10-29 10:01:28 +00:00
|
|
|
TypedArray<String> XROrigin3D::get_configuration_warnings() const {
|
|
|
|
TypedArray<String> warnings = Node::get_configuration_warnings();
|
2020-05-14 20:59:27 +00:00
|
|
|
|
2020-10-29 10:01:28 +00:00
|
|
|
if (is_visible() && is_inside_tree()) {
|
|
|
|
if (tracked_camera == nullptr) {
|
|
|
|
warnings.push_back(TTR("XROrigin3D requires an XRCamera3D child node."));
|
2020-05-14 20:59:27 +00:00
|
|
|
}
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2017-04-23 12:10:41 +00:00
|
|
|
|
2021-06-14 01:05:16 +00:00
|
|
|
bool xr_enabled = GLOBAL_GET("rendering/xr/enabled");
|
|
|
|
if (!xr_enabled) {
|
|
|
|
warnings.push_back(TTR("XR is not enabled in rendering project settings. Stereoscopic output is not supported unless this is enabled."));
|
2021-05-07 13:19:04 +00:00
|
|
|
}
|
|
|
|
|
2020-10-29 10:01:28 +00:00
|
|
|
return warnings;
|
2017-04-23 12:10:41 +00:00
|
|
|
};
|
|
|
|
|
2020-04-08 14:47:36 +00:00
|
|
|
void XROrigin3D::_bind_methods() {
|
|
|
|
ClassDB::bind_method(D_METHOD("set_world_scale", "world_scale"), &XROrigin3D::set_world_scale);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_world_scale"), &XROrigin3D::get_world_scale);
|
Variant: Added 64-bit packed arrays, renamed Variant::REAL to FLOAT.
- Renames PackedIntArray to PackedInt32Array.
- Renames PackedFloatArray to PackedFloat32Array.
- Adds PackedInt64Array and PackedFloat64Array.
- Renames Variant::REAL to Variant::FLOAT for consistency.
Packed arrays are for storing large amount of data and creating stuff like
meshes, buffers. textures, etc. Forcing them to be 64 is a huge waste of
memory. That said, many users requested the ability to have 64 bits packed
arrays for their games, so this is just an optional added type.
For Variant, the float datatype is always 64 bits, and exposed as `float`.
We still have `real_t` which is the datatype that can change from 32 to 64
bits depending on a compile flag (not entirely working right now, but that's
the idea). It affects math related datatypes and code only.
Neither Variant nor PackedArray make use of real_t, which is only intended
for math precision, so the term is removed from there to keep only float.
2020-02-24 18:20:53 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "world_scale"), "set_world_scale", "get_world_scale");
|
2017-04-23 12:10:41 +00:00
|
|
|
};
|
|
|
|
|
2020-04-08 14:47:36 +00:00
|
|
|
void XROrigin3D::set_tracked_camera(XRCamera3D *p_tracked_camera) {
|
2017-04-23 12:10:41 +00:00
|
|
|
tracked_camera = p_tracked_camera;
|
|
|
|
};
|
|
|
|
|
2020-04-08 14:47:36 +00:00
|
|
|
void XROrigin3D::clear_tracked_camera_if(XRCamera3D *p_tracked_camera) {
|
2017-04-23 12:10:41 +00:00
|
|
|
if (tracked_camera == p_tracked_camera) {
|
2020-04-01 23:20:12 +00:00
|
|
|
tracked_camera = nullptr;
|
2017-04-23 12:10:41 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2021-01-30 00:55:54 +00:00
|
|
|
real_t XROrigin3D::get_world_scale() const {
|
2020-04-08 14:47:36 +00:00
|
|
|
// get our XRServer
|
|
|
|
XRServer *xr_server = XRServer::get_singleton();
|
|
|
|
ERR_FAIL_NULL_V(xr_server, 1.0);
|
2017-04-23 12:10:41 +00:00
|
|
|
|
2020-04-08 14:47:36 +00:00
|
|
|
return xr_server->get_world_scale();
|
2017-04-23 12:10:41 +00:00
|
|
|
};
|
|
|
|
|
2021-01-30 00:55:54 +00:00
|
|
|
void XROrigin3D::set_world_scale(real_t p_world_scale) {
|
2020-04-08 14:47:36 +00:00
|
|
|
// get our XRServer
|
|
|
|
XRServer *xr_server = XRServer::get_singleton();
|
|
|
|
ERR_FAIL_NULL(xr_server);
|
2017-04-23 12:10:41 +00:00
|
|
|
|
2020-04-08 14:47:36 +00:00
|
|
|
xr_server->set_world_scale(p_world_scale);
|
2017-04-23 12:10:41 +00:00
|
|
|
};
|
|
|
|
|
2020-04-08 14:47:36 +00:00
|
|
|
void XROrigin3D::_notification(int p_what) {
|
|
|
|
// get our XRServer
|
|
|
|
XRServer *xr_server = XRServer::get_singleton();
|
|
|
|
ERR_FAIL_NULL(xr_server);
|
2019-03-13 09:43:21 +00:00
|
|
|
|
2017-04-23 12:10:41 +00:00
|
|
|
switch (p_what) {
|
|
|
|
case NOTIFICATION_ENTER_TREE: {
|
|
|
|
set_process_internal(true);
|
|
|
|
}; break;
|
|
|
|
case NOTIFICATION_EXIT_TREE: {
|
|
|
|
set_process_internal(false);
|
|
|
|
}; break;
|
|
|
|
case NOTIFICATION_INTERNAL_PROCESS: {
|
|
|
|
// set our world origin to our node transform
|
2020-04-08 14:47:36 +00:00
|
|
|
xr_server->set_world_origin(get_global_transform());
|
2017-04-23 12:10:41 +00:00
|
|
|
|
|
|
|
// check if we have a primary interface
|
2020-04-08 14:47:36 +00:00
|
|
|
Ref<XRInterface> xr_interface = xr_server->get_primary_interface();
|
|
|
|
if (xr_interface.is_valid() && tracked_camera != nullptr) {
|
2017-04-23 12:10:41 +00:00
|
|
|
// get our positioning transform for our headset
|
2021-05-07 13:19:04 +00:00
|
|
|
Transform3D t = xr_interface->get_camera_transform();
|
2017-04-23 12:10:41 +00:00
|
|
|
|
|
|
|
// now apply this to our camera
|
|
|
|
tracked_camera->set_transform(t);
|
|
|
|
};
|
|
|
|
}; break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
};
|
2019-03-13 09:43:21 +00:00
|
|
|
|
2020-04-08 14:47:36 +00:00
|
|
|
// send our notification to all active XE interfaces, they may need to react to it also
|
|
|
|
for (int i = 0; i < xr_server->get_interface_count(); i++) {
|
|
|
|
Ref<XRInterface> interface = xr_server->get_interface(i);
|
2019-03-13 09:43:21 +00:00
|
|
|
if (interface.is_valid() && interface->is_initialized()) {
|
|
|
|
interface->notification(p_what);
|
|
|
|
}
|
|
|
|
}
|
2017-04-23 12:10:41 +00:00
|
|
|
};
|