天天看點

[Unity3D]Shader程式設計之動态螢幕遮罩

螢幕可視範圍跟随目标物體移動,可修改可視範圍大小,邊緣漸變大小、以及遮罩顔色,支援最高物體數量可在Shader中修改,目前版本支援最多9個物體。

效果圖如下:

[Unity3D]Shader程式設計之動态螢幕遮罩

控制台如下:

[Unity3D]Shader程式設計之動态螢幕遮罩

Shader代碼如下:

Shader "Peter/DarkEffect"
{
    Properties
    {
        _MainTex ("Texture", D) = "white" {}
    }

    SubShader
    {
        // No culling or depth
        Cull Off ZWrite Off ZTest Always

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

            //追蹤物體最多個數
            #define ItemSize 9

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
            };

            sampler2D _MainTex;

            fixed4 _DarkColor;
            float _SmoothLength;
            fixed _ItemCnt;
            float4 _Item[ItemSize];

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = v.uv;
                return o;
            }

            fixed CalcAlpha(float4 vt, float4 pt)
            {
                if(pt.z < )
                {
                    return ;
                }

                float distPow2 = pow(vt.x - pt.x, ) + pow(vt.y - pt.y, );
                float dist = (distPow2 > ) ? sqrt(distPow2) : ;

                float smoothLength = _SmoothLength;
                if(smoothLength < )
                {
                    smoothLength = ;
                }

                float maxValue = pt.z;
                float minValue = pt.z - smoothLength;
                if(minValue < )
                {
                    minValue = ;
                    smoothLength = pt.z;
                }

                if(dist <= minValue)
                {
                    return ;
                }
                else if (dist > maxValue)
                {
                    return ;
                }

                fixed retVal = (dist - minValue) / smoothLength;

                return retVal;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                fixed alphaVal = ;
                fixed tmpVal = ;

                for(fixed index = ; index < _ItemCnt; ++index)
                {
                    tmpVal = CalcAlpha(i.vertex, _Item[index]);
                    if(tmpVal < alphaVal)
                    {
                        alphaVal = tmpVal;
                    }
                }

                alphaVal *= _DarkColor.a;

                return tex2D(_MainTex, i.uv) * (  - alphaVal) + _DarkColor * alphaVal;
            }

            ENDCG
        }
    }
}
           

C#調用代碼如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[ExecuteInEditMode]
[RequireComponent(typeof(Camera))]
public class DarkEffect : MonoBehaviour
{
    [System.Serializable]
    public class Item
    {
        [SerializeField]
        public Transform target;

        [SerializeField]
        public int radius;

        public Vector3 GetScreenPosition(Camera cam)
        {
            return cam.WorldToScreenPoint(target.position);
        }
    }

    //漸變像素數量
    public int _smoothLength = ;
    //遮罩混合顔色
    public Color _darkColor = Color.black;
    //目标物體
    public List<Item> _items = new List<Item>();

    protected Material _mainMaterial;
    protected Camera _mainCamera;

    Vector4[] _itemDatas;
    Item _tmpItem;
    Vector4 _tmpVt;
    Vector3 _tmpPos;
    int _tmpScreenHeight;

    private void OnEnable()
    {
        _mainMaterial = new Material(Shader.Find("Peter/DarkEffect"));
        _mainCamera = GetComponent<Camera>();
    }

    private void OnRenderImage(RenderTexture source, RenderTexture destination)
    {

        if (_itemDatas == null || _itemDatas.Length != _items.Count)
        {
            _itemDatas = new Vector4[_items.Count];
        }

        _tmpScreenHeight = Screen.height;

        for (int i = ; i < _items.Count; i++)
        {
            _tmpItem = _items[i];
            _tmpPos = _tmpItem.GetScreenPosition(_mainCamera);

            _tmpVt.x = _tmpPos.x;
            _tmpVt.y = _tmpScreenHeight - _tmpPos.y;
            _tmpVt.z = _tmpItem.radius;
            _tmpVt.w = ;

            _itemDatas[i] = _tmpVt;
        }

        _mainMaterial.SetInt("_SmoothLength", _smoothLength);
        _mainMaterial.SetColor("_DarkColor", _darkColor);
        _mainMaterial.SetInt("_ItemCnt", _itemDatas.Length);
        _mainMaterial.SetVectorArray("_Item", _itemDatas);

        Graphics.Blit(source, destination, _mainMaterial);
    }
}
           
[Unity3D]Shader程式設計之動态螢幕遮罩

繼續閱讀