2020-09-27 00:15:21 +02:00
|
|
|
/*************************************************************************/
|
|
|
|
/* webxr_interface_js.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/*************************************************************************/
|
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). */
|
2020-09-27 00:15:21 +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. */
|
|
|
|
/*************************************************************************/
|
|
|
|
|
2022-08-28 20:27:45 +02:00
|
|
|
#ifdef WEB_ENABLED
|
2020-09-27 00:15:21 +02:00
|
|
|
|
|
|
|
#include "webxr_interface_js.h"
|
2022-02-16 13:56:32 +01:00
|
|
|
|
2020-09-27 00:15:21 +02:00
|
|
|
#include "core/input/input.h"
|
|
|
|
#include "core/os/os.h"
|
2022-08-17 00:33:26 +02:00
|
|
|
#include "drivers/gles3/storage/texture_storage.h"
|
2020-09-27 00:15:21 +02:00
|
|
|
#include "emscripten.h"
|
|
|
|
#include "godot_webxr.h"
|
2021-07-17 21:55:19 +02:00
|
|
|
#include "servers/rendering/renderer_compositor.h"
|
2022-08-17 00:33:26 +02:00
|
|
|
#include "servers/rendering/rendering_server_globals.h"
|
2022-02-16 13:56:32 +01:00
|
|
|
|
2020-09-27 00:15:21 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
void _emwebxr_on_session_supported(char *p_session_mode, int p_supported) {
|
|
|
|
XRServer *xr_server = XRServer::get_singleton();
|
|
|
|
ERR_FAIL_NULL(xr_server);
|
|
|
|
|
|
|
|
Ref<XRInterface> interface = xr_server->find_interface("WebXR");
|
|
|
|
ERR_FAIL_COND(interface.is_null());
|
|
|
|
|
|
|
|
String session_mode = String(p_session_mode);
|
2021-07-17 23:22:52 +02:00
|
|
|
interface->emit_signal(SNAME("session_supported"), session_mode, p_supported ? true : false);
|
2020-09-27 00:15:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void _emwebxr_on_session_started(char *p_reference_space_type) {
|
|
|
|
XRServer *xr_server = XRServer::get_singleton();
|
|
|
|
ERR_FAIL_NULL(xr_server);
|
|
|
|
|
|
|
|
Ref<XRInterface> interface = xr_server->find_interface("WebXR");
|
|
|
|
ERR_FAIL_COND(interface.is_null());
|
|
|
|
|
|
|
|
String reference_space_type = String(p_reference_space_type);
|
2022-04-07 12:23:40 +02:00
|
|
|
static_cast<WebXRInterfaceJS *>(interface.ptr())->_set_reference_space_type(reference_space_type);
|
2021-07-17 23:22:52 +02:00
|
|
|
interface->emit_signal(SNAME("session_started"));
|
2020-09-27 00:15:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void _emwebxr_on_session_ended() {
|
|
|
|
XRServer *xr_server = XRServer::get_singleton();
|
|
|
|
ERR_FAIL_NULL(xr_server);
|
|
|
|
|
|
|
|
Ref<XRInterface> interface = xr_server->find_interface("WebXR");
|
|
|
|
ERR_FAIL_COND(interface.is_null());
|
|
|
|
|
|
|
|
interface->uninitialize();
|
2021-07-17 23:22:52 +02:00
|
|
|
interface->emit_signal(SNAME("session_ended"));
|
2020-09-27 00:15:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void _emwebxr_on_session_failed(char *p_message) {
|
|
|
|
XRServer *xr_server = XRServer::get_singleton();
|
|
|
|
ERR_FAIL_NULL(xr_server);
|
|
|
|
|
|
|
|
Ref<XRInterface> interface = xr_server->find_interface("WebXR");
|
|
|
|
ERR_FAIL_COND(interface.is_null());
|
|
|
|
|
2021-01-27 21:16:00 +01:00
|
|
|
interface->uninitialize();
|
|
|
|
|
2020-09-27 00:15:21 +02:00
|
|
|
String message = String(p_message);
|
2021-07-17 23:22:52 +02:00
|
|
|
interface->emit_signal(SNAME("session_failed"), message);
|
2020-09-27 00:15:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void _emwebxr_on_controller_changed() {
|
|
|
|
XRServer *xr_server = XRServer::get_singleton();
|
|
|
|
ERR_FAIL_NULL(xr_server);
|
|
|
|
|
|
|
|
Ref<XRInterface> interface = xr_server->find_interface("WebXR");
|
|
|
|
ERR_FAIL_COND(interface.is_null());
|
|
|
|
|
2022-04-07 12:23:40 +02:00
|
|
|
static_cast<WebXRInterfaceJS *>(interface.ptr())->_on_controller_changed();
|
2020-09-27 00:15:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" EMSCRIPTEN_KEEPALIVE void _emwebxr_on_input_event(char *p_signal_name, int p_input_source) {
|
|
|
|
XRServer *xr_server = XRServer::get_singleton();
|
|
|
|
ERR_FAIL_NULL(xr_server);
|
|
|
|
|
|
|
|
Ref<XRInterface> interface = xr_server->find_interface("WebXR");
|
|
|
|
ERR_FAIL_COND(interface.is_null());
|
|
|
|
|
|
|
|
StringName signal_name = StringName(p_signal_name);
|
2021-07-21 09:28:42 +02:00
|
|
|
interface->emit_signal(signal_name, p_input_source + 1);
|
2020-09-27 00:15:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" EMSCRIPTEN_KEEPALIVE void _emwebxr_on_simple_event(char *p_signal_name) {
|
|
|
|
XRServer *xr_server = XRServer::get_singleton();
|
|
|
|
ERR_FAIL_NULL(xr_server);
|
|
|
|
|
|
|
|
Ref<XRInterface> interface = xr_server->find_interface("WebXR");
|
|
|
|
ERR_FAIL_COND(interface.is_null());
|
|
|
|
|
|
|
|
StringName signal_name = StringName(p_signal_name);
|
2021-07-21 09:28:42 +02:00
|
|
|
interface->emit_signal(signal_name);
|
2020-09-27 00:15:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void WebXRInterfaceJS::is_session_supported(const String &p_session_mode) {
|
|
|
|
godot_webxr_is_session_supported(p_session_mode.utf8().get_data(), &_emwebxr_on_session_supported);
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebXRInterfaceJS::set_session_mode(String p_session_mode) {
|
|
|
|
session_mode = p_session_mode;
|
|
|
|
}
|
|
|
|
|
|
|
|
String WebXRInterfaceJS::get_session_mode() const {
|
|
|
|
return session_mode;
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebXRInterfaceJS::set_required_features(String p_required_features) {
|
|
|
|
required_features = p_required_features;
|
|
|
|
}
|
|
|
|
|
|
|
|
String WebXRInterfaceJS::get_required_features() const {
|
|
|
|
return required_features;
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebXRInterfaceJS::set_optional_features(String p_optional_features) {
|
|
|
|
optional_features = p_optional_features;
|
|
|
|
}
|
|
|
|
|
|
|
|
String WebXRInterfaceJS::get_optional_features() const {
|
|
|
|
return optional_features;
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebXRInterfaceJS::set_requested_reference_space_types(String p_requested_reference_space_types) {
|
|
|
|
requested_reference_space_types = p_requested_reference_space_types;
|
|
|
|
}
|
|
|
|
|
|
|
|
String WebXRInterfaceJS::get_requested_reference_space_types() const {
|
|
|
|
return requested_reference_space_types;
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebXRInterfaceJS::_set_reference_space_type(String p_reference_space_type) {
|
|
|
|
reference_space_type = p_reference_space_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
String WebXRInterfaceJS::get_reference_space_type() const {
|
|
|
|
return reference_space_type;
|
|
|
|
}
|
|
|
|
|
2021-03-29 12:15:53 +02:00
|
|
|
Ref<XRPositionalTracker> WebXRInterfaceJS::get_controller(int p_controller_id) const {
|
2020-09-27 00:15:21 +02:00
|
|
|
XRServer *xr_server = XRServer::get_singleton();
|
2021-08-29 08:05:11 +02:00
|
|
|
ERR_FAIL_NULL_V(xr_server, Ref<XRPositionalTracker>());
|
2020-09-27 00:15:21 +02:00
|
|
|
|
2021-08-29 08:05:11 +02:00
|
|
|
// TODO support more then two controllers
|
|
|
|
if (p_controller_id >= 0 && p_controller_id < 2) {
|
|
|
|
return controllers[p_controller_id];
|
|
|
|
};
|
|
|
|
|
|
|
|
return Ref<XRPositionalTracker>();
|
2020-09-27 00:15:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
String WebXRInterfaceJS::get_visibility_state() const {
|
|
|
|
char *c_str = godot_webxr_get_visibility_state();
|
|
|
|
if (c_str) {
|
|
|
|
String visibility_state = String(c_str);
|
|
|
|
free(c_str);
|
|
|
|
|
|
|
|
return visibility_state;
|
|
|
|
}
|
|
|
|
return String();
|
|
|
|
}
|
|
|
|
|
|
|
|
PackedVector3Array WebXRInterfaceJS::get_bounds_geometry() const {
|
|
|
|
PackedVector3Array ret;
|
|
|
|
|
|
|
|
int *js_bounds = godot_webxr_get_bounds_geometry();
|
|
|
|
if (js_bounds) {
|
|
|
|
ret.resize(js_bounds[0]);
|
|
|
|
for (int i = 0; i < js_bounds[0]; i++) {
|
|
|
|
float *js_vector3 = ((float *)js_bounds) + (i * 3) + 1;
|
|
|
|
ret.set(i, Vector3(js_vector3[0], js_vector3[1], js_vector3[2]));
|
|
|
|
}
|
|
|
|
free(js_bounds);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
StringName WebXRInterfaceJS::get_name() const {
|
|
|
|
return "WebXR";
|
|
|
|
};
|
|
|
|
|
2021-08-23 07:23:16 +02:00
|
|
|
uint32_t WebXRInterfaceJS::get_capabilities() const {
|
2021-01-25 15:35:26 +01:00
|
|
|
return XRInterface::XR_STEREO | XRInterface::XR_MONO;
|
2020-09-27 00:15:21 +02:00
|
|
|
};
|
|
|
|
|
2021-07-07 16:31:06 +02:00
|
|
|
uint32_t WebXRInterfaceJS::get_view_count() {
|
|
|
|
return godot_webxr_get_view_count();
|
2020-09-27 00:15:21 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
bool WebXRInterfaceJS::is_initialized() const {
|
|
|
|
return (initialized);
|
|
|
|
};
|
|
|
|
|
|
|
|
bool WebXRInterfaceJS::initialize() {
|
|
|
|
XRServer *xr_server = XRServer::get_singleton();
|
|
|
|
ERR_FAIL_NULL_V(xr_server, false);
|
|
|
|
|
|
|
|
if (!initialized) {
|
|
|
|
if (!godot_webxr_is_supported()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (requested_reference_space_types.size() == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-08-29 08:05:11 +02:00
|
|
|
// we must create a tracker for our head
|
2022-08-17 00:33:26 +02:00
|
|
|
head_transform.basis = Basis();
|
|
|
|
head_transform.origin = Vector3();
|
2021-08-29 08:05:11 +02:00
|
|
|
head_tracker.instantiate();
|
|
|
|
head_tracker->set_tracker_type(XRServer::TRACKER_HEAD);
|
|
|
|
head_tracker->set_tracker_name("head");
|
|
|
|
head_tracker->set_tracker_desc("Players head");
|
|
|
|
xr_server->add_tracker(head_tracker);
|
|
|
|
|
2020-09-27 00:15:21 +02:00
|
|
|
// make this our primary interface
|
|
|
|
xr_server->set_primary_interface(this);
|
|
|
|
|
2021-01-27 21:16:00 +01:00
|
|
|
// Clear render_targetsize to make sure it gets reset to the new size.
|
|
|
|
// Clearing in uninitialize() doesn't work because a frame can still be
|
|
|
|
// rendered after it's called, which will fill render_targetsize again.
|
|
|
|
render_targetsize.width = 0;
|
|
|
|
render_targetsize.height = 0;
|
|
|
|
|
2020-09-27 00:15:21 +02:00
|
|
|
initialized = true;
|
|
|
|
|
|
|
|
godot_webxr_initialize(
|
|
|
|
session_mode.utf8().get_data(),
|
|
|
|
required_features.utf8().get_data(),
|
|
|
|
optional_features.utf8().get_data(),
|
|
|
|
requested_reference_space_types.utf8().get_data(),
|
|
|
|
&_emwebxr_on_session_started,
|
|
|
|
&_emwebxr_on_session_ended,
|
|
|
|
&_emwebxr_on_session_failed,
|
|
|
|
&_emwebxr_on_controller_changed,
|
|
|
|
&_emwebxr_on_input_event,
|
|
|
|
&_emwebxr_on_simple_event);
|
|
|
|
};
|
|
|
|
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
void WebXRInterfaceJS::uninitialize() {
|
|
|
|
if (initialized) {
|
|
|
|
XRServer *xr_server = XRServer::get_singleton();
|
2021-08-29 08:05:11 +02:00
|
|
|
if (xr_server != nullptr) {
|
|
|
|
if (head_tracker.is_valid()) {
|
|
|
|
xr_server->remove_tracker(head_tracker);
|
|
|
|
|
|
|
|
head_tracker.unref();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (xr_server->get_primary_interface() == this) {
|
|
|
|
// no longer our primary interface
|
|
|
|
xr_server->set_primary_interface(nullptr);
|
|
|
|
}
|
2020-09-27 00:15:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
godot_webxr_uninitialize();
|
|
|
|
|
|
|
|
reference_space_type = "";
|
|
|
|
initialized = false;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2021-05-07 15:19:04 +02:00
|
|
|
Transform3D WebXRInterfaceJS::_js_matrix_to_transform(float *p_js_matrix) {
|
2020-10-17 07:08:21 +02:00
|
|
|
Transform3D transform;
|
2020-09-27 00:15:21 +02:00
|
|
|
|
2022-04-25 00:07:35 +02:00
|
|
|
transform.basis.rows[0].x = p_js_matrix[0];
|
|
|
|
transform.basis.rows[1].x = p_js_matrix[1];
|
|
|
|
transform.basis.rows[2].x = p_js_matrix[2];
|
|
|
|
transform.basis.rows[0].y = p_js_matrix[4];
|
|
|
|
transform.basis.rows[1].y = p_js_matrix[5];
|
|
|
|
transform.basis.rows[2].y = p_js_matrix[6];
|
|
|
|
transform.basis.rows[0].z = p_js_matrix[8];
|
|
|
|
transform.basis.rows[1].z = p_js_matrix[9];
|
|
|
|
transform.basis.rows[2].z = p_js_matrix[10];
|
2020-09-27 00:15:21 +02:00
|
|
|
transform.origin.x = p_js_matrix[12];
|
|
|
|
transform.origin.y = p_js_matrix[13];
|
|
|
|
transform.origin.z = p_js_matrix[14];
|
|
|
|
|
|
|
|
return transform;
|
|
|
|
}
|
|
|
|
|
2021-08-23 07:23:16 +02:00
|
|
|
Size2 WebXRInterfaceJS::get_render_target_size() {
|
2021-01-27 21:16:00 +01:00
|
|
|
if (render_targetsize.width != 0 && render_targetsize.height != 0) {
|
|
|
|
return render_targetsize;
|
|
|
|
}
|
2020-09-27 00:15:21 +02:00
|
|
|
|
2021-08-23 07:23:16 +02:00
|
|
|
int *js_size = godot_webxr_get_render_target_size();
|
2020-09-27 00:15:21 +02:00
|
|
|
if (!initialized || js_size == nullptr) {
|
2021-01-27 21:16:00 +01:00
|
|
|
// As a temporary default (until WebXR is fully initialized), use half the window size.
|
|
|
|
Size2 temp = DisplayServer::get_singleton()->window_get_size();
|
|
|
|
temp.width /= 2.0;
|
|
|
|
return temp;
|
2020-09-27 00:15:21 +02:00
|
|
|
}
|
|
|
|
|
2021-01-27 21:16:00 +01:00
|
|
|
render_targetsize.width = js_size[0];
|
|
|
|
render_targetsize.height = js_size[1];
|
2020-09-27 00:15:21 +02:00
|
|
|
|
|
|
|
free(js_size);
|
|
|
|
|
2021-01-27 21:16:00 +01:00
|
|
|
return render_targetsize;
|
2020-09-27 00:15:21 +02:00
|
|
|
};
|
|
|
|
|
2021-05-07 15:19:04 +02:00
|
|
|
Transform3D WebXRInterfaceJS::get_camera_transform() {
|
2020-10-17 07:08:21 +02:00
|
|
|
Transform3D transform_for_eye;
|
2020-09-27 00:15:21 +02:00
|
|
|
|
|
|
|
XRServer *xr_server = XRServer::get_singleton();
|
|
|
|
ERR_FAIL_NULL_V(xr_server, transform_for_eye);
|
|
|
|
|
2022-08-17 00:33:26 +02:00
|
|
|
if (initialized) {
|
|
|
|
float world_scale = xr_server->get_world_scale();
|
2021-05-07 15:19:04 +02:00
|
|
|
|
2022-08-17 00:33:26 +02:00
|
|
|
// just scale our origin point of our transform
|
|
|
|
Transform3D _head_transform = head_transform;
|
|
|
|
_head_transform.origin *= world_scale;
|
|
|
|
|
|
|
|
transform_for_eye = (xr_server->get_reference_frame()) * _head_transform;
|
|
|
|
}
|
2021-05-07 15:19:04 +02:00
|
|
|
|
2022-08-17 00:33:26 +02:00
|
|
|
return transform_for_eye;
|
2021-05-07 15:19:04 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
Transform3D WebXRInterfaceJS::get_transform_for_view(uint32_t p_view, const Transform3D &p_cam_transform) {
|
|
|
|
Transform3D transform_for_eye;
|
|
|
|
|
|
|
|
XRServer *xr_server = XRServer::get_singleton();
|
|
|
|
ERR_FAIL_NULL_V(xr_server, transform_for_eye);
|
|
|
|
|
|
|
|
float *js_matrix = godot_webxr_get_transform_for_eye(p_view + 1);
|
2020-09-27 00:15:21 +02:00
|
|
|
if (!initialized || js_matrix == nullptr) {
|
|
|
|
transform_for_eye = p_cam_transform;
|
|
|
|
return transform_for_eye;
|
|
|
|
}
|
|
|
|
|
|
|
|
transform_for_eye = _js_matrix_to_transform(js_matrix);
|
|
|
|
free(js_matrix);
|
|
|
|
|
2022-08-17 00:33:26 +02:00
|
|
|
float world_scale = xr_server->get_world_scale();
|
|
|
|
// Scale only the center point of our eye transform, so we don't scale the
|
|
|
|
// distance between the eyes.
|
|
|
|
Transform3D _head_transform = head_transform;
|
|
|
|
transform_for_eye.origin -= _head_transform.origin;
|
|
|
|
_head_transform.origin *= world_scale;
|
|
|
|
transform_for_eye.origin += _head_transform.origin;
|
|
|
|
|
2020-09-27 00:15:21 +02:00
|
|
|
return p_cam_transform * xr_server->get_reference_frame() * transform_for_eye;
|
|
|
|
};
|
|
|
|
|
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 WebXRInterfaceJS::get_projection_for_view(uint32_t p_view, double p_aspect, double p_z_near, double p_z_far) {
|
|
|
|
Projection eye;
|
2020-09-27 00:15:21 +02:00
|
|
|
|
2021-05-07 15:19:04 +02:00
|
|
|
float *js_matrix = godot_webxr_get_projection_for_eye(p_view + 1);
|
2020-09-27 00:15:21 +02:00
|
|
|
if (!initialized || js_matrix == nullptr) {
|
|
|
|
return eye;
|
|
|
|
}
|
|
|
|
|
|
|
|
int k = 0;
|
|
|
|
for (int i = 0; i < 4; i++) {
|
|
|
|
for (int j = 0; j < 4; j++) {
|
|
|
|
eye.matrix[i][j] = js_matrix[k++];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
free(js_matrix);
|
|
|
|
|
|
|
|
// Copied from godot_oculus_mobile's ovr_mobile_session.cpp
|
|
|
|
eye.matrix[2][2] = -(p_z_far + p_z_near) / (p_z_far - p_z_near);
|
|
|
|
eye.matrix[3][2] = -(2.0f * p_z_far * p_z_near) / (p_z_far - p_z_near);
|
|
|
|
|
|
|
|
return eye;
|
|
|
|
}
|
|
|
|
|
2022-01-26 02:25:20 +01:00
|
|
|
Vector<BlitToScreen> WebXRInterfaceJS::post_draw_viewport(RID p_render_target, const Rect2 &p_screen_rect) {
|
2021-07-17 21:55:19 +02:00
|
|
|
Vector<BlitToScreen> blit_to_screen;
|
|
|
|
|
|
|
|
if (!initialized) {
|
|
|
|
return blit_to_screen;
|
|
|
|
}
|
|
|
|
|
2022-08-17 00:33:26 +02:00
|
|
|
GLES3::TextureStorage *texture_storage = dynamic_cast<GLES3::TextureStorage *>(RSG::texture_storage);
|
|
|
|
if (!texture_storage) {
|
|
|
|
return blit_to_screen;
|
2021-07-17 21:55:19 +02:00
|
|
|
}
|
|
|
|
|
2022-08-17 00:33:26 +02:00
|
|
|
GLES3::RenderTarget *rt = texture_storage->get_render_target(p_render_target);
|
|
|
|
|
|
|
|
// @todo Support multiple eyes!
|
|
|
|
godot_webxr_commit_for_eye(1, rt->fbo);
|
|
|
|
|
2021-07-17 21:55:19 +02:00
|
|
|
return blit_to_screen;
|
2020-09-27 00:15:21 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
void WebXRInterfaceJS::process() {
|
|
|
|
if (initialized) {
|
2022-08-17 00:33:26 +02:00
|
|
|
// Get the "head" position.
|
|
|
|
float *js_matrix = godot_webxr_get_transform_for_eye(0);
|
|
|
|
if (js_matrix != nullptr) {
|
|
|
|
head_transform = _js_matrix_to_transform(js_matrix);
|
|
|
|
free(js_matrix);
|
|
|
|
}
|
2021-08-29 08:05:11 +02:00
|
|
|
if (head_tracker.is_valid()) {
|
2022-08-17 00:33:26 +02:00
|
|
|
head_tracker->set_pose("default", head_transform, Vector3(), Vector3());
|
2021-08-29 08:05:11 +02:00
|
|
|
}
|
|
|
|
|
2022-08-17 00:33:26 +02:00
|
|
|
godot_webxr_sample_controller_data();
|
2020-09-27 00:15:21 +02:00
|
|
|
int controller_count = godot_webxr_get_controller_count();
|
|
|
|
for (int i = 0; i < controller_count; i++) {
|
|
|
|
_update_tracker(i);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
void WebXRInterfaceJS::_update_tracker(int p_controller_id) {
|
|
|
|
XRServer *xr_server = XRServer::get_singleton();
|
|
|
|
ERR_FAIL_NULL(xr_server);
|
|
|
|
|
2021-08-29 08:05:11 +02:00
|
|
|
// need to support more then two controllers...
|
|
|
|
if (p_controller_id < 0 || p_controller_id > 1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ref<XRPositionalTracker> tracker = controllers[p_controller_id];
|
2020-09-27 00:15:21 +02:00
|
|
|
if (godot_webxr_is_controller_connected(p_controller_id)) {
|
2021-03-29 12:15:53 +02:00
|
|
|
if (tracker.is_null()) {
|
2021-06-18 00:03:09 +02:00
|
|
|
tracker.instantiate();
|
2021-01-10 11:03:50 +01:00
|
|
|
tracker->set_tracker_type(XRServer::TRACKER_CONTROLLER);
|
2020-09-27 00:15:21 +02:00
|
|
|
// Controller id's 0 and 1 are always the left and right hands.
|
|
|
|
if (p_controller_id < 2) {
|
2021-08-29 08:05:11 +02:00
|
|
|
tracker->set_tracker_name(p_controller_id == 0 ? "left_hand" : "right_hand");
|
|
|
|
tracker->set_tracker_desc(p_controller_id == 0 ? "Left hand controller" : "Right hand controller");
|
2021-01-10 11:03:50 +01:00
|
|
|
tracker->set_tracker_hand(p_controller_id == 0 ? XRPositionalTracker::TRACKER_HAND_LEFT : XRPositionalTracker::TRACKER_HAND_RIGHT);
|
2021-08-29 08:05:11 +02:00
|
|
|
} else {
|
|
|
|
char name[1024];
|
|
|
|
sprintf(name, "tracker_%i", p_controller_id);
|
|
|
|
tracker->set_tracker_name(name);
|
|
|
|
tracker->set_tracker_desc(name);
|
2020-09-27 00:15:21 +02:00
|
|
|
}
|
|
|
|
xr_server->add_tracker(tracker);
|
|
|
|
}
|
|
|
|
|
|
|
|
float *tracker_matrix = godot_webxr_get_controller_transform(p_controller_id);
|
|
|
|
if (tracker_matrix) {
|
2021-08-29 08:05:11 +02:00
|
|
|
// Note, poses should NOT have world scale and our reference frame applied!
|
2020-10-17 07:08:21 +02:00
|
|
|
Transform3D transform = _js_matrix_to_transform(tracker_matrix);
|
2021-08-29 08:05:11 +02:00
|
|
|
tracker->set_pose("default", transform, Vector3(), Vector3());
|
2020-09-27 00:15:21 +02:00
|
|
|
free(tracker_matrix);
|
|
|
|
}
|
|
|
|
|
2021-08-29 08:05:11 +02:00
|
|
|
// TODO implement additional poses such as "aim" and "grip"
|
|
|
|
|
2020-09-27 00:15:21 +02:00
|
|
|
int *buttons = godot_webxr_get_controller_buttons(p_controller_id);
|
|
|
|
if (buttons) {
|
2021-08-29 08:05:11 +02:00
|
|
|
// TODO buttons should be named properly, this is just a temporary fix
|
2020-09-27 00:15:21 +02:00
|
|
|
for (int i = 0; i < buttons[0]; i++) {
|
2021-08-29 08:05:11 +02:00
|
|
|
char name[1024];
|
|
|
|
sprintf(name, "button_%i", i);
|
|
|
|
|
|
|
|
float value = *((float *)buttons + (i + 1));
|
|
|
|
bool state = value > 0.0;
|
|
|
|
tracker->set_input(name, state);
|
2020-09-27 00:15:21 +02:00
|
|
|
}
|
|
|
|
free(buttons);
|
|
|
|
}
|
|
|
|
|
|
|
|
int *axes = godot_webxr_get_controller_axes(p_controller_id);
|
|
|
|
if (axes) {
|
2021-08-29 08:05:11 +02:00
|
|
|
// TODO again just a temporary fix, split these between proper float and vector2 inputs
|
2020-09-27 00:15:21 +02:00
|
|
|
for (int i = 0; i < axes[0]; i++) {
|
2021-08-29 08:05:11 +02:00
|
|
|
char name[1024];
|
|
|
|
sprintf(name, "axis_%i", i);
|
|
|
|
|
|
|
|
float value = *((float *)axes + (i + 1));
|
|
|
|
tracker->set_input(name, value);
|
2020-09-27 00:15:21 +02:00
|
|
|
}
|
|
|
|
free(axes);
|
|
|
|
}
|
2021-03-29 12:15:53 +02:00
|
|
|
} else if (tracker.is_valid()) {
|
2020-09-27 00:15:21 +02:00
|
|
|
xr_server->remove_tracker(tracker);
|
2021-08-29 08:05:11 +02:00
|
|
|
controllers[p_controller_id].unref();
|
2020-09-27 00:15:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebXRInterfaceJS::_on_controller_changed() {
|
|
|
|
// Register "virtual" gamepads with Godot for the ones we get from WebXR.
|
|
|
|
godot_webxr_sample_controller_data();
|
|
|
|
for (int i = 0; i < 2; i++) {
|
|
|
|
bool controller_connected = godot_webxr_is_controller_connected(i);
|
|
|
|
if (controllers_state[i] != controller_connected) {
|
2021-08-29 08:05:11 +02:00
|
|
|
// Input::get_singleton()->joy_connection_changed(i + 100, controller_connected, i == 0 ? "Left" : "Right", "");
|
2020-09-27 00:15:21 +02:00
|
|
|
controllers_state[i] = controller_connected;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
WebXRInterfaceJS::WebXRInterfaceJS() {
|
|
|
|
initialized = false;
|
|
|
|
session_mode = "inline";
|
|
|
|
requested_reference_space_types = "local";
|
|
|
|
};
|
|
|
|
|
|
|
|
WebXRInterfaceJS::~WebXRInterfaceJS() {
|
|
|
|
// and make sure we cleanup if we haven't already
|
|
|
|
if (initialized) {
|
|
|
|
uninitialize();
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2022-08-28 20:27:45 +02:00
|
|
|
#endif // WEB_ENABLED
|