Shader混合模式--正片叠底、滤色、叠加

本文介绍了Shader中的正片叠底(Multiply)、滤色(Screen)和叠加(Overlay)三种混合模式。正片叠底会导致图像变暗,滤色则使图像变亮,叠加则是结合两者特性,使亮部更亮、暗部更暗。
摘要由CSDN通过智能技术生成

叠加在书本168页。


Shader "Custom/BlendMode_Effect" {

Properties {
_MainTex ("Albedo (RGB)", 2D) = "white" {}
_Blendtex("Blend Texture",2D) = "white"{}
_Opacity("Blend Opacity",Range(0,1)) = 1
}
SubShader {
Pass
{
CGPROGRAM
#pragma vertex vert_img
#pragma fragment frag
#pragma fragmentoption ARB_precision_hint_fastest
#include "UnityCG.cginc"
uniform sampler2D _MainTex;
uniform sampler2D _BlendTex;
fixed _Opacity;


fixed4 frag(v2f_img i) : COLOR
{
fixed4 renderTex = tex2D(_MainTex,i.uv);
fixed4 blendTex= tex2D(_BlendTex,i.uv);

//fixed4 blendedMultiply = renderTex * blendTex;
fixed4 blendedScreen = (1.0 - ((1.0 - renderTex) * (1.0 - blendTex)));//这里是颜色计算核心
renderTex= lerp(renderTex,blendedScreen,_Opacity);
return renderTex;
}
ENDCG



}

}


			fixed OverlayBlendMode(fixed basePixel, fixed blendPixel) {
				if (basePixel < 0.5) {
					return (2.0 * basePixel * blendPixel);
				} else {
					return (1.0 - 2.0 * (1.0 - basePixel) * (1.0 - blendPixel));
				}
			}

			fixed4 frag(v2f_img i) : COLOR {
				//Get the colors from the RenderTexture and the uv's
				//from the v2f_img struct
				fixed4 renderTex = tex2D(_MainTex, i.uv);
				fixed4 blendTex = tex2D(_BlendTex, i.uv);
				
				fixed4 blendedImage = renderTex;
				
				blendedImage.r = OverlayBlendMode(renderTex.r, blendTex.r);
				blendedImage.g = OverlayBlendMode(renderTex.g, blendTex.g);
				blendedImage.b = OverlayBlendMode(renderTex.b, blendTex.b);

				// Adjust amount of Blend Mode with a lerp
				renderTex = lerp(renderTex, blendedImage,  _Opacity);
				
				return renderTex;
			}

叠加模式的算法



知识补习


这里增加一个内容,就是对各种混合模式的理解。


正片叠底(Multiply)和滤色(Screen)


正片叠底(Multiply)和滤色(Screen)是两种基本的混合模式,分别用于使图片变暗和变亮。它们之间的组合还可以形成更复杂的混合模式,如叠加(Overlay)和柔光(Soft Light)。


正片叠底 —— 就是把两层图像的像素相乘,最后会得到一个更暗的图像。这个模式是对称的,也就是说交换基色和混合色得到的结果是一样的。


,其中a是基色,b是混合色。


滤色 —— 首先把两层图像的像素值取互补数,然后将它们相乘,最后再去互补数。这和正片叠底得到的结果是相反的。它会得到一个更亮的图像。

,其中a是基色,b是混合色。


叠加 —— 结合了正片叠底和滤色两种混合模式。基色中亮色的部分会更加亮,而暗色的部分会更暗。

,其中a是基色,b是混合色。


更多信息:http://blog.csdn.net/candycat1992/article/details/39343309


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值