[Unity Shader]溶解效果

一、效果

这里写图片描述

二、BurnToFadeOut.shader

Shader "BurnToFadeOut" {
Properties {
    _StartColor ("Start Color", Color) = (1,1,1,1)
    _EndColor ("End Color", Color) = (1,1,1,1)
    _MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
    _Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
    _Emit ("Emit level", Range(1,100)) = 0
    _Range ("Range", Range(0,1)) = 0
}
SubShader {
    Tags {"Queue"="AlphaTest" "IgnoreProjector"="True" "RenderType"="TransparentCutout"}
    LOD 100

    Lighting Off

    Pass { 
        CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

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

            struct v2f {
                float4 vertex : SV_POSITION;
                half2 texcoord : TEXCOORD0;
            };

            sampler2D _MainTex;
            float4 _MainTex_ST;
            fixed4 _StartColor;
            fixed4 _EndColor;
            fixed _Cutoff;
            half _Emit;
            half _Range;

            v2f vert (appdata_t v)
            {
                v2f o;
                o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
                o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
                return o;
            }

            fixed4 frag (v2f i) : COLOR
            {
                fixed4 col = tex2D(_MainTex, i.texcoord);
                fixed a = dot(col.xyz, fixed3(0.3, 0.59, 0.11));
                col.a = a;
                clip(a - _Cutoff);
                if(a < _Cutoff + _Range)
                    col.xyz = lerp(_StartColor.xyz, _EndColor.xyz, (saturate(a - _Cutoff) / _Range)) * _Emit;
                return col;
            }
        ENDCG
    }
}

SubShader {
    Tags {"Queue"="AlphaTest" "IgnoreProjector"="True" "RenderType"="TransparentCutout"}
    LOD 100

    Pass {
        Lighting Off
        Alphatest Greater [_Cutoff]
        SetTexture [_MainTex] { combine texture }
    }
}
}

三、使用

光晕效果需要把摄像机HDR打开并挂上这个脚本(Pro版导入Standard Asset里的)

@script ExecuteInEditMode
@script RequireComponent (Camera)
@script AddComponentMenu ("Image Effects/Bloom and Glow/Bloom (Optimized)")

class FastBloom extends PostEffectsBase {

    public enum Resolution {
        Low = 0,
        High = 1,
    }

    public enum BlurType {
        Standard = 0,
        Sgx = 1,
    }

    @Range(0.0f, 1.5f)
    public var threshhold : float = 0.25f;
    @Range(0.0f, 2.5f)
    public var intensity : float = 0.75f;

    @Range(0.25f, 5.5f)
    public var blurSize : float = 1.0f;

    var resolution : Resolution = Resolution.Low;
    @Range(1, 4)
    public var blurIterations : int = 1;

    public var blurType = BlurType.Standard;

    public var fastBloomShader : Shader;
    private var fastBloomMaterial : Material = null;

    function CheckResources () : boolean { 
        CheckSupport (false);  

        fastBloomMaterial = CheckShaderAndCreateMaterial (fastBloomShader, fastBloomMaterial);

        if(!isSupported)
            ReportAutoDisable ();
        return isSupported;            
    }

    function OnDisable() {
        if(fastBloomMaterial)
            DestroyImmediate (fastBloomMaterial);
    }

    function OnRenderImage (source : RenderTexture, destination : RenderTexture) { 
        if(CheckResources() == false) {
            Graphics.Blit (source, destination);
            return;
        }

        var divider : int = resolution == Resolution.Low ? 4 : 2;
        var widthMod : float = resolution == Resolution.Low ? 0.5f : 1.0f;

        fastBloomMaterial.SetVector ("_Parameter", Vector4 (blurSize * widthMod, 0.0f, threshhold, intensity));
        source.filterMode = FilterMode.Bilinear;

        var rtW = source.width/divider;
        var rtH = source.height/divider;

        // downsample
        var rt : RenderTexture = RenderTexture.GetTemporary (rtW, rtH, 0, source.format);
        rt.filterMode = FilterMode.Bilinear;
        Graphics.Blit (source, rt, fastBloomMaterial, 1);

        var passOffs = blurType == BlurType.Standard ? 0 : 2;

        for(var i : int = 0; i < blurIterations; i++) {
            fastBloomMaterial.SetVector ("_Parameter", Vector4 (blurSize * widthMod + (i*1.0f), 0.0f, threshhold, intensity));

            // vertical blur
            var rt2 : RenderTexture = RenderTexture.GetTemporary (rtW, rtH, 0, source.format);
            rt2.filterMode = FilterMode.Bilinear;
            Graphics.Blit (rt, rt2, fastBloomMaterial, 2 + passOffs);
            RenderTexture.ReleaseTemporary (rt);
            rt = rt2;

            // horizontal blur
            rt2 = RenderTexture.GetTemporary (rtW, rtH, 0, source.format);
            rt2.filterMode = FilterMode.Bilinear;
            Graphics.Blit (rt, rt2, fastBloomMaterial, 3 + passOffs);
            RenderTexture.ReleaseTemporary (rt);
            rt = rt2;
        }

        fastBloomMaterial.SetTexture ("_Bloom", rt);

        Graphics.Blit (source, destination, fastBloomMaterial, 0);

        RenderTexture.ReleaseTemporary (rt);
    }  
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值