Search Unity

Accessing Directional Lightmap in Surface shader

Discussion in 'Shaders' started by tigershan, Jul 12, 2012.

  1. tigershan

    tigershan

    Joined:
    Jul 29, 2010
    Posts:
    73
    How do I create Lighting(name)_DirLightmap(), every time I try to create this, I get current lightning mode does not exist, I am looking at the example in cginc, seems like they have to create actual lighting model and pre pass first, but I tried that, still I can't get LigtingCustomeLightmap_DirLightmap to appear.. am I understanding something wrong with the shader itself?
    Code (csharp):
    1.  
    2.     CGPROGRAM
    3.     #pragma surface surf CustomName_DirLightmap
    4.    
    5.     struct MySurfaceOutput {
    6.         fixed3 Albedo;
    7.         fixed3 Normal;
    8.         fixed3 Emission;
    9.         half Specular;
    10.         fixed Gloss;
    11.         fixed Alpha;
    12.     };
    13.    
    14.     inline fixed4 LightingCustomeName_DirLightmap (MySurfaceOutput s, fixed4 color, fixed4 scale, bool surfFuncWritesNormal)
    15.     {
    16.         UNITY_DIRBASIS
    17.         half3 scalePerBasisVector;
    18.        
    19.         half3 lm = DirLightmapDiffuse (unity_DirBasis, color, scale, s.Normal, surfFuncWritesNormal, scalePerBasisVector);
    20.        
    21.         return half4(lm, 0);
    22.     }
    23.  
     
    Last edited: Jul 12, 2012
  2. tigershan

    tigershan

    Joined:
    Jul 29, 2010
    Posts:
    73
    anybody?
     
  3. Kuba

    Kuba

    Moderator

    Joined:
    Jan 13, 2009
    Posts:
    416
    In the #pragma surface directive the light model name should not have "_DirLightmap" as that name (e.g. Lambert or BlinnPhong) is used as the base to look for the lighting function used in forward rendering (no suffix), in deferred ("_PrePass") and when decoding directional lightmaps ("_DirLightmap").

    That said, apart from providing the CustomName_DirLightmap function you should also provide your custom lighting function for forward and possibly deferred. If they don't need to be custom, you can just call one of the functions provided in Lighting.cginc from your functions:

    Code (csharp):
    1. #pragma surface surf CustomName
    2.  
    3. inline fixed4 LightingCustomName (SurfaceOutput s, fixed3 lightDir, fixed atten)
    4. {
    5.     return LightingLambert (s, lightDir, atten);
    6. }
    7.  
    8.  
    9. inline fixed4 LightingCustomName_PrePass (SurfaceOutput s, half4 light)
    10. {
    11.     return LightingLambert_PrePass (s, light);
    12. }
    13.  
    14. inline half4 LightingCustomName_DirLightmap (SurfaceOutput s, fixed4 color, fixed4 scale, bool surfFuncWritesNormal)
    15. {
    16.     return LightingLambert_DirLightmap (s, color, scale, surfFuncWritesNormal);
    17. }
    See Writing Surface Shaders and Custom Lighting models in Surface Shaders.
     
    morepixels likes this.
  4. tigershan

    tigershan

    Joined:
    Jul 29, 2010
    Posts:
    73
    So basically, if I want custom lighting model and directional lightmap, I only need LightingCustomName, and LightingCustomName_DirLightmap, where pre pass is not needed....?
     
  5. Kuba

    Kuba

    Moderator

    Joined:
    Jan 13, 2009
    Posts:
    416
    Yes.