本系列主要参考《Unity Shaders and Effects Cookbook》一书(感谢原书作者),同时会加上一点个人理解或拓展。
这里是本书所有的插图。这里是本书所需的代码和资源(当然你也可以从官网下载)。
========================================== 分割线 ==========================================
考虑到法向贴图(normal mapping)和程序贴图纹理(procedural textures)两节的内容要么很常见或者很少使用,因此我决定偷懒先不看了。
终于到了Using Textures for Effects的最后一节!这次主要讲一个小技巧,用Shader实现Photoshop的色阶效果。
如果你曾经做过图像编辑工作,例如给你们家的一张合影调色啊,制作游戏贴等,那么你一定明白使用色阶来全局调整图像的重要性和实用性。现在,可以告诉你,你完全可以使用Shaders来创建类似Photoshop色阶的效果。
Photoshop里所有的图像编辑工具和混合模型就是一系列数学操作的结果。从本质上来说,我们是在对像素值与其他某些量进行乘法、加法、除法、或者比较等操作,来得到最后的返回值。这个返回值就是新图像的一个像素值。
当然,我们可以写一大本书来仅仅描述Photoshop效果使用的数学技巧,但这里,我们仅仅关注色阶这一块。在本书的第十章,Screen Effects with Unity Render Textures中,将会涉及更多的高级混合模式。(噢,第十章感觉好远的样子。)
Properties { _MainTex ("Base (RGB)", 2D) = "white" {} //Add the Input Levels Values _inBlack ("Input Black", Range(0, 255)) = 0 _inGamma ("Input Gamma", Range(0, 2)) = 1.61 _inWhite ("Input White", Range(0, 255)) = 255 //Add the Output Levels _outWhite ("Output White", Range(0, 255)) = 255 _outBlack ("Output Black", Range(0, 255)) = 0 }
CGPROGRAM #pragma surface surf Lambert sampler2D _MainTex; //Add these variables //to the CGPROGRAM float _inBlack; float _inGamma; float _inWhite; float _outWhite; float _outBlack;
float GetPixelLevel(float pixelColor) { float pixelResult; pixelResult = (pixelColor * 255.0); pixelResult = max(0, pixelResult - _inBlack); pixelResult = saturate(pow(pixelResult / (_inWhite - _inBlack), _inGamma)); pixelResult = (pixelResult * (_outWhite - _outBlack) + _outBlack)/255.0; return pixelResult; }
//Create a variable to store //a pixel channel from our _MainTex texture float outRPixel = GetPixelLevel(c.r); float outGPixel = GetPixelLevel(c.g); float outBPixel = GetPixelLevel(c.b);
o.Albedo = float3(outRPixel,outGPixel,outBPixel); o.Alpha = c.a;
Shader "Custom/PhotoshopLevels" { Properties { _MainTex ("Base (RGB)", 2D) = "white" {} //Add the Input Levels Values _inBlack ("Input Black", Range(0, 255)) = 0 _inGamma ("Input Gamma", Range(0, 2)) = 1.61 _inWhite ("Input White", Range(0, 255)) = 255 //Add the Output Levels _outWhite ("Output White", Range(0, 255)) = 255 _outBlack ("Output Black", Range(0, 255)) = 0 } SubShader { Tags { "RenderType"="Opaque" } LOD 200 CGPROGRAM #pragma surface surf Lambert sampler2D _MainTex; //Add these variables //to the CGPROGRAM float _inBlack; float _inGamma; float _inWhite; float _outWhite; float _outBlack; struct Input { float2 uv_MainTex; }; float GetPixelLevel(float pixelColor) { float pixelResult; pixelResult = (pixelColor * 255.0); pixelResult = max(0, pixelResult - _inBlack); pixelResult = saturate(pow(pixelResult / (_inWhite - _inBlack), _inGamma)); pixelResult = (pixelResult * (_outWhite - _outBlack) + _outBlack)/255.0; return pixelResult; } void surf (Input IN, inout SurfaceOutput o) { half4 c = tex2D (_MainTex, IN.uv_MainTex); //Create a variable to store //a pixel channel from our _MainTex texture float outRPixel = GetPixelLevel(c.r); float outGPixel = GetPixelLevel(c.g); float outBPixel = GetPixelLevel(c.b); o.Albedo = float3(outRPixel,outGPixel,outBPixel); o.Alpha = c.a; } ENDCG } FallBack "Diffuse" }
pixelResult = (pixelColor * 255.0);
pixelResult = max(0, pixelResult - _inBlack);
pixelResult = saturate(pow(pixelResult / (_inWhite - _inBlack), _inGamma));
pixelResult = (pixelResult * (_outWhite - _outBlack) + _outBlack)/255.0;