Search Unity

Unlit LightMapped Shader

Discussion in 'Shaders' started by nielsmillikan, Jun 27, 2013.

  1. nielsmillikan

    nielsmillikan

    Joined:
    Nov 29, 2010
    Posts:
    72
    Can anyone tell me how to write an Unlit Lightmapped Shader which can receive shadows? I tried writing a surface shader with a custom lighting model (for being unlit) but it didn't receive or cast any shadows. Please help me.
    Code (csharp):
    1. Shader "Custom/Lightmapped/Unlit" {
    2. Properties {
    3.     _Color ("Main Color", Color) = (1,1,1,1)
    4.     _MainTex ("Base (RGB)", 2D) = "white" {}
    5.     _LightMap ("Lightmap (RGB)", 2D) = "black" {}
    6. }
    7.  
    8. SubShader {
    9.     LOD 300
    10.     Tags { "RenderType" = "Opaque" }
    11. CGPROGRAM
    12. #pragma surface surf None
    13.  
    14. half4 LightingNone (SurfaceOutput s, half3 lightDir, half atten)
    15. {
    16.     half4 c;
    17.     c.rgb = s.Albedo;
    18.     c.a = s.Alpha;
    19.     return c;
    20. }
    21.      
    22. struct Input {
    23.   float2 uv_MainTex;
    24.   float2 uv2_LightMap;
    25. };
    26. sampler2D _MainTex;
    27. sampler2D _BumpMap;
    28. sampler2D _LightMap;
    29. fixed4 _Color;
    30. void surf (Input IN, inout SurfaceOutput o)
    31. {
    32.   half4 c;
    33.   o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb;
    34.   half4 lm = tex2D (_LightMap, IN.uv2_LightMap);
    35.   o.Albedo *= lm.rgb;
    36.   o.Alpha = lm.a;
    37. }
    38. ENDCG
    39. }
    40. FallBack "Legacy Shaders/Lightmapped/Diffuse"
    41. }
    42.  
     
  2. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    This might sound facetious, but shadows are complementary to lighting. If you're talking about Unity's projected shadows specifically, all they do is prevent specific lights from affecting specific parts of an object. If something is unlit, where do you expect shadows to come from?
     
  3. nielsmillikan

    nielsmillikan

    Joined:
    Nov 29, 2010
    Posts:
    72
    oh, I thought the shadows projected by Unity3d darkened the specific areas on the object. So I was expecting it to work on unlit objects as well. Thanks for clearing that out.
     
  4. Kragh

    Kragh

    Joined:
    Jan 22, 2008
    Posts:
    657
    Daniel, you could still just make a lightning function of the surface shader, where the light itself is not taken into consideration (and no dot operations on normals/lightdirs and such), but only the "atten" feed is multiplied on. So it's still a lit shader, just not using the light.


    Here's something to get you going (The atten parameter is what contains the shadow info, so it is just being multiplied on the color output of the lightning function) You can play around with how to scale this atten property (multiply it by 2, or stretch its range, whatever ;)):

    Shader "Custom/Lightmapped/Unlit" {

    Code (csharp):
    1. Properties {
    2.  
    3.     _Color ("Main Color", Color) = (1,1,1,1)
    4.  
    5.     _MainTex ("Base (RGB)", 2D) = "white" {}
    6.  
    7.     _LightMap ("Lightmap (RGB)", 2D) = "black" {}
    8.  
    9. }
    10.  
    11.  
    12.  
    13. SubShader {
    14.  
    15.     LOD 300
    16.  
    17.     Tags { "RenderType" = "Opaque" }
    18.  
    19. CGPROGRAM
    20.  
    21. #pragma surface surf None
    22.  
    23.  
    24.  
    25. half4 LightingNone (SurfaceOutput s, half3 lightDir, half atten)
    26.  
    27. {
    28.  
    29.     half4 c;
    30.  
    31.     c.rgb = s.Albedo*atten;
    32.  
    33.     c.a = s.Alpha;
    34.  
    35.     return c;
    36.  
    37. }
    38.  
    39.      
    40.  
    41. struct Input {
    42.  
    43.   float2 uv_MainTex;
    44.  
    45.   float2 uv2_LightMap;
    46.  
    47. };
    48.  
    49. sampler2D _MainTex;
    50.  
    51. sampler2D _BumpMap;
    52.  
    53. sampler2D _LightMap;
    54.  
    55. fixed4 _Color;
    56.  
    57. void surf (Input IN, inout SurfaceOutput o)
    58.  
    59. {
    60.  
    61.   half4 c;
    62.  
    63.   o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb*_Color;
    64.  
    65.   half4 lm = tex2D (_LightMap, IN.uv2_LightMap);
    66.  
    67.   o.Albedo *= lm.rgb;
    68.  
    69.   o.Alpha = lm.a;
    70.  
    71. }
    72.  
    73. ENDCG
    74.  
    75. }
    76.  
    77. FallBack "Legacy Shaders/Lightmapped/Diffuse"
    78.  
    79. }
    EDIT: Just remember that the atten (atten stands for "attenuation") parameter also contains the fallof of lights (spots and points), so you will have more than just shadows in this, so use it carefully to get the result you want :)
     
    Last edited: Jun 28, 2013
  5. nielsmillikan

    nielsmillikan

    Joined:
    Nov 29, 2010
    Posts:
    72
    Thanks a lot Kragh. It works properly for me since I'm using only a directional light as its an outdoor scene. :)
     
  6. payeah

    payeah

    Joined:
    Sep 26, 2019
    Posts:
    1
    Hi, I'm using it now, I love it, but I'd like the albedo to use its alpha channel, and I can't make it work! Thanks!