本系列主要参考《Unity Shaders and Effects Cookbook》一书(感谢原书作者),同时会加上一点个人理解或拓展。
这里是本书所有的插图。这里是本书所需的代码和资源(当然你也可以从官网下载)。
========================================== 分割线 ==========================================
在上一篇里,我们学习了一些技巧来初步优化Shader。这次,我们学习更多的技术来实现一个更复杂的Shader:Normal-Mapped Specular Shader。这些技术包括:使用光照函数的两个新变量halfasview或者approxview,减少使用的贴图数量,以及对贴图进行更好的压缩。
Properties {
_Diffuse ("Base (RGB) Specular Amount (A)", 2D) = "white" {}
_NormalMap ("Normal Map", 2D) = "bump"{}
_SpecIntensity ("Specular Width", Range(0.01, 1)) = 0.5
}struct SurfaceOutput {
half3 Albedo; // 该像素的反射率,反应了像素的基色
half3 Normal; // 该像素的法线方向
half3 Emission; // 该像素的自发光颜色,使得即便没有光照也可以物体本身也可以发出光
half Specular; // 该像素的高光指数
half Gloss; // 该像素的高光光滑度,值越大高光反射越清晰,反之越模糊
half Alpha; // 该像素的不透明度
};CGPROGRAM #pragma surface surf MobileBlinnPhong exclude_path:prepass nolightmap noforwardadd halfasview
sampler2D _Diffuse; sampler2D _NormalMap; fixed _SpecIntensity;
struct Input
{
half2 uv_Diffuse;
}; inline fixed4 LightingMobileBlinnPhong (SurfaceOutput s, fixed3 lightDir, fixed3 halfDir, fixed atten)
{
fixed diff = max (0, dot (s.Normal, lightDir));
fixed nh = max (0, dot (s.Normal, halfDir));
fixed spec = pow (nh, s.Specular * 128) * s.Gloss;
fixed4 c;
c.rgb = (s.Albedo * _LightColor0.rgb * diff + _LightColor0.rgb * spec) * (atten * 2);
c.a = 0.0;
return c;
} void surf (Input IN, inout SurfaceOutput o)
{
fixed4 diffuseTex = tex2D (_Diffuse, IN.uv_Diffuse);
o.Albedo = diffuseTex.rgb;
o.Gloss = diffuseTex.a;
o.Alpha = 0.0;
o.Specular = _SpecIntensity;
o.Normal = UnpackNormal(tex2D(_NormalMap, IN.uv_Diffuse));
}【Unity Shaders】Mobile Shader Adjustment —— 为手机定制Shader,布布扣,bubuko.com
【Unity Shaders】Mobile Shader Adjustment —— 为手机定制Shader
原文:http://blog.csdn.net/candycat1992/article/details/38585569