适合手机上用的实时光照shader,移动平台上着色器的优化

移动平台上着色器的优化

最近在看本书叫《unity着色器和屏幕特效开发秘籍》挺不错的。喜欢的朋友可以下载http://pan.baidu.com/s/1bXwMEM

直接上源码吧,有注释看着舒服!

Shader "LT/NoLightmap/MobileBlinnPhongr" {
Properties {
_Color ("Color", Color) = (1,1,1,1)
_MainTex ("Albedo (RGB)", 2D) = "white" {}    //A通道存储高光
_NormalMap ("Normal Map", 2D) = "bump" {}    
_SpecIntensity ("Specular Width", Range(0.01,1)) = 0.5
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 200

CGPROGRAM
// Physically based Standard lighting model, and enable shadows on all light types
//忽略延迟光照,不支持光照贴图,noforwardadd只接受一个单一的平行光光源作为逐像素光源,其他灯光强制转为逐顶点的光照
//最后,使用halfasview声明告诉Unity,我们使用一个介于光照方向和观察方向之间的half vector来代替真正的观察方向viewDir来计算光照函数。
//这将加速Shader的处理时间,因为这是基于逐顶点而非逐像素计算而得的。虽然这样得到的结果是近似值,但对于移动平台来说足够了。
#pragma surface surf MobileBlinnPhong exclude_path:prepass nolightmap noforwardadd  halfasview
#pragma exclude_renderers flash d3d11

// Use shader model 3.0 target, to get nicer looking lighting
#pragma target 2.0


sampler2D _MainTex;
sampler2D _NormalMap;
half _SpecIntensity;
fixed4 _Color;

//前面还一个顶点函数给出IN

//IN相当于是 frag的输入
struct Input {
half2 uv_MainTex;
};

void surf (Input IN, inout SurfaceOutput o) {
// Albedo comes from a texture tinted by color
fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
o.Albedo = c.rgb;
o.Gloss = c.a;
o.Alpha = 0;
o.Specular = _SpecIntensity;
o.Normal = UnpackNormal(tex2D(_NormalMap, IN.uv_MainTex));
o.Alpha = c.a;
}

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;
return c;
}
ENDCG
}
FallBack "Diffuse"
}

你可能感兴趣的:(unity,shader,unity,surface,shader)