Created
November 18, 2022 19:56
-
-
Save vrchat-developer/129c1647667945158b14709f8d65d471 to your computer and use it in GitHub Desktop.
Example of sending Virtual Tracker data to VRChat over OSC
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This code is licensed under MIT license | |
using UnityEngine; | |
public class OscTrackerSenderExample : MonoBehaviour | |
{ | |
public float UserHeightInMeters = 1.7f; | |
public string OscTargetIp = "127.0.0.1"; | |
public int OscTargetPort = 9000; | |
public Transform HeadReference; | |
public Transform[] Trackers = new Transform[8]; | |
// You must download and add OscCore to your project: https://github.com/vrchat/osccore/tree/all-in-one | |
private OscCore.OscClient oscClient; | |
/// This is the height of VRChat's default robot. | |
/// You must adjust this to the standing height of the avatar | |
/// in your scene used to produce a pose. | |
private const float SENDER_AVATAR_HEIGHT = 1.87f; | |
private float scaleFactor = 0.909091f; // (UserHeightInMeters / SENDER_AVATAR_HEIGHT) | |
void Start() | |
{ | |
scaleFactor = UserHeightInMeters / SENDER_AVATAR_HEIGHT; | |
oscClient = new OscCore.OscClient(OscTargetIp, OscTargetPort); | |
} | |
private void OnValidate() | |
{ | |
if (Trackers.Length > 8) | |
System.Array.Resize(ref Trackers, 8); | |
} | |
void Update() | |
{ | |
if (oscClient == null) | |
return; | |
if (HeadReference) | |
{ | |
oscClient.Send("/tracking/trackers/head/position", HeadReference.position * scaleFactor); | |
oscClient.Send("/tracking/trackers/head/rotation", HeadReference.eulerAngles); | |
} | |
for (int i = 0; i < Trackers.Length; i++) | |
{ | |
if (Trackers[i]) | |
{ | |
/// Valid addresses will look like the the following | |
/// where "1" can be a number between 1 and 8. | |
/// Note: there is no tracker 0. | |
/// "/tracking/trackers/1/position" | |
/// "/tracking/trackers/1/rotation" | |
oscClient.Send($"/tracking/trackers/{i + 1}/position", Trackers[i].position * scaleFactor); | |
oscClient.Send($"/tracking/trackers/{i + 1}/rotation", Trackers[i].eulerAngles); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment