AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
CameraMotionInfo.cs
Go to the documentation of this file.
1 //
2 // Copyright (c) Microsoft Corporation. All rights reserved.
3 // Licensed under the MIT License. See LICENSE in the project root for license information.
4 //
5 using System.Collections;
6 using System.Collections.Generic;
7 using UnityEngine;
8 using HoloToolkit.Unity;
9 
10 namespace HoloToolkit.Unity.InputModule
11 {
15  public class CameraMotionInfo : Singleton<CameraMotionInfo>
16  {
17  #region Public accessors
18  public Vector3 HeadVelocity { get { return headVelocity; } }
19  public Vector3 MoveDirection { get { return headMoveDirection; } }
20 
21  [Tooltip("Minimum velicoty threshold")]
22  public float headVelIdleThresh = 0.5f;
23  [Tooltip("Maximum velicoty threshold")]
24  public float headVelMoveThresh = 2f;
25  #endregion
26 
27  #region Private members
28  private Vector3 headVelocity;
29  private Vector3 lastHeadPos;
30  private Vector3 newHeadMoveDirection;
31  private Vector3 headMoveDirection = Vector3.one;
32 
33  [SerializeField]
34  private bool debugDrawHeadVelocity = true;
35  [SerializeField]
36  private bool debugDrawHeadDirection = true;
37  #endregion
38 
39  private void FixedUpdate()
40  {
41  // Update headVelocity
42  Vector3 newHeadPos = CameraCache.Main.transform.position;
43  Vector3 headDelta = newHeadPos - lastHeadPos;
44 
45  float moveThreshold = 0.01f;
46  if (headDelta.sqrMagnitude < moveThreshold * moveThreshold)
47  {
48  headDelta = Vector3.zero;
49  }
50 
51  if (Time.fixedDeltaTime > 0)
52  {
53  float velAdjustRate = 3f * Time.fixedDeltaTime;
54  headVelocity = headVelocity * (1f - velAdjustRate) + headDelta * velAdjustRate / Time.fixedDeltaTime;
55 
56  float velThreshold = .1f;
57  if (headVelocity.sqrMagnitude < velThreshold * velThreshold)
58  {
59  headVelocity = Vector3.zero;
60  }
61  }
62 
63  // Update headDirection
64  float velP = Mathf.Clamp01(Mathf.InverseLerp(headVelIdleThresh, headVelMoveThresh, headVelocity.magnitude));
65 
66  newHeadMoveDirection = Vector3.Lerp(newHeadPos, headVelocity, velP).normalized;
67  lastHeadPos = newHeadPos;
68  float dirAdjustRate = Mathf.Clamp01(5f * Time.fixedDeltaTime);
69 
70  headMoveDirection = Vector3.Slerp(headMoveDirection, newHeadMoveDirection, dirAdjustRate);
71 
72  if(debugDrawHeadDirection)
73  {
74  Debug.DrawLine(lastHeadPos, lastHeadPos + headMoveDirection * 10f, Color.Lerp(Color.red, Color.green, velP));
75  }
76 
77  if(debugDrawHeadVelocity)
78  {
79  Debug.DrawLine(lastHeadPos, lastHeadPos + headVelocity, Color.yellow);
80  }
81  }
82  }
83 }
The purpose of this class is to provide a cached reference to the main camera. Calling Camera...
Definition: CameraCache.cs:12
static Camera Main
Returns a cached reference to the main camera and uses Camera.main if it hasn&#39;t been cached yet...
Definition: CameraCache.cs:20
CameraMotionInfo calculates the velocity and direction of the camera.
Singleton behaviour class, used for components that should only have one instance.
Definition: Singleton.cs:14