Skip to content

Commit 2bc7066

Browse files
committed
networked: ability to track hands
1 parent f0a0c1a commit 2bc7066

File tree

4 files changed

+75
-2
lines changed

4 files changed

+75
-2
lines changed

src/XRSharp.CommunityToolkit.Networked/NetworkedScene.cs

+31
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public class NetworkedScene : RootComponent
2020
new() { Type = ResourceType.Script, Source = $"{ResourcesPath}/socket.io.slim.js" },
2121
new() { Type = ResourceType.Script, Source = $"{ResourcesPath}/easyrtc.js" },
2222
new() { Type = ResourceType.Script, Source = $"{ResourcesPath}/networked-aframe.min.js" },
23+
new() { Type = ResourceType.Script, Source = $"{ResourcesPath}/networked-hand-tracking.js" },
2324
};
2425

2526
/// <summary>
@@ -64,6 +65,13 @@ public class NetworkedScene : RootComponent
6465
/// </summary>
6566
public bool Debug { get; set; } = false;
6667

68+
/// <summary>
69+
/// Whether user hands should be visible to others.
70+
/// Currently only dots model is supported.
71+
/// Default is <see langword="false"/>.
72+
/// </summary>
73+
public bool HandTrackingEnabled { get; set; }
74+
6775
public bool IsConnected => IsInitialized && Interop.ExecuteJavaScriptGetResult<bool>("NAF.connection.isConnected();");
6876

6977
/// <summary>
@@ -156,6 +164,29 @@ protected override void Init()
156164
}
157165
}
158166

167+
if (HandTrackingEnabled)
168+
{
169+
Interop.ExecuteJavaScriptVoid(@$"
170+
var assets = {Root.JsElement}.querySelector('a-assets');
171+
const handJointTemplate = document.createElement('template');
172+
handJointTemplate.setAttribute('id', 'hand-joint-template');
173+
174+
const jointSphere = document.createElement('a-sphere');
175+
jointSphere.setAttribute('radius', 0.01);
176+
177+
handJointTemplate.content.appendChild(jointSphere);
178+
assets.appendChild(handJointTemplate);
179+
180+
const handElements = document.querySelectorAll('[hand-tracking-controls]');
181+
182+
for (var i = 0; i < handElements.length; i++) {{
183+
var hand = handElements[i];
184+
hand.setAttribute('hand-tracking-controls', 'modelStyle: dots');
185+
hand.setAttribute('networked-hand-tracking', '');
186+
}}
187+
");
188+
}
189+
159190
if (Audio)
160191
{
161192
UpdateMicState();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
AFRAME.registerComponent('networked-hand-tracking', {
2+
dependencies: ['hand-tracking-controls'],
3+
4+
schema: {
5+
options: { default: 'template:#hand-joint-template; attachTemplateToLocal:false' }
6+
},
7+
8+
init: function () {
9+
var el = this.el;
10+
this.trackingControls = el.components['hand-tracking-controls'];
11+
this.trackingControls.initDotsModel = this.initDotsModel();
12+
13+
this.onExitVR = AFRAME.utils.bind(this.onExitVR, this);
14+
el.sceneEl.addEventListener('exit-vr', this.onExitVR);
15+
},
16+
17+
initDotsModel: function () {
18+
this.trackingControls.initDotsModel();
19+
20+
var jointEls = this.trackingControls.jointEls;
21+
22+
for (var i = 0; i < jointEls.length; i++) {
23+
jointEls[i].setAttribute('networked', this.data.options);
24+
}
25+
},
26+
27+
onExitVR: function () {
28+
var jointEls = this.trackingControls.jointEls;
29+
this.trackingControls.jointEls = [];
30+
31+
for (var i = 0; i < jointEls.length; i++) {
32+
jointEls[i].removeAttribute('networked');
33+
}
34+
},
35+
});

src/XRSharp.CommunityToolkit.Networked/XRSharp.CommunityToolkit.Networked.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
<ItemGroup>
2323
<EmbeddedResource Include="Resources\easyrtc.js" />
2424
<EmbeddedResource Include="Resources\networked-aframe.min.js" />
25+
<EmbeddedResource Include="Resources\networked-hand-tracking.js" />
2526
<EmbeddedResource Include="Resources\socket.io.slim.js" />
2627
</ItemGroup>
2728

src/XRSharp.CommunityToolkit/XRSharp.CommunityToolkit/Examples/Networked/Networked.xaml

+8-2
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,16 @@
99
<xr:BasicEnvironment BackgroundColor="LightBlue"/>
1010
</xr:Root3D.Environment>
1111
<xr:Root3D.Components>
12-
<n:NetworkedScene Debug="True" Audio="True" Adapter="easyrtc" ServerURL="http://localhost:8080/">
12+
<n:NetworkedScene Adapter="easyrtc"
13+
Audio="True"
14+
Debug="True"
15+
HandTrackingEnabled="True"
16+
ServerURL="http://localhost:8080/">
1317
<n:NetworkedScene.Avatar>
1418
<xr:Canvas3D>
15-
<xr:Box SizeX="0.5" SizeY="0.7" SizeZ="0.5"/>
19+
<xr:Box SizeX="0.5"
20+
SizeY="0.7"
21+
SizeZ="0.5"/>
1622
</xr:Canvas3D>
1723
</n:NetworkedScene.Avatar>
1824
</n:NetworkedScene>

0 commit comments

Comments
 (0)