Unity Udexreal开发插件包
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

124 lines
3.5 KiB

using System;
using System.Collections.Generic;
using TMPro;
using Unity.VisualScripting;
using Unity.XR.PXR;
using UnityEngine;
public class MotionTracker : MonoBehaviour
{
public enum HandChoice
{
LeftHand,
RightHand,
Extra
}
public HandChoice handChoice;
public Transform Target;
private int ChoiceIndex => (int)handChoice;
int DeviceCount = 1;
private bool updateOT = true;
List<long> trackerIds = new List<long>();
public bool UseOldCalibrationVersion = true;
public MotionTrackerNum motionTrackerNum;
private void RequestMotionTrackerComplete(RequestMotionTrackerCompleteEventData obj)
{
DeviceCount = (int)obj.trackerCount;
for (int i = 0; i < DeviceCount; i++)
{
trackerIds.Add(obj.trackerIds[i]);
}
updateOT = true;
Debug.LogError("sss " + trackerIds.Count);
}
private readonly Quaternion[] TrackerCalibrationRot = new Quaternion[2]
{
Quaternion.Inverse(new Quaternion(0.30924f, 0.12111f, 0.35876f, 0.87235f)),
Quaternion.Inverse(new Quaternion(0.31865f, -0.07844f, -0.28810f, 0.89962f))
};
private readonly Vector3[] TrackerCalibrationPos = new Vector3[2]
{
Vector3.zero,
new Vector3(0.01f, 0.01f, -0.02f)
};
private readonly Quaternion[] TrackerCalibrationRot_FixVersion = new Quaternion[2]
{
new Quaternion(0.807f, 0.005f, 0.043f, 0.589f),
new Quaternion(0.873f, 0.069f, -0.129f, 0.466f)
};
private readonly Vector3[] TrackerCalibrationPos_FixVersion = new Vector3[2]
{
new Vector3(-0.018f, 0.040f, -0.041f),
new Vector3(0.036f, 0.043f, -0.049f)
};
void Start()
{
if (UseOldCalibrationVersion)
{
transform.GetChild(0).rotation *= TrackerCalibrationRot[ChoiceIndex];
transform.GetChild(0).localPosition += TrackerCalibrationPos[ChoiceIndex];
}
else
{
transform.GetChild(0).localRotation = TrackerCalibrationRot_FixVersion[ChoiceIndex];
transform.GetChild(0).localPosition = TrackerCalibrationPos_FixVersion[ChoiceIndex];
}
int res = -1;
PXR_MotionTracking.RequestMotionTrackerCompleteAction += RequestMotionTrackerComplete;
res = PXR_MotionTracking.CheckMotionTrackerNumber(motionTrackerNum);
}
void Update()
{
#if UNITY_ANDROID
if (updateOT && trackerIds.Count > 0)
{
MotionTrackerLocation location = new MotionTrackerLocation();
bool isValidPose = false;
int result = PXR_MotionTracking.GetMotionTrackerLocation(trackerIds[ChoiceIndex], ref location, ref isValidPose);
if (result == 0)
{
transform.localPosition = location.pose.Position.ToVector3();
transform.rotation = location.pose.Orientation.ToQuat();
}
}
#endif
}
private void LateUpdate()
{
Target.SetPositionAndRotation(transform.GetChild(0).position, transform.GetChild(0).rotation);
#if UNITY_EDITOR
Target.SetPositionAndRotation(transform.GetChild(1).position, transform.GetChild(1).rotation);
#endif
}
public Transform GetControlledHand(bool specific = false)
{
if (specific)
{
#if UNITY_EDITOR
return transform.GetChild(1);
#else
return transform.GetChild(0);
#endif
}
else
{
return transform.parent;
}
}
}