Tanoda
VisualizerManager.cs
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (C) Ultraleap, Inc. 2011-2020. *
3 * *
4 * Use subject to the terms of the Apache License 2.0 available at *
5 * http://www.apache.org/licenses/LICENSE-2.0, or another agreement *
6 * between Ultraleap and you, your company or other organization. *
7 ******************************************************************************/
8
9using UnityEngine;
10using UnityEngine.SceneManagement;
11using Leap;
12using System;
13
15 public class VisualizerManager : MonoBehaviour {
16 public GameObject m_PCVisualizer = null;
17 public GameObject m_VRVisualizer = null;
22
23 public KeyCode keyToSwitchViewMode = KeyCode.V;
24
25 private Controller m_controller = null;
26 private bool m_leapConnected = false;
27
28 private SmoothedFloat m_deltaTime;
29 private int m_framrateUpdateCount = 0;
30 private int m_framerateUpdateInterval = 30;
31
32 private const bool m_startInScreenTopViewMode = false;
33
34 private void FindController() {
35 LeapServiceProvider provider = FindObjectOfType<LeapServiceProvider>();
36 if (provider != null)
37 m_controller = provider.GetLeapController();
38 }
39
40 private void goVR() {
41 m_PCVisualizer.gameObject.SetActive(false);
42
43 var provider = m_VRVisualizer.gameObject
44 .GetComponentInChildren<LeapXRServiceProvider>();
45 m_VRVisualizer.gameObject.GetComponentInChildren<HandModelManager>()
46 .leapProvider = provider;
47 m_VRVisualizer.gameObject.SetActive(true);
48
49
50 m_warningText.text = "Please put on your head-mounted display";
51 }
52
53 private void goDesktop() {
54 m_PCVisualizer.gameObject.SetActive(true);
55 m_VRVisualizer.gameObject.SetActive(false);
56 m_warningText.text = "No head-mounted display detected.";
57 }
58
59 private void goScreenTop()
60 {
61 m_PCVisualizer.gameObject.SetActive(true);
62 m_VRVisualizer.gameObject.SetActive(false);
63 m_warningText.text = "ScreenTop tracking mode activated";
64 m_controller.SetPolicy(Controller.PolicyFlag.POLICY_OPTIMIZE_SCREENTOP);
65 }
66
67 void Start()
68 {
69 m_trackingText.text = "";
70 FindController();
71 if (m_controller != null)
72 m_leapConnected = m_controller.IsConnected;
73
74 if (m_startInScreenTopViewMode)
75 {
76 Screen.SetResolution(Screen.currentResolution.width, Screen.currentResolution.height, false);
77 goScreenTop();
78 }
79 else if (XRSupportUtil.IsXRDevicePresent())
80 {
81 Screen.SetResolution(640, 480, false);
82 goVR();
83 }
84 else
85 {
86 Screen.SetResolution(Screen.currentResolution.width, Screen.currentResolution.height, false);
87 goDesktop();
88 }
89
90 m_deltaTime = new SmoothedFloat();
91 m_deltaTime.delay = 0.1f;
92 }
93
94 void Update()
95 {
96 if (m_controller == null)
97 {
98 FindController();
99 return;
100 }
101
102 m_leapConnected = m_controller.IsConnected;
103 if (!m_leapConnected)
104 {
105 m_trackingText.text = "";
106 return;
107 }
108
109 if (m_controller.IsPolicySet(Controller.PolicyFlag.POLICY_OPTIMIZE_SCREENTOP))
110 {
111 m_trackingText.text = String.Format(
112 "Tracking Mode: Screen-Top (Press '{0}' to switch to desktop mode)",
114 if (Input.GetKeyDown(keyToSwitchViewMode))
115 {
116 m_controller.ClearPolicy(Controller.PolicyFlag.POLICY_OPTIMIZE_SCREENTOP);
117 m_controller.ClearPolicy(Controller.PolicyFlag.POLICY_OPTIMIZE_HMD);
118 }
119 }
120 else if (m_controller.IsPolicySet(Controller.PolicyFlag.POLICY_OPTIMIZE_HMD))
121 {
122 m_trackingText.text = String.Format(
123 "Tracking Mode: Head-Mounted (Press '{0}' to switch to screen-top mode)",
125 if (Input.GetKeyDown(keyToSwitchViewMode))
126 {
127 m_controller.ClearPolicy(Controller.PolicyFlag.POLICY_OPTIMIZE_HMD);
128 m_controller.SetPolicy(Controller.PolicyFlag.POLICY_OPTIMIZE_SCREENTOP);
129 }
130 }
131 else
132 {
133 m_trackingText.text = String.Format(
134 "Tracking Mode: Desktop (Press '{0}' to switch to head-mounted mode)",
136 if (Input.GetKeyDown(keyToSwitchViewMode))
137 {
138 m_controller.ClearPolicy(Controller.PolicyFlag.POLICY_OPTIMIZE_SCREENTOP);
139 m_controller.SetPolicy(Controller.PolicyFlag.POLICY_OPTIMIZE_HMD);
140 }
141 }
142
143 //update render frame display
144 m_deltaTime.Update(Time.deltaTime, Time.deltaTime);
145 if (m_framrateUpdateCount > m_framerateUpdateInterval) {
146 updateRenderFrameRate();
147 m_framrateUpdateCount = 0;
148 }
149 m_framrateUpdateCount++;
150 }
151
152 private void updateRenderFrameRate() {
153 float msec = m_deltaTime.value * 1000.0f;
154 float fps = 1.0f / m_deltaTime.value;
155 string text = string.Format("{0:0.0} ms ({1:0.} fps)", msec, fps);
156 m_frameRateText.text = "Render Time: " + text;
157 m_dataFrameRateText.text = "Data Framerate: " + m_controller.Frame().CurrentFramesPerSecond;
158 }
159 }
160}
The Controller class is your main interface to the Leap Motion Controller.
void ClearPolicy(PolicyFlag policy)
Requests clearing a policy.
void SetPolicy(PolicyFlag policy)
Requests setting a policy.
bool IsConnected
Reports whether this Controller is connected to the Leap Motion service and the Leap Motion hardware ...
Frame Frame(int history=0)
In most cases you should get Frame objects using the LeapProvider.CurrentFrame property....
bool IsPolicySet(PolicyFlag policy)
Gets the active setting for a specific policy.
float CurrentFramesPerSecond
The instantaneous framerate.
Definition: Frame.cs:148
The HandModelManager manages a pool of HandModelBases and makes HandRepresentations when a it detects...
The LeapServiceProvider provides tracked Leap Hand data and images from the device via the Leap servi...
Controller GetLeapController()
Returns the Leap Controller instance.
The LeapXRServiceProvider expands on the standard LeapServiceProvider to account for the offset of th...
Time-step independent exponential smoothing.
float Update(float input, float deltaTime=1f)