今天我写一个最简单的溶解shader,在标准surface基础上写

下面是新建的surface shader

Shader \"Custom/dissolve\" {
	Properties {
		_Color (\"Color\", Color) = (1,1,1,1)
		_MainTex (\"Albedo (RGB)\", 2D) = \"white\" {}
		_Glossiness (\"Smoothness\", Range(0,1)) = 0.5
		_ llic (\" llic\", Range(0,1)) = 0.0
	}
	SubShader {
		Tags { \"RenderType\"=\"Opaque\" }
		LOD 200

		CGPROGRAM
		// Physically  d Standard lighting model, and enable shadows on all light types
		#pragma surface surf Standard fullforwardshadows

		// Use shader model 3.0 target, to get nicer looking lighting
		#pragma target 3.0

		sampler2D _MainTex;

		struct Input {
			float2 uv_MainTex;
		};

		half _Glossiness;
		half _ llic;
		fixed4 _Color;

		// Add instancing support for this shader. You need to check \'Enable Instancing\' on materials that use the shader.
		// See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
		// #pragma instancing_options assumeuniformscaling
		UNITY_INSTANCING_BUFFER_START(Props)
			// put more per-instance properties here
		UNITY_INSTANCING_BUFFER_END(Props)

		void surf (Input IN, inout SurfaceOutputStandard o) {
			// Albedo comes from a texture tinted by color
			fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
			o.Albedo = c.rgb;
			//  llic and smoothness come from slider variables
			o. llic = _ llic;
			o.Smoothness = _Glossiness;
			o.Alpha = c.a;
		}
		ENDCG
	}
	FallBack \"Diffuse\"
}

溶解我们需要加参数,溶解图,溶解颜色,溶解进度

		_DissolveTex(\"dissolve tex\",2D) = \"\"{}
		//溶解纹理,根据这张图的R值(因为是黑白图所以用rgb其中一个通道都可以)来做溶解判断,如果是彩色图自己去判断用哪个通道
		//其实就是用这张图的特征来实现溶解样式
		_DissolveColor(\"dissolve color\",Color) = (0,0,0,1)//溶解颜色
		_DissolveProgress(\"dissolve Progress\",Range(0,10)) = 0//溶解进度

下面是溶解的计算

原理是拿UV上面的像素颜色R和当前的进度做对比,如果进度大于像素颜色值就抛弃像素就不显示出来

这个我取颜色的R通道,因为是黑白图用哪个通道都可以

            //获取uv坐标上的像素颜色
			float dissolve_c_r = tex2D(_DissolveTex, IN.uv_MainTex).r;

			//计算进度(0,1)范围内
			float Progress = saturate(_DissolveProgress / 10);

			//如果进度大于像素颜色则抛弃这个像素
			if (Progress > dissolve_c_r)
			{
				//抛弃像素
				discard;
			}

			//计算单个像素颜色插值速度
			float rate = Progress / dissolve_c_r;

			//原本颜色和溶解颜色插值
			c.rgb = lerp(c.rgb, _DissolveColor.rgb, rate);

下面是效果

\"\"

用什么shader在颜色赋值之前加入溶解代码就能实现溶解,如果有描边那些就不好处理,可以搞个开关屏蔽描边再做溶解效果

下面是完整shader

Shader \"Custom/dissolve\" {
	Properties {
		_Color (\"Color\", Color) = (1,1,1,1)
		_MainTex (\"Albedo (RGB)\", 2D) = \"white\" {}
		_Glossiness (\"Smoothness\", Range(0,1)) = 0.5
		_ llic (\" llic\", Range(0,1)) = 0.0

		_DissolveTex(\"dissolve tex\",2D) = \"\"{}
		//溶解纹理,根据这张图的R值(因为是黑白图所以用rgb其中一个通道都可以)来做溶解判断,如果是彩色图自己去判断用哪个通道
		//其实就是用这张图的特征来实现溶解样式
		_DissolveColor(\"dissolve color\",Color) = (0,0,0,1)//溶解颜色
		_DissolveProgress(\"dissolve Progress\",Range(0,10)) = 0//溶解进度
	}
	SubShader {
		Tags { \"RenderType\"=\"Opaque\" }
		LOD 200

		CGPROGRAM
		// Physically  d Standard lighting model, and enable shadows on all light types
		#pragma surface surf Standard fullforwardshadows

		// Use shader model 3.0 target, to get nicer looking lighting
		#pragma target 3.0

		sampler2D _MainTex;

		sampler2D _DissolveTex;
		float4 _DissolveColor;
		float _DissolveProgress;

		struct Input {
			float2 uv_MainTex;
		};

		half _Glossiness;
		half _ llic;
		fixed4 _Color;

		// Add instancing support for this shader. You need to check \'Enable Instancing\' on materials that use the shader.
		// See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
		// #pragma instancing_options assumeuniformscaling
		UNITY_INSTANCING_BUFFER_START(Props)
			// put more per-instance properties here
		UNITY_INSTANCING_BUFFER_END(Props)

		void surf (Input IN, inout SurfaceOutputStandard o) {
			// Albedo comes from a texture tinted by color
			fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;

			//获取uv坐标上的像素颜色
			float dissolve_c_r = tex2D(_DissolveTex, IN.uv_MainTex).r;

			//计算进度(0,1)范围内
			float Progress = saturate(_DissolveProgress / 10);

			//如果进度大于像素颜色则抛弃这个像素
			if (Progress > dissolve_c_r)
			{
				//抛弃像素
				discard;
			}

			//计算单个像素颜色插值速度
			float rate = Progress / dissolve_c_r;

			//原本颜色和溶解颜色插值
			c.rgb = lerp(c.rgb, _DissolveColor.rgb, rate);

			o.Albedo = c.rgb;
			//  llic and smoothness come from slider variables
			o. llic = _ llic;
			o.Smoothness = _Glossiness;
			o.Alpha = c.a;
		}
		ENDCG
	}
	FallBack \"Diffuse\"
}

下面是工程链接

链接:https://pan.baidu.com/s/1n-orx6V4yDbswEMC4mcmHg 
提取码:nj42 

收藏 打印