Search Unity

How can i output emission value to lightmap in vert/frag shader?

Discussion in 'Global Illumination' started by tsangwailam, Mar 26, 2017.

  1. tsangwailam

    tsangwailam

    Joined:
    Jul 30, 2013
    Posts:
    280
    I can output emission value to baked lightmap with a surface shader. But how can i do it in vert/fragment shader.

    I try to add a meta pass like this. Suppose to get red emission on the light map but nothing. Or did i miss something. Thanks.:)

    Code (CSharp):
    1. // ------------------------------------------------------------------
    2.     // Extracts information for lightmapping, GI (emission, albedo, ...)
    3.     // This pass it not used during regular rendering.
    4.         Pass
    5.         {
    6.             Name "META"
    7.             Tags {"LightMode"="Meta"}
    8.             Cull Off
    9.             CGPROGRAM
    10.             #include"UnityStandardMeta.cginc"
    11.             float4 frag_meta2 (v2f_meta i): SV_Target
    12.             {
    13.                 // we're interested in diffuse & specular colors,
    14.                 // and surface roughness to produce final albedo.
    15.            
    16.                 FragmentCommonData data = UNITY_SETUP_BRDF_INPUT (i.uv);
    17.                 UnityMetaInput o;
    18.                 UNITY_INITIALIZE_OUTPUT(UnityMetaInput, o);
    19.                 o.Albedo = fixed3(1,0,0);
    20.                 o.Emission = fixed3(1,0,0);
    21.                 return UnityMetaFragment(o);
    22.             }
    23.        
    24.             #pragma vertex vert_meta
    25.             #pragma fragment frag_meta2
    26.             #pragma shader_feature _EMISSION
    27.             #pragma shader_feature _METALLICGLOSSMAP
    28.             #pragma shader_feature ___ _DETAIL_MULX2
    29.             ENDCG
    30.         }
     
  2. tsangwailam

    tsangwailam

    Joined:
    Jul 30, 2013
    Posts:
    280
    I have working out the code. For those whose interesting to add a emission to their shader, here is the minimal setup.

    Code (CSharp):
    1.  
    2. Pass
    3. {
    4.     Name "META"
    5.     Tags {"LightMode"="Meta"}
    6.     Cull Off
    7.     CGPROGRAM
    8.                
    9.     #include "UnityStandardMeta.cginc"
    10.     #pragma vertex vert_meta
    11.     #pragma fragment frag_meta_custom
    12.  
    13.     fixed4 frag_meta_custom (v2f i) : SV_Target
    14.     {
    15.         // Colors              
    16.         fixed4 col = fixed(1,0,0); // The emission color
    17.  
    18.         // Calculate emission
    19.         UnityMetaInput metaIN;
    20.           UNITY_INITIALIZE_OUTPUT(UnityMetaInput, metaIN);
    21.           metaIN.Albedo = col.rgb;
    22.           metaIN.Emission = col.rgb;
    23.           return UnityMetaFragment(metaIN);
    24.  
    25.         return col;
    26.     }
    27.  
    28.     ENDCG
    29. }
    30.  
     
    UnityLighting likes this.