forked from microsoft/MixedRealityToolkit-Unity
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCameraExtensions.cs
More file actions
40 lines (34 loc) · 1.58 KB
/
Copy pathCameraExtensions.cs
File metadata and controls
40 lines (34 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
using UnityEngine;
namespace HoloToolkit.Unity
{
public static class CameraExtensions
{
/// <summary>
/// Get the horizontal FOV from the stereo camera
/// </summary>
/// <returns></returns>
public static float GetHorizontalFieldOfViewRadians(this Camera camera)
{
float horizontalFovRadians = 2f * Mathf.Atan(Mathf.Tan(camera.fieldOfView * Mathf.Deg2Rad * 0.5f) * camera.aspect);
return horizontalFovRadians;
}
/// <summary>
/// Returns if a point will be rendered on the screen in either eye
/// </summary>
/// <param name="camera">The camera to check the point against</param>
/// <param name="position"></param>
/// <returns></returns>
public static bool IsInFOV(this Camera camera, Vector3 position)
{
float verticalFovHalf = camera.fieldOfView * 0.5f;
float horizontalFovHalf = camera.GetHorizontalFieldOfViewRadians() * Mathf.Rad2Deg * 0.5f;
Vector3 deltaPos = position - camera.transform.position;
Vector3 headDeltaPos = MathUtils.TransformDirectionFromTo(null, camera.transform, deltaPos).normalized;
float yaw = Mathf.Asin(headDeltaPos.x) * Mathf.Rad2Deg;
float pitch = Mathf.Asin(headDeltaPos.y) * Mathf.Rad2Deg;
return (Mathf.Abs(yaw) < horizontalFovHalf && Mathf.Abs(pitch) < verticalFovHalf);
}
}
}