Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

How to write emission to lightmap in vert/frag shader?

Discussion in 'Shaders' 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.
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,375
    You need a "meta" pass. Look and the generated code from a surface shader to see what that looks like, or look at the standard shader.
     
  3. tsangwailam

    tsangwailam

    Joined:
    Jul 30, 2013
    Posts:
    280
    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.


    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.         }
     
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,375
    Try posting in the shader and / or global illumination forums. I don't have a ton of experience with the meta pass unfortunately.
     
  5. tsangwailam

    tsangwailam

    Joined:
    Jul 30, 2013
    Posts:
    280
    Thanks. Will try that.
     
  6. 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.  
     
    theANMATOR2b likes this.
  7. vx4

    vx4

    Joined:
    Dec 11, 2012
    Posts:
    182
    Thank
     
  8. ButterBoy93

    ButterBoy93

    Joined:
    Sep 5, 2017
    Posts:
    1
    Hi there, just wanna know how do you actually output emission value to baked lightmap with surface shader? As shown here, the image which shows the environment lighted up is done by the standard shader, attaching the circuitry texture to the emission section of the standard shader. Where as what I have now, I'm able to attached the similar circuitry texture and adjust the brightness of the texture, however, it is not emitting light to the surrounding like what the standard shader does. Please advise. custom.png standard.png
     
  9. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,375
  10. ohookins

    ohookins

    Joined:
    Oct 15, 2018
    Posts:
    10
    Thanks for the example code, but I notice there are two return statements in that fragment shader (I guess the second one is unnecessary). Anyway for me it doesn't work at all.