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