diff options
Diffstat (limited to 'modules/webxr/native/library_godot_webxr.js')
-rw-r--r-- | modules/webxr/native/library_godot_webxr.js | 623 |
1 files changed, 277 insertions, 346 deletions
diff --git a/modules/webxr/native/library_godot_webxr.js b/modules/webxr/native/library_godot_webxr.js index c4b21defce..1c00ebebb4 100644 --- a/modules/webxr/native/library_godot_webxr.js +++ b/modules/webxr/native/library_godot_webxr.js @@ -1,44 +1,47 @@ -/*************************************************************************/ -/* library_godot_webxr.js */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 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 */ -/* "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. */ -/*************************************************************************/ +/**************************************************************************/ +/* library_godot_webxr.js */ +/**************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/**************************************************************************/ +/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ +/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ +/* */ +/* 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. */ +/**************************************************************************/ + const GodotWebXR = { - $GodotWebXR__deps: ['$Browser', '$GL', '$GodotRuntime'], + $GodotWebXR__deps: ['$Browser', '$GL', '$GodotRuntime', '$runtimeKeepalivePush', '$runtimeKeepalivePop'], $GodotWebXR: { gl: null, - texture_ids: [null, null], - textures: [null, null], - session: null, + gl_binding: null, + layer: null, space: null, frame: null, pose: null, + view_count: 1, + input_sources: new Array(16), + touches: new Array(5), // Monkey-patch the requestAnimationFrame() used by Emscripten for the main // loop, so that we can swap it out for XRSession.requestAnimationFrame() @@ -72,143 +75,135 @@ const GodotWebXR = { // gets picked up automatically, however, in the Oculus Browser // on the Quest, we need to pause and resume the main loop. Browser.mainLoop.pause(); + runtimeKeepalivePush(); // eslint-disable-line no-undef window.setTimeout(function () { + runtimeKeepalivePop(); // eslint-disable-line no-undef Browser.mainLoop.resume(); }, 0); }, - // Some custom WebGL code for blitting our eye textures to the - // framebuffer we get from WebXR. - shaderProgram: null, - programInfo: null, - buffer: null, - // Vertex shader source. - vsSource: ` - const vec2 scale = vec2(0.5, 0.5); - attribute vec4 aVertexPosition; - - varying highp vec2 vTextureCoord; - - void main () { - gl_Position = aVertexPosition; - vTextureCoord = aVertexPosition.xy * scale + scale; - } - `, - // Fragment shader source. - fsSource: ` - varying highp vec2 vTextureCoord; + getLayer: () => { + const new_view_count = (GodotWebXR.pose) ? GodotWebXR.pose.views.length : 1; + let layer = GodotWebXR.layer; - uniform sampler2D uSampler; + // If the view count hasn't changed since creating this layer, then + // we can simply return it. + if (layer && GodotWebXR.view_count === new_view_count) { + return layer; + } - void main() { - gl_FragColor = texture2D(uSampler, vTextureCoord); + if (!GodotWebXR.session || !GodotWebXR.gl_binding) { + return null; } - `, - initShaderProgram: (gl, vsSource, fsSource) => { - const vertexShader = GodotWebXR.loadShader(gl, gl.VERTEX_SHADER, vsSource); - const fragmentShader = GodotWebXR.loadShader(gl, gl.FRAGMENT_SHADER, fsSource); + const gl = GodotWebXR.gl; - const shaderProgram = gl.createProgram(); - gl.attachShader(shaderProgram, vertexShader); - gl.attachShader(shaderProgram, fragmentShader); - gl.linkProgram(shaderProgram); + layer = GodotWebXR.gl_binding.createProjectionLayer({ + textureType: new_view_count > 1 ? 'texture-array' : 'texture', + colorFormat: gl.RGBA8, + depthFormat: gl.DEPTH_COMPONENT24, + }); + GodotWebXR.session.updateRenderState({ layers: [layer] }); - if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)) { - GodotRuntime.error(`Unable to initialize the shader program: ${gl.getProgramInfoLog(shaderProgram)}`); + GodotWebXR.layer = layer; + GodotWebXR.view_count = new_view_count; + return layer; + }, + + getSubImage: () => { + if (!GodotWebXR.pose) { return null; } - - return shaderProgram; - }, - loadShader: (gl, type, source) => { - const shader = gl.createShader(type); - gl.shaderSource(shader, source); - gl.compileShader(shader); - - if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) { - GodotRuntime.error(`An error occurred compiling the shader: ${gl.getShaderInfoLog(shader)}`); - gl.deleteShader(shader); + const layer = GodotWebXR.getLayer(); + if (layer === null) { return null; } - return shader; + // Because we always use "texture-array" for multiview and "texture" + // when there is only 1 view, it should be safe to only grab the + // subimage for the first view. + return GodotWebXR.gl_binding.getViewSubImage(layer, GodotWebXR.pose.views[0]); }, - initBuffer: (gl) => { - const positionBuffer = gl.createBuffer(); - gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer); - const positions = [ - -1.0, -1.0, - 1.0, -1.0, - -1.0, 1.0, - 1.0, 1.0, - ]; - gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(positions), gl.STATIC_DRAW); - return positionBuffer; - }, - blitTexture: (gl, texture) => { - if (GodotWebXR.shaderProgram === null) { - GodotWebXR.shaderProgram = GodotWebXR.initShaderProgram(gl, GodotWebXR.vsSource, GodotWebXR.fsSource); - GodotWebXR.programInfo = { - program: GodotWebXR.shaderProgram, - attribLocations: { - vertexPosition: gl.getAttribLocation(GodotWebXR.shaderProgram, 'aVertexPosition'), - }, - uniformLocations: { - uSampler: gl.getUniformLocation(GodotWebXR.shaderProgram, 'uSampler'), - }, - }; - GodotWebXR.buffer = GodotWebXR.initBuffer(gl); - } - const orig_program = gl.getParameter(gl.CURRENT_PROGRAM); - gl.useProgram(GodotWebXR.shaderProgram); - - gl.bindBuffer(gl.ARRAY_BUFFER, GodotWebXR.buffer); - gl.vertexAttribPointer(GodotWebXR.programInfo.attribLocations.vertexPosition, 2, gl.FLOAT, false, 0, 0); - gl.enableVertexAttribArray(GodotWebXR.programInfo.attribLocations.vertexPosition); - - gl.activeTexture(gl.TEXTURE0); - gl.bindTexture(gl.TEXTURE_2D, texture); - gl.uniform1i(GodotWebXR.programInfo.uniformLocations.uSampler, 0); + getTextureId: (texture) => { + if (texture.name !== undefined) { + return texture.name; + } - gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4); + const id = GL.getNewId(GL.textures); + texture.name = id; + GL.textures[id] = texture; - // Restore state. - gl.bindTexture(gl.TEXTURE_2D, null); - gl.disableVertexAttribArray(GodotWebXR.programInfo.attribLocations.vertexPosition); - gl.bindBuffer(gl.ARRAY_BUFFER, null); - gl.useProgram(orig_program); + return id; }, - // Holds the controllers list between function calls. - controllers: [], - - // Updates controllers array, where the left hand (or sole tracker) is - // the first element, and the right hand is the second element, and any - // others placed at the 3rd position and up. - sampleControllers: () => { - if (!GodotWebXR.session || !GodotWebXR.frame) { - return; + addInputSource: (input_source) => { + let name = -1; + if (input_source.targetRayMode === 'tracked-pointer' && input_source.handedness === 'left') { + name = 0; + } else if (input_source.targetRayMode === 'tracked-pointer' && input_source.handedness === 'right') { + name = 1; + } else { + for (let i = 2; i < 16; i++) { + if (!GodotWebXR.input_sources[i]) { + name = i; + break; + } + } + } + if (name >= 0) { + GodotWebXR.input_sources[name] = input_source; + input_source.name = name; + + // Find a free touch index for screen sources. + if (input_source.targetRayMode === 'screen') { + let touch_index = -1; + for (let i = 0; i < 5; i++) { + if (!GodotWebXR.touches[i]) { + touch_index = i; + break; + } + } + if (touch_index >= 0) { + GodotWebXR.touches[touch_index] = input_source; + input_source.touch_index = touch_index; + } + } } + return name; + }, + + removeInputSource: (input_source) => { + if (input_source.name !== undefined) { + const name = input_source.name; + if (name >= 0 && name < 16) { + GodotWebXR.input_sources[name] = null; + } - let other_index = 2; - const controllers = []; - GodotWebXR.session.inputSources.forEach((input_source) => { - if (input_source.targetRayMode === 'tracked-pointer') { - if (input_source.handedness === 'right') { - controllers[1] = input_source; - } else if (input_source.handedness === 'left' || !controllers[0]) { - controllers[0] = input_source; + if (input_source.touch_index !== undefined) { + const touch_index = input_source.touch_index; + if (touch_index >= 0 && touch_index < 5) { + GodotWebXR.touches[touch_index] = null; } - } else { - controllers[other_index++] = input_source; } - }); - GodotWebXR.controllers = controllers; + return name; + } + return -1; }, - getControllerId: (input_source) => GodotWebXR.controllers.indexOf(input_source), + getInputSourceId: (input_source) => { + if (input_source !== undefined) { + return input_source.name; + } + return -1; + }, + + getTouchIndex: (input_source) => { + if (input_source.touch_index !== undefined) { + return input_source.touch_index; + } + return -1; + }, }, godot_webxr_is_supported__proxy: 'sync', @@ -237,8 +232,8 @@ const GodotWebXR = { godot_webxr_initialize__deps: ['emscripten_webgl_get_current_context'], godot_webxr_initialize__proxy: 'sync', - godot_webxr_initialize__sig: 'viiiiiiiiii', - godot_webxr_initialize: function (p_session_mode, p_required_features, p_optional_features, p_requested_reference_spaces, p_on_session_started, p_on_session_ended, p_on_session_failed, p_on_controller_changed, p_on_input_event, p_on_simple_event) { + godot_webxr_initialize__sig: 'viiiiiiiii', + godot_webxr_initialize: function (p_session_mode, p_required_features, p_optional_features, p_requested_reference_spaces, p_on_session_started, p_on_session_ended, p_on_session_failed, p_on_input_event, p_on_simple_event) { GodotWebXR.monkeyPatchRequestAnimationFrame(true); const session_mode = GodotRuntime.parseString(p_session_mode); @@ -248,7 +243,6 @@ const GodotWebXR = { const onstarted = GodotRuntime.get_func(p_on_session_started); const onended = GodotRuntime.get_func(p_on_session_ended); const onfailed = GodotRuntime.get_func(p_on_session_failed); - const oncontroller = GodotRuntime.get_func(p_on_controller_changed); const oninputevent = GodotRuntime.get_func(p_on_input_event); const onsimpleevent = GodotRuntime.get_func(p_on_simple_event); @@ -268,24 +262,18 @@ const GodotWebXR = { }); session.addEventListener('inputsourceschange', function (evt) { - let controller_changed = false; - [evt.added, evt.removed].forEach((lst) => { - lst.forEach((input_source) => { - if (input_source.targetRayMode === 'tracked-pointer') { - controller_changed = true; - } - }); - }); - if (controller_changed) { - oncontroller(); - } + evt.added.forEach(GodotWebXR.addInputSource); + evt.removed.forEach(GodotWebXR.removeInputSource); }); - ['selectstart', 'select', 'selectend', 'squeezestart', 'squeeze', 'squeezeend'].forEach((input_event) => { + ['selectstart', 'selectend', 'squeezestart', 'squeezeend'].forEach((input_event, index) => { session.addEventListener(input_event, function (evt) { - const c_str = GodotRuntime.allocString(input_event); - oninputevent(c_str, GodotWebXR.getControllerId(evt.inputSource)); - GodotRuntime.free(c_str); + // Since this happens in-between normal frames, we need to + // grab the frame from the event in order to get poses for + // the input sources. + GodotWebXR.frame = evt.frame; + oninputevent(index, GodotWebXR.getInputSourceId(evt.inputSource)); + GodotWebXR.frame = null; }); }); @@ -300,9 +288,10 @@ const GodotWebXR = { GodotWebXR.gl = gl; gl.makeXRCompatible().then(function () { - session.updateRenderState({ - baseLayer: new XRWebGLLayer(session, gl), - }); + GodotWebXR.gl_binding = new XRWebGLBinding(session, gl); // eslint-disable-line no-undef + + // This will trigger the layer to get created. + GodotWebXR.getLayer(); function onReferenceSpaceSuccess(reference_space, reference_space_type) { GodotWebXR.space = reference_space; @@ -370,26 +359,15 @@ const GodotWebXR = { .catch((e) => { }); } - // Clean-up the textures we allocated for each view. - const gl = GodotWebXR.gl; - for (let i = 0; i < GodotWebXR.textures.length; i++) { - const texture = GodotWebXR.textures[i]; - if (texture !== null) { - gl.deleteTexture(texture); - } - GodotWebXR.textures[i] = null; - - const texture_id = GodotWebXR.texture_ids[i]; - if (texture_id !== null) { - GL.textures[texture_id] = null; - } - GodotWebXR.texture_ids[i] = null; - } - GodotWebXR.session = null; + GodotWebXR.gl_binding = null; + GodotWebXR.layer = null; GodotWebXR.space = null; GodotWebXR.frame = null; GodotWebXR.pose = null; + GodotWebXR.view_count = 1; + GodotWebXR.input_sources = new Array(16); + GodotWebXR.touches = new Array(5); // Disable the monkey-patched window.requestAnimationFrame() and // pause/restart the main loop to activate it on all platforms. @@ -401,234 +379,186 @@ const GodotWebXR = { godot_webxr_get_view_count__sig: 'i', godot_webxr_get_view_count: function () { if (!GodotWebXR.session || !GodotWebXR.pose) { - return 0; + return 1; } - return GodotWebXR.pose.views.length; + const view_count = GodotWebXR.pose.views.length; + return view_count > 0 ? view_count : 1; }, godot_webxr_get_render_target_size__proxy: 'sync', - godot_webxr_get_render_target_size__sig: 'i', - godot_webxr_get_render_target_size: function () { - if (!GodotWebXR.session || !GodotWebXR.pose) { - return 0; + godot_webxr_get_render_target_size__sig: 'ii', + godot_webxr_get_render_target_size: function (r_size) { + const subimage = GodotWebXR.getSubImage(); + if (subimage === null) { + return false; } - const glLayer = GodotWebXR.session.renderState.baseLayer; - const view = GodotWebXR.pose.views[0]; - const viewport = glLayer.getViewport(view); + GodotRuntime.setHeapValue(r_size + 0, subimage.viewport.width, 'i32'); + GodotRuntime.setHeapValue(r_size + 4, subimage.viewport.height, 'i32'); - const buf = GodotRuntime.malloc(2 * 4); - GodotRuntime.setHeapValue(buf + 0, viewport.width, 'i32'); - GodotRuntime.setHeapValue(buf + 4, viewport.height, 'i32'); - return buf; + return true; }, - godot_webxr_get_transform_for_eye__proxy: 'sync', - godot_webxr_get_transform_for_eye__sig: 'ii', - godot_webxr_get_transform_for_eye: function (p_eye) { + godot_webxr_get_transform_for_view__proxy: 'sync', + godot_webxr_get_transform_for_view__sig: 'iii', + godot_webxr_get_transform_for_view: function (p_view, r_transform) { if (!GodotWebXR.session || !GodotWebXR.pose) { - return 0; + return false; } const views = GodotWebXR.pose.views; let matrix; - if (p_eye === 0) { - matrix = GodotWebXR.pose.transform.matrix; + if (p_view >= 0) { + matrix = views[p_view].transform.matrix; } else { - matrix = views[p_eye - 1].transform.matrix; + // For -1 (or any other negative value) return the HMD transform. + matrix = GodotWebXR.pose.transform.matrix; } - const buf = GodotRuntime.malloc(16 * 4); + for (let i = 0; i < 16; i++) { - GodotRuntime.setHeapValue(buf + (i * 4), matrix[i], 'float'); + GodotRuntime.setHeapValue(r_transform + (i * 4), matrix[i], 'float'); } - return buf; + + return true; }, - godot_webxr_get_projection_for_eye__proxy: 'sync', - godot_webxr_get_projection_for_eye__sig: 'ii', - godot_webxr_get_projection_for_eye: function (p_eye) { + godot_webxr_get_projection_for_view__proxy: 'sync', + godot_webxr_get_projection_for_view__sig: 'iii', + godot_webxr_get_projection_for_view: function (p_view, r_transform) { if (!GodotWebXR.session || !GodotWebXR.pose) { - return 0; + return false; } - const view_index = (p_eye === 2 /* ARVRInterface::EYE_RIGHT */) ? 1 : 0; - const matrix = GodotWebXR.pose.views[view_index].projectionMatrix; - const buf = GodotRuntime.malloc(16 * 4); + const matrix = GodotWebXR.pose.views[p_view].projectionMatrix; for (let i = 0; i < 16; i++) { - GodotRuntime.setHeapValue(buf + (i * 4), matrix[i], 'float'); + GodotRuntime.setHeapValue(r_transform + (i * 4), matrix[i], 'float'); } - return buf; + + return true; }, - godot_webxr_get_external_texture_for_eye__proxy: 'sync', - godot_webxr_get_external_texture_for_eye__sig: 'ii', - godot_webxr_get_external_texture_for_eye: function (p_eye) { - if (!GodotWebXR.session) { + godot_webxr_get_color_texture__proxy: 'sync', + godot_webxr_get_color_texture__sig: 'i', + godot_webxr_get_color_texture: function () { + const subimage = GodotWebXR.getSubImage(); + if (subimage === null) { return 0; } + return GodotWebXR.getTextureId(subimage.colorTexture); + }, - const view_index = (p_eye === 2 /* ARVRInterface::EYE_RIGHT */) ? 1 : 0; - if (GodotWebXR.texture_ids[view_index]) { - return GodotWebXR.texture_ids[view_index]; - } - - // Check pose separately and after returning the cached texture id, - // because we won't get a pose in some cases if we lose tracking, and - // we don't want to return 0 just because tracking was lost. - if (!GodotWebXR.pose) { + godot_webxr_get_depth_texture__proxy: 'sync', + godot_webxr_get_depth_texture__sig: 'i', + godot_webxr_get_depth_texture: function () { + const subimage = GodotWebXR.getSubImage(); + if (subimage === null) { return 0; } - - const glLayer = GodotWebXR.session.renderState.baseLayer; - const view = GodotWebXR.pose.views[view_index]; - const viewport = glLayer.getViewport(view); - const gl = GodotWebXR.gl; - - const texture = gl.createTexture(); - gl.bindTexture(gl.TEXTURE_2D, texture); - gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, viewport.width, viewport.height, 0, gl.RGBA, gl.UNSIGNED_BYTE, null); - - gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE); - gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE); - gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST); - gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST); - gl.bindTexture(gl.TEXTURE_2D, null); - - const texture_id = GL.getNewId(GL.textures); - GL.textures[texture_id] = texture; - GodotWebXR.textures[view_index] = texture; - GodotWebXR.texture_ids[view_index] = texture_id; - return texture_id; - }, - - godot_webxr_commit_for_eye__proxy: 'sync', - godot_webxr_commit_for_eye__sig: 'vi', - godot_webxr_commit_for_eye: function (p_eye) { - if (!GodotWebXR.session || !GodotWebXR.pose) { - return; + if (!subimage.depthStencilTexture) { + return 0; } - - const view_index = (p_eye === 2 /* ARVRInterface::EYE_RIGHT */) ? 1 : 0; - const glLayer = GodotWebXR.session.renderState.baseLayer; - const view = GodotWebXR.pose.views[view_index]; - const viewport = glLayer.getViewport(view); - const gl = GodotWebXR.gl; - - const orig_framebuffer = gl.getParameter(gl.FRAMEBUFFER_BINDING); - const orig_viewport = gl.getParameter(gl.VIEWPORT); - - // Bind to WebXR's framebuffer. - gl.bindFramebuffer(gl.FRAMEBUFFER, glLayer.framebuffer); - gl.viewport(viewport.x, viewport.y, viewport.width, viewport.height); - - GodotWebXR.blitTexture(gl, GodotWebXR.textures[view_index]); - - // Restore state. - gl.bindFramebuffer(gl.FRAMEBUFFER, orig_framebuffer); - gl.viewport(orig_viewport[0], orig_viewport[1], orig_viewport[2], orig_viewport[3]); + return GodotWebXR.getTextureId(subimage.depthStencilTexture); }, - godot_webxr_sample_controller_data__proxy: 'sync', - godot_webxr_sample_controller_data__sig: 'v', - godot_webxr_sample_controller_data: function () { - GodotWebXR.sampleControllers(); - }, - - godot_webxr_get_controller_count__proxy: 'sync', - godot_webxr_get_controller_count__sig: 'i', - godot_webxr_get_controller_count: function () { - if (!GodotWebXR.session || !GodotWebXR.frame) { + godot_webxr_get_velocity_texture__proxy: 'sync', + godot_webxr_get_velocity_texture__sig: 'i', + godot_webxr_get_velocity_texture: function () { + const subimage = GodotWebXR.getSubImage(); + if (subimage === null) { return 0; } - return GodotWebXR.controllers.length; - }, - - godot_webxr_is_controller_connected__proxy: 'sync', - godot_webxr_is_controller_connected__sig: 'ii', - godot_webxr_is_controller_connected: function (p_controller) { - if (!GodotWebXR.session || !GodotWebXR.frame) { - return false; + if (!subimage.motionVectorTexture) { + return 0; } - return !!GodotWebXR.controllers[p_controller]; + return GodotWebXR.getTextureId(subimage.motionVectorTexture); }, - godot_webxr_get_controller_transform__proxy: 'sync', - godot_webxr_get_controller_transform__sig: 'ii', - godot_webxr_get_controller_transform: function (p_controller) { + godot_webxr_update_input_source__proxy: 'sync', + godot_webxr_update_input_source__sig: 'iiiiiiiiiiii', + godot_webxr_update_input_source: function (p_input_source_id, r_target_pose, r_target_ray_mode, r_touch_index, r_has_grip_pose, r_grip_pose, r_has_standard_mapping, r_button_count, r_buttons, r_axes_count, r_axes) { if (!GodotWebXR.session || !GodotWebXR.frame) { return 0; } - const controller = GodotWebXR.controllers[p_controller]; - if (!controller) { - return 0; + if (p_input_source_id < 0 || p_input_source_id >= GodotWebXR.input_sources.length || !GodotWebXR.input_sources[p_input_source_id]) { + return false; } + const input_source = GodotWebXR.input_sources[p_input_source_id]; const frame = GodotWebXR.frame; const space = GodotWebXR.space; - const pose = frame.getPose(controller.targetRaySpace, space); - if (!pose) { + // Target pose. + const target_pose = frame.getPose(input_source.targetRaySpace, space); + if (!target_pose) { // This can mean that the controller lost tracking. - return 0; + return false; } - const matrix = pose.transform.matrix; - - const buf = GodotRuntime.malloc(16 * 4); + const target_pose_matrix = target_pose.transform.matrix; for (let i = 0; i < 16; i++) { - GodotRuntime.setHeapValue(buf + (i * 4), matrix[i], 'float'); + GodotRuntime.setHeapValue(r_target_pose + (i * 4), target_pose_matrix[i], 'float'); } - return buf; - }, - godot_webxr_get_controller_buttons__proxy: 'sync', - godot_webxr_get_controller_buttons__sig: 'ii', - godot_webxr_get_controller_buttons: function (p_controller) { - if (GodotWebXR.controllers.length === 0) { - return 0; - } + // Target ray mode. + let target_ray_mode = 0; + switch (input_source.targetRayMode) { + case 'gaze': + target_ray_mode = 1; + break; - const controller = GodotWebXR.controllers[p_controller]; - if (!controller || !controller.gamepad) { - return 0; - } + case 'tracked-pointer': + target_ray_mode = 2; + break; - const button_count = controller.gamepad.buttons.length; + case 'screen': + target_ray_mode = 3; + break; - const buf = GodotRuntime.malloc((button_count + 1) * 4); - GodotRuntime.setHeapValue(buf, button_count, 'i32'); - for (let i = 0; i < button_count; i++) { - GodotRuntime.setHeapValue(buf + 4 + (i * 4), controller.gamepad.buttons[i].value, 'float'); + default: } - return buf; - }, + GodotRuntime.setHeapValue(r_target_ray_mode, target_ray_mode, 'i32'); - godot_webxr_get_controller_axes__proxy: 'sync', - godot_webxr_get_controller_axes__sig: 'ii', - godot_webxr_get_controller_axes: function (p_controller) { - if (GodotWebXR.controllers.length === 0) { - return 0; - } + // Touch index. + GodotRuntime.setHeapValue(r_touch_index, GodotWebXR.getTouchIndex(input_source), 'i32'); - const controller = GodotWebXR.controllers[p_controller]; - if (!controller || !controller.gamepad) { - return 0; + // Grip pose. + let has_grip_pose = false; + if (input_source.gripSpace) { + const grip_pose = frame.getPose(input_source.gripSpace, space); + if (grip_pose) { + const grip_pose_matrix = grip_pose.transform.matrix; + for (let i = 0; i < 16; i++) { + GodotRuntime.setHeapValue(r_grip_pose + (i * 4), grip_pose_matrix[i], 'float'); + } + has_grip_pose = true; + } } + GodotRuntime.setHeapValue(r_has_grip_pose, has_grip_pose ? 1 : 0, 'i32'); + + // Gamepad data (mapping, buttons and axes). + let has_standard_mapping = false; + let button_count = 0; + let axes_count = 0; + if (input_source.gamepad) { + if (input_source.gamepad.mapping === 'xr-standard') { + has_standard_mapping = true; + } - const axes_count = controller.gamepad.axes.length; + button_count = Math.min(input_source.gamepad.buttons.length, 10); + for (let i = 0; i < button_count; i++) { + GodotRuntime.setHeapValue(r_buttons + (i * 4), input_source.gamepad.buttons[i].value, 'float'); + } - const buf = GodotRuntime.malloc((axes_count + 1) * 4); - GodotRuntime.setHeapValue(buf, axes_count, 'i32'); - for (let i = 0; i < axes_count; i++) { - let value = controller.gamepad.axes[i]; - if (i === 1 || i === 3) { - // Invert the Y-axis on thumbsticks and trackpads, in order to - // match OpenXR and other XR platform SDKs. - value *= -1.0; + axes_count = Math.min(input_source.gamepad.axes.length, 10); + for (let i = 0; i < axes_count; i++) { + GodotRuntime.setHeapValue(r_axes + (i * 4), input_source.gamepad.axes[i], 'float'); } - GodotRuntime.setHeapValue(buf + 4 + (i * 4), value, 'float'); } - return buf; + GodotRuntime.setHeapValue(r_has_standard_mapping, has_standard_mapping ? 1 : 0, 'i32'); + GodotRuntime.setHeapValue(r_button_count, button_count, 'i32'); + GodotRuntime.setHeapValue(r_axes_count, axes_count, 'i32'); + + return true; }, godot_webxr_get_visibility_state__proxy: 'sync', @@ -642,8 +572,8 @@ const GodotWebXR = { }, godot_webxr_get_bounds_geometry__proxy: 'sync', - godot_webxr_get_bounds_geometry__sig: 'i', - godot_webxr_get_bounds_geometry: function () { + godot_webxr_get_bounds_geometry__sig: 'ii', + godot_webxr_get_bounds_geometry: function (r_points) { if (!GodotWebXR.space || !GodotWebXR.space.boundsGeometry) { return 0; } @@ -653,7 +583,7 @@ const GodotWebXR = { return 0; } - const buf = GodotRuntime.malloc(((point_count * 3) + 1) * 4); + const buf = GodotRuntime.malloc(point_count * 3 * 4); GodotRuntime.setHeapValue(buf, point_count, 'i32'); for (let i = 0; i < point_count; i++) { const point = GodotWebXR.space.boundsGeometry[i]; @@ -661,8 +591,9 @@ const GodotWebXR = { GodotRuntime.setHeapValue(buf + ((i * 3) + 2) * 4, point.y, 'float'); GodotRuntime.setHeapValue(buf + ((i * 3) + 3) * 4, point.z, 'float'); } + GodotRuntime.setHeapValue(r_points, buf, 'i32'); - return buf; + return point_count; }, }; |