第六章 移動端shader優化

                                       移動端shader優化


着色器中常用的 數據類型 精度 優化

  • (32位精度浮點數值) float   對應數組類型:   float2   float3   float4
  • (16位精度浮點數值) half    對應數組類型:   half2    half3    half4                      UV 數據 顏色信息
  • (12位精度浮點數值) fixed   對應數組類型:   fixedfixed3   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"
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章