自定义RectMask2D组件以支持四个方向的softness

用于设置自定义的softness的monobehavior脚本

使用额外的脚本设置设置自定义softness参数,padding则仍需要用默认的RectMask2D

using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

namespace BorderlessFramework.Utility
{
    public class CustomRectMask2D : UIBehaviour
    {
        [Header("softness+padding值不要超过该物体的Rect中心到RectMask2D物体对应边界的距离")]
        [SerializeField] [Max(0)] private float softnessLeft;
        [SerializeField] [Max(0)] private float softnessRight;
        [SerializeField] [Max(0)] private float softnessTop;
        [SerializeField] [Max(0)] private float softnessBottom;

        private int softnessLeftPropertyID = Shader.PropertyToID("_SoftnessLeft");
        private int softnessRightPropertyID = Shader.PropertyToID("_SoftnessRight");
        private int softnessTopPropertyID = Shader.PropertyToID("_SoftnessTop");
        private int softnessBottomPropertyID = Shader.PropertyToID("_SoftnessBottom");

        private const string shaderName = "UI/RectMask2D Smoothstep FourBorderSoftness";

        /// <summary>
        /// 如果使用脚本修改了该脚本softness,需要调用该方法后才会生效
        /// </summary>
        public void Refresh(bool isOn)
        {
            var maskableGraphics = GetComponentsInChildren<MaskableGraphic>();
            foreach (var maskableGraphic in maskableGraphics)
            {
                if (maskableGraphic.maskable && maskableGraphic.material.shader.name == shaderName)
                {
                    maskableGraphic.material.SetFloat(softnessLeftPropertyID, isOn ? softnessLeft : 0f);
                    maskableGraphic.material.SetFloat(softnessRightPropertyID, isOn ? softnessRight : 0f);
                    maskableGraphic.material.SetFloat(softnessTopPropertyID, isOn ? softnessTop : 0f);
                    maskableGraphic.material.SetFloat(softnessBottomPropertyID, isOn ? softnessBottom : 0f);
                    maskableGraphic.SetMaterialDirty();
                }
            }
        }

        protected override void OnEnable()
        {
            Refresh(true);
        }

        protected override void OnDisable()
        {
            Refresh(false);
        }

#if UNITY_EDITOR
        protected override void OnValidate()
        {
            if (!Application.isPlaying) Refresh(enabled);
        }

        protected override void Reset()
        {
            softnessLeft = softnessRight = softnessTop = softnessBottom = 0;
            Refresh(false);
        }
#endif

    }
}

MaxAttribute相关脚本

MaxAttribute放在非Editor目录下

using UnityEngine;

namespace BorderlessFramework.Utility
{
    public class MaxAttribute : PropertyAttribute
    {
        public float max;

        public MaxAttribute(float max)
        {
            this.max = max;
        }
    }
}

MaxPropertyDrawer放在Editor目录下

using UnityEditor;
using UnityEngine;

namespace BorderlessFramework.Utility
{
    [CustomPropertyDrawer(typeof(MaxAttribute))]
    public class MaxPropertyDrawer : PropertyDrawer
    {
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            MaxAttribute minAttribute = attribute as MaxAttribute;
            property.floatValue = Mathf.Max(property.floatValue, minAttribute.max);
            EditorGUI.PropertyField(position, property);
        }
    }
}

从UI-Default修改的shader

对于任何需要受影响的UI,需要设置material为使用了该shader的自定义material才会有效果

Shader "UI/RectMask2D Smoothstep FourBorderSoftness"
{
    Properties
    {
        [PerRendererData] _MainTex("Sprite Texture", 2D) = "white" {}
        _Color("Tint", Color) = (1,1,1,1)

        _StencilComp("Stencil Comparison", Float) = 8
        _Stencil("Stencil ID", Float) = 0
        _StencilOp("Stencil Operation", Float) = 0
        _StencilWriteMask("Stencil Write Mask", Float) = 255
        _StencilReadMask("Stencil Read Mask", Float) = 255

        _ColorMask("Color Mask", Float) = 15

        [Toggle(UNITY_UI_ALPHACLIP)] _UseUIAlphaClip("Use Alpha Clip", Float) = 0

        // 序列化这些属性在Material上,但不显示在Inspector上
        [HideInInspector]_SoftnessLeft("_SoftnessLeft", Float) = 0
        [HideInInspector]_SoftnessRight("_SoftnessRight", Float) = 0
        [HideInInspector]_SoftnessTop("_SoftnessTop", Float) = 0
        [HideInInspector]_SoftnessBottom("_SoftnessBottom", Float) = 0
    }

        SubShader
        {
            Tags
            {
                "Queue" = "Transparent"
                "IgnoreProjector" = "True"
                "RenderType" = "Transparent"
                "PreviewType" = "Plane"
                "CanUseSpriteAtlas" = "True"
            }

            Stencil
            {
                Ref[_Stencil]
                Comp[_StencilComp]
                Pass[_StencilOp]
                ReadMask[_StencilReadMask]
                WriteMask[_StencilWriteMask]
            }

            Cull Off
            Lighting Off
            ZWrite Off
            ZTest[unity_GUIZTestMode]
            Blend One OneMinusSrcAlpha
            ColorMask[_ColorMask]

            Pass
            {
                Name "Default"
            CGPROGRAM
                #pragma vertex vert
                #pragma fragment frag
                #pragma target 2.0

                #include "UnityCG.cginc"
                #include "UnityUI.cginc"

                #pragma multi_compile_local _ UNITY_UI_CLIP_RECT
                #pragma multi_compile_local _ UNITY_UI_ALPHACLIP

                struct appdata_t
                {
                    float4 vertex   : POSITION;
                    float4 color    : COLOR;
                    float2 texcoord : TEXCOORD0;
                    UNITY_VERTEX_INPUT_INSTANCE_ID
                };

                struct v2f
                {
                    float4 vertex   : SV_POSITION;
                    fixed4 color : COLOR;
                    float2 texcoord  : TEXCOORD0;
                    float4 worldPosition : TEXCOORD1;
                    half4  mask : TEXCOORD2;
                    UNITY_VERTEX_OUTPUT_STEREO
                };

                sampler2D _MainTex;
                fixed4 _Color;
                fixed4 _TextureSampleAdd;
                float4 _ClipRect;
                float4 _MainTex_ST;
                float _UIMaskSoftnessX;
                float _UIMaskSoftnessY;
                float _SoftnessLeft;
                float _SoftnessRight;
                float _SoftnessTop;
                float _SoftnessBottom;

                v2f vert(appdata_t v)
                {
                    v2f OUT;
                    UNITY_SETUP_INSTANCE_ID(v);
                    UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(OUT);
                    float4 vPosition = UnityObjectToClipPos(v.vertex);
                    OUT.worldPosition = v.vertex;
                    OUT.vertex = vPosition;

                    float2 pixelSize = vPosition.w;
                    pixelSize /= float2(1, 1) * abs(mul((float2x2)UNITY_MATRIX_P, _ScreenParams.xy));

                    float4 clampedRect = clamp(_ClipRect, -2e10, 2e10);
                    float2 maskUV = (v.vertex.xy - clampedRect.xy) / (clampedRect.zw - clampedRect.xy);
                    OUT.texcoord = TRANSFORM_TEX(v.texcoord.xy, _MainTex);
                    OUT.mask = half4(v.vertex.xy * 2 - clampedRect.xy - clampedRect.zw, 0.25 / (0.25 * half2(_UIMaskSoftnessX, _UIMaskSoftnessY) + abs(pixelSize.xy)));
                    OUT.color = v.color * _Color;
                    return OUT;
                }

                fixed4 frag(v2f IN) : SV_Target
                {
                    half4 color = IN.color * (tex2D(_MainTex, IN.texcoord) + _TextureSampleAdd);
#ifdef UNITY_UI_CLIP_RECT
                    half kx = step(IN.mask.x, 0);
                    half ky = step(IN.mask.y, 0);
                    half2 division = half2(
                        (kx * _SoftnessLeft + (1 - kx) * _SoftnessRight) * 2,
                        (ky * _SoftnessBottom + (1 - ky) * _SoftnessTop) * 2);
                    half2 m = saturate((_ClipRect.zw - _ClipRect.xy - abs(IN.mask.xy)) / division);
                    // 让最后边缘部分不是线性变化,否则会出现明显的条带
                    m = smoothstep(0, 1, m);
                    color.a *= m.x * m.y;
#endif

                    #ifdef UNITY_UI_ALPHACLIP
                    clip(color.a - 0.001);
                    #endif

                    color.rgb *= color.a;

                    return color;
                }
            ENDCG
            }
        }
}

注意

计算softness时,只能进行缩放,使得计算得到的alpha由边缘向Rect矩形的中心线性增长 如果softness+padding超过该物体的Rect中心到RectMask2D物体对应边界的距离,则中心的alpha会小于1,但另一边的alpha全为1,会出现明显的边界(从小于1的值跳变到1)

image-20221025172809900

另一种方式

仍使用自带的RectMask2D,不想要softness的一侧设置padding为负数。缺点是设置了padding的那一侧虽然没有softness了,但是也会没有mask效果

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
很抱歉,作为AI语言模型,我无法提供您所需的代码。但是,我可以为您提供一些关于创建基于sprite的圆形扩散shader的技巧和思路。 首先,您需要了解一些基本概念: - Shader:一种编程语言,用于在图像渲染过程中控制像素的颜色和其他属性。 - Sprite:在游戏开发中,一个简单的图形,可以在屏幕上进行渲染,通常用于表示游戏中的角色、对象和背景。Sprite通常是由一张图片生成的,可以根据需要进行缩放、旋转和移动。 - 圆形扩散:一种特殊的视觉效果,可以让一个物体在其周围产生一个圆形的扩散效果,看起来像是物体在发光或者散发一种能量。 接下来,您可以使用Unity的ShaderLab编写一个基本的sprite shader,然后添加圆形扩散效果。下面是一个简单的sprite shader示例: ``` Shader "Custom/Sprite" { Properties { _MainTex ("Texture", 2D) = "white" {} _Color ("Color", Color) = (1,1,1,1) } SubShader { Tags {"Queue"="Transparent" "RenderType"="Transparent"} LOD 100 Pass { CGPROGRAM #pragma vertex vert #pragma fragment frag #include "UnityCG.cginc" struct appdata { float4 vertex : POSITION; float2 uv : TEXCOORD0; }; struct v2f { float2 uv : TEXCOORD0; float4 vertex : SV_POSITION; }; sampler2D _MainTex; float4 _MainTex_ST; float4 _Color; v2f vert (appdata v) { v2f o; o.vertex = UnityObjectToClipPos(v.vertex); o.uv = TRANSFORM_TEX(v.uv, _MainTex); return o; } fixed4 frag (v2f i) : SV_Target { fixed4 col = tex2D(_MainTex, i.uv) * _Color; return col; } ENDCG } } } ``` 这个shader仅仅是将sprite渲染为一张纹理,并且可以设置颜色。接下来,您可以添加圆形扩散效果。 首先,您需要通过修改vertex shader来将sprite渲染为一个圆形。下面是一个简单的vertex shader示例: ``` v2f vert (appdata v) { v2f o; o.vertex = UnityObjectToClipPos(v.vertex); o.uv = v.uv; o.vertex.xy *= 2.0 * _Radius; return o; } ``` 这个vertex shader将sprite的顶点坐标进行了缩放,使其成为一个圆形。_Radius是一个自定义的shader变量,用于控制圆形的大小。 接下来,您需要通过修改fragment shader来添加圆形扩散效果。下面是一个简单的fragment shader示例: ``` fixed4 frag (v2f i) : SV_Target { fixed4 col = tex2D(_MainTex, i.uv) * _Color; float dist = length(i.vertex.xy); float alpha = smoothstep(_Radius - _Softness, _Radius, dist); col.a *= alpha; return col; } ``` 这个fragment shader使用了smoothstep函数来创建一个平滑的渐变效果,并通过调整alpha值来控制圆形扩散的强度。_Softness是一个自定义的shader变量,用于控制圆形扩散的柔和程度。 最后,您需要将shader应用到一个sprite上,然后通过调整_Radius和_Softness变量来控制圆形扩散的大小和柔和程度。下面是一个示例: ``` SpriteRenderer spriteRenderer; Material material = new Material(Shader.Find("Custom/Sprite")); spriteRenderer.material = material; material.SetFloat("_Radius", 0.5f); material.SetFloat("_Softness", 0.1f); ``` 通过以上步骤,您可以创建一个基于sprite的圆形扩散shader。当然,实际的实现可能需要更多的调整和优化,但是这个示例可以为您提供一个基本的思路和技巧。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值