AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
AdaptiveViewport.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 using System;
4 using UnityEngine;
5 
6 namespace HoloToolkit.Unity
7 {
14 
15  public class AdaptiveViewport : MonoBehaviour
16  {
17  [SerializeField]
18  [Tooltip("The quality level where the viewport will be at full size.")]
19  private int FullSizeQualityLevel = 5;
20 
21  [SerializeField]
22  [Tooltip("The quality level where the viewport will be at Min Viewport Size.")]
23  private int MinSizeQualityLevel = -5;
24 
25  [SerializeField]
26  [Tooltip("Percentage size of viewport when quality is at Min Size Quality Level.")]
27  private float MinViewportSize = 0.5f;
28 
29  [SerializeField]
30  private AdaptiveQuality qualityController = null;
31 
32  public float CurrentScale { get; private set; }
33 
34  private void OnEnable()
35  {
36  CurrentScale = 1.0f;
37 
38  Debug.Assert(qualityController != null, "The AdpativeViewport needs a connection to a AdaptiveQuality component.");
39 
40  //Register our callback to the AdaptiveQuality component
41  if (qualityController)
42  {
43  qualityController.QualityChanged += QualityChangedEvent;
44  SetScaleFromQuality(qualityController.QualityLevel);
45  }
46  }
47 
48  private void OnDisable()
49  {
50  if (qualityController)
51  {
52  qualityController.QualityChanged -= QualityChangedEvent;
53  }
54 
55 #if UNITY_2017_2_OR_NEWER
56  UnityEngine.XR.XRSettings.renderViewportScale = 1.0f;
57 #else
58  UnityEngine.VR.VRSettings.renderViewportScale = 1.0f;
59 #endif
60  }
61 
62  protected void OnPreCull()
63  {
64 #if UNITY_2017_2_OR_NEWER
65  UnityEngine.XR.XRSettings.renderViewportScale = CurrentScale;
66 #else
67  UnityEngine.VR.VRSettings.renderViewportScale = CurrentScale;
68 #endif
69  }
70 
71  private void QualityChangedEvent(int newQuality, int previousQuality)
72  {
73  SetScaleFromQuality(newQuality);
74  }
75 
76  private void SetScaleFromQuality(int quality)
77  {
78  //Clamp the quality to our min and max
79  int clampedQuality = Mathf.Clamp(quality, MinSizeQualityLevel, FullSizeQualityLevel);
80 
81  //Calculate our new scale value based on quality
82  float lerpVal = Mathf.InverseLerp(MinSizeQualityLevel, FullSizeQualityLevel, clampedQuality);
83  CurrentScale = Mathf.Lerp(MinViewportSize, 1.0f, lerpVal);
84  }
85  }
86 }
Main components for controlling the quality of the system to maintain a steady frame rate...
Changes the VR viewport to correlate to the requested quality, trying to maintain a steady frame rate...
QualityChangedEvent QualityChanged