AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
ClipPlane.cs
Go to the documentation of this file.
1 // Copyright (c) Microsoft Corporation. All rights reserved.
2 // Licensed under the MIT License.
3 
4 using UnityEngine;
5 
6 namespace HoloToolkit.Unity
7 {
12  [ExecuteInEditMode]
13  public class ClipPlane : MonoBehaviour
14  {
15  [SerializeField]
16  private Renderer[] renderers = null;
17 
18  private int clipPlaneID;
19  private Material[] materials;
20  private MaterialPropertyBlock materialPropertyBlock;
21 
22  private void OnEnable()
23  {
24  Initialize();
25  UpdatePlanePosition();
26  ToggleClippingPlane(true);
27  }
28 
29  private void OnDisable()
30  {
31  UpdatePlanePosition();
32  ToggleClippingPlane(false);
33  }
34 
35 #if UNITY_EDITOR
36  private void Update()
37  {
38  if (Application.isPlaying)
39  {
40  return;
41  }
42 
43  Initialize();
44  UpdatePlanePosition();
45  }
46 #endif
47 
48  private void LateUpdate()
49  {
50  UpdatePlanePosition();
51  }
52 
53  private void OnDrawGizmosSelected()
54  {
55  if (!enabled)
56  {
57  return;
58  }
59 
60  Gizmos.matrix = transform.localToWorldMatrix;
61  Gizmos.DrawLine(Vector3.zero, Vector3.up * 0.5f);
62  Gizmos.DrawWireCube(Vector3.zero, new Vector3(1.0f, 0.0f, 1.0f));
63  }
64 
65  private void OnDestroy()
66  {
67  if (materials != null)
68  {
69  foreach (Material material in materials)
70  {
71  if (Application.isPlaying)
72  {
73  Destroy(material);
74  }
75  }
76 
77  materials = null;
78  }
79  }
80 
81  private void Initialize()
82  {
83  clipPlaneID = Shader.PropertyToID("_ClipPlane");
84 
85  materials = new Material[renderers.Length];
86 
87  for (int i = 0; i < renderers.Length; ++i)
88  {
89  if (Application.isPlaying)
90  {
91  materials[i] = renderers[i].material;
92  }
93  else
94  {
95  materials[i] = renderers[i].sharedMaterial;
96  }
97  }
98 
99  materialPropertyBlock = new MaterialPropertyBlock();
100  }
101 
102  private void UpdatePlanePosition()
103  {
104  if (renderers == null)
105  {
106  return;
107  }
108 
109  Vector3 up = transform.up;
110  Vector4 plane = new Vector4(up.x, up.y, up.z, Vector3.Dot(up, transform.position));
111 
112  foreach (Renderer renderer in renderers)
113  {
114  if (renderer == null)
115  {
116  continue;
117  }
118 
119  renderer.GetPropertyBlock(materialPropertyBlock);
120  materialPropertyBlock.SetVector(clipPlaneID, plane);
121  renderer.SetPropertyBlock(materialPropertyBlock);
122  }
123  }
124 
125  private void ToggleClippingPlane(bool isClippingPlaneOn)
126  {
127  if (materials == null)
128  {
129  return;
130  }
131 
132  foreach (Material material in materials)
133  {
134  if (material == null)
135  {
136  continue;
137  }
138 
139  const string clippingPlaneKeyword = "_CLIPPING_PLANE";
140 
141  if (isClippingPlaneOn)
142  {
143  if (!material.IsKeywordEnabled(clippingPlaneKeyword))
144  {
145  material.EnableKeyword(clippingPlaneKeyword);
146  }
147  }
148  else
149  {
150  if (material.IsKeywordEnabled(clippingPlaneKeyword))
151  {
152  material.DisableKeyword(clippingPlaneKeyword);
153  }
154  }
155  }
156  }
157  }
158 }
Utility component to animate and visualize a clipping plane that can be used with the "MixedRealityTo...
Definition: ClipPlane.cs:13