Search Unity

[Free] Directional Light Soft Shadow (unity indie)

Discussion in 'Assets and Asset Store' started by mgear, Jul 17, 2014.

  1. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,438
    This is kind of softer shadow for unity indie(free), for directional light only.
    (using modified AutoLight.cginc)

    Still needs help to make it work without editing any .cginc files
    (also no documentation yet, just check the included AutoLight.cginc to see what is happening..)

    Original thread:

    http://forum.unity3d.com/threads/implementing-custom-shadow-map-filtering.256998/

    More info (+webplayer demo) :

    http://unitycoder.com/blog/2014/07/15/unity-indie-soft-shadows-directional-light/

    Sources:
    https://github.com/unitycoder/IndieSoftShadow

    Image:
    fake_soft_shadows_unity_indie_2.jpg
     
  2. Play_Edu

    Play_Edu

    Joined:
    Jun 10, 2012
    Posts:
    722
  3. Cyrien5100

    Cyrien5100

    Joined:
    Oct 17, 2012
    Posts:
    145
    Great ! Is it PCF or a simple shadow map blur ?
     
  4. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,438
    its just a simple blur right now, if anyone has ideas how to make it better please post info :)
     
  5. FuzzyQuills

    FuzzyQuills

    Joined:
    Jun 8, 2013
    Posts:
    2,871
    Hi guys, I am the one who started all this, and mgear was one of the ones who came to my aid. so here's my result also, after working out some projection bugs (this is from my upcoming fighting game Knuckle Iron): 2017 edit: Such game has been in hiatus for a very long time.
    SoftShadowExample.png
    If you look closely, you will notice there is absolutely no pixilation on the shadow whatsoever, and this is on medium resolution with no cascades! :p (shadow distance is also 50, giving this a good deal of detail)

    @mgear: if you wish, I can incorporate these changes as a secondary technique. (i found your technique had some weird graphics discontinuities, of which with mine, they are less obvious.)

    everyone: also, like mgear said, if you have any ideas on improving filtering, please let us know! :)
     
    Last edited: Apr 6, 2017
  6. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,438
    nice! yes please post them in :)

    did you yet find any way to do this without modifying autolight.cginc itself?
     
  7. FuzzyQuills

    FuzzyQuills

    Joined:
    Jun 8, 2013
    Posts:
    2,871
    No, unfortunately... :( to do it, we would need to find a way of accessing the shadow map in a custom shader. but unless mr. shadow softener figured out something not documented, we are stuck... o_O
     
  8. FuzzyQuills

    FuzzyQuills

    Joined:
    Jun 8, 2013
    Posts:
    2,871
    BTW, here's a modified .cginc from me. (attached to post)

    I would recommend this for mine: make the resolution as low as you want to, and it will blur in the pixels. at least that's how it went with me... ;)

    EDIT: Also, unless something in your game needs it to work right, try turning off cascades altogether. this will improve the blurring as the shadow map will switch to a lower resolution.
     

    Attached Files:

    Streamfall and tyoc213 like this.
  9. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,438
    Thanks, I'll add that to the github later.

    Some progress on sampling shadowtexture inside shader directly,
    otherwise this works but some projection or direction is wrong.. shadow is messed up.

    Code (JavaScript):
    1. Shader "Custom/IndieShadow1" {
    2.         Properties {
    3.             _Color ("Main Color", Color) = (1,1,1,0.5)
    4.             _MainTex ("Texture", 2D) = "white" { }
    5.         }
    6.         SubShader {
    7.             Pass {
    8.  
    9.         CGPROGRAM
    10.  
    11.         #pragma vertex vert
    12.         #pragma fragment frag
    13.         #include "UnityCG.cginc"
    14.  
    15.         float4 _Color;
    16.         sampler2D _MainTex;
    17.         sampler2D _ShadowMapTexture;
    18.  
    19.         struct v2f {
    20.             float4 pos : SV_POSITION;
    21.             float2 uv : TEXCOORD0;
    22.             float4 shadowCoord;
    23.         };
    24.  
    25.         float4 _MainTex_ST;
    26.  
    27.         v2f vert (appdata_base v)
    28.         {
    29.             v2f o;
    30.             o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
    31.             o.uv = TRANSFORM_TEX (v.texcoord, _MainTex);
    32.          
    33.             // FIXME: problem could be here?
    34.             o.shadowCoord =  mul( unity_World2Shadow[0], mul( _Object2World, v.vertex ) );
    35.             return o;
    36.         }
    37.  
    38.  
    39.  
    40.         half4 frag (v2f i) : COLOR
    41.         {
    42.             half4 texcol = tex2D (_MainTex, i.uv);
    43.          
    44.             // Hard Shadow
    45.               fixed shadow = tex2D(_ShadowMapTexture, i.shadowCoord.xyz).r;
    46.             shadow = _LightShadowData.r + shadow * (1-_LightShadowData.r);
    47.          
    48.             return texcol * _Color * shadow;
    49.         }
    50.         ENDCG
    51.  
    52.             }
    53.         }
    54.         Fallback "VertexLit"
    55.     }
    *image:
    shadow_problem1.jpg
     
    Last edited: Jul 24, 2014
  10. FuzzyQuills

    FuzzyQuills

    Joined:
    Jun 8, 2013
    Posts:
    2,871
    what's it look like?
     
  11. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,438
  12. FuzzyQuills

    FuzzyQuills

    Joined:
    Jun 8, 2013
    Posts:
    2,871
    WTH? It looks like the shadow map is being tiled!

    try turning off cascades if they are turned on.
     
  13. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,438
    huh, gave up and closed unity.. now started it again and it works :) works with your shadow code also.

    (seems to do that even if duplicate same shader, use that, broken, restart, fixed..)

    softshadow_indie.jpg
     
  14. FuzzyQuills

    FuzzyQuills

    Joined:
    Jun 8, 2013
    Posts:
    2,871
    How did you do that?

    and BTW, check out my new avatar! I finally got around to finishing 2.0 of my FuzzyQuills model.

    EDIT: Ok... it shows up on my profile, but on any thread my avatar is still the original... HUH?!
     
  15. Cyrien5100

    Cyrien5100

    Joined:
    Oct 17, 2012
    Posts:
    145
    If you get good projection, try to do some poisson disk blur, it gives a good soft shadow, i tried myself to do it in screen space it looks good but there is haloing.
     
  16. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,438
  17. Cyrien5100

    Cyrien5100

    Joined:
    Oct 17, 2012
    Posts:
    145
  18. FuzzyQuills

    FuzzyQuills

    Joined:
    Jun 8, 2013
    Posts:
    2,871
    http://www.opengl-tutorial.org/intermediate-tutorials/tutorial-16-shadow-mapping/#Poisson_Sampling
    Here you go. Where I went to find sampling techniques.

    I actually got a pretty good result from both the one here and mgear's code, but mgears only works in DX11. (for some reason, there is a EXTREME amount of 'fragment noise' when I switched to DX9. this doesn't happen on the former from the URL, or on the other filtering type.)

    EDIT: I fixed it, it turns out mgears were using floats, and trying to squeeze that into a half definitely won't work... I will post a screen soon

    EDIT2: another note: this sadly won't work on mobile, all it does is mess up the projection. this only happens on the mobile device itself, it doesn't show up on PC.
     
    Last edited: Aug 1, 2014
  19. FuzzyQuills

    FuzzyQuills

    Joined:
    Jun 8, 2013
    Posts:
    2,871
    BTW, how to use the shadowmap shader mgear? not sure how to use it... :D
     
  20. Frostbite23

    Frostbite23

    Joined:
    Mar 8, 2013
    Posts:
    458
    Nice work, thought that the hard shadows can't be modified, turns out you did it! BTW since this is soft shadowing you should do some PCF filtering (Percentage - Close - Filtering).

    http://www.gamerendering.com/2008/11/15/percentage-closer-filtering-for-shadow-mapping/ - i would recommend because they show code and make it easier to understand.
    http://ogldev.atspace.co.uk/www/tutorial42/tutorial42.html

    ill give that a shot and make a demo scenes with those shadows and indie effects to see how realistic i can get.
     
    Last edited: Aug 5, 2014
  21. Cyrien5100

    Cyrien5100

    Joined:
    Oct 17, 2012
    Posts:
    145
    The problem is we need to access to the shadow depth map to do pcf :/
     
  22. FuzzyQuills

    FuzzyQuills

    Joined:
    Jun 8, 2013
    Posts:
    2,871
    Well, without haloing, that is. for custom mapping, yes we would need depth, but for applying filters... :D

    EDIT: I could actually do that second link in OpenGL C++, but not in unity... ;)
     
  23. FuzzyQuills

    FuzzyQuills

    Joined:
    Jun 8, 2013
    Posts:
    2,871
    BTW Cyrien, your attempt to do poisson disk blur... where is it? I would like to see it! :)
     
  24. JecoGames

    JecoGames

    Joined:
    Jan 10, 2013
    Posts:
    135
    Can someone please give me instructions on how to make this work? After all the back and forth discussion in this thread its not too clear how to use this:p
     
  25. FuzzyQuills

    FuzzyQuills

    Joined:
    Jun 8, 2013
    Posts:
    2,871
    Oh, sorry! I know how to get this to work, as I was the one who asked how to do this in the first place before this thread was born. then mgear and I came up with something, and here we go...

    But anyway, here's what you do:
    1. First, download the AutoLight.cginc, and put it into a folder in your unity project marked "Resources"
    2. Place ALL the shaders into this folder that you wish to have soft shadows.
    3. Once you have it all set up, click play: DONE!
    I am also sure that, given unity has a restart, you can also have the soft shadows on other shaders, but I will have to check on that one... :D
     
    Streamfall and JecoGames like this.
  26. JecoGames

    JecoGames

    Joined:
    Jan 10, 2013
    Posts:
    135
    Thanks!!!
     
  27. FuzzyQuills

    FuzzyQuills

    Joined:
    Jun 8, 2013
    Posts:
    2,871
    So it worked? Cool! :)
     
    JecoGames likes this.
  28. JecoGames

    JecoGames

    Joined:
    Jan 10, 2013
    Posts:
    135
    Yep:)
     
  29. Streamfall

    Streamfall

    Joined:
    Aug 8, 2011
    Posts:
    43
    Heya @FuzzyQuills,
    Been trying to use your AutoLight.cginc script with a shader I built using Shader Forge. Its a cool shader, has alpha transparency AND shadows, nifty stuff.

    The shadows look great in-editor with the build process set to iOS. However, when building, I get a load of errors. Any ideas?
    The error appears to be pointing to the shader, but I've tracked it down to AutoLight.cginc. Here is the error :
    Code (CSharp):
    1. Shader error in 'Shader Forge/AlphaWithShadows': cannot access field `x' of non-structure / non-vector; type mismatch at line 59
    These errors are associated with these lines :
    Code (CSharp):
    1.  
    2. shadows2[0] = tex2Dproj( _ShadowMapTexture, UNITY_PROJ_COORD(coord + half4(0,0.001,0,0)) ).r;
    3.     shadows2[1] = tex2Dproj( _ShadowMapTexture, UNITY_PROJ_COORD(coord + half4(0.001,0,0,0)) ).r;
    4.     shadows2[2] = tex2Dproj( _ShadowMapTexture, UNITY_PROJ_COORD(coord + half4(0,-0.001,0,0)) ).r;
    5.     shadows2[3] = tex2Dproj( _ShadowMapTexture, UNITY_PROJ_COORD(coord + half4(-0.001,0,0,0)) ).r;
    6.     shadows2 = _LightShadowData.rrrr + shadows2 * (1-_LightShadowData.rrrr);
    7.     fixed shadowA = dot(shadows2, 0.25);
    8.  
    9.     fixed4 shadows3;
    10.     shadows3[0] = tex2Dproj( _ShadowMapTexture, UNITY_PROJ_COORD(coord + half4(0,0.003,0,0)) ).r;
    11.     shadows3[1] = tex2Dproj( _ShadowMapTexture, UNITY_PROJ_COORD(coord + half4(0.003,0,0,0)) ).r;
    12.     shadows3[2] = tex2Dproj( _ShadowMapTexture, UNITY_PROJ_COORD(coord + half4(0,-0.003,0,0)) ).r;
    13.     shadows3[3] = tex2Dproj( _ShadowMapTexture, UNITY_PROJ_COORD(coord + half4(-0.003,0,0,0)) ).r;
    Have you tried your script using 4.6x? Thats what I'm using currently.
    Any guidance you might be able to offer would be greatly appreciated!
     
    Last edited: Sep 12, 2015
  30. FuzzyQuills

    FuzzyQuills

    Joined:
    Jun 8, 2013
    Posts:
    2,871
    I haven't used that method in a loooong time since upgrading to unity 5. Which shading API are you using; GLES or Metal?

    Try getting rid of the ".r" at the end of the shadow samples, like this:
    Code (csharp):
    1. shadows3[0] = tex2Dproj( _ShadowMapTexture, UNITY_PROJ_COORD(coord + half4(0,0.003,0,0)) ) *removed it here*;
    Where marked, remove those and see if it compiles. I've found odd issues with GLES shaders not compiling for stupiud things in the past... :D
     
    Streamfall likes this.
  31. FuzzyQuills

    FuzzyQuills

    Joined:
    Jun 8, 2013
    Posts:
    2,871
    Oh, another thing; I don't think this one works on mobile, last I checked, I couldn't get it to work without it stuffing up... :(

    EDIT: At this point, I am now considering posting one of my newer approaches to getting better looking shadows on mobile.
     
    Streamfall likes this.
  32. Streamfall

    Streamfall

    Joined:
    Aug 8, 2011
    Posts:
    43
    Hi @FuzzyQuills, thanks for taking some time to look at this. What is your new method?

    I've tried several different things like that, substituting nothing for 'r' and trying to access elements of the fixed4. None of these changes resulted in the shadows mapping right. I've used each of the GL methods available, most recently with ES 3.0. Do I have a better chance with any particular one? Its problematic because I usually only notice the problems after waiting for a build.

    Starting to tinker with this in Unity5 - which is great, because you can choose to compile and show code from the editor without building. This is cool.

    I'll be tinkering with this today, really hoping for a solution!
    Thanks!

    Edit: I could probably upgrade to 5 if we can get some nice soft shadows. This project is client driven, and maybe I could talk them into aiding in the purchase :)
     
    Last edited: Sep 12, 2015
  33. Streamfall

    Streamfall

    Joined:
    Aug 8, 2011
    Posts:
    43
    Just an update :
    Ive managed to get this working by removing '.r'. However, the shader drops frame rate on the app too much to be useable.

    Looking into some ways to speed up the shader, nothing found yet.
     
  34. FuzzyQuills

    FuzzyQuills

    Joined:
    Jun 8, 2013
    Posts:
    2,871
    This is because the shadow uses 8 texture samples, which is probably a bit much for an iOS device.

    EDIT: On top of that, was the shadow projection correct? I had a couple of issues with shadow coords stuffing up in the past.
    Custom shadow mapper, means I can do almost any kind of filtering I like. :) Yet to try VSM... :D