sync
This commit is contained in:
parent
290a6a6529
commit
8ef01bce47
@ -1,104 +1,71 @@
|
||||
using System.Linq;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
[ExecuteInEditMode]
|
||||
[RequireComponent(typeof(Image))]
|
||||
public class UIImageAlphaMask : MonoBehaviour
|
||||
{
|
||||
private Image m_MaskImage;
|
||||
private Canvas m_Canvas;
|
||||
private RectTransform m_RectTransform;
|
||||
private Image[] m_TargetImageList;
|
||||
private Material m_Material;
|
||||
private Canvas canvas;
|
||||
private RectTransform rectTransform;
|
||||
private Image maskImage;
|
||||
|
||||
private Material Material
|
||||
{
|
||||
get
|
||||
{
|
||||
if (m_Material == null)
|
||||
{
|
||||
m_Material = new Material(Shader.Find("Hidden/UI/AlphaMask"));
|
||||
}
|
||||
return m_Material;
|
||||
}
|
||||
}
|
||||
[SerializeField, HideInInspector]
|
||||
private Image[] targetImageList;
|
||||
|
||||
private Image MaskImage
|
||||
{
|
||||
get
|
||||
{
|
||||
if (m_MaskImage == null)
|
||||
{
|
||||
m_MaskImage = GetComponent<Image>();
|
||||
}
|
||||
return m_MaskImage;
|
||||
}
|
||||
}
|
||||
|
||||
private RectTransform SelfRect
|
||||
{
|
||||
get
|
||||
{
|
||||
if (m_RectTransform == null)
|
||||
{
|
||||
m_RectTransform = GetComponent<RectTransform>();
|
||||
}
|
||||
return m_RectTransform;
|
||||
}
|
||||
}
|
||||
[SerializeField]
|
||||
public Texture2D MaskTex;
|
||||
|
||||
private Canvas RootCanvas
|
||||
{
|
||||
get
|
||||
{
|
||||
if (m_Canvas == null)
|
||||
if (canvas == null)
|
||||
{
|
||||
var canvas = GetComponentInParent<Canvas>();
|
||||
if (canvas != null)
|
||||
{
|
||||
if (canvas.rootCanvas != null)
|
||||
{
|
||||
m_Canvas = canvas.rootCanvas;
|
||||
this.canvas = canvas.rootCanvas;
|
||||
}
|
||||
else
|
||||
{
|
||||
m_Canvas = canvas;
|
||||
this.canvas = canvas;
|
||||
}
|
||||
}
|
||||
}
|
||||
return m_Canvas;
|
||||
return canvas;
|
||||
}
|
||||
}
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
maskImage = GetComponent<Image>();
|
||||
rectTransform = GetComponent<RectTransform>();
|
||||
|
||||
private void SetMaterial(Image[] imageList, Material material)
|
||||
if (targetImageList == null || targetImageList.Length <= 0)
|
||||
{
|
||||
if (imageList == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
for (int i = 0; i < imageList.Length; i++)
|
||||
{
|
||||
var image = imageList[i];
|
||||
image.material = material;
|
||||
targetImageList = GetComponentsInChildren<Image>(true);
|
||||
targetImageList = targetImageList.Where(o => o.transform != this.transform).ToArray();
|
||||
}
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
if (!IsValid())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (m_TargetImageList == null || m_TargetImageList.Length <= 0)
|
||||
if (targetImageList == null || targetImageList.Length <= 0)
|
||||
{
|
||||
m_TargetImageList = GetComponentsInChildren<Image>(true);
|
||||
m_TargetImageList = m_TargetImageList.Where(o => o.transform != this.transform).ToArray();
|
||||
SetMaterial(m_TargetImageList, Material);
|
||||
targetImageList = GetComponentsInChildren<Image>(true);
|
||||
targetImageList = targetImageList.Where(o => o.transform != this.transform).ToArray();
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
||||
SetProperties();
|
||||
}
|
||||
@ -115,8 +82,15 @@ public class UIImageAlphaMask : MonoBehaviour
|
||||
return false;
|
||||
}
|
||||
|
||||
if (MaskImage.sprite == null)
|
||||
if(MaskTex == null)
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
var path = AssetDatabase.GetAssetPath(maskImage.sprite);
|
||||
if(!string.IsNullOrEmpty(path))
|
||||
{
|
||||
MaskTex = AssetDatabase.LoadAssetAtPath<Texture2D>(path);
|
||||
}
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -132,9 +106,12 @@ public class UIImageAlphaMask : MonoBehaviour
|
||||
}
|
||||
|
||||
var matrix = CalculateMatrix(camera);
|
||||
|
||||
Material.SetTexture("_MaskTex", MaskImage.sprite.texture);
|
||||
Material.SetMatrix("_MaskMatrix", matrix);
|
||||
for (int i = 0; i < targetImageList.Length; i++)
|
||||
{
|
||||
var imgMat = targetImageList[i].material;
|
||||
imgMat.SetTexture("_Alpha_MaskTex", MaskTex);
|
||||
imgMat.SetMatrix("_MaskMatrix", matrix);
|
||||
}
|
||||
}
|
||||
|
||||
private Camera GetCamera()
|
||||
@ -144,7 +121,7 @@ public class UIImageAlphaMask : MonoBehaviour
|
||||
|
||||
private Matrix4x4 CalculateMatrix(Camera camera)
|
||||
{
|
||||
var rect = CalcurateViewportRect(camera);
|
||||
var rect = CalculateViewportRect(camera);
|
||||
|
||||
Matrix4x4 result = Matrix4x4.identity;
|
||||
|
||||
@ -161,11 +138,11 @@ public class UIImageAlphaMask : MonoBehaviour
|
||||
return result;
|
||||
}
|
||||
|
||||
private Rect CalcurateViewportRect(Camera camera)
|
||||
private Rect CalculateViewportRect(Camera camera)
|
||||
{
|
||||
var corners = new Vector3[4];
|
||||
|
||||
SelfRect.GetWorldCorners(corners);
|
||||
rectTransform.GetWorldCorners(corners);
|
||||
|
||||
var p1 = RectTransformUtility.WorldToScreenPoint(camera, corners[1]);
|
||||
var p3 = RectTransformUtility.WorldToScreenPoint(camera, corners[3]);
|
||||
|
||||
@ -97,12 +97,12 @@
|
||||
}
|
||||
|
||||
sampler2D _MainTex;
|
||||
sampler2D _MaskTex;
|
||||
sampler2D _Alpha_MaskTex;
|
||||
|
||||
fixed4 frag(v2f IN) : SV_Target
|
||||
{
|
||||
half4 color = (tex2D(_MainTex, IN.texcoord) + _TextureSampleAdd) * IN.color;
|
||||
half4 mask = tex2D(_MaskTex, IN.maskUv);
|
||||
half4 mask = tex2D(_Alpha_MaskTex, IN.maskUv);
|
||||
|
||||
color.a *= UnityGet2DClipping(IN.worldPosition.xy, _ClipRect);
|
||||
color.a *= mask.a;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user