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.

64 lines
1.3 KiB

#pragma multi_compile __ VR
float4x4 _PVMMatrix;
float4x4 _Cam2WorldMatrix;
#if VR
float4x4 _PVMMatrix_L;
float4x4 _PVMMatrix_R;
float4x4 _Cam2WorldMatrix_L;
float4x4 _Cam2WorldMatrix_R;
#endif
float4 toWorldPosition(float4 p_in)
{
return mul(_PVMMatrix, p_in);
}
float toDepth(float4 p_in)
{
#if defined(VR)
return -toWorldPosition(p_in).z;
#else
return toWorldPosition(p_in).z;
#endif
}
float3 getRayDirection(float2 uv)
{
#if VR
if (unity_CameraProjection[0][2] < 0)
{
_Cam2WorldMatrix = _Cam2WorldMatrix_L;
_PVMMatrix = mul(unity_CameraProjection, _PVMMatrix_L);
}
else
{
_Cam2WorldMatrix = _Cam2WorldMatrix_R;
_PVMMatrix = mul(unity_CameraProjection, _PVMMatrix_R);
}
#endif
float4x4 m = unity_CameraProjection;
float near = m[2][3] / (m[2][2] - 1);
float far = m[2][3] / (m[2][2] + 1);
float bottom = near * (m[1][2] - 1) / m[1][1];
float top = near * (m[1][2] + 1) / m[1][1];
float left = near * (m[0][2] - 1) / m[0][0];
float right = near * (m[0][2] + 1) / m[0][0];
float x = uv.x * (right - left) + left;
float y = uv.y * (top - bottom) + bottom;
#if VR
float z = near;
#else
float z = -near;
#endif
float3 rayDirection = float3(x, y, z);
rayDirection = mul(_Cam2WorldMatrix, float4(rayDirection, 0)).xyz;
rayDirection = normalize(rayDirection);
return rayDirection;
}