Skip to content

Commit

Permalink
Merge pull request #86 from mobfishgmbh/issue-cam_clip_dynamic
Browse files Browse the repository at this point in the history
Add method to set near and far clip.
  • Loading branch information
cdytoby authored Nov 11, 2020
2 parents d392983 + 0e43a27 commit e86f233
Show file tree
Hide file tree
Showing 4 changed files with 169 additions and 25 deletions.
15 changes: 12 additions & 3 deletions Assets/Cardboard/Scripts/CardboardLensDistortion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public static class CardboardLensDistortion

private static CardboardMesh leftEyeMesh;
private static CardboardMesh rightEyeMesh;
private static float eyeNearClip = 0.1f;
private static float eyeFarClip = 1000f;

private static float[] projectionMatrixLeft;
private static float[] projectionMatrixRight;
Expand Down Expand Up @@ -87,7 +89,8 @@ private static CardboardUv CardboardLensDistortion_undistortedUvForDistortedUv(
private static void CardboardLensDistortion_getProjectionMatrix(IntPtr lens_Distortion, CardboardEye eye,
float z_near, float z_far, float[] projection_matrix)
{
CardboardUtility.Matrix4x4ToArray(Matrix4x4.Perspective(70, 0.8f, 0.5f, 1000)).CopyTo(projection_matrix, 0);
CardboardUtility.Matrix4x4ToArray(
Matrix4x4.Perspective(70, 0.8f, z_near, z_far)).CopyTo(projection_matrix, 0);
}

private static void CardboardLensDistortion_getEyeFromHeadMatrix(IntPtr lens_Distortion, CardboardEye eye,
Expand Down Expand Up @@ -133,15 +136,21 @@ public static (CardboardMesh, CardboardMesh) GetEyeMeshes()
return (leftEyeMesh, rightEyeMesh);
}

public static void SetCamClip(float newNear, float newFar)
{
eyeNearClip = newNear;
eyeFarClip = newFar;
}

public static void RefreshProjectionMatrix()
{
projectionMatrixLeft = new float[16];
eyeFromHeadMatrixLeft = new float[16];
projectionMatrixRight = new float[16];
eyeFromHeadMatrixRight = new float[16];
CardboardLensDistortion_getProjectionMatrix(_lensDistortion, CardboardEye.kLeft, 0.1f, 100f,
CardboardLensDistortion_getProjectionMatrix(_lensDistortion, CardboardEye.kLeft, eyeNearClip, eyeFarClip,
projectionMatrixLeft);
CardboardLensDistortion_getProjectionMatrix(_lensDistortion, CardboardEye.kRight, 0.1f, 100f,
CardboardLensDistortion_getProjectionMatrix(_lensDistortion, CardboardEye.kRight, eyeNearClip, eyeFarClip,
projectionMatrixRight);

CardboardLensDistortion_getEyeFromHeadMatrix(_lensDistortion, CardboardEye.kLeft, eyeFromHeadMatrixLeft);
Expand Down
7 changes: 7 additions & 0 deletions Assets/Cardboard/Scripts/CardboardManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ public static void SetCardboardProfile(string url)
CardboardQrCode.SetCardboardProfile(url);
}

public static void SetCameraNearFarClip(float newNearClip, float newFarClip)
{
CardboardLensDistortion.SetCamClip(newNearClip, newFarClip);
if (initiated)
RefreshParameters();
}

//This method will set cardbaord profile ONLY when cardboard qr has not yet been scanned by camera.
//This behaviour is the same as the legacy cardboard gvr_set_default_viewer_profile
public static void SetCardboardInitialProfile(string url)
Expand Down
Loading

0 comments on commit e86f233

Please sign in to comment.