Skip to content

Instantly share code, notes, and snippets.

@vrchat-developer
Last active July 24, 2024 02:14
Show Gist options
  • Select an option

  • Save vrchat-developer/bc07d3dba46206f6ee42d36323c034eb to your computer and use it in GitHub Desktop.

Select an option

Save vrchat-developer/bc07d3dba46206f6ee42d36323c034eb to your computer and use it in GitHub Desktop.

Revisions

  1. vrchat-developer revised this gist Apr 5, 2023. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion OscEyeTrackingSenderExample.cs
    Original file line number Diff line number Diff line change
    @@ -19,7 +19,7 @@ public class OscEyeTrackingSenderExample : MonoBehaviour
    public bool SendEyeLeftRightPitchYaw;
    public bool SendEyeLeftRightVec;

    // You must download and add OscCore to your project: https://github.com/vrchat/osccore/tree/all-in-one
    // You must download and add OscCore to your project: https://github.com/vrchat/OscCore/tree/send-more-values
    private OscCore.OscClient oscClient;

    void Start()
  2. vrchat-developer created this gist Apr 4, 2023.
    127 changes: 127 additions & 0 deletions OscEyeTrackingSenderExample.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,127 @@
    // This code is licensed under MIT license

    using UnityEngine;

    public class OscEyeTrackingSenderExample : MonoBehaviour
    {
    public string OscTargetIp = "127.0.0.1";
    public int OscTargetPort = 9000;
    public Transform EyeRoot;
    public Transform EyeTarget;
    public float UserInterpupillaryDistance = 0.064f;
    public bool SendEyesClosedAmount;
    [Range(0f, 1f)]
    public float EyesClosedAmount;
    public bool SendEyeCenterPitchYaw;
    public bool SendEyeCenterPitchYawDist;
    public bool SendEyeCenterVec;
    public bool SendEyeCenterVecFull;
    public bool SendEyeLeftRightPitchYaw;
    public bool SendEyeLeftRightVec;

    // You must download and add OscCore to your project: https://github.com/vrchat/osccore/tree/all-in-one
    private OscCore.OscClient oscClient;

    void Start()
    {
    oscClient = new OscCore.OscClient(OscTargetIp, OscTargetPort);
    }

    void Update()
    {
    if (oscClient == null)
    return;

    if (SendEyesClosedAmount)
    {
    oscClient.Send("/tracking/eye/EyesClosedAmount", EyesClosedAmount);
    }

    if (EyeRoot && EyeTarget)
    {

    // Data should be sent to only one of the following eyelook addresses at a time
    // (EyeClosedAmount data may be sent simultaneously with one of the eyelook addresses)

    if (SendEyeCenterPitchYaw)
    {
    Vector3 eyeLookDirection = EyeRoot.InverseTransformDirection(EyeTarget.position - EyeRoot.position);
    Quaternion eyeLookQuat = Quaternion.LookRotation(eyeLookDirection);
    float eyeLookPitch = ToPlusMinus180(eyeLookQuat.eulerAngles.x);
    float eyeLookYaw = ToPlusMinus180(eyeLookQuat.eulerAngles.y);

    oscClient.Send("/tracking/eye/CenterPitchYaw", eyeLookPitch, eyeLookYaw);

    Debug.DrawRay(EyeRoot.position, (EyeTarget.position - EyeRoot.position).normalized, Color.yellow);
    }

    if (SendEyeCenterPitchYawDist)
    {
    Vector3 eyeLookDirection = EyeRoot.InverseTransformDirection(EyeTarget.position - EyeRoot.position);
    Quaternion eyeLookQuat = Quaternion.LookRotation(eyeLookDirection);
    float eyeLookPitch = ToPlusMinus180(eyeLookQuat.eulerAngles.x);
    float eyeLookYaw = ToPlusMinus180(eyeLookQuat.eulerAngles.y);

    oscClient.Send("/tracking/eye/CenterPitchYawDist", eyeLookPitch, eyeLookYaw, eyeLookDirection.magnitude);

    Debug.DrawLine(EyeRoot.position, EyeTarget.position, Color.yellow);
    }

    if (SendEyeCenterVec)
    {
    Vector3 eyeLookDirection = EyeRoot.InverseTransformDirection(EyeTarget.position - EyeRoot.position);

    oscClient.Send("/tracking/eye/CenterVec", eyeLookDirection.normalized);

    Debug.DrawRay(EyeRoot.position, (EyeTarget.position - EyeRoot.position).normalized, Color.yellow);
    }

    if (SendEyeCenterVecFull)
    {
    oscClient.Send("/tracking/eye/CenterVecFull", EyeRoot.InverseTransformDirection(EyeTarget.position - EyeRoot.position));

    Debug.DrawLine(EyeRoot.position, EyeTarget.position, Color.yellow);
    }

    if (SendEyeLeftRightPitchYaw)
    {
    Vector3 leftEyeRoot = EyeRoot.position + EyeRoot.rotation * Vector3.left * (UserInterpupillaryDistance * 0.5f);
    Vector3 rightEyeRoot = EyeRoot.position + EyeRoot.rotation * Vector3.right * (UserInterpupillaryDistance * 0.5f);

    Vector3 leftEyeLookDirection = EyeRoot.InverseTransformDirection(EyeTarget.position - leftEyeRoot);
    Quaternion leftEyeLookQuat = Quaternion.LookRotation(leftEyeLookDirection);
    float leftEyeLookPitch = ToPlusMinus180(leftEyeLookQuat.eulerAngles.x);
    float leftEyeLookYaw = ToPlusMinus180(leftEyeLookQuat.eulerAngles.y);

    Vector3 rightEyeLookDirection = EyeRoot.InverseTransformDirection(EyeTarget.position - rightEyeRoot);
    Quaternion rightEyeLookQuat = Quaternion.LookRotation(rightEyeLookDirection);
    float rightEyeLookPitch = ToPlusMinus180(rightEyeLookQuat.eulerAngles.x);
    float rightEyeLookYaw = ToPlusMinus180(rightEyeLookQuat.eulerAngles.y);

    oscClient.Send("/tracking/eye/LeftRightPitchYaw", leftEyeLookPitch, leftEyeLookYaw, rightEyeLookPitch, rightEyeLookYaw);

    Debug.DrawRay(leftEyeRoot, (EyeTarget.position - leftEyeRoot).normalized, Color.blue);
    Debug.DrawRay(rightEyeRoot, (EyeTarget.position - rightEyeRoot).normalized, Color.red);
    }

    if (SendEyeLeftRightVec)
    {
    Vector3 leftEyeRoot = EyeRoot.position + EyeRoot.rotation * Vector3.left * (UserInterpupillaryDistance * 0.5f);
    Vector3 rightEyeRoot = EyeRoot.position + EyeRoot.rotation * Vector3.right * (UserInterpupillaryDistance * 0.5f);

    Vector3 leftEyeLookDirection = EyeRoot.InverseTransformDirection(EyeTarget.position - leftEyeRoot);
    Vector3 rightEyeLookDirection = EyeRoot.InverseTransformDirection(EyeTarget.position - rightEyeRoot);

    oscClient.Send("/tracking/eye/LeftRightVec", leftEyeLookDirection.normalized, rightEyeLookDirection.normalized);

    Debug.DrawRay(leftEyeRoot, (EyeTarget.position - leftEyeRoot).normalized, Color.blue);
    Debug.DrawRay(rightEyeRoot, (EyeTarget.position - rightEyeRoot).normalized, Color.red);
    }
    }
    }

    private float ToPlusMinus180(float inputAngle)
    {
    return ((inputAngle + 180f) % 360f) - 180f;
    }
    }