Shader学习案例三---屏幕特效之色彩调节
2017-06-22 15:56
323 查看
更新:意外发现乐乐女神(《Unity Shader入门精要》的作者)早在csdn中写了很多案例,所以请观众转场吧,比我写的好太多,有清晰的解释,后续相同的不再写了,如果有我自己写的Shader,再贴出来供大家参考
来源
书名:Unity 5.x Shaders and Effects Cookbook
目标
调节屏幕的亮度、饱和度、对比度
过程
一、Unity操作
1. 新建场景,创建三个球体,分别赋予红、绿、蓝色的Material;
二、编写Shader
1. Properties块
SubShader块
至于实现原理,我也不知道。。。
三、编写C#脚本
脚本挂载到Camera上即可,代码:
结果
![](https://img-blog.csdn.net/20170622155537870?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXFfMzQ0Njk3MTc=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
来源
书名:Unity 5.x Shaders and Effects Cookbook
目标
调节屏幕的亮度、饱和度、对比度
过程
一、Unity操作
1. 新建场景,创建三个球体,分别赋予红、绿、蓝色的Material;
二、编写Shader
1. Properties块
Properties { _MainTex ("Texture", 2D) = "white" {} _Brightness ("Brightness Amount",Range(0.0,1.0)) = 1.0 _Saturation ("Stauration Amount",Range(0.0,1.0)) = 1.0 _Contrast ("Contrast Amount",Range(0.0,1.0)) = 1.0 }
SubShader块
SubShader { Pass { CGPROGRAM #pragma vertex vert_img #pragma fragment frag #pragma fragmentoption ARB_precision_hint_fastest #include "UnityCG.cginc" sampler2D _MainTex; fixed _Brightness; fixed _Saturation; fixed _Contrast; //自定义色彩调节函数 fixed3 BSCAdjust(fixed3 color,fixed brt,fixed sat,fixed cont){ //构造色彩中值 fixed3 avgLum = fixed3(0.5,0.5,0.5); //构造调节系数 fixed3 lumcoeff = fixed3(0.2125,0.7154,0.0721); //控制亮度Brightness fixed3 brtColor = color * brt; fixed intensityf = dot(brtColor,lumcoeff); fixed3 intensity = fixed3(intensityf,intensityf,intensityf); //控制饱和度 fixed3 satColor = lerp(intensity,brtColor,sat); //控制对比度 fixed3 conColor = lerp(avgLum,satColor,cont); return conColor; } fixed4 frag (v2f_img i) : SV_Target { fixed4 col = tex2D(_MainTex, i.uv); // 调用调节函数 col.rgb = BSCAdjust(col.rgb,_Brightness,_Saturation,_Contrast); return col; } ENDCG } }
至于实现原理,我也不知道。。。
三、编写C#脚本
脚本挂载到Camera上即可,代码:
using System.Collections; using System.Collections.Generic; using UnityEngine; [ExecuteInEditMode] public class BSCAdjust : MonoBehaviour { public Shader curShader; [Header("Adjust Parameters")] [Range(0.0f,1.0f)] public float brightness = 1.0f; [Range(0.0f,1.0f)] public float saturation = 1.0f; [Range(0.0f,1.0f)] public float contrast = 1.0f; private Material material; public Material curMaterial { get{ if (material == null) { material = new Material (curShader); material.hideFlags = HideFlags.HideAndDontSave; } return material; } } // Use this for initialization void Start () { if (!SystemInfo.supportsImageEffects) { enabled = false; return; } } void OnDisable(){ if (material != null) { DestroyImmediate (material); } } void OnRenderImage(RenderTexture srcTexture,RenderTexture destTexture){ if (curShader != null && curShader.isSupported) { curMaterial.SetFloat ("_Brightness", brightness); curMaterial.SetFloat ("_Saturation", saturation); curMaterial.SetFloat ("_Contrast", contrast); Graphics.Blit (srcTexture, destTexture, curMaterial); } else { Graphics.Blit (srcTexture, destTexture); } } }
结果
相关文章推荐
- Shader学习案例一---屏幕特效之黑白化当前屏幕
- 【猫猫的Unity Shader之旅】之初识屏幕特效
- UnityShader实例12:屏幕特效之马赛克(Mosaic)材质
- Unity3D Shader编程】之十一 深入理解Unity5中的Standard Shader(三)&屏幕像素化特效的实现
- unity使用屏幕后处理实现闪烁特效,创建新的shader文件过程
- UnityShader 屏幕特效入门前
- c#学习(2):大图片的特效处理与获取屏幕上任一点的rgb值
- 【unity】【shader】屏幕高斯模糊(Gaussian Blur)后期特效的实现
- Unity3D Shader编程】之八 Unity5新版Shader模板源码解析&运动模糊(径向模糊)屏幕特效的实现
- Unity&Shader案例篇—屏幕渐暗效果
- Unity shader学习之屏幕后期处理效果之边缘检测
- UnityShader实例13:屏幕特效之均值模糊(Box Blur)
- 深入理解Unity5中的StandardShader屏幕像素化特效的实现
- 【浅墨Unity3D Shader编程】之十 深入理解Unity5中的Standard Shader(二)&屏幕油画特效的实现
- Unity3D Shader编程】之九 深入理解Unity5中的Standard Shader (一)&屏幕水幕特效的实现
- UnityShader - 屏幕特效 - 高斯模糊(Gaussian Blur)
- Shader创建老电影风格的屏幕特效
- 【浅墨Unity3D Shader编程】之八 Unity5新版Shader模板源码解析&运动模糊(径向模糊)屏幕特效的实现
- Shader学习之案例—《镜子1》
- Unity&Shader案例篇—屏幕渐暗效果