// Copyright (c) Microsoft Corporation. // Licensed under the MIT License. using UnityEngine; namespace Microsoft.MixedReality.Toolkit.Utilities { /// /// Component to animate and visualize a plane that can be used with /// per pixel based clipping. /// [ExecuteInEditMode] [AddComponentMenu("Scripts/MRTK/Core/ClippingPlane")] public class ClippingPlane : ClippingPrimitive { /// /// The property name of the clip plane data within the shader. /// protected int clipPlaneID; private Vector4 clipPlane; /// protected override string Keyword { get { return "_CLIPPING_PLANE"; } } /// protected override string ClippingSideProperty { get { return "_ClipPlaneSide"; } } /// /// Renders a visual representation of the clipping primitive when selected. /// protected void OnDrawGizmosSelected() { if (enabled) { Gizmos.matrix = transform.localToWorldMatrix; Gizmos.DrawWireCube(Vector3.zero, new Vector3(1.0f, 0.0f, 1.0f)); Gizmos.DrawLine(Vector3.zero, Vector3.up * -0.5f); } } /// protected override void Initialize() { base.Initialize(); clipPlaneID = Shader.PropertyToID("_ClipPlane"); } protected override void BeginUpdateShaderProperties() { Vector3 up = transform.up; clipPlane = new Vector4(up.x, up.y, up.z, Vector3.Dot(up, transform.position)); base.BeginUpdateShaderProperties(); } /// protected override void UpdateShaderProperties(MaterialPropertyBlock materialPropertyBlock) { materialPropertyBlock.SetVector(clipPlaneID, clipPlane); } } }