From 2bc70664aadd213b39c216c883df499f15e04826 Mon Sep 17 00:00:00 2001 From: Andrei Arekhva Date: Wed, 13 Dec 2023 15:50:51 +0300 Subject: [PATCH] networked: ability to track hands --- .../NetworkedScene.cs | 31 ++++++++++++++++ .../Resources/networked-hand-tracking.js | 35 +++++++++++++++++++ .../XRSharp.CommunityToolkit.Networked.csproj | 1 + .../Examples/Networked/Networked.xaml | 10 ++++-- 4 files changed, 75 insertions(+), 2 deletions(-) create mode 100644 src/XRSharp.CommunityToolkit.Networked/Resources/networked-hand-tracking.js diff --git a/src/XRSharp.CommunityToolkit.Networked/NetworkedScene.cs b/src/XRSharp.CommunityToolkit.Networked/NetworkedScene.cs index cad407f..b81779e 100644 --- a/src/XRSharp.CommunityToolkit.Networked/NetworkedScene.cs +++ b/src/XRSharp.CommunityToolkit.Networked/NetworkedScene.cs @@ -20,6 +20,7 @@ public class NetworkedScene : RootComponent new() { Type = ResourceType.Script, Source = $"{ResourcesPath}/socket.io.slim.js" }, new() { Type = ResourceType.Script, Source = $"{ResourcesPath}/easyrtc.js" }, new() { Type = ResourceType.Script, Source = $"{ResourcesPath}/networked-aframe.min.js" }, + new() { Type = ResourceType.Script, Source = $"{ResourcesPath}/networked-hand-tracking.js" }, }; /// @@ -64,6 +65,13 @@ public class NetworkedScene : RootComponent /// public bool Debug { get; set; } = false; + /// + /// Whether user hands should be visible to others. + /// Currently only dots model is supported. + /// Default is . + /// + public bool HandTrackingEnabled { get; set; } + public bool IsConnected => IsInitialized && Interop.ExecuteJavaScriptGetResult("NAF.connection.isConnected();"); /// @@ -156,6 +164,29 @@ protected override void Init() } } + if (HandTrackingEnabled) + { + Interop.ExecuteJavaScriptVoid(@$" +var assets = {Root.JsElement}.querySelector('a-assets'); +const handJointTemplate = document.createElement('template'); +handJointTemplate.setAttribute('id', 'hand-joint-template'); + +const jointSphere = document.createElement('a-sphere'); +jointSphere.setAttribute('radius', 0.01); + +handJointTemplate.content.appendChild(jointSphere); +assets.appendChild(handJointTemplate); + +const handElements = document.querySelectorAll('[hand-tracking-controls]'); + +for (var i = 0; i < handElements.length; i++) {{ + var hand = handElements[i]; + hand.setAttribute('hand-tracking-controls', 'modelStyle: dots'); + hand.setAttribute('networked-hand-tracking', ''); +}} +"); + } + if (Audio) { UpdateMicState(); diff --git a/src/XRSharp.CommunityToolkit.Networked/Resources/networked-hand-tracking.js b/src/XRSharp.CommunityToolkit.Networked/Resources/networked-hand-tracking.js new file mode 100644 index 0000000..49cbd2a --- /dev/null +++ b/src/XRSharp.CommunityToolkit.Networked/Resources/networked-hand-tracking.js @@ -0,0 +1,35 @@ +AFRAME.registerComponent('networked-hand-tracking', { + dependencies: ['hand-tracking-controls'], + + schema: { + options: { default: 'template:#hand-joint-template; attachTemplateToLocal:false' } + }, + + init: function () { + var el = this.el; + this.trackingControls = el.components['hand-tracking-controls']; + this.trackingControls.initDotsModel = this.initDotsModel(); + + this.onExitVR = AFRAME.utils.bind(this.onExitVR, this); + el.sceneEl.addEventListener('exit-vr', this.onExitVR); + }, + + initDotsModel: function () { + this.trackingControls.initDotsModel(); + + var jointEls = this.trackingControls.jointEls; + + for (var i = 0; i < jointEls.length; i++) { + jointEls[i].setAttribute('networked', this.data.options); + } + }, + + onExitVR: function () { + var jointEls = this.trackingControls.jointEls; + this.trackingControls.jointEls = []; + + for (var i = 0; i < jointEls.length; i++) { + jointEls[i].removeAttribute('networked'); + } + }, +}); \ No newline at end of file diff --git a/src/XRSharp.CommunityToolkit.Networked/XRSharp.CommunityToolkit.Networked.csproj b/src/XRSharp.CommunityToolkit.Networked/XRSharp.CommunityToolkit.Networked.csproj index 55274c8..8fc10de 100644 --- a/src/XRSharp.CommunityToolkit.Networked/XRSharp.CommunityToolkit.Networked.csproj +++ b/src/XRSharp.CommunityToolkit.Networked/XRSharp.CommunityToolkit.Networked.csproj @@ -22,6 +22,7 @@ + diff --git a/src/XRSharp.CommunityToolkit/XRSharp.CommunityToolkit/Examples/Networked/Networked.xaml b/src/XRSharp.CommunityToolkit/XRSharp.CommunityToolkit/Examples/Networked/Networked.xaml index f9f1fbd..c06860f 100644 --- a/src/XRSharp.CommunityToolkit/XRSharp.CommunityToolkit/Examples/Networked/Networked.xaml +++ b/src/XRSharp.CommunityToolkit/XRSharp.CommunityToolkit/Examples/Networked/Networked.xaml @@ -9,10 +9,16 @@ - + - +