First Code Commit
This commit is contained in:
78
OSCCollar/OSCCollar.cs
Normal file
78
OSCCollar/OSCCollar.cs
Normal file
@ -0,0 +1,78 @@
|
||||
using BuildSoft.OscCore;
|
||||
|
||||
namespace VRC_Console;
|
||||
|
||||
public class OSCCollar
|
||||
{
|
||||
private OscServer Server { get; init; }
|
||||
private OscClient Client { get; init; }
|
||||
private float _collarAngle, _collarStretch;
|
||||
private const float StretchDeadzone = 0.1f;
|
||||
private const float TurnDeadZone = 5f / 180f; //5 degrees
|
||||
private DateTime _lastMessageSent = DateTime.UnixEpoch;
|
||||
private bool _sendingMessage = false;
|
||||
|
||||
public OSCCollar()
|
||||
{
|
||||
this.Server = new OscServer(9001);
|
||||
this.Client = new OscClient("127.0.0.1", 9000);
|
||||
this._collarAngle = 0;
|
||||
this._collarStretch = 0;
|
||||
Server.TryAddMethodPair("/avatar/parameters/Collar_Angle", CollarAngleHandle, HandleCollarChange);
|
||||
Server.TryAddMethodPair("/avatar/parameters/Collar_Stretch", CollarStretchHandle, HandleCollarChange);
|
||||
|
||||
Thread sendNullInputThread = new Thread(SendNullInput);
|
||||
sendNullInputThread.Start();
|
||||
}
|
||||
|
||||
private void CollarAngleHandle(OscMessageValues messageValues)
|
||||
{
|
||||
this._collarAngle = messageValues.ReadFloatElement(0);
|
||||
}
|
||||
|
||||
private void CollarStretchHandle(OscMessageValues messageValues)
|
||||
{
|
||||
this._collarStretch = messageValues.ReadFloatElement(0);
|
||||
}
|
||||
|
||||
private void SendNullInput()
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
if (!_sendingMessage)
|
||||
{
|
||||
this.Client.Send("/input/Vertical", 0f);
|
||||
this.Client.Send("/input/LookHorizontal", 0f);
|
||||
this._lastMessageSent = DateTime.Now;
|
||||
}
|
||||
Thread.Sleep(400);
|
||||
}
|
||||
}
|
||||
|
||||
private static readonly TimeSpan MessageTimeoutSpan = TimeSpan.FromMilliseconds(10);
|
||||
private void HandleCollarChange()
|
||||
{
|
||||
_sendingMessage = true;
|
||||
TimeSpan messageTimeout = _lastMessageSent.Add(MessageTimeoutSpan).Subtract(DateTime.Now);
|
||||
if(messageTimeout > TimeSpan.FromMilliseconds(0))
|
||||
Thread.Sleep(messageTimeout);
|
||||
|
||||
if (this._collarStretch > StretchDeadzone)
|
||||
this.Client.Send("/input/Vertical", this._collarStretch);
|
||||
else
|
||||
this.Client.Send("/input/Vertical", 0f);
|
||||
|
||||
if (Math.Abs(this._collarAngle) > TurnDeadZone)
|
||||
this.Client.Send("/input/LookHorizontal", CalculateTurnPower(this._collarAngle));
|
||||
else
|
||||
this.Client.Send("/input/LookHorizontal", 0f);
|
||||
_sendingMessage = false;
|
||||
}
|
||||
|
||||
private const float MaxAngle = 180f;
|
||||
private const float MaxPower = 0.5f;
|
||||
private static float CalculateTurnPower(float boneAngle)
|
||||
{
|
||||
return (boneAngle / MaxAngle) * MaxPower;
|
||||
}
|
||||
}
|
15
OSCCollar/OSCCollar.csproj
Normal file
15
OSCCollar/OSCCollar.csproj
Normal file
@ -0,0 +1,15 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net7.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<RootNamespace>VRC_Console</RootNamespace>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="BuildSoft.OscCore" Version="1.2.1.1" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
Reference in New Issue
Block a user