AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
NearPlaneFade.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 {
11  [ExecuteInEditMode]
12  public class NearPlaneFade : MonoBehaviour
13  {
14  public float FadeDistanceStart = 0.85f;
15  public float FadeDistanceEnd = 0.5f;
16 
17  public bool NearPlaneFadeOn = true;
18 
19  private const string FadeKeywordOn = "_NEAR_PLANE_FADE_ON";
20 
21  private int fadeDistancePropertyID;
22 
23  private void Awake()
24  {
25  fadeDistancePropertyID = Shader.PropertyToID("_NearPlaneFadeDistance");
26  UpdateShaderParams();
27  }
28 
29  private void OnValidate()
30  {
31  UpdateShaderParams();
32  }
33 
34  private void UpdateShaderParams()
35  {
36  FadeDistanceStart = Mathf.Max(FadeDistanceStart, 0);
37  FadeDistanceEnd = Mathf.Max(FadeDistanceEnd, 0);
38  FadeDistanceStart = Mathf.Max(FadeDistanceStart, FadeDistanceEnd);
39 
40  if (FadeDistanceStart != FadeDistanceEnd)
41  {
42  float rangeInverse = 1.0f / (FadeDistanceStart - FadeDistanceEnd);
43  var fadeDist = new Vector4(-FadeDistanceEnd * rangeInverse, rangeInverse, 0, 0);
44  Shader.SetGlobalVector(fadeDistancePropertyID, fadeDist);
45  }
46 
47  if (NearPlaneFadeOn)
48  {
49  Shader.EnableKeyword(FadeKeywordOn);
50  }
51  else
52  {
53  Shader.DisableKeyword(FadeKeywordOn);
54  }
55  }
56  }
57 }
Sets global shader variables relating to near plane fade