Search Unity

Question Using light maps in a custom shader (built-in render pipeline)

Discussion in 'Shaders' started by Samuel_Herb, Jun 23, 2020.

  1. Samuel_Herb

    Samuel_Herb

    Joined:
    Jun 23, 2020
    Posts:
    8
    Hey all! first time posting here. I'm an artist / tech artist / designer at a medium sized indie studio.

    I'm having some trouble reading lightmaps into my custom shader. Objects appear as if the lightmap is pure black. Any idea why this might be? I've uploaded my shader here. It's for the built-in deferred pipeline. Sorry it's a bit of a mess. This is the relevant line-
    half3 lightmap = (DecodeLightmap(UNITY_SAMPLE_TEX2D(unity_Lightmap, i.uv1)));

    A few things that do work-
    The GI UVs are working properly as far as I can tell.
    The shader is writing into the light maps (and light probes) correctly using a meta pass.
    The shader can read from the light probes correctly using unity's ShadeSH9 trickiness.
    The shader can output to SV_Target3 (the ambient light buffer) correctly.
     

    Attached Files:

  2. Samuel_Herb

    Samuel_Herb

    Joined:
    Jun 23, 2020
    Posts:
    8
    Code (CSharp):
    1. Shader "Serenity Forge/WitchShader"
    2. {
    3.     Properties
    4.     {
    5.         _Color ("Color", Color) = (1,1,1,1)
    6.         _MainTex ("Texture", 2D) = "white" {}
    7.         _NormTex("Normal Map", 2D) = "bump" {}
    8.         _AmbOverride("Ambient Override", 2D) = "black"{}
    9.         _AmbColor("Ambient Color", Color) = (0,0,0,1)
    10.         _Rough("Roughness", range(0,1)) = 0
    11.         _Metalic("Metalic", range(0,1)) = 0
    12.         _Jitter ("Jitter", range(0,1)) = 0
    13.     }
    14.  
    15.         SubShader{
    16.         // Copied directly from Unity documentation. Need to rewrite to support vertex colors
    17.  
    18.         // Extracts information for lightmapping, GI (emission, albedo, ...)
    19.         // This pass is not used during regular rendering.
    20.             Pass
    21.             {
    22.                 Name "META"
    23.                 Tags {"LightMode" = "Meta"}
    24.                 Cull Off
    25.                 CGPROGRAM
    26.  
    27.                 #include"UnityStandardMeta.cginc"
    28.  
    29.                 sampler2D _GIAlbedoTex;
    30.                 fixed4 _GIAlbedoColor;
    31.                 float4 frag_meta2(v2f_meta i) : SV_Target
    32.                 {
    33.                     // We're interested in diffuse & specular colors
    34.                     // and surface roughness to produce final albedo.
    35.  
    36.                     FragmentCommonData data = UNITY_SETUP_BRDF_INPUT(i.uv);
    37.                     UnityMetaInput o;
    38.                     UNITY_INITIALIZE_OUTPUT(UnityMetaInput, o);
    39.                     fixed4 c = tex2D(_GIAlbedoTex, i.uv);
    40.                     float4 mainTex = tex2D(_MainTex, i.uv);
    41.                     o.Albedo = mainTex * _Color;
    42.                     //o.Emission = Emission(i.uv.xy);
    43.                     return UnityMetaFragment(o);
    44.                 }
    45.  
    46.                 #pragma vertex vert_meta
    47.                 #pragma fragment frag_meta2
    48.                 #pragma shader_feature _EMISSION
    49.                 #pragma shader_feature _METALLICGLOSSMAP
    50.                 #pragma shader_feature ___ _DETAIL_MULX2
    51.                 ENDCG
    52.             }
    53.         }
    54.  
    55.     SubShader
    56.     {
    57.         Tags { "RenderType"="Cutout" "LightMode" = "Deferred"}
    58.         LOD 100
    59.  
    60.         //Cull Off
    61.        
    62.  
    63.         Pass
    64.         {
    65.             CGPROGRAM
    66.             #pragma vertex vert
    67.             #pragma fragment frag
    68.             #pragma multi_compile _ LIGHTMAP_ON
    69.  
    70.             #include "UnityCG.cginc"
    71.             #include "UnityShaderVariables.cginc"
    72.             #include "UnityStandardBRDF.cginc"
    73.  
    74.             struct appdata
    75.             {
    76.                 float2 uv : TEXCOORD0;
    77.                 float4 uv1 : TEXCOORD1;
    78.                 float4 vertex : POSITION;
    79.                 float3 normal : NORMAL;
    80.                 float4 tangent : TANGENT;
    81.                 float4 color : COLOR;
    82.             };
    83.  
    84.             struct v2f
    85.             {
    86.                 float2 uv : TEXCOORD0;
    87.                 float4 uv1 : TEXCOORD1;
    88.                 half3 worldNormal : TEXCOORD2;
    89.                 float3 tangentDir : TEXCOORD3;
    90.                 float3 bitangentDir : TEXCOORD4;
    91.                 half3 viewNormal : TEXCOORD5;
    92.                 half4 color : TEXCOORD6;
    93.                 float3 worldPos : TEXCOORD7;
    94.                 float4 vertex : SV_POSITION;
    95.             };
    96.  
    97.             float4 _Color;
    98.             sampler2D _MainTex;
    99.             float4 _MainTex_ST;
    100.             sampler2D _NormTex;
    101.             float4 _NormTex_ST;
    102.             sampler2D _AmbOverride;
    103.             float4 _AmbColor;
    104.             fixed _Jitter;
    105.             fixed _Rough;
    106.             fixed _Metalic;
    107.  
    108.  
    109.             v2f vert (appdata v)
    110.             {
    111.                 v2f o;
    112.                 o.vertex = UnityObjectToClipPos(v.vertex);
    113.                 o.color = v.color;
    114.                 o.worldNormal = UnityObjectToWorldNormal(v.normal);
    115.                 o.tangentDir = normalize(mul(unity_ObjectToWorld, float4(v.tangent.xyz, 0.0)).xyz);
    116.                 o.bitangentDir = normalize(cross(o.worldNormal, o.tangentDir) * v.tangent.w);
    117.                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    118.                 UNITY_TRANSFER_FOG(o,o.vertex);
    119.                 o.uv.xy = v.uv.xy * _MainTex_ST.xy + _MainTex_ST.zw;
    120.                 o.uv1.xy = v.uv1.xy * unity_LightmapST.xy + unity_LightmapST.zw;
    121.                 return o;
    122.             }
    123.  
    124.             struct fragmentdata {
    125.                 half4 albedo : SV_Target0; //RT0, ARGB32 format: Diffuse color (RGB), occlusion (A).
    126.                 half4 specular : SV_Target1; //RT1, ARGB32 format: Specular color (RGB), roughness(A).
    127.                 half4 normal : SV_Target2; //RT2, ARGB2101010 format: World space normal (RGB), unused (A).
    128.                 half4 light : SV_Target3; //RT3, ARGB2101010 (non-HDR) or ARGBHalf (HDR) format: Emission + lighting + lightmaps + reflection probes buffer.
    129.             };
    130.  
    131.             fragmentdata frag (v2f i) : SV_Target
    132.             {
    133.  
    134.                 fragmentdata f;
    135.                
    136.                 //albedo
    137.                 float4 mainTex = tex2D(_MainTex, i.uv);
    138.                 fixed4 color =  mainTex * i.color * _Color;
    139.                 f.albedo.rgb = color * (_Metalic*-1+1);
    140.                 f.albedo.a = 1; //occulusion
    141.  
    142.                 //specular (could replace with metalic and deffer the specular calculation)
    143.                 f.specular.rgb = (color * _Metalic)+.22;
    144.  
    145.                 //roughness
    146.                 f.specular.a = (_Rough*-1+1)*mainTex.a;
    147.  
    148.                 //normals
    149.                 float3x3 tangentTransform = float3x3(i.tangentDir, i.bitangentDir, i.worldNormal);
    150.                 float3 normalMap = UnpackNormal(tex2D(_NormTex, TRANSFORM_TEX(i.uv, _NormTex)));
    151.                 float3 normalDir = normalize(mul(normalMap, tangentTransform));
    152.                 f.normal.rgb = normalDir *.5+.5;
    153.  
    154.                 //ambient light
    155.                 half3 lightmap = (DecodeLightmap(UNITY_SAMPLE_TEX2D(unity_Lightmap, i.uv1)));
    156.                 float3 shl = ShadeSH9(float4(i.worldNormal,1));
    157.                 f.light.rgb = (_AmbColor.rgb + shl*2 + lightmap) * color;
    158.  
    159.                 //Jitter
    160.                 f.normal.a = _Jitter * -1 + 1;
    161.                 return f;
    162.             }
    163.             ENDCG
    164.         }
    165.     }
    166.             FallBack "Diffuse"              
    167. }
     
    ekoops likes this.