Search Unity

Custom shader with GI and emissive not working with multi-colored emissive map

Discussion in 'Shaders' started by TEEBQNE, Jun 27, 2021.

  1. TEEBQNE

    TEEBQNE

    Joined:
    Jan 25, 2017
    Posts:
    88
    I have a custom shader that uses the Unity GI template as a base and am trying to use an emissive map that has multiple colors. Item_Ball_Emission.png When I use this emissive map for a standard shader, it works properly and as expected by having colored emissive where the circles are. However, when using a custom shader that only adds an emissive to the Unity GI base shader, the entire object turns white. Here is the current code I am using

    Code (CSharp):
    1. Shader "Custom/TestShader"
    2. {
    3.     Properties{
    4.             _MainTex("Albedo (RGB)", 2D) = "white" {}
    5.             [HDR]_EmissionColor("Color", Color) = (0,0,0)
    6.             _EmissionMap("Emission", 2D) = "white" {}
    7.     }
    8.         SubShader{
    9.             Tags { "RenderType" = "Opaque" }
    10.    
    11.             CGPROGRAM
    12.             #pragma surface surf StandardDefaultGI
    13.    
    14.             #include "UnityPBSLighting.cginc"
    15.    
    16.             sampler2D _MainTex;
    17.    
    18.             inline half4 LightingStandardDefaultGI(SurfaceOutputStandard s, half3 viewDir, UnityGI gi)
    19.             {
    20.                 return LightingStandard(s, viewDir, gi);
    21.             }
    22.    
    23.             inline void LightingStandardDefaultGI_GI(
    24.                 SurfaceOutputStandard s,
    25.                 UnityGIInput data,
    26.                 inout UnityGI gi)
    27.             {
    28.                 LightingStandard_GI(s, data, gi);
    29.             }
    30.  
    31.             fixed4 _EmissionColor;
    32.             sampler2D _EmissionMap;
    33.  
    34.    
    35.             struct Input {
    36.                 float2 uv_MainTex;
    37.             };
    38.    
    39.             void surf(Input IN, inout SurfaceOutputStandard o) {
    40.                 o.Albedo = tex2D(_MainTex, IN.uv_MainTex);
    41.                 o.Emission = tex2D(_EmissionMap, IN.uv_MainTex).a *_EmissionColor;
    42.             }
    43.             ENDCG
    44.     }
    45.         FallBack "Diffuse"
    46. }
    Screen Shot 2021-06-27 at 1.19.03 AM.png
    The left image is the current shader I have attached and the right is a standard shader with the emissive map. Whenever I use any other emissive maps, the current shader is fine. See below:

    Screen Shot 2021-06-27 at 1.49.22 AM.png

    How should I properly incorporate the emissive into the Unity templated GI shader to properly use my emssive map?
     
  2. TEEBQNE

    TEEBQNE

    Joined:
    Jan 25, 2017
    Posts:
    88
    Here is the solution I came up with to get Emission working with GI.

    Code (csharp):
    1.  
    2. Shader "Custom/TestShader"
    3. {
    4.     Properties{
    5.             _MainTex("Albedo (RGB)", 2D) = "white" {}
    6.             [HDR]_EmissionColor("Color", Color) = (0,0,0)
    7.             _EmissionMap("Emission", 2D) = "white" {}
    8.             _Color ("Color", Color) = (1,1,1,1)
    9.     }
    10.         SubShader{
    11.             Pass
    12.             {
    13.                 Name "META"
    14.                 Tags {"LightMode"="Meta"}
    15.                 Cull Off
    16.                 CGPROGRAM
    17.  
    18.                 #include"UnityStandardMeta.cginc"
    19.  
    20.                 sampler2D _GIAlbedoTex;
    21.                 fixed4 _GIAlbedoColor;
    22.  
    23.                 float4 frag_meta2 (v2f_meta i): SV_Target
    24.                 {
    25.                     FragmentCommonData data = UNITY_SETUP_BRDF_INPUT (i.uv);
    26.                     UnityMetaInput o;
    27.                     UNITY_INITIALIZE_OUTPUT(UnityMetaInput, o);
    28.                     fixed4 c = tex2D (_GIAlbedoTex, i.uv);
    29.                     o.Albedo = fixed3(c.rgb * _GIAlbedoColor.rgb);
    30.                     o.Emission = tex2D(_EmissionMap, i.uv) * _EmissionColor;
    31.                     return UnityMetaFragment(o);
    32.                 }
    33.                
    34.                 #pragma vertex vert_meta
    35.                 #pragma fragment frag_meta2
    36.                 #pragma shader_feature _EMISSION
    37.                 #pragma shader_feature _METALLICGLOSSMAP
    38.                 #pragma shader_feature ___ _DETAIL_MULX2
    39.                 ENDCG
    40.             }
    41.  
    42.             Tags { "RenderType" = "Opaque" }
    43.    
    44.             CGPROGRAM
    45.             #pragma surface surf StandardDefaultGI
    46.    
    47.             #include "UnityPBSLighting.cginc"
    48.    
    49.             sampler2D _MainTex;
    50.             sampler2D _EmissionMap;
    51.             fixed4 _EmissionColor;
    52.    
    53.             inline half4 LightingStandardDefaultGI(SurfaceOutputStandard s, half3 viewDir, UnityGI gi)
    54.             {
    55.                 return LightingStandard(s, viewDir, gi);
    56.             }
    57.    
    58.             inline void LightingStandardDefaultGI_GI(
    59.                 SurfaceOutputStandard s,
    60.                 UnityGIInput data,
    61.                 inout UnityGI gi)
    62.             {
    63.                 LightingStandard_GI(s, data, gi);
    64.             }
    65.  
    66.             fixed4 _Color;
    67.  
    68.    
    69.             struct Input {
    70.                 float2 uv_MainTex;
    71.             };
    72.    
    73.             void surf(Input IN, inout SurfaceOutputStandard o) {
    74.                 half4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
    75.                 o.Albedo = c.rgb;
    76.                 o.Alpha = c.a;
    77.                 o.Emission = tex2D(_EmissionMap, IN.uv_MainTex) * _EmissionColor;
    78.             }
    79.             ENDCG
    80.     }
    81.         FallBack "Diffuse"
    82. }
    83. [/code