diff --git a/AxSlime/AxSlime.csproj b/AxSlime/AxSlime.csproj
index ca9a772..566ab65 100644
--- a/AxSlime/AxSlime.csproj
+++ b/AxSlime/AxSlime.csproj
@@ -10,10 +10,11 @@
-
+
all
runtime; build; native; contentfiles; analyzers; buildtransitive
+
diff --git a/AxSlime/Axis/AxisRawData.cs b/AxSlime/Axis/AxisRawData.cs
index 33fbeb1..2762050 100644
--- a/AxSlime/Axis/AxisRawData.cs
+++ b/AxSlime/Axis/AxisRawData.cs
@@ -2,7 +2,7 @@
namespace AxSlime.Axis
{
- public enum NodeBinding
+ public enum NodeBinding : byte
{
RightThigh,
RightCalf,
diff --git a/AxSlime/Config/AxSlimeConfig.cs b/AxSlime/Config/AxSlimeConfig.cs
index b331597..a8d62f2 100644
--- a/AxSlime/Config/AxSlimeConfig.cs
+++ b/AxSlime/Config/AxSlimeConfig.cs
@@ -8,13 +8,27 @@ public record AxSlimeConfig
public static readonly AxSlimeConfig Default = new();
[JsonPropertyName("config_version")]
- public int ConfigVersion { get; set; } = 0;
+ public int ConfigVersion { get; set; } = 1;
[JsonPropertyName("slimevr_endpoint")]
public string SlimeVrEndPointStr { get; set; } = "127.0.0.1:6969";
+ [JsonPropertyName("osc_enabled")]
+ public bool OscEnabled { get; set; } = false;
+
+ [JsonPropertyName("osc_receive_endpoint")]
+ public string OscReceiveEndPointStr { get; set; } = "127.0.0.1:9001";
+
+ [JsonPropertyName("haptic_vibration")]
+ public HapticsConfig Haptics { get; set; } = new();
+
+ // Utilities
+
[JsonIgnore]
public IPEndPoint SlimeVrEndPoint => IPEndPoint.Parse(SlimeVrEndPointStr);
+
+ [JsonIgnore]
+ public IPEndPoint OscReceiveEndPoint => IPEndPoint.Parse(OscReceiveEndPointStr);
}
[JsonSerializable(typeof(AxSlimeConfig))]
diff --git a/AxSlime/Config/HapticsConfig.cs b/AxSlime/Config/HapticsConfig.cs
new file mode 100644
index 0000000..f63f2de
--- /dev/null
+++ b/AxSlime/Config/HapticsConfig.cs
@@ -0,0 +1,57 @@
+using System.Text.Json.Serialization;
+
+namespace AxSlime.Config
+{
+ public record class HapticsConfig
+ {
+ public static readonly HapticsConfig Default = new();
+
+ [JsonPropertyName("enable_touch")]
+ public bool EnableTouch { get; set; } = true;
+
+ [JsonPropertyName("touch_intensity")]
+ public float TouchIntensity { get; set; } = 1f;
+
+ [JsonPropertyName("touch_duration_s")]
+ public float TouchDurationS { get; set; } = 1f;
+
+ [JsonPropertyName("enable_proximity")]
+ public bool EnableProx { get; set; } = true;
+
+ [JsonPropertyName("proximity_threshold")]
+ public float ProxThreshold { get; set; } = 0f;
+
+ [JsonPropertyName("proximity_min_intensity")]
+ public float ProxMinIntensity { get; set; } = 0.25f;
+
+ [JsonPropertyName("proximity_max_intensity")]
+ public float ProxMaxIntensity { get; set; } = 1f;
+
+ [JsonPropertyName("proximity_duration_s")]
+ public float ProxDurationS { get; set; } = 0.1f;
+
+ [JsonPropertyName("linear_proximity")]
+ public bool LinearProx { get; set; } = false;
+
+ [JsonPropertyName("enable_axhaptics_support")]
+ public bool EnableAxHaptics { get; set; } = true;
+
+ [JsonPropertyName("enable_bhaptics_support")]
+ public bool EnableBHaptics { get; set; } = true;
+
+ // Utilities
+
+ [JsonIgnore]
+ public float ProxIntensityRange => ProxMaxIntensity - ProxMinIntensity;
+
+ public float CalcIntensity(float proximity)
+ {
+ var scaledProx = LinearProx ? proximity : proximity * proximity;
+ return float.Clamp(
+ ProxMinIntensity + (scaledProx * ProxIntensityRange),
+ ProxMinIntensity,
+ ProxMaxIntensity
+ );
+ }
+ }
+}
diff --git a/AxSlime/Osc/AxHaptics.cs b/AxSlime/Osc/AxHaptics.cs
new file mode 100644
index 0000000..5c5f3dd
--- /dev/null
+++ b/AxSlime/Osc/AxHaptics.cs
@@ -0,0 +1,57 @@
+using AxSlime.Axis;
+using AxSlime.Config;
+using LucHeart.CoreOSC;
+
+namespace AxSlime.Osc
+{
+ public class AxHaptics : HapticsSource
+ {
+ public static readonly string AxHapticsPrefix = "VRCOSC/AXHaptics/";
+ public static readonly string BinaryPrefix = "Touched";
+ public static readonly string AnalogPrefix = "Proximity";
+
+ private static readonly Dictionary _nameToNode =
+ Enum.GetValues().ToDictionary(v => Enum.GetName(v)!);
+
+ private readonly AxSlimeConfig _config;
+
+ public AxHaptics(AxSlimeConfig config)
+ {
+ _config = config;
+ }
+
+ public HapticEvent[] ComputeHaptics(string parameter, OscMessage message)
+ {
+ var axHaptics = parameter[AxHapticsPrefix.Length..];
+ if (_config.Haptics.EnableTouch && axHaptics.StartsWith(BinaryPrefix))
+ {
+ var trigger = message.Arguments[0] as bool?;
+ if (trigger != true)
+ return [];
+
+ if (_nameToNode.TryGetValue(axHaptics[BinaryPrefix.Length..], out var nodeVal))
+ return [new HapticEvent(nodeVal)];
+ }
+ else if (_config.Haptics.EnableProx && axHaptics.StartsWith(AnalogPrefix))
+ {
+ var proximity = message.Arguments[0] as float? ?? -1f;
+ if (proximity <= _config.Haptics.ProxThreshold)
+ return [];
+
+ var intensity = _config.Haptics.CalcIntensity(proximity);
+ if (
+ intensity > 0f
+ && _nameToNode.TryGetValue(axHaptics[AnalogPrefix.Length..], out var nodeVal)
+ )
+ return [new HapticEvent(nodeVal, intensity, _config.Haptics.ProxDurationS)];
+ }
+
+ return [];
+ }
+
+ public bool IsSource(string parameter, OscMessage message)
+ {
+ return _config.Haptics.EnableAxHaptics && parameter.StartsWith(AxHapticsPrefix);
+ }
+ }
+}
diff --git a/AxSlime/Osc/HapticEvent.cs b/AxSlime/Osc/HapticEvent.cs
new file mode 100644
index 0000000..2a77d90
--- /dev/null
+++ b/AxSlime/Osc/HapticEvent.cs
@@ -0,0 +1,18 @@
+using AxSlime.Axis;
+
+namespace AxSlime.Osc
+{
+ public readonly record struct HapticEvent
+ {
+ public readonly NodeBinding Node;
+ public readonly float? Intensity;
+ public readonly float? Duration;
+
+ public HapticEvent(NodeBinding node, float? intensity = null, float? duration = null)
+ {
+ Node = node;
+ Intensity = intensity;
+ Duration = duration;
+ }
+ }
+}
diff --git a/AxSlime/Osc/HapticsSource.cs b/AxSlime/Osc/HapticsSource.cs
new file mode 100644
index 0000000..81f7f4e
--- /dev/null
+++ b/AxSlime/Osc/HapticsSource.cs
@@ -0,0 +1,10 @@
+using LucHeart.CoreOSC;
+
+namespace AxSlime.Osc
+{
+ public interface HapticsSource
+ {
+ public HapticEvent[] ComputeHaptics(string parameter, OscMessage message);
+ public bool IsSource(string parameter, OscMessage message);
+ }
+}
diff --git a/AxSlime/Osc/OscHandler.cs b/AxSlime/Osc/OscHandler.cs
new file mode 100644
index 0000000..4be493c
--- /dev/null
+++ b/AxSlime/Osc/OscHandler.cs
@@ -0,0 +1,132 @@
+using System.Net.Sockets;
+using System.Text;
+using AxSlime.Axis;
+using AxSlime.Config;
+using LucHeart.CoreOSC;
+
+namespace AxSlime.Osc
+{
+ public class OscHandler : IDisposable
+ {
+ public static readonly string BundleAddress = "#bundle\0";
+ public static readonly byte[] BundleAddressBytes = Encoding.ASCII.GetBytes(BundleAddress);
+ public static readonly string AvatarParamPrefix = "/avatar/parameters/";
+
+ private readonly AxSlimeConfig _config;
+ private readonly AxisCommander _axisCommander;
+ private readonly UdpClient _oscClient;
+
+ private readonly CancellationTokenSource _cancelTokenSource = new();
+ private readonly Task _oscReceiveTask;
+
+ private readonly AxHaptics _axHaptics;
+ private readonly bHaptics _bHaptics;
+
+ private readonly HapticsSource[] _hapticsSources;
+
+ public OscHandler(AxSlimeConfig config, AxisCommander axisCommander)
+ {
+ _config = config;
+ _axisCommander = axisCommander;
+ _oscClient = new UdpClient(config.OscReceiveEndPoint);
+ _oscReceiveTask = OscReceiveTask(_cancelTokenSource.Token);
+
+ _axHaptics = new(config);
+ _bHaptics = new(config);
+
+ _hapticsSources = [_axHaptics, _bHaptics];
+ }
+
+ private static bool IsBundle(ReadOnlySpan buffer)
+ {
+ return buffer.Length > 16 && buffer[..8].SequenceEqual(BundleAddressBytes);
+ }
+
+ private async Task OscReceiveTask(CancellationToken cancelToken = default)
+ {
+ while (!cancelToken.IsCancellationRequested)
+ {
+ try
+ {
+ var packet = await _oscClient.ReceiveAsync(cancelToken);
+ if (IsBundle(packet.Buffer))
+ {
+ var bundle = OscBundle.ParseBundle(packet.Buffer);
+ if (bundle.Timestamp > DateTime.Now)
+ {
+ // Wait for the specified timestamp
+ _ = Task.Run(
+ async () =>
+ {
+ await Task.Delay(bundle.Timestamp - DateTime.Now, cancelToken);
+ OnOscBundle(bundle);
+ },
+ cancelToken
+ );
+ }
+ else
+ {
+ OnOscBundle(bundle);
+ }
+ }
+ else
+ {
+ OnOscMessage(OscMessage.ParseMessage(packet.Buffer));
+ }
+ }
+ catch (OperationCanceledException) { }
+ catch (Exception e)
+ {
+ Console.Error.WriteLine(e);
+ }
+ }
+ }
+
+ private void OnOscBundle(OscBundle bundle)
+ {
+ foreach (var message in bundle.Messages)
+ {
+ OnOscMessage(message);
+ }
+ }
+
+ private void OnOscMessage(OscMessage message)
+ {
+ if (message.Arguments.Length <= 0)
+ return;
+
+ foreach (var @event in ComputeEvents(message))
+ {
+ _axisCommander.SetNodeVibration(
+ (byte)@event.Node,
+ @event.Intensity ?? _config.Haptics.TouchIntensity,
+ @event.Duration ?? _config.Haptics.TouchDurationS
+ );
+ }
+ }
+
+ private HapticEvent[] ComputeEvents(OscMessage message)
+ {
+ if (message.Address.Length <= AvatarParamPrefix.Length)
+ return [];
+
+ var param = message.Address[AvatarParamPrefix.Length..];
+ foreach (var source in _hapticsSources)
+ {
+ if (source.IsSource(param, message))
+ return source.ComputeHaptics(param, message);
+ }
+
+ return [];
+ }
+
+ public void Dispose()
+ {
+ _cancelTokenSource.Cancel();
+ _oscReceiveTask.Wait();
+ _cancelTokenSource.Dispose();
+ _oscClient.Dispose();
+ GC.SuppressFinalize(this);
+ }
+ }
+}
diff --git a/AxSlime/Osc/bHaptics.cs b/AxSlime/Osc/bHaptics.cs
new file mode 100644
index 0000000..cb05ef0
--- /dev/null
+++ b/AxSlime/Osc/bHaptics.cs
@@ -0,0 +1,66 @@
+using AxSlime.Axis;
+using AxSlime.Config;
+using LucHeart.CoreOSC;
+
+namespace AxSlime.Osc
+{
+ public class bHaptics : HapticsSource
+ {
+ public static readonly string bHapticsPrefix = "bHapticsOSC_";
+
+ private static readonly Dictionary _mappings =
+ new()
+ {
+ { "Vest_Front", [NodeBinding.Chest, NodeBinding.Hips] },
+ { "Vest_Back", [NodeBinding.Chest, NodeBinding.Hips] },
+ { "Arm_Left", [NodeBinding.LeftUpperArm, NodeBinding.LeftForeArm] },
+ { "Arm_Right", [NodeBinding.RightUpperArm, NodeBinding.RightForeArm] },
+ {
+ "Foot_Left",
+ [NodeBinding.LeftFoot, NodeBinding.LeftCalf, NodeBinding.LeftThigh]
+ },
+ {
+ "Foot_Right",
+ [NodeBinding.RightFoot, NodeBinding.RightCalf, NodeBinding.RightThigh]
+ },
+ { "Hand_Left", [NodeBinding.LeftHand] },
+ { "Hand_Right", [NodeBinding.RightHand] },
+ { "Head", [NodeBinding.Head] },
+ };
+
+ private static readonly Dictionary _eventMap = _mappings
+ .Select(m => (m.Key, m.Value.Select(n => new HapticEvent(n)).ToArray()))
+ .ToDictionary();
+
+ private readonly AxSlimeConfig _config;
+
+ public bHaptics(AxSlimeConfig config)
+ {
+ _config = config;
+ }
+
+ public HapticEvent[] ComputeHaptics(string parameter, OscMessage message)
+ {
+ if (!_config.Haptics.EnableTouch)
+ return [];
+
+ var trigger = message.Arguments[0] as bool?;
+ if (trigger != true)
+ return [];
+
+ var bHaptics = parameter[bHapticsPrefix.Length..];
+ foreach (var binding in _eventMap)
+ {
+ if (bHaptics.StartsWith(binding.Key))
+ return binding.Value;
+ }
+
+ return [];
+ }
+
+ public bool IsSource(string parameter, OscMessage message)
+ {
+ return _config.Haptics.EnableBHaptics && parameter.StartsWith(bHapticsPrefix);
+ }
+ }
+}
diff --git a/AxSlime/Program.cs b/AxSlime/Program.cs
index 6052c71..3ac091f 100644
--- a/AxSlime/Program.cs
+++ b/AxSlime/Program.cs
@@ -2,8 +2,10 @@
using AxSlime.Axis;
using AxSlime.Bridge;
using AxSlime.Config;
+using AxSlime.Osc;
AxSlimeConfig config;
+OscHandler? oscHandler = null;
BridgeController? bridge = null;
void OnTrackerData(object? sender, AxisOutputData data)
{
@@ -40,6 +42,12 @@ void OnTrackerData(object? sender, AxisOutputData data)
axisSocket.OnAxisData += OnTrackerData;
axisSocket.Start();
+ if (config.OscEnabled)
+ {
+ oscHandler = new OscHandler(config, axisSocket.AxisRuntimeCommander);
+ Console.WriteLine($"Started OSC receiver on {config.OscReceiveEndPoint}.");
+ }
+
Console.WriteLine("AXIS receiver is running, press any key to stop the receiver.");
Console.ReadKey();
}
@@ -49,6 +57,7 @@ void OnTrackerData(object? sender, AxisOutputData data)
}
finally
{
+ oscHandler?.Dispose();
bridge?.Dispose();
}
diff --git a/README.md b/README.md
index 711a37d..675cda7 100644
--- a/README.md
+++ b/README.md
@@ -10,3 +10,7 @@ A bridge to make AXIS trackers work with SlimeVR.
-
-
-
+- CoreOSC
+ -
+- AxHaptics
+ -
diff --git a/ThirdPartyNotices.txt b/ThirdPartyNotices.txt
index e763338..9fab33d 100644
--- a/ThirdPartyNotices.txt
+++ b/ThirdPartyNotices.txt
@@ -16,3 +16,27 @@ The above copyright notice and this permission notice shall be included in all c
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.
---------------------------------------------------------------
+https://github.com/LucHeart/CoreOSC-UTF8-ASYNC
+
+MIT License
+
+Copyright (c) 2023 LucHeart
+
+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.
+---------------------------------------------------------------