shader之——光照烘焙

Shader "Game_XXX/Scenes/HouseShow"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
    }
    SubShader
    {
        Tags { "Queue"="Geometry"}
 
    Pass
    {
        CGPROGRAM
        #pragma vertex vert
        #pragma fragment frag
        #include "UnityCG.cginc"
//------------------add-------------------
        #pragma multi_compile LIGHTMAP_OFF LIGHTMAP_ON ///// LightMap打开或者关闭
//------------------add-------------------
 
        struct appdata
        {
            float4 vertex : POSITION;
            float2 uv : TEXCOORD0;
//------------------add-------------------
            float2 uvLM:TEXCOORD1;///// 第二套UV
//------------------add-------------------
 
        };
 
        struct v2f
        {
            float2 uv : TEXCOORD0;
            float4 vertex: SV_POSITION;
//------------------add-------------------
            #ifdef LIGHTMAP_ON       
            half2 uvLM : TEXCOORD2;  ///// 如果有烘焙图的话,定义lightMapUV
            #endif  
//------------------add-------------------
                 
        };
 
        sampler2D _MainTex;
        float4 _MainTex_ST;
 
 
        v2f vert (appdata v)
        {
            v2f o;
            o.vertex = UnityObjectToClipPos(v.vertex);
            o.uv = TRANSFORM_TEX(v.uv,_MainTex);
 
//------------------add-------------------
            #ifdef LIGHTMAP_ON 
            o.uvLM = v.uvLM.xy * unity_LightmapST.xy + unity_LightmapST.zw;/////如果有红配图,算UV
            #endif
//------------------add-------------------
 
            return o;
        }
 
 
        fixed4 frag (v2f i) : SV_Target
        {
            fixed4 col = tex2D(_MainTex, i.uv);
 
//------------------add-------------------
            #ifdef LIGHTMAP_ON
            fixed3 lm = ( DecodeLightmap (UNITY_SAMPLE_TEX2D(unity_Lightmap, i.uvLM)));/////
            col.rgb *= lm;/////如果有烘焙图,将烘焙图参与计算
            #endif  
//------------------add-------------------
 
            return col;
        }
        ENDCG
      }
    }
}

猜你喜欢

转载自blog.csdn.net/qq_34562355/article/details/91868184