AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
CameraExtensions.cs
Go to the documentation of this file.
1 // Copyright (c) Microsoft Corporation. All rights reserved.
2 // Licensed under the MIT License. See LICENSE in the project root for license information.
3 
4 using UnityEngine;
5 
6 namespace HoloToolkit.Unity
7 {
8  public static class CameraExtensions
9  {
14  public static float GetHorizontalFieldOfViewRadians(this Camera camera)
15  {
16  return 2f * Mathf.Atan(Mathf.Tan(camera.fieldOfView * Mathf.Deg2Rad * 0.5f) * camera.aspect);
17  }
18 
25  public static bool IsInFOV(this Camera camera, Vector3 position)
26  {
27  float verticalFovHalf = camera.fieldOfView * 0.5f;
28  float horizontalFovHalf = camera.GetHorizontalFieldOfViewRadians() * Mathf.Rad2Deg * 0.5f;
29 
30  Vector3 deltaPos = position - camera.transform.position;
31  Vector3 headDeltaPos = MathUtils.TransformDirectionFromTo(null, camera.transform, deltaPos).normalized;
32 
33  float yaw = Mathf.Asin(headDeltaPos.x) * Mathf.Rad2Deg;
34  float pitch = Mathf.Asin(headDeltaPos.y) * Mathf.Rad2Deg;
35 
36  return (Mathf.Abs(yaw) < horizontalFovHalf && Mathf.Abs(pitch) < verticalFovHalf);
37  }
38  }
39 }
static Vector3 TransformDirectionFromTo(Transform from, Transform to, Vector3 dirInFrom)
Takes a direction in the coordinate space specified by the "from" transform and transforms it to be t...
Definition: MathUtils.cs:59
static float GetHorizontalFieldOfViewRadians(this Camera camera)
Get the horizontal FOV from the stereo camera
static bool IsInFOV(this Camera camera, Vector3 position)
Returns if a point will be rendered on the screen in either eye
Math Utilities class.
Definition: MathUtils.cs:13