forked from microsoft/MixedRealityToolkit-Unity
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNearPlaneFade.cs
More file actions
55 lines (44 loc) · 1.59 KB
/
NearPlaneFade.cs
File metadata and controls
55 lines (44 loc) · 1.59 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// 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
{
/// <summary>
/// Updates the shader parameters for use in near plade fading.
/// </summary>
[ExecuteInEditMode]
public class NearPlaneFade : MonoBehaviour
{
public float FadeDistanceStart = 0.85f;
public float FadeDistanceEnd = 0.5f;
public bool NearPlaneFadeOn = true;
private const string FadeKeywordOn = "_NEAR_PLANE_FADE_ON";
private int fadeDistancePropertyID;
private void Start()
{
fadeDistancePropertyID = Shader.PropertyToID("_NearPlaneFadeDistance");
UpdateShaderParams();
}
private void OnValidate()
{
FadeDistanceStart = Mathf.Max(FadeDistanceStart, 0);
FadeDistanceEnd = Mathf.Max(FadeDistanceEnd, 0);
FadeDistanceStart = Mathf.Max(FadeDistanceStart, FadeDistanceEnd);
UpdateShaderParams();
}
private void UpdateShaderParams()
{
float rangeInverse = 1.0f / (FadeDistanceStart - FadeDistanceEnd);
var fadeDist = new Vector4(-FadeDistanceEnd * rangeInverse, rangeInverse, 0, 0);
Shader.SetGlobalVector(fadeDistancePropertyID, fadeDist);
if (NearPlaneFadeOn)
{
Shader.EnableKeyword(FadeKeywordOn);
}
else
{
Shader.DisableKeyword(FadeKeywordOn);
}
}
}
}