Search Unity

Shader for colored shadows?

Discussion in 'Shaders' started by CaptainKiyaku, Mar 30, 2013.

  1. CaptainKiyaku

    CaptainKiyaku

    Joined:
    Feb 8, 2009
    Posts:
    324
    Hi,

    i've been looking into how to create colored shadows. Tried to search around for quite a while but haven't really found anything.

    I know that shadows don't have colors in a real world, but i am aiming for a somewhat different style.
    Here is an example of what i want to achieve:
    $newStyle.png

    So i am wondering if there is a way of having an object cast a colored shadow or receive a colored shadow?
    I do need the ambient light to change through different colors while keeping the shadow the same color.

    Sadly i don't know anything about writing shaders, so i am a bit lost.
    Any type of hint/tip would help me a lot.

    Thanks!
     
  2. Lulucifer

    Lulucifer

    Joined:
    Jul 8, 2012
    Posts:
    358
    try ShadowCaster
     
  3. MikeUpchat

    MikeUpchat

    Joined:
    Sep 24, 2010
    Posts:
    1,056
    Did you create those models or are they for sale somewhere, very much like the style.
     
  4. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    Have you tried just colouring your scene's ambient light? "Shadow colour" is whatever other light manages to reach a shadowed area, and ambient light reaches everything.
     
  5. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    You can also render unity's shadows to a texture then render this back over the scene. Seems wholly really stupid to do when its probably one line of code on unity's part. Whoever at unity decided we were not allowed to unlock shadow colour - please reconsider.

    This is one change I feel unity need to allow flexibility on. Stylised graphics are very welcome.
     
  6. CaptainKiyaku

    CaptainKiyaku

    Joined:
    Feb 8, 2009
    Posts:
    324
    I played around with it for a while but still haven't come up with the results i want. The closest i got is that every shadow BEHIND my model became purple... Maybe i can use this somehow, not sure. I'll keep testing.

    Thanks! I made the tree myself. The house is from the Human RTS pack from bitgem (http://bitgem3d.com/) and i just modified the colors and the mesh a bit.

    I did, and the results aren't too bad. It may work but i'm not happy that it re-colors every texture as well. I also wanted to add a Day/Night cycle and change ambient light over time (but this may have to affect the shadow anyway, which would be okay).

    With ambient light it looks like this:
    $ambientLight.png

    While the original colors are more like this:
    $noAmbientLight.png

    So i'm getting very close, but i'm not 100% happy with the overall colored look through ambient light.
    Maybe i can find a shader for the terrain that does not account ambient light, but i assume this would also eliminate the colored shadow again?

    This is an interesting approach, i will check this out and see if i can get it to work! Thanks.
    And yeah, i've used other 3d engines before and shadow color has always been a feature in the ones i used, so i am a bit surprised that Unity doesn't offer this as an option. I understand that it's not realistic, but not everyone is aiming for realistic styles heh.

    Thanks so far!
     
  7. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    You should decrease your directional light by subtracting the ambient colour. That way it will sum with the ambient light to white.
     
  8. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    I'm pretty sure that you can accomplish this easily with a custom lighting model.
     
  9. Thomas-Pasieka

    Thomas-Pasieka

    Joined:
    Sep 19, 2005
    Posts:
    2,174

    I have been asking for shadow color option for years and have been ignored for years. Maybe if a few more people cry out loud the gods up there may hear us.
     
  10. Lulucifer

    Lulucifer

    Joined:
    Jul 8, 2012
    Posts:
    358
    In Surface shder, light.a is shadow combined with light atten,may be you want to try this
     
  11. ootz0rz

    ootz0rz

    Joined:
    Mar 31, 2013
    Posts:
    4
    Curious...could you be more specific please? Maybe an example of how to get the light.a or something..
     
  12. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Gossip on the grapevine from Unity tech says they're on this issue, and while there can be no promises, it's something they're aware of and want to improve :)
     
  13. CaptainKiyaku

    CaptainKiyaku

    Joined:
    Feb 8, 2009
    Posts:
    324
    Hey,
    thanks for the tip. Not knowing that much about colors, i tried to literally substract the ambient color from the directional light color but that didn't quite work heh (unless i did something wrong).

    However, i just tried to manually change the directional light color so it sort of balances back to white and it seems to work.
    I ended up with this:

    $ambientLight2.png
     
  14. XeviaN360

    XeviaN360

    Joined:
    Jun 3, 2010
    Posts:
    181
    I do want shadow color parameter when writing shaders! :D
     
  15. Farfarer

    Farfarer

    Joined:
    Aug 17, 2010
    Posts:
    2,249
    You could easily write a shadow color into the shader, but at best you'd only be able to specify a color per type of light rather than per light (at least easily).
     
  16. XeviaN360

    XeviaN360

    Joined:
    Jun 3, 2010
    Posts:
    181
    Yes, it is not as handy as having the control per material :)
     
  17. Ippokratis

    Ippokratis

    Joined:
    Oct 13, 2008
    Posts:
    1,521
  18. MylesLambert

    MylesLambert

    Joined:
    Dec 31, 2012
    Posts:
    61
    Hey Farfarer,
    Would you be able to link to any documentation or articles on changing shadow passes in shaders? I don't believe I've seen much on the subject.
     
  19. Farfarer

    Farfarer

    Joined:
    Aug 17, 2010
    Posts:
    2,249
    No documentation to hand, but it's possible to do it with the defines you can lift from AutoLight.cginc. I've not tested this, but it should show the principle...

    Code (csharp):
    1. // Inside a cginc which you include in your shader.
    2.  
    3.  
    4. // Uniform shadow colour for all lights.
    5. uniform fixed3 _ShadowColor = fixed3(0.0, 0.0, 0.0);
    6.  
    7. // Or if you want a different shadow colour per light type, use this instead.
    8. #ifdef POINT
    9.     uniform fixed3 _ShadowColor = fixed3(1.0, 0.0, 0.0);
    10. #endif
    11.  
    12. #ifdef SPOT
    13.     uniform fixed3 _ShadowColor = fixed3(0.0, 1.0, 0.0);
    14. #endif
    15.  
    16. #ifdef DIRECTIONAL
    17.     uniform fixed3 _ShadowColor = fixed3(0.0, 0.0, 1.0);
    18. #endif
    19.  
    20. #ifdef POINT_COOKIE
    21.     uniform fixed3 _ShadowColor = fixed3(1.0, 1.0, 0.0);
    22. #endif
    23.  
    24. #ifdef DIRECTIONAL_COOKIE
    25.     uniform fixed3 _ShadowColor = fixed3(0.0, 1.0, 1.0);
    26. #endif
    27.  
    28.  
    29.  
    30. // Then inside your fragment shader.
    31.  
    32.  
    33. // If custom vert/frag rather than surface shader.
    34.     // This gets the shadow * attenuation value.
    35.     fixed atten = LIGHT_ATTENUATION (i);
    36.  
    37.     // Or this gets just the shadow value (without attenuation multiplied into it).
    38.     fixed atten = SHADOW_ATTENUATION(a);
    39.  
    40.     // If you are using surface shader, then the variable atten is already defined for you as shadow * attenuation.
    41.  
    42.  
    43.  
    44. // Get the shadow value and apply it.
    45. fixed4 c = tex2D(_MainTex, i.uv);
    46. c.rgb *= _ShadowColor * (1-atten);
    47. return c;
     
    bariscigal likes this.
  20. Arkhivrag

    Arkhivrag

    Joined:
    Apr 25, 2012
    Posts:
    2,982