标签:des style blog http color io os 使用 ar
本系列主要参考《Unity Shaders and Effects Cookbook》一书(感谢原书作者),同时会加上一点个人理解或拓展。
这里是本书所有的插图。这里是本书所需的代码和资源(当然你也可以从官网下载)。
========================================== 分割线 ==========================================
画面特效并不只限于调整游戏画面的颜色。我们还可以使用它们来和其他的图片混合起来。这个技术和在Photoshop里新建一个layer很像,我们可以选择一个混合模式来混合两张图片,在我们的例子里,其中一张就是指render texture。这使得美术人员可以在游戏里面模拟各种混合效果,而不是仅仅在Photoshop里。
这篇文章里,我们将要学习一些常见的混合模式,例如,正片叠底(Multiply),Add,滤色(Screen,这竟然是滤色的意思。。。)。你将会发现这些都不难实现~
这里增加一个内容,就是对各种混合模式的理解。
正片叠底(Multiply)和滤色(Screen)是两种基本的混合模式,分别用于使图片变暗和变亮。它们之间的组合还可以形成更复杂的混合模式,如叠加(Overlay)和柔光(Soft Light)。
正片叠底 —— 就是把两层图像的像素相乘,最后会得到一个更暗的图像。这个模式是对称的,也就是说交换基色和混合色得到的结果是一样的。
,其中a是基色,b是混合色。
滤色 —— 首先把两层图像的像素值取互补数,然后将它们相乘,最后再去互补数。这和正片叠底得到的结果是相反的。它会得到一个更亮的图像。
,其中a是基色,b是混合色。
叠加 —— 结合了正片叠底和滤色两种混合模式。基色中亮色的部分会更加亮,而暗色的部分会更暗。
,其中a是基色,b是混合色。
这一篇同样很多代码是建立在上一篇的基础上,所以很多代码不用写啦~
Properties {
_MainTex ("Base (RGB)", 2D) = "white" {}
_BlendTex ("Blend Texture", 2D) = "white" {}
_Opacity ("Blend Opacity", Range(0.0, 1.0)) = 1.0
} Pass {
CGPROGRAM
#pragma vertex vert_img
#pragma fragment frag
#include "UnityCG.cginc"
uniform sampler2D _MainTex;
uniform sampler2D _BlendTex;
fixed _Opacity; 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);
// Perform a multiply Blend mode
fixed4 blendedMultiply = renderTex * blendTex;
// Adjust amount of Blend Mode with a lerp
renderTex = lerp(renderTex, blendedMultiply, _Opacity);
return renderTex;
}#region Variables public Shader curShader; public Texture2D blendTexture; public float blendOpacity = 1.0f; private Material curMaterial; #endregion
void OnRenderImage (RenderTexture sourceTexture, RenderTexture destTexture){
if (curShader != null) {
material.SetTexture("_BlendTex", blendTexture);
material.SetFloat("_Opacity", blendOpacity);
Graphics.Blit(sourceTexture, destTexture, material);
} else {
Graphics.Blit(sourceTexture, destTexture);
}
} void Update () {
blendOpacity = Mathf.Clamp(blendOpacity, 0.0f, 1.0f);
} 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);
// Perform a multiply Blend mode
// fixed4 blendedMultiply = renderTex * blendTex;
// Perform a add Blend mode
fixed4 blendedAdd = renderTex + blendTex;
// Adjust amount of Blend Mode with a lerp
renderTex = lerp(renderTex, blendedAdd, _Opacity);
return renderTex;
} 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);
// Perform a multiply Blend mode
// fixed4 blendedMultiply = renderTex * blendTex;
// Perform a add Blend mode
// fixed4 blendedAdd = renderTex + blendTex;
// Perform a screen render Blend mode
fixed4 blendedScreen = 1.0 - ((1.0 - renderTex) * (1.0 - blendTex));
// Adjust amount of Blend Mode with a lerp
renderTex = lerp(renderTex, blendedScreen, _Opacity);
return renderTex;
}【Unity Shaders】使用Unity Render Textures实现画面特效——画面特效中的类似Photoshop的基本混合模式
标签:des style blog http color io os 使用 ar
原文地址:http://blog.csdn.net/candycat1992/article/details/39343309