标签:des style blog http color io 使用 ar strong
本系列主要参考《Unity Shaders and Effects Cookbook》一书(感谢原书作者),同时会加上一点个人理解或拓展。
这里是本书所有的插图。这里是本书所需的代码和资源(当然你也可以从官网下载)。
========================================== 分割线 ==========================================
通过上一篇我们建立了一个简化的屏幕特效系统,在这一篇,我们开始学习创建更复杂的像素操作来实现一些在当代游戏中常见的屏幕特效。
使用屏幕特效来调整游戏的整体色彩是非常重要的,这给了设计师控制游戏最后全貌的能力。比如,通过颜色调整条来调整游戏最终画面的红蓝绿三色的密度,或者通过添加一定的色调使整个屏幕看起来像是老电影那样的效果。
这篇文章里,我们将会更多地学习关于调整图像颜色方面的内容。也就是,亮度、饱和度和对比度。学习这些调整颜色的方法可以让我们对屏幕特效有一个很好的理解。
这一篇很多代码是建立在上一篇的基础上,所以很多代码不用写啦~
Properties { _MainTex ("Base (RGB)", 2D) = "white" {} _BrightnessAmount ("Brightness Amount", Range(0.0, 2.0)) = 1.0 _SaturationAmount ("Saturation Amount", Range(0.0, 1.0)) = 1.0 _ContrastAmount ("Contrast Amount", Range(0.0, 1.0)) = 1.0 }
SubShader { Pass { CGPROGRAM #pragma vertex vert_img #pragma fragment frag #include "UnityCG.cginc" uniform sampler2D _MainTex; fixed _BrightnessAmount; fixed _SaturationAmount; fixed _ContrastAmoun
float3 ContrastSaturationBrightness (float3 color, float brt, float sat, float con) { // Increase or decrease these values to // adjust r, g and b color channels separately float avgLumR = 0.5; float avgLumG = 0.5; float avgLumB = 0.5; // Luminance coefficients for getting luminance from the image float3 LuminanceCoeff = float3 (0.2125, 0.7154, 0.0721); // Operation for brightmess float3 avgLumin = float3 (avgLumR, avgLumG, avgLumB); float3 brtColor = color * brt; float intensityf = dot (brtColor, LuminanceCoeff); float3 intensity = float3 (intensityf, intensityf, intensityf); // Operation for saturation float3 satColor = lerp (intensity, brtColor, sat); // Operation for contrast float3 conColor = lerp (avgLumin, satColor, con); return conColor; }
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); //Apply the brightness, saturation, contrast operations renderTex.rgb = ContrastSaturationBrightness (renderTex.rgb, _BrightnessAmount, _SaturationAmount, _ContrastAmount); return renderTex; }
#region Variables public Shader curShader; public float brightnessAmount = 1.0f; public float saturationAmount = 1.0f; public float contrastAmount = 1.0f; private Material curMaterial; #endregion
void OnRenderImage (RenderTexture sourceTexture, RenderTexture destTexture){ if (curShader != null) { material.SetFloat("_BrightnessAmount", brightnessAmount); material.SetFloat("_SaturationAmount", saturationAmount); material.SetFloat("_ContrastAmount", contrastAmount); Graphics.Blit(sourceTexture, destTexture, material); } else { Graphics.Blit(sourceTexture, destTexture); } }
// Update is called once per frame void Update () { brightnessAmount = Mathf.Clamp(brightnessAmount, 0.0f, 2.0f); saturationAmount = Mathf.Clamp(saturationAmount, 0.0f, 2.0f); contrastAmount = Mathf.Clamp(contrastAmount, 0.0f, 3.0f); }
【Unity Shaders】使用Unity Render Textures实现画面特效——画面特效中的亮度、饱和度和对比度
标签:des style blog http color io 使用 ar strong
原文地址:http://blog.csdn.net/candycat1992/article/details/39315065