// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using UnityEngine;
namespace Microsoft.MixedReality.Toolkit.Utilities
{
///
/// Component to animate and visualize a sphere that can be used with
/// per pixel based clipping.
///
[ExecuteInEditMode]
[AddComponentMenu("Scripts/MRTK/Core/ClippingSphere")]
public class ClippingSphere : ClippingPrimitive
{
///
/// The radius of the clipping sphere, which is determined by the largest axis of the transform's scale.
///
public float Radius
{
get
{
Vector3 lossyScale = transform.lossyScale * 0.5f;
return Mathf.Max(Mathf.Max(lossyScale.x, lossyScale.y), lossyScale.z);
}
}
///
/// The property name of the clip sphere data within the shader.
///
protected int clipSphereID;
///
protected override string Keyword
{
get { return "_CLIPPING_SPHERE"; }
}
///
protected override string ClippingSideProperty
{
get { return "_ClipSphereSide"; }
}
///
/// Renders a visual representation of the clipping primitive when selected.
///
protected void OnDrawGizmosSelected()
{
if (enabled)
{
Gizmos.DrawWireSphere(transform.position, Radius);
}
}
///
protected override void Initialize()
{
base.Initialize();
clipSphereID = Shader.PropertyToID("_ClipSphere");
}
///
protected override void UpdateShaderProperties(MaterialPropertyBlock materialPropertyBlock)
{
Vector3 position = transform.position;
Vector4 sphere = new Vector4(position.x, position.y, position.z, Radius);
materialPropertyBlock.SetVector(clipSphereID, sphere);
}
}
}