Unity Shader溶解 软硬边--基于Blend One One

之前做过两版溶解效果都是基于Blend SrcAlpha OneMinusSrcAlpha,众所周知这种模式是通过透明度来实现半透效果的,但是如果特效跟你说我现在要基于叠加(Blend One One)模式呢?代码会跟半透明模式有些许区别 因为Blend One One是无法使用透明度那么它现在来了!

Shader "Custom/Add" {
	Properties{
		_Diffuse("Diffuse", 2D) = "white" {}
		_DiffuseColor("DiffuseColor", Color) = (1,1,1,1)
		_DiffusePower("DiffusePower", Float) = 1
		_DissolveTex("DissolveTex", 2D) = "white" {}
		_SoftSize("SoftSize",Range(0,2)) = 0
		_DissolveStep("DissolveStep",Range(0,1)) = 0
		[Toggle(CustomColor)]_IsCustom("自定义颜色",float) = 0
		_DissolveColor("DissolveColor",Color) = (1,1,1,1)
		_DissolveColorPW("DissolveColorPW",float) = 1
	}
		SubShader{
			Tags {
				"IgnoreProjector" = "True"
				"Queue" = "Transparent"
				"RenderType" = "Transparent"
			}
			Pass {
				Blend One One
				Cull Off
				ZWrite Off

				CGPROGRAM
				#pragma vertex vert
				#pragma fragment frag
				#include "UnityCG.cginc"
				#pragma target 2.0
				uniform sampler2D _Diffuse; uniform float4 _Diffuse_ST;
				uniform float4 _DiffuseColor;
				uniform sampler2D _DissolveTex; uniform float4 _DissolveTex_ST;
				uniform float _DiffusePower;
				float _DissolveStep;
				float _SoftSize;
				float4 _DissolveColor;
				float _DissolveColorPW;
				float _IsCustom;
				struct VertexInput {
					float4 vertex : POSITION;
					float2 uv0 : TEXCOORD0;
					float4 uv1 : TEXCOORD1;
					float4 uv2 : TEXCOORD2;
					float4 vertexColor : COLOR;
				};
				struct VertexOutput {
					float4 pos : SV_POSITION;
					float2 uv0 : TEXCOORD0;
					float4 uv1 : TEXCOORD1;
					float4 uv2 : TEXCOORD2;
					float4 vertexColor : COLOR;
				};
				VertexOutput vert(VertexInput v) {
					VertexOutput o = (VertexOutput)0;
					o.uv0 = v.uv0;
					o.uv1 = v.uv1;
					o.uv2 = v.uv2;
					o.vertexColor = v.vertexColor;
					o.pos = UnityObjectToClipPos(v.vertex);
					return o;
				}
				float4 frag(VertexOutput i) : COLOR {
					float4 _Diffuse_var = tex2D(_Diffuse,TRANSFORM_TEX(i.uv0 ,_Diffuse));
					float4 dissolveVar = tex2D(_DissolveTex,TRANSFORM_TEX(i.uv0, _DissolveTex));
					float3 diffColor = fixed3(_Diffuse_var.rgb*_DiffusePower*_DiffuseColor.rgb*_DiffuseColor.a*_Diffuse_var.a);
					float3 emissive = i.vertexColor.rgb*diffColor;
					float softTmp = _SoftSize;
					float DissolveStepTmp = _DissolveStep;
					float percent = saturate((max(0.001,dissolveVar.r) - DissolveStepTmp) / (softTmp*DissolveStepTmp));
					float3 finalColor = lerp(emissive, _DissolveColor*_DissolveColorPW*i.vertexColor.rgb*_Diffuse_var.a,step(percent,0.8)*_IsCustom);  
					return fixed4(finalColor*percent,1);

				}
				ENDCG
			}
		}
}

你可能感兴趣的:(shader,图形学,unity,unity3d)