Files
PrinceOfGlory/Packages/PICO Unity Integration SDK-3.0.0-240902/Runtime/Scripts/Utils/PXR_VstModelPosCheck.cs
kridoo 6e91a0c7f0 111
2025-09-15 17:32:08 +08:00

73 lines
1.8 KiB
C#

using System.Collections;
using System.Collections.Generic;
using Unity.XR.PXR;
using UnityEngine;
using UnityEngine.XR.Interaction.Toolkit;
public class PXR_VstModelPosCheck : MonoBehaviour
{
public bool IsController = false;
private Transform mMainCamTrans;
private XRBaseController mXRBaseController;
private PXR_Hand mPXR_Hand;
private float mVirtualWorldOffset = 0.03f;
private readonly Vector3 mStartDirection = new Vector3(0f, 0f, 1.0f);
private Quaternion mHeadRotation;
private Vector3 mOffsetDirection;
private Vector3 mOffsetPos;
// Start is called before the first frame update
void Start()
{
if (IsController)
{
if (mXRBaseController == null)
mXRBaseController = GetComponent<XRBaseController>();
}
else
{
if (mPXR_Hand == null)
mPXR_Hand = GetComponent<PXR_Hand>();
}
mMainCamTrans = Camera.main.transform;
mVirtualWorldOffset = PXR_Plugin.System.UPxr_VstModelOffset();
}
private void OnEnable()
{
Application.onBeforeRender += CheckPos;
}
private void OnDisable()
{
Application.onBeforeRender -= CheckPos;
}
private void UpdatePos()
{
mHeadRotation = mMainCamTrans.localRotation;
mOffsetDirection = mHeadRotation * (-1f * mStartDirection);
mOffsetPos = mOffsetDirection * mVirtualWorldOffset;
}
private void CheckPos()
{
if (IsController)
{
UpdatePos();
transform.localPosition = mXRBaseController.currentControllerState.position + mOffsetPos;
}
}
public Vector3 GetHandPosOffset()
{
UpdatePos();
return mOffsetPos;
}
}