2018-11-20 07:49:14 +08:00
|
|
|
|
using System.Collections;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
using UnityEngine.Rendering;
|
|
|
|
|
using UnityEngine.UI;
|
2019-01-26 20:30:41 +08:00
|
|
|
|
using MaskIntr = UnityEngine.SpriteMaskInteraction;
|
|
|
|
|
using UnityEngine.Serialization;
|
2018-11-20 07:49:14 +08:00
|
|
|
|
|
|
|
|
|
namespace Coffee.UIExtensions
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Soft maskable.
|
|
|
|
|
/// Add this component to Graphic under SoftMask for smooth masking.
|
|
|
|
|
/// </summary>
|
|
|
|
|
[ExecuteInEditMode]
|
2019-01-26 20:30:41 +08:00
|
|
|
|
public class SoftMaskable : MonoBehaviour, IMaterialModifier, ICanvasRaycastFilter, ISerializationCallbackReceiver
|
2018-11-20 07:49:14 +08:00
|
|
|
|
{
|
|
|
|
|
//################################
|
|
|
|
|
// Constant or Static Members.
|
|
|
|
|
//################################
|
2019-01-26 20:30:41 +08:00
|
|
|
|
const int kVisibleInside = (1 << 0) + (1 << 2) + (1 << 4) + (1 << 6);
|
|
|
|
|
const int kVisibleOutside = (2 << 0) + (2 << 2) + (2 << 4) + (2 << 6);
|
2018-11-20 07:49:14 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//################################
|
|
|
|
|
// Serialize Members.
|
|
|
|
|
//################################
|
|
|
|
|
[Tooltip("The graphic will be visible only in areas where no mask is present.")]
|
2019-01-26 20:30:41 +08:00
|
|
|
|
[System.Obsolete]
|
|
|
|
|
[HideInInspector]
|
2018-11-20 07:49:14 +08:00
|
|
|
|
[SerializeField] bool m_Inverse = false;
|
2019-01-26 20:30:41 +08:00
|
|
|
|
[Tooltip("The interaction for each masks.")]
|
|
|
|
|
[HideInInspector]
|
|
|
|
|
[SerializeField] int m_MaskInteraction = kVisibleInside;
|
|
|
|
|
[Tooltip("Use stencil for masking.")]
|
2019-02-01 15:28:11 +08:00
|
|
|
|
[SerializeField] bool m_UseStencil = false;
|
2018-11-20 07:49:14 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//################################
|
|
|
|
|
// Public Members.
|
|
|
|
|
//################################
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Perform material modification in this function.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>Modified material.</returns>
|
|
|
|
|
/// <param name="baseMaterial">Configured Material.</param>
|
|
|
|
|
public Material GetModifiedMaterial(Material baseMaterial)
|
|
|
|
|
{
|
|
|
|
|
_softMask = null;
|
|
|
|
|
if (!isActiveAndEnabled)
|
|
|
|
|
{
|
|
|
|
|
return baseMaterial;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Find the nearest parent softmask.
|
2018-12-16 19:43:15 +08:00
|
|
|
|
var parentTransform = transform.parent;
|
2018-11-20 07:49:14 +08:00
|
|
|
|
while (parentTransform)
|
|
|
|
|
{
|
|
|
|
|
var sm = parentTransform.GetComponent<SoftMask>();
|
|
|
|
|
if (sm && sm.enabled)
|
|
|
|
|
{
|
|
|
|
|
_softMask = sm;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
parentTransform = parentTransform.parent;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Material result = baseMaterial;
|
|
|
|
|
if (_softMask)
|
|
|
|
|
{
|
|
|
|
|
result = new Material(baseMaterial);
|
|
|
|
|
result.hideFlags = HideFlags.HideAndDontSave;
|
|
|
|
|
result.SetTexture(s_SoftMaskTexId, _softMask.softMaskBuffer);
|
2019-01-26 20:30:41 +08:00
|
|
|
|
result.SetInt(s_StencilCompId, m_UseStencil ? (int)CompareFunction.Equal : (int)CompareFunction.Always);
|
|
|
|
|
result.SetVector(s_MaskInteractionId, new Vector4(
|
|
|
|
|
(m_MaskInteraction & 0x3),
|
|
|
|
|
((m_MaskInteraction >> 2) & 0x3),
|
|
|
|
|
((m_MaskInteraction >> 4) & 0x3),
|
|
|
|
|
((m_MaskInteraction >> 6) & 0x3)
|
|
|
|
|
));
|
2018-11-20 07:49:14 +08:00
|
|
|
|
|
|
|
|
|
StencilMaterial.Remove(baseMaterial);
|
|
|
|
|
ReleaseMaterial(ref _maskMaterial);
|
|
|
|
|
_maskMaterial = result;
|
|
|
|
|
|
|
|
|
|
#if UNITY_EDITOR
|
|
|
|
|
result.EnableKeyword("SOFTMASK_EDITOR");
|
|
|
|
|
UpdateSceneViewMatrixForShader();
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
baseMaterial.SetTexture(s_SoftMaskTexId, Texture2D.whiteTexture);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <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>
|
|
|
|
|
public bool IsRaycastLocationValid(Vector2 sp, Camera eventCamera)
|
|
|
|
|
{
|
|
|
|
|
if (!isActiveAndEnabled || !_softMask)
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
if (!RectTransformUtility.RectangleContainsScreenPoint(transform as RectTransform, sp, eventCamera))
|
2019-01-26 20:30:41 +08:00
|
|
|
|
{
|
2018-11-20 07:49:14 +08:00
|
|
|
|
return false;
|
2019-01-26 20:30:41 +08:00
|
|
|
|
}
|
2018-11-20 07:49:14 +08:00
|
|
|
|
|
2019-01-26 20:30:41 +08:00
|
|
|
|
var sm = _softMask;
|
|
|
|
|
for (int 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);
|
2018-11-20 07:49:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-01-26 20:30:41 +08:00
|
|
|
|
|
2018-11-20 07:49:14 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The graphic will be visible only in areas where no mask is present.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public bool inverse
|
|
|
|
|
{
|
2019-01-26 20:30:41 +08:00
|
|
|
|
get { return m_MaskInteraction == kVisibleOutside; }
|
2018-11-20 07:49:14 +08:00
|
|
|
|
set
|
|
|
|
|
{
|
2019-01-26 20:30:41 +08:00
|
|
|
|
int intValue = value ? kVisibleOutside : kVisibleInside;
|
|
|
|
|
if (m_MaskInteraction != intValue)
|
2018-11-20 07:49:14 +08:00
|
|
|
|
{
|
2019-01-26 20:30:41 +08:00
|
|
|
|
m_MaskInteraction = intValue;
|
2018-11-20 07:49:14 +08:00
|
|
|
|
graphic.SetMaterialDirty();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The graphic associated with the soft mask.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public Graphic graphic{ get { return _graphic ? _graphic : _graphic = GetComponent<Graphic>(); } }
|
|
|
|
|
|
2019-01-26 20:30:41 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Set the interaction for each mask.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void SetMaskInteraction(SpriteMaskInteraction intr)
|
|
|
|
|
{
|
|
|
|
|
SetMaskInteraction(intr, intr, intr, intr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Set the interaction for each mask.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void SetMaskInteraction(SpriteMaskInteraction layer0, SpriteMaskInteraction layer1, SpriteMaskInteraction layer2, SpriteMaskInteraction layer3)
|
|
|
|
|
{
|
|
|
|
|
m_MaskInteraction = (int)layer0 + ((int)layer1 << 2) + ((int)layer2 << 4) + ((int)layer3 << 6);
|
|
|
|
|
if (graphic)
|
|
|
|
|
{
|
|
|
|
|
graphic.SetMaterialDirty();
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-11-20 07:49:14 +08:00
|
|
|
|
|
|
|
|
|
//################################
|
|
|
|
|
// Private Members.
|
|
|
|
|
//################################
|
|
|
|
|
Graphic _graphic = null;
|
|
|
|
|
SoftMask _softMask = null;
|
|
|
|
|
Material _maskMaterial = null;
|
|
|
|
|
static int s_SoftMaskTexId;
|
|
|
|
|
static int s_StencilCompId;
|
2019-01-26 20:30:41 +08:00
|
|
|
|
static int s_MaskInteractionId;
|
2019-02-01 15:27:56 +08:00
|
|
|
|
static int s_GameVPId;
|
2019-03-11 03:37:42 +08:00
|
|
|
|
static int s_GameTVPId;
|
2019-01-26 20:30:41 +08:00
|
|
|
|
static List<SoftMaskable> s_ActiveSoftMaskables;
|
|
|
|
|
static int[] s_Interactions = new int[4];
|
|
|
|
|
static Material s_DefaultMaterial;
|
2018-11-20 07:49:14 +08:00
|
|
|
|
|
|
|
|
|
#if UNITY_EDITOR
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Update the scene view matrix for shader.
|
|
|
|
|
/// </summary>
|
|
|
|
|
static void UpdateSceneViewMatrixForShader()
|
|
|
|
|
{
|
|
|
|
|
UnityEditor.SceneView sceneView = UnityEditor.SceneView.lastActiveSceneView;
|
|
|
|
|
if (!sceneView || !sceneView.camera)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Camera cam = sceneView.camera;
|
|
|
|
|
Matrix4x4 w2c = cam.worldToCameraMatrix;
|
|
|
|
|
Matrix4x4 prj = cam.projectionMatrix;
|
|
|
|
|
|
2019-02-01 15:27:56 +08:00
|
|
|
|
s_ActiveSoftMaskables.RemoveAll(x=>!x);
|
2018-11-20 07:49:14 +08:00
|
|
|
|
foreach (var sm in s_ActiveSoftMaskables)
|
|
|
|
|
{
|
2019-02-01 15:27:56 +08:00
|
|
|
|
if (!sm || !sm._maskMaterial || !sm.graphic || !sm.graphic.canvas)
|
2018-11-20 07:49:14 +08:00
|
|
|
|
{
|
2019-02-01 15:27:56 +08:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Material mat = sm._maskMaterial;
|
|
|
|
|
var c = sm.graphic.canvas.rootCanvas;
|
|
|
|
|
if (c.renderMode != RenderMode.ScreenSpaceOverlay && c.worldCamera)
|
|
|
|
|
{
|
|
|
|
|
var wcam = c.worldCamera;
|
2019-03-11 03:37:42 +08:00
|
|
|
|
var pv = GL.GetGPUProjectionMatrix (wcam.projectionMatrix, false) * wcam.worldToCameraMatrix;
|
2019-02-01 15:27:56 +08:00
|
|
|
|
mat.SetMatrix(s_GameVPId, pv);
|
2019-03-11 03:37:42 +08:00
|
|
|
|
mat.SetMatrix(s_GameTVPId, pv);
|
2019-02-01 15:27:56 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var pos = c.transform.localPosition;
|
2019-03-11 03:37:42 +08:00
|
|
|
|
var pv = Matrix4x4.TRS(new Vector3(0, 0, 0), Quaternion.identity, new Vector3(1 / pos.x, 1 / pos.y, -2 / 2000f));
|
2019-02-01 15:27:56 +08:00
|
|
|
|
mat.SetMatrix(s_GameVPId, pv);
|
2019-03-11 03:37:42 +08:00
|
|
|
|
mat.SetMatrix(s_GameTVPId, pv * Matrix4x4.Translate(-pos));
|
2018-11-20 07:49:14 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// This function is called when the script is loaded or a value is changed in the inspector (Called in the editor only).
|
|
|
|
|
/// </summary>
|
|
|
|
|
void OnValidate()
|
|
|
|
|
{
|
|
|
|
|
if (graphic)
|
|
|
|
|
{
|
|
|
|
|
graphic.SetMaterialDirty();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// This function is called when the object becomes enabled and active.
|
|
|
|
|
/// </summary>
|
|
|
|
|
void OnEnable()
|
|
|
|
|
{
|
|
|
|
|
// Register.
|
|
|
|
|
if (s_ActiveSoftMaskables == null)
|
|
|
|
|
{
|
|
|
|
|
s_ActiveSoftMaskables = new List<SoftMaskable>();
|
|
|
|
|
|
2018-11-20 10:30:07 +08:00
|
|
|
|
#if UNITY_EDITOR
|
2018-11-20 07:49:14 +08:00
|
|
|
|
UnityEditor.EditorApplication.update += UpdateSceneViewMatrixForShader;
|
2019-02-01 15:27:56 +08:00
|
|
|
|
s_GameVPId = Shader.PropertyToID("_GameVP");
|
2019-03-11 03:37:42 +08:00
|
|
|
|
s_GameTVPId = Shader.PropertyToID("_GameTVP");
|
2018-11-20 10:30:07 +08:00
|
|
|
|
#endif
|
2018-11-20 07:49:14 +08:00
|
|
|
|
|
|
|
|
|
s_SoftMaskTexId = Shader.PropertyToID("_SoftMaskTex");
|
|
|
|
|
s_StencilCompId = Shader.PropertyToID("_StencilComp");
|
2019-01-26 20:30:41 +08:00
|
|
|
|
s_MaskInteractionId = Shader.PropertyToID("_MaskInteraction");
|
2018-11-20 07:49:14 +08:00
|
|
|
|
}
|
|
|
|
|
s_ActiveSoftMaskables.Add(this);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var g = graphic;
|
|
|
|
|
if (g)
|
|
|
|
|
{
|
|
|
|
|
if (!g.material || g.material == Graphic.defaultGraphicMaterial)
|
|
|
|
|
{
|
2019-01-26 20:30:41 +08:00
|
|
|
|
g.material = s_DefaultMaterial ?? (s_DefaultMaterial = new Material(Resources.Load<Shader>("UI-Default-SoftMask")) { hideFlags = HideFlags.HideAndDontSave, });
|
2018-11-20 07:49:14 +08:00
|
|
|
|
}
|
|
|
|
|
g.SetMaterialDirty();
|
|
|
|
|
}
|
|
|
|
|
_softMask = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// This function is called when the behaviour becomes disabled.
|
|
|
|
|
/// </summary>
|
|
|
|
|
void OnDisable()
|
|
|
|
|
{
|
|
|
|
|
s_ActiveSoftMaskables.Remove(this);
|
|
|
|
|
|
|
|
|
|
var g = graphic;
|
|
|
|
|
if (g)
|
|
|
|
|
{
|
2019-01-26 20:30:41 +08:00
|
|
|
|
if (g.material == s_DefaultMaterial)
|
2018-11-20 07:49:14 +08:00
|
|
|
|
{
|
|
|
|
|
g.material = null;
|
|
|
|
|
}
|
|
|
|
|
g.SetMaterialDirty();
|
|
|
|
|
}
|
|
|
|
|
ReleaseMaterial(ref _maskMaterial);
|
|
|
|
|
|
|
|
|
|
_softMask = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Release the material.
|
|
|
|
|
/// </summary>
|
|
|
|
|
void ReleaseMaterial(ref Material mat)
|
|
|
|
|
{
|
|
|
|
|
if (mat)
|
|
|
|
|
{
|
|
|
|
|
StencilMaterial.Remove(mat);
|
|
|
|
|
|
|
|
|
|
#if UNITY_EDITOR
|
|
|
|
|
if (!Application.isPlaying)
|
|
|
|
|
{
|
|
|
|
|
DestroyImmediate(mat);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
#endif
|
|
|
|
|
{
|
|
|
|
|
Destroy(mat);
|
|
|
|
|
}
|
|
|
|
|
mat = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-01-26 20:30:41 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void ISerializationCallbackReceiver.OnBeforeSerialize()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ISerializationCallbackReceiver.OnAfterDeserialize()
|
|
|
|
|
{
|
2019-02-01 15:28:11 +08:00
|
|
|
|
#pragma warning disable 0612
|
2019-01-26 20:30:41 +08:00
|
|
|
|
if (m_Inverse)
|
|
|
|
|
{
|
|
|
|
|
m_Inverse = false;
|
|
|
|
|
m_MaskInteraction = (2 << 0) + (2 << 2) + (2 << 4) + (2 << 6);
|
|
|
|
|
}
|
2019-02-01 15:28:11 +08:00
|
|
|
|
#pragma warning restore 0612
|
2019-01-26 20:30:41 +08:00
|
|
|
|
}
|
2018-11-20 07:49:14 +08:00
|
|
|
|
}
|
|
|
|
|
}
|