2020-05-11 15:34:22 +08:00
|
|
|
|
using System.Collections.Generic;
|
2018-11-20 07:49:14 +08:00
|
|
|
|
using UnityEngine;
|
|
|
|
|
using UnityEngine.Rendering;
|
|
|
|
|
using UnityEngine.UI;
|
2019-01-26 20:30:41 +08:00
|
|
|
|
using MaskIntr = UnityEngine.SpriteMaskInteraction;
|
2018-11-20 07:49:14 +08:00
|
|
|
|
|
2020-05-11 15:34:22 +08:00
|
|
|
|
namespace Coffee.UISoftMask
|
2018-11-20 07:49:14 +08:00
|
|
|
|
{
|
2020-05-11 15:34:22 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Soft maskable.
|
|
|
|
|
/// Add this component to Graphic under SoftMask for smooth masking.
|
|
|
|
|
/// </summary>
|
2019-08-27 10:39:01 +08:00
|
|
|
|
#if UNITY_2018_3_OR_NEWER
|
2020-05-11 15:34:22 +08:00
|
|
|
|
[ExecuteAlways]
|
2019-08-27 10:39:01 +08:00
|
|
|
|
#else
|
2018-11-20 07:49:14 +08:00
|
|
|
|
[ExecuteInEditMode]
|
2019-08-27 10:39:01 +08:00
|
|
|
|
# endif
|
2020-06-04 12:43:17 +08:00
|
|
|
|
[RequireComponent(typeof(Graphic))]
|
2020-05-11 15:34:22 +08:00
|
|
|
|
public class SoftMaskable : MonoBehaviour, IMaterialModifier, ICanvasRaycastFilter
|
|
|
|
|
#if UNITY_EDITOR
|
|
|
|
|
, ISerializationCallbackReceiver
|
|
|
|
|
# endif
|
|
|
|
|
{
|
2020-06-04 12:43:17 +08:00
|
|
|
|
private const int kVisibleInside = (1 << 0) + (1 << 2) + (1 << 4) + (1 << 6);
|
|
|
|
|
private const int kVisibleOutside = (2 << 0) + (2 << 2) + (2 << 4) + (2 << 6);
|
|
|
|
|
private static readonly Hash128 k_InvalidHash = new Hash128();
|
2020-05-11 15:34:22 +08:00
|
|
|
|
|
2020-06-04 12:43:17 +08:00
|
|
|
|
private static int s_SoftMaskTexId;
|
|
|
|
|
private static int s_StencilCompId;
|
|
|
|
|
private static int s_MaskInteractionId;
|
|
|
|
|
private static List<SoftMaskable> s_ActiveSoftMaskables;
|
|
|
|
|
private static int[] s_Interactions = new int[4];
|
2020-05-11 15:34:22 +08:00
|
|
|
|
|
2020-06-04 12:43:17 +08:00
|
|
|
|
[SerializeField, HideInInspector, System.Obsolete]
|
|
|
|
|
private bool m_Inverse;
|
2020-05-11 15:34:22 +08:00
|
|
|
|
|
2020-06-04 12:43:17 +08:00
|
|
|
|
[SerializeField, Tooltip("The interaction for each masks."), HideInInspector]
|
|
|
|
|
private int m_MaskInteraction = kVisibleInside;
|
2020-05-11 15:34:22 +08:00
|
|
|
|
|
2020-06-04 12:43:17 +08:00
|
|
|
|
[SerializeField, Tooltip("Use stencil to mask.")]
|
2020-09-08 10:33:57 +08:00
|
|
|
|
private bool m_UseStencil = true;
|
2020-05-11 15:34:22 +08:00
|
|
|
|
|
2020-06-04 12:43:17 +08:00
|
|
|
|
[SerializeField, Tooltip("Use soft-masked raycast target.\n\nNote: This option is expensive.")]
|
|
|
|
|
private bool m_RaycastFilter;
|
2020-05-11 15:34:22 +08:00
|
|
|
|
|
2020-06-04 12:43:17 +08:00
|
|
|
|
private Graphic _graphic;
|
|
|
|
|
private SoftMask _softMask;
|
|
|
|
|
private Hash128 _effectMaterialHash;
|
2020-05-11 15:34:22 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The graphic will be visible only in areas where no mask is present.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public bool inverse
|
|
|
|
|
{
|
|
|
|
|
get { return m_MaskInteraction == kVisibleOutside; }
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
var intValue = value ? kVisibleOutside : kVisibleInside;
|
|
|
|
|
if (m_MaskInteraction == intValue) return;
|
|
|
|
|
m_MaskInteraction = intValue;
|
|
|
|
|
graphic.SetMaterialDirtyEx();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Use soft-masked raycast target. This option is expensive.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public bool raycastFilter
|
|
|
|
|
{
|
|
|
|
|
get { return m_RaycastFilter; }
|
|
|
|
|
set { m_RaycastFilter = value; }
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-07 12:19:49 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Use stencil to mask.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public bool useStencil
|
|
|
|
|
{
|
|
|
|
|
get { return m_UseStencil; }
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (m_UseStencil == value) return;
|
|
|
|
|
m_UseStencil = value;
|
|
|
|
|
graphic.SetMaterialDirtyEx();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-11 15:34:22 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The graphic associated with the soft mask.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public Graphic graphic
|
|
|
|
|
{
|
|
|
|
|
get { return _graphic ? _graphic : _graphic = GetComponent<Graphic>(); }
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-04 12:43:17 +08:00
|
|
|
|
public SoftMask softMask
|
|
|
|
|
{
|
|
|
|
|
get { return _softMask ? _softMask : _softMask = this.GetComponentInParentEx<SoftMask>(); }
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-11 15:34:22 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Perform material modification in this function.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>Modified material.</returns>
|
|
|
|
|
/// <param name="baseMaterial">Configured Material.</param>
|
|
|
|
|
Material IMaterialModifier.GetModifiedMaterial(Material baseMaterial)
|
|
|
|
|
{
|
|
|
|
|
_softMask = null;
|
|
|
|
|
|
|
|
|
|
// If this component is disabled, the material is returned as is.
|
|
|
|
|
// If the parents do not have a soft mask component, the material is returned as is.
|
2020-09-27 17:46:58 +08:00
|
|
|
|
if (!isActiveAndEnabled || !softMask)
|
|
|
|
|
{
|
|
|
|
|
MaterialCache.Unregister(_effectMaterialHash);
|
|
|
|
|
_effectMaterialHash = k_InvalidHash;
|
|
|
|
|
return baseMaterial;
|
|
|
|
|
}
|
2020-05-11 15:34:22 +08:00
|
|
|
|
|
|
|
|
|
// Generate soft maskable material.
|
2020-09-27 17:46:58 +08:00
|
|
|
|
var previousHash = _effectMaterialHash;
|
2020-05-11 15:34:22 +08:00
|
|
|
|
_effectMaterialHash = new Hash128(
|
|
|
|
|
(uint) baseMaterial.GetInstanceID(),
|
2020-06-04 12:43:17 +08:00
|
|
|
|
(uint) softMask.GetInstanceID(),
|
2020-05-11 15:34:22 +08:00
|
|
|
|
(uint) m_MaskInteraction,
|
|
|
|
|
(uint) (m_UseStencil ? 1 : 0)
|
|
|
|
|
);
|
2018-11-20 07:49:14 +08:00
|
|
|
|
|
2020-05-11 15:34:22 +08:00
|
|
|
|
// Generate soft maskable material.
|
|
|
|
|
var modifiedMaterial = MaterialCache.Register(baseMaterial, _effectMaterialHash, mat =>
|
|
|
|
|
{
|
|
|
|
|
mat.shader = Shader.Find(string.Format("Hidden/{0} (SoftMaskable)", mat.shader.name));
|
2020-05-03 19:38:06 +08:00
|
|
|
|
#if UNITY_EDITOR
|
2020-05-11 15:34:22 +08:00
|
|
|
|
mat.EnableKeyword("SOFTMASK_EDITOR");
|
2020-05-03 19:38:06 +08:00
|
|
|
|
#endif
|
2020-06-04 12:43:17 +08:00
|
|
|
|
mat.SetTexture(s_SoftMaskTexId, softMask.softMaskBuffer);
|
2020-06-09 13:41:36 +08:00
|
|
|
|
mat.SetInt(s_StencilCompId, m_UseStencil ? (int) CompareFunction.Equal : (int) CompareFunction.Always);
|
2020-06-07 12:19:49 +08:00
|
|
|
|
|
|
|
|
|
var root = MaskUtilities.FindRootSortOverrideCanvas(transform);
|
|
|
|
|
var stencil = MaskUtilities.GetStencilDepth(transform, root);
|
2020-05-11 15:34:22 +08:00
|
|
|
|
mat.SetVector(s_MaskInteractionId, new Vector4(
|
2020-06-07 12:19:49 +08:00
|
|
|
|
1 <= stencil ? (m_MaskInteraction >> 0 & 0x3) : 0,
|
|
|
|
|
2 <= stencil ? (m_MaskInteraction >> 2 & 0x3) : 0,
|
|
|
|
|
3 <= stencil ? (m_MaskInteraction >> 4 & 0x3) : 0,
|
|
|
|
|
4 <= stencil ? (m_MaskInteraction >> 6 & 0x3) : 0
|
2020-05-11 15:34:22 +08:00
|
|
|
|
));
|
|
|
|
|
});
|
|
|
|
|
|
2020-09-27 17:46:58 +08:00
|
|
|
|
// Unregister the previous material.
|
|
|
|
|
MaterialCache.Unregister(previousHash);
|
|
|
|
|
|
2020-05-11 15:34:22 +08:00
|
|
|
|
return modifiedMaterial;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Given a point and a camera is the raycast valid.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>Valid.</returns>
|
|
|
|
|
/// <param name="sp">Screen position.</param>
|
|
|
|
|
/// <param name="eventCamera">Raycast camera.</param>
|
|
|
|
|
bool ICanvasRaycastFilter.IsRaycastLocationValid(Vector2 sp, Camera eventCamera)
|
|
|
|
|
{
|
2020-06-04 12:43:17 +08:00
|
|
|
|
if (!isActiveAndEnabled || !softMask)
|
2020-05-11 15:34:22 +08:00
|
|
|
|
return true;
|
|
|
|
|
if (!RectTransformUtility.RectangleContainsScreenPoint(transform as RectTransform, sp, eventCamera))
|
|
|
|
|
return false;
|
|
|
|
|
|
2020-06-04 12:43:17 +08:00
|
|
|
|
if (m_RaycastFilter)
|
2020-05-11 15:34:22 +08:00
|
|
|
|
{
|
2020-06-04 12:43:17 +08:00
|
|
|
|
var sm = _softMask;
|
|
|
|
|
for (var i = 0; i < 4; i++)
|
|
|
|
|
{
|
|
|
|
|
s_Interactions[i] = sm ? ((m_MaskInteraction >> i * 2) & 0x3) : 0;
|
|
|
|
|
sm = sm ? sm.parent : null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return _softMask.IsRaycastLocationValid(sp, eventCamera, graphic, s_Interactions);
|
2020-05-11 15:34:22 +08:00
|
|
|
|
}
|
2020-06-04 12:43:17 +08:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var sm = _softMask;
|
|
|
|
|
for (var i = 0; i < 4; i++)
|
|
|
|
|
{
|
|
|
|
|
if (!sm) break;
|
2020-06-09 13:41:36 +08:00
|
|
|
|
|
2020-06-04 12:43:17 +08:00
|
|
|
|
s_Interactions[i] = sm ? ((m_MaskInteraction >> i * 2) & 0x3) : 0;
|
|
|
|
|
var interaction = s_Interactions[i] == 1;
|
2020-06-09 13:41:36 +08:00
|
|
|
|
var rt = sm.transform as RectTransform;
|
|
|
|
|
var inRect = RectTransformUtility.RectangleContainsScreenPoint(rt, sp, eventCamera);
|
2020-06-04 12:43:17 +08:00
|
|
|
|
if (!sm.ignoreSelfGraphic && interaction != inRect) return false;
|
|
|
|
|
|
|
|
|
|
foreach (var child in sm._children)
|
|
|
|
|
{
|
2020-06-09 13:41:36 +08:00
|
|
|
|
if (!child) continue;
|
|
|
|
|
|
|
|
|
|
var childRt = child.transform as RectTransform;
|
|
|
|
|
var inRectChild = RectTransformUtility.RectangleContainsScreenPoint(childRt, sp, eventCamera);
|
2020-06-04 12:43:17 +08:00
|
|
|
|
if (!child.ignoreSelfGraphic && interaction != inRectChild) return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sm = sm ? sm.parent : null;
|
|
|
|
|
}
|
2020-05-11 15:34:22 +08:00
|
|
|
|
|
2020-06-04 12:43:17 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
2020-05-11 15:34:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Set the interaction for each mask.
|
|
|
|
|
/// </summary>
|
2020-06-09 13:41:36 +08:00
|
|
|
|
public void SetMaskInteraction(MaskIntr intr)
|
2020-05-11 15:34:22 +08:00
|
|
|
|
{
|
|
|
|
|
SetMaskInteraction(intr, intr, intr, intr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Set the interaction for each mask.
|
|
|
|
|
/// </summary>
|
2020-06-09 13:41:36 +08:00
|
|
|
|
public void SetMaskInteraction(MaskIntr layer0, MaskIntr layer1, MaskIntr layer2, MaskIntr layer3)
|
2020-05-11 15:34:22 +08:00
|
|
|
|
{
|
|
|
|
|
m_MaskInteraction = (int) layer0 + ((int) layer1 << 2) + ((int) layer2 << 4) + ((int) layer3 << 6);
|
|
|
|
|
graphic.SetMaterialDirtyEx();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// This function is called when the object becomes enabled and active.
|
|
|
|
|
/// </summary>
|
|
|
|
|
private void OnEnable()
|
|
|
|
|
{
|
|
|
|
|
// Register.
|
|
|
|
|
if (s_ActiveSoftMaskables == null)
|
|
|
|
|
{
|
|
|
|
|
s_ActiveSoftMaskables = new List<SoftMaskable>();
|
|
|
|
|
|
|
|
|
|
s_SoftMaskTexId = Shader.PropertyToID("_SoftMaskTex");
|
|
|
|
|
s_StencilCompId = Shader.PropertyToID("_StencilComp");
|
|
|
|
|
s_MaskInteractionId = Shader.PropertyToID("_MaskInteraction");
|
|
|
|
|
}
|
2018-11-20 07:49:14 +08:00
|
|
|
|
|
2020-05-11 15:34:22 +08:00
|
|
|
|
s_ActiveSoftMaskables.Add(this);
|
|
|
|
|
|
|
|
|
|
graphic.SetMaterialDirtyEx();
|
|
|
|
|
_softMask = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// This function is called when the behaviour becomes disabled.
|
|
|
|
|
/// </summary>
|
|
|
|
|
private void OnDisable()
|
|
|
|
|
{
|
|
|
|
|
s_ActiveSoftMaskables.Remove(this);
|
|
|
|
|
|
|
|
|
|
graphic.SetMaterialDirtyEx();
|
|
|
|
|
_softMask = null;
|
|
|
|
|
|
|
|
|
|
MaterialCache.Unregister(_effectMaterialHash);
|
|
|
|
|
_effectMaterialHash = k_InvalidHash;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#if UNITY_EDITOR
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// This function is called when the script is loaded or a value is changed in the inspector (Called in the editor only).
|
|
|
|
|
/// </summary>
|
|
|
|
|
private void OnValidate()
|
|
|
|
|
{
|
|
|
|
|
graphic.SetMaterialDirtyEx();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ISerializationCallbackReceiver.OnBeforeSerialize()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ISerializationCallbackReceiver.OnAfterDeserialize()
|
|
|
|
|
{
|
|
|
|
|
#pragma warning disable 0612
|
|
|
|
|
if (m_Inverse)
|
|
|
|
|
{
|
|
|
|
|
m_Inverse = false;
|
2020-06-04 12:43:17 +08:00
|
|
|
|
m_MaskInteraction = kVisibleOutside;
|
2020-05-11 15:34:22 +08:00
|
|
|
|
}
|
|
|
|
|
#pragma warning restore 0612
|
|
|
|
|
|
|
|
|
|
var current = this;
|
|
|
|
|
UnityEditor.EditorApplication.delayCall += () =>
|
|
|
|
|
{
|
2020-06-09 13:41:36 +08:00
|
|
|
|
if (!current) return;
|
|
|
|
|
if (!graphic) return;
|
2020-09-08 10:44:49 +08:00
|
|
|
|
if (graphic.name.Contains("TMP SubMeshUI")) return;
|
2020-06-09 13:41:36 +08:00
|
|
|
|
if (!graphic.material) return;
|
|
|
|
|
if (!graphic.material.shader) return;
|
|
|
|
|
if (graphic.material.shader.name != "Hidden/UI/Default (SoftMaskable)") return;
|
|
|
|
|
|
|
|
|
|
graphic.material = null;
|
|
|
|
|
graphic.SetMaterialDirtyEx();
|
2020-05-11 15:34:22 +08:00
|
|
|
|
};
|
|
|
|
|
}
|
2020-05-13 13:43:18 +08:00
|
|
|
|
#endif
|
2020-05-11 15:34:22 +08:00
|
|
|
|
}
|
2020-05-03 19:38:06 +08:00
|
|
|
|
}
|