移动端shader优化
着色器中常用的 数据类型 精度 优化
- (32位精度浮点数值) float 对应数组类型: float2 float3 float4
- (16位精度浮点数值) half 对应数组类型: half2 half3 half4 UV 数据 颜色信息
- (12位精度浮点数值) fixed 对应数组类型: fixed2 fixed3 fixed4 光照计算颜色信息
编译器指令的优化:
- exclude_path:prepass :排除延迟光照 通道;
- nolightmap : 不支持光线映射
- noforwardadd : 只接受 单一直线光源
- halfasview 不使用标准光照函数 处理反射,使用半矢量的方式
Shader "Custom/chapter07/MoblieDiffuse"
{
Properties
{
_Diffuse("Base (RGB) Texture", 2D) = "white"{}
_SpecularInt("Specular Intensity",Range(0.01,1)) = 0.5
_NormalMap("Normal Map", 2D) = "bump"{}
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 200
CGPROGRAM
#pragma surface surf MobileBlinnPhong exclude_path:prepass nolightmap noforwardadd halfasview
sampler2D _Diffuse;
fixed _SpecularInt;
sampler2D _NormalMap;
struct Input
{
half2 uv_Diffuse;
};
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 = _SpecularInt;
o.Normal = UnpackNormal(tex2D(_NormalMap, IN.uv_Diffuse));
}
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;
c.a = 0.0;
return c;
}
ENDCG
}
FallBack "Diffuse"
}