Unity Shader Example 7 (溶解)

<pre class="plain" name="code">Shader "Test" 
{
    Properties 
    {
        _MainTex ("Base (RGB)", 2D) = "white" {}
		_DissolveTex ("Dissolve Texture", 2D) = "white" {}
		_LimitValue ("Percent", Range(0.0, 1.0)) = 0.0
		_BrightFactor ("Bright Factor", Range(0.0, 4.0)) = 1.0
		_Color ("Color", Color) = (1, 1, 1, 1)
		_EdgeColor("EdgeColor", Color) = (1, 1, 1, 1)
		_EdgeWidth("EdgeWidth", Range(0.01, 1.0)) = 0.5
    }
    SubShader 
    {
        Tags {"Queue"="Transparent"}
        Pass
        {
			Blend SrcAlpha One 
			Cull Off 
			ZWrite Off

            CGPROGRAM
            //#pragma vertex vert_img
			#pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"
			#pragma multi_compile_particles
            
			struct appdata_t {
					float4 vertex : POSITION;
					float2 texcoord : TEXCOORD0;
					fixed4 color : COLOR;
			};

			struct v2f {
				float4 vertex : SV_POSITION;
				half2 texcoord : TEXCOORD0;
				float2 texcoord1 : TEXCOORD1;
				fixed4 color : COLOR;
			};

			sampler2D _MainTex;
			fixed4 _MainTex_ST;
			sampler2D _DissolveTex;
			fixed4 _DissolveTex_ST;
			float _LimitValue;
			float _BrightFactor;
			float4 _Color;

			// edge
			float4 _EdgeColor;
			float _EdgeWidth;

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

            float4 frag(v2f i) : COLOR
            {
				float maskValue = tex2D(_DissolveTex,i.texcoord1).r;

				//  clip(x) Discards the current pixel, if any component of x is less than zero. 
				float dist = maskValue - _LimitValue;
				clip(dist);
				float4 texColor = tex2D(_MainTex,i.texcoord);
				texColor *= _Color * i.color;
				texColor.rgb *= _BrightFactor;
				float blendFactor = saturate(dist / _EdgeWidth);

				// lerp(a,b f) = (1-f) * a + b * f
				float3 resCol = lerp(_EdgeColor.rgb, texColor.rgb,  blendFactor);
				return float4(resCol.rgb, texColor.a);
			}
            
            ENDCG
        }
    } 
    
    FallBack Off
}

 


思路:




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值