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.
63 lines
2.5 KiB
63 lines
2.5 KiB
using UnityEngine;
|
|
#if UC_VR
|
|
using Valve.VR;
|
|
#endif
|
|
|
|
namespace UC
|
|
{
|
|
public static class RayCastUtils
|
|
{
|
|
public static void SetupMaterial(Material material/*, Camera camera*/)
|
|
{
|
|
Camera camera = Camera.current;
|
|
#if UC_VR
|
|
if (SteamVR.enabled)
|
|
{
|
|
material.EnableKeyword ("VR");
|
|
Matrix4x4 ptt_l = GetSteamVRProjectionMatrix(camera, EVREye.Eye_Left);
|
|
Matrix4x4 ptt_r = GetSteamVRProjectionMatrix(camera, EVREye.Eye_Right);
|
|
|
|
Matrix4x4 ttt_l = GetSteamVREyeToHeadTransform(EVREye.Eye_Left);
|
|
ttt_l = transform.localToWorldMatrix * ttt_l;
|
|
|
|
Matrix4x4 ttt_r = GetSteamVREyeToHeadTransform(EVREye.Eye_Right);
|
|
ttt_r = transform.localToWorldMatrix * ttt_r;
|
|
|
|
material.SetMatrix("_Cam2WorldMatrix_L", ttt_l);
|
|
material.SetMatrix("_PVMMatrix_L", ttt_l.inverse);
|
|
material.SetMatrix("_Cam2WorldMatrix_R", ttt_r);
|
|
material.SetMatrix("_PVMMatrix_R", ttt_r.inverse);
|
|
}
|
|
else
|
|
#endif
|
|
{
|
|
material.DisableKeyword("VR");
|
|
material.SetMatrix("_Cam2WorldMatrix", camera.cameraToWorldMatrix);
|
|
material.SetMatrix("_PVMMatrix", camera.projectionMatrix * camera.worldToCameraMatrix);
|
|
}
|
|
}
|
|
|
|
#if UC_VR
|
|
static Matrix4x4 GetSteamVRProjectionMatrix(Camera cam, EVREye eye)
|
|
{
|
|
HmdMatrix44_t proj = SteamVR.instance.hmd.GetProjectionMatrix(eye, cam.nearClipPlane, cam.farClipPlane, SteamVR.instance.graphicsAPI);
|
|
Matrix4x4 m = new Matrix4x4();
|
|
m.m00 = proj.m0; m.m01 = proj.m1; m.m02 = proj.m2; m.m03 = proj.m3;
|
|
m.m10 = proj.m4; m.m11 = proj.m5; m.m12 = proj.m6; m.m13 = proj.m7;
|
|
m.m20 = proj.m8; m.m21 = proj.m9; m.m22 = proj.m10; m.m23 = proj.m11;
|
|
m.m30 = proj.m12; m.m31 = proj.m13; m.m32 = proj.m14; m.m33 = proj.m15;
|
|
return m;
|
|
}
|
|
|
|
static Matrix4x4 GetSteamVREyeToHeadTransform(EVREye eye)
|
|
{
|
|
HmdMatrix34_t input = SteamVR.instance.hmd.GetEyeToHeadTransform(eye);
|
|
var m = Matrix4x4.identity;
|
|
m.m00 = input.m0; m.m01 = input.m1; m.m02 = input.m2; m.m03 = input.m3;
|
|
m.m10 = input.m4; m.m11 = input.m5; m.m12 = input.m6; m.m13 = input.m7;
|
|
m.m20 = input.m8; m.m21 = input.m9; m.m22 = input.m10; m.m23 = input.m11;
|
|
return m;
|
|
}
|
|
#endif
|
|
}
|
|
}
|
|
|