Search Unity

Shadow Volumes Toolkit (now with stencil buffer support)

Discussion in 'Assets and Asset Store' started by gustavolsson, Aug 26, 2011.

?

Webplayer feedback

Poll closed Sep 4, 2019.
  1. The shadows look good (Mac OSX)

    79 vote(s)
    27.2%
  2. The shadows look good (Windows)

    178 vote(s)
    61.4%
  3. There seems to be a problem with the shadows (Mac OSX)

    6 vote(s)
    2.1%
  4. There seems to be a problem with the shadows (Windows)

    13 vote(s)
    4.5%
  5. Other problem

    14 vote(s)
    4.8%
  1. ruslite

    ruslite

    Joined:
    May 25, 2014
    Posts:
    61
    How to remove the blackout on the object, but the object to accept a shadow?
     
  2. gustavolsson

    gustavolsson

    Joined:
    Jan 14, 2011
    Posts:
    339
    The lines are caused by having anti-aliasing enabled in conjunction with the "Alpha Channel" backend, disable AA or use the "Stencil Buffer" backend instead. If there are other errors with that particular model, could you please send it to me via email so that I can take a look at it?

    You can use the ShadowComponentToGO.cs script (attach it to a game object with a ShadowVolume component) to convert the ShadowVolume components to MeshRenderer/Filters at Start (when the Play-button is pressed). This will enable dynamic/static batching.
     
  3. rrh

    rrh

    Joined:
    Jul 12, 2012
    Posts:
    331
    Volume shadows disappear when I'm using Direct3D 11. Is there any way around that?
     
  4. Mumasboy

    Mumasboy

    Joined:
    Feb 24, 2011
    Posts:
    15
    Is there a way to exclude certain objects from having shadows cast on them with this system?
     
  5. okumasama

    okumasama

    Joined:
    Jun 17, 2014
    Posts:
    16
    Hi. I noticed Stencil Buffer shadow doesn't work with deferred lighting. ( Self-shadow is seemed to be right, but it casts any shadow to other object )
    Doesn't this asset support deferred lighting? And is there any text saying it?

    One more thing. Why does this asset consider about two or more point lights in one room? The result is awful, except a last point light, all point lights' shadow were ignored.
     
    Last edited: Aug 11, 2014
  6. rrh

    rrh

    Joined:
    Jul 12, 2012
    Posts:
    331
    It definitely says only one light source at a time on this page under limitations:
    http://gustavolsson.squarespace.com/shadow-volumes-toolkit/

    I don't know about the deferred lighting and stencils.


    I've looked for this before and found nothing. It doesn't mean there is nothing to be found, but it's not promising. I've also played with projector-based shadows, and those allow you to specify a layermask for the projector.
    This is what I used as the basis for this method:
    http://wiki.unity3d.com/index.php?title=Character_Shadow

    The down-sides compared to Volume Shadow Toolkit are 1) you need to put each thing that casts a shadow onto a layer, which will be a problem if you have more than a couple of them. 2) It renders the shadow to a texture, so you have to pick a resolution you're happy with (versus VST you can get as close as you want to the shadow and it's not pixelated) 3) when two shadows land on the same point, it doubles up in a way that isn't realistic.
     
  7. okumasama

    okumasama

    Joined:
    Jun 17, 2014
    Posts:
    16
    >I don't know about the deferred lighting and stencils.
    You don't know? You mean your environment works stencils with deferred lighting? Or you say you don't care about deferred and stencils?

    Definitely my environment doesn't works stencil buffer shadow with deferred lighting:

    And it works when I changed main camera rendering path to forward lighting.
    Unity Pro version 4.5.2f1
     
  8. ruslite

    ruslite

    Joined:
    May 25, 2014
    Posts:
    61
    How to remove the blackout on the object, but the object to accept a shadow?
     
  9. ErikRobson

    ErikRobson

    Joined:
    Mar 4, 2014
    Posts:
    11
    Hi Gustav!

    Any thoughts on getting the toolkit working in DX11?
     
  10. SteveB

    SteveB

    Joined:
    Jan 17, 2009
    Posts:
    1,451
    Long time no speak Gustav!

    I was wondering if you could implement a Near Bias and Far Bias for Extrude Bias, so the further away the shadow is from the camera, the greater the bias. Right now I too am suffering from very bad z-fighting on surfaces further in the distance; to reasonably fix it, I have to increase the Extrude Bias, but unfortunately then the shadows 'pull away' from the surface which is very noticeable when you're close to said surface.

    In other words I'd love for the Extrude Bias to be 0.03 for objects/surfaces/vertices that are within a few units of the camera, and climb to as high as 0.5 (or more) for objects that are off in the distance. As for struggling with Z-fighting, I tried everything, including writing a vertex shader that moves the vertices of my actual meshes (not the shadow meshes) a fraction which has helped as well.

    Thank you!

    -Steven
     
  11. kfc

    kfc

    Joined:
    Dec 17, 2010
    Posts:
    27
    Hi Gustav,

    Thanks for your excellent plugin. Been loving it so far in IOS build. However, I have just realised that the Android build seems to suffers performance issue. Is there any way that we can match the performance of IOS build? or I should just ditch shadows in Android build entirely?

    Cheers.
     
  12. SteveB

    SteveB

    Joined:
    Jan 17, 2009
    Posts:
    1,451
    ...and this doesn't appear to work in the 4.6 Beta 21 huh?

    Gustav are you still reading these?
     
  13. gustavolsson

    gustavolsson

    Joined:
    Jan 14, 2011
    Posts:
    339
    Sorry for the delay people!

    So, I've added a FAQ to my website where I will answer common questions I get via email or on this forum. Please let me know if you think anything is missing. Also, I'm now @gustav_olsson on Twitter, if you need a short answer to a short question :)

    This looks like a classic case of Z-fighting. Try to keep the difference between the camera near and far planes as low as possible. Good values: something like 0.3 for the near plane and 1000 for the far plane. If you want to increase the distance between the object and the point where the shadow starts, increase the "Extrude Bias" of the ShadowVolumeSource component (under Advanced).

    If you still want to remove the self-shadowing, see this FAQ question.

    Actually, I'm not sure why it doesn't work on Direct3D 11. I will examine this further when I get back to my windows computer.

    Yes, see this FAQ question.

    Deferred rendering with the Stencil Buffer backend works for me in Unity 4.5.5. That said, this didn't work in earlier versions of Unity and I could never really find out why. It might have been a bug in Unity, but I'm still not sure. Could someone else confirm that this indeed works in the latest (non-beta) version of Unity?

    Have you tried adjusting the near and far planes of the camera? Z-fighting usually occurs when the difference between the planes is too great. Try to have them within a 1:1000 ratio. I could maybe add something like what you suggest, but it would impact the general performance in a negative way since the check/lerp would have to be done in the shader. Could you see if it is the near/far plane ratio that causes the problem?

    The performance is a bit out of my hands actually. It might be that the specific Android device you're targeting has worse fill rate performance than the iOS device you're running it on. I've been looking into ways of optimising the toolkit for specific use-cases (like static game objects), but the basic performance impact will always be there.

    What do you mean by this? I don't really want to debug the toolkit on beta versions of Unity, much can still change. That said, I've heard that the toolkit works fine on Unity 4.6 beta 20.

    As a side note, it seems that Unity is opening up the Stencil Buffer to all users, even those with Unity Free, in Unity 4.6. This is great news for the toolkit!

    Hope this helps :)
     
    Last edited: Nov 1, 2014
  14. Project-Mysh

    Project-Mysh

    Joined:
    Nov 3, 2013
    Posts:
    223
    There isnt any change when I use ShadowComponentToGO.cs. The draw calls are the same using stencil buffer. I dont need to use this script for static batching when using stencil buffer mode?
     
  15. gustavolsson

    gustavolsson

    Joined:
    Jan 14, 2011
    Posts:
    339
    Static batching doesn't seem to work on instantiated game objects, and the ShadowComponentToGO.cs script instantiates "Shadow" game objects at Start. You can look at the instantiated "Shadow" game objects during Play-mode and then set them up manually, without using the ShadowComponentToGO.cs script. Then static batching should work like with any other game object. Maybe a future version of the toolkit could include an Edit-mode version of the ShadowComponentToGO.cs script to make this easier.
     
  16. layola

    layola

    Joined:
    Aug 6, 2013
    Posts:
    94
    hi,work perfect on pc but not work on android platform ?any help?
     
  17. Project-Mysh

    Project-Mysh

    Joined:
    Nov 3, 2013
    Posts:
    223
    Are you using stencil buffer? You need Android Pro to use stencil buffer on android.
     
  18. layola

    layola

    Joined:
    Aug 6, 2013
    Posts:
    94
    Ok only base Android version install ,will you support base Android version in feature?
    ----------------
    I recheck my license,it's android pro instead android base ,but still not work for your example .
    my unity version is 4.3.2,and android phone version is android 4.0.3
     
    Last edited: Nov 8, 2014
  19. Project-Mysh

    Project-Mysh

    Joined:
    Nov 3, 2013
    Posts:
    223
    Its not a limitation of "Shadow Volumes Toolkit". Stencil buffer is unity pro only feature. If you want to use "Shadow Volumes Toolkit" in Android basic version, switch "Shadow volume renderer" to "Alpha Channel" or "Alpha Channel no blend".
     
  20. FuzzyQuills

    FuzzyQuills

    Joined:
    Jun 8, 2013
    Posts:
    2,871
    @gustavolsson: Hi! Is there any possibility of including stencil buffer method in 4.6? (Came out today actually! :))
     
  21. Dazza007

    Dazza007

    Joined:
    Feb 11, 2014
    Posts:
    9
    OK - Just purchased this - and there is one major issue which pretty much make it unusable

    1 - Doesn't work with Direct3D 11 - Without shadows my Scenes framerate is 200FPS+ in D3D11, without it my scene runs at 30FPS. So needless to say switching D3D off is not an option

    Any idea if/when the D3D issues will be resolved - otherwise I'll be going for the money back option - as like I mentioned it is totally unusable without D3D11 support.

    Many Thanks
    Darrin
     
  22. FuzzyQuills

    FuzzyQuills

    Joined:
    Jun 8, 2013
    Posts:
    2,871
    @Dazza007: Not sure how this pack works, but if vertex extrusion is done on the CPU, that might be why. (This is because DX9 is more intensive on CPU, and might prove a bottleneck. Not sure why this is either, incidentally... :D)

    There's always the option of trying --force-opengl in a built game, which might do something for you. (Surprisingly, that mode works fine in a build, but doing it in the editor's atrocious on Windows!)
     
  23. FuzzyQuills

    FuzzyQuills

    Joined:
    Jun 8, 2013
    Posts:
    2,871
    Also, is the stencil buffer available in unity 4.6 android basic? I know it was opened up for free users, but with my stencil shadow implementation, all I get is a pink screen on my tab... :(
     
  24. Joker921

    Joker921

    Joined:
    Mar 27, 2015
    Posts:
    4
    I use shadow volumes toolkit in my project. But there are some "holes" in my shadow. Anybody knows the reason?

    aaa.png ccc.png
     
  25. tatoforever

    tatoforever

    Joined:
    Apr 16, 2009
    Posts:
    4,368
    You need to provide a models without any holes (all vertices's must be weld together). Otherwise when the extrusion happens surfaces might get inverted (as you can see in your image). This is not a fault on the shader is a limitation of shadow volumes technique.
    [EDIT] The stencil test can be improved to avoid overlapping shadows in which if stencil pass it must either replace the existing buffer value or keep it. The simpler thing to do is to always replace the current value (zdepth fails or pass). Gotta check that out with my stencil implementation, I'm not sure how was done with shadow volumes.
     
    Last edited: Mar 27, 2015
  26. Joker921

    Joker921

    Joined:
    Mar 27, 2015
    Posts:
    4
    Thanks for your reply. Where can I get your stencil implementation?
     
  27. tatoforever

    tatoforever

    Joined:
    Apr 16, 2009
    Posts:
    4,368
    I'm not sure if it will work with Shadow Volumes Toolkit. I'll try to get it tomorrow when i get to the office.
     
  28. Joker921

    Joker921

    Joined:
    Mar 27, 2015
    Posts:
    4
    Thanks buddy;)
     
  29. gustavolsson

    gustavolsson

    Joined:
    Jan 14, 2011
    Posts:
    339
    Hi, sorry for the absence.

    This is actually not a problem with the shadow volumes implementation, but a shortcoming with Unity's skinned meshes and the toolkit. Notice that the holes only appear in skinned/animated models? This is because the normals of the shadow meshes are updated by Unity when skinning is performed, and in this step they might become tilted. The shadow volumes implementation needs the normal to be exactly perpendicular to the triangle surfaces, and since we don't have any control over the skinning process, I can't enforce this constraint and holes appear when limbs are stretched too much.

    If anyone knows how to pipe into the Unity's animation system (in particular, how to tell Unity how the normals should be updated), it would be super helpful for the toolkit :)
     
  30. gustavolsson

    gustavolsson

    Joined:
    Jan 14, 2011
    Posts:
    339
    The toolkit uses the stencil buffer if you select the "Stencil Buffer" backend on the Shadow Volume Renderer component.

    Yea, this is a problem. I still don't know why it doesn't work on DX11 but I'm debugging it when I have the free time.

    The vertex extrusion is done on the GPU.

    Tried with Unity 5? Pink shaders usually means that the android device doesn't support the required stencil operations :/
     
  31. ruslite

    ruslite

    Joined:
    May 25, 2014
    Posts:
    61
    With Unitн 5 doesn`t work. Why?
     
  32. gustavolsson

    gustavolsson

    Joined:
    Jan 14, 2011
    Posts:
    339
    Have you disabled the DX11 mode?
     
  33. ruslite

    ruslite

    Joined:
    May 25, 2014
    Posts:
    61
    Thanks. Evetything works fine! My question: why not work with DX11 mode?
     
  34. gustavolsson

    gustavolsson

    Joined:
    Jan 14, 2011
    Posts:
    339
    Nice! That is my question as well. I'm going to look into this when I have some free time. Thanks for using the toolkit :)
     
  35. gustavolsson

    gustavolsson

    Joined:
    Jan 14, 2011
    Posts:
    339
    Regarding DX11: I haven't had the time to look into it yet, and haven't had any luck in asking Unity devs about the issue. Something is different between the DX11 and DX9 renderers of Unity, but I don't know what. I will have some time to work on it during the summer.

    New stuff:
    As an experiment, I've changed the price of the shadow volumes toolkit to be FREE for the time being. As soon as Unity updates the state of the package, you should all be able to download the Shadow Volumes Toolkit here: https://www.assetstore.unity3d.com/en/#!/content/1861

    Hopefully this will get my main asset, the Shatter Toolkit (last day of sale, btw), some more traction. Also, since Unity Pro features are now basically free, it doesn't make much sense to charge for it. I will probably charge for it again when I have the time to make an update for it though, so get it while you can. Hope you like it :)
     
  36. scopigno

    scopigno

    Joined:
    Jul 10, 2014
    Posts:
    9
    Just edit ShadowVolumeVertex function on ShadowVolume.cginc.
    You may need to reimport all the other shaders.

    Before:
    Code (CSharp):
    1. output.position = mul(UNITY_MATRIX_MVP, float4(input.vertex.xyz + sourceDirection * -movement, 1.0f));
    After:
    Code (CSharp):
    1. output.position = mul(UNITY_MATRIX_MVP, float4(input.vertex.xyz + sourceDirection * -movement, 1.001f));
     
  37. scopigno

    scopigno

    Joined:
    Jul 10, 2014
    Posts:
    9
    This is what I did to soften the shadows using the stencil buffer.
    I just changed the SVT_StencilInterpolate.shader to blur the generated texture.

    Code (CSharp):
    1. // Shadow Blur by Bruno Scopigno
    2. Shader "Shadow Volumes/Stencil Interpolate"
    3. {
    4.     Properties {
    5.         _Size ("Blur Size", Range(0, 20)) = 1
    6.     }
    7.      
    8.     SubShader
    9.     {
    10.         Tags
    11.         {
    12.             "Queue" = "AlphaTest+511"
    13.             "IgnoreProjector" = "True"
    14.         }
    15.      
    16.         GrabPass { "_OriginalGrab" }
    17.      
    18.         // Linearly interpolate the scene color towoards the shadow color
    19.         Pass
    20.         {
    21.             Stencil
    22.             {
    23.                 Ref 0
    24.                 Comp NotEqual
    25.             }
    26.          
    27.             Lighting Off
    28.             Cull Off
    29.             ZTest Always
    30.             ZWrite Off
    31.             Blend One Zero
    32.          
    33.             Fog
    34.             {
    35.                 Mode Off
    36.             }
    37.          
    38.             CGPROGRAM
    39.             #pragma vertex ScreenVertex
    40.             #pragma fragment ScreenColorFragment
    41.             float4 _shadowVolumeColor;
    42.  
    43.             struct VertexInput
    44.             {
    45.                 float4 vertex : POSITION;
    46.             };
    47.  
    48.             struct VertexOutput
    49.             {
    50.                 float4 position : POSITION;
    51.             };
    52.  
    53.             VertexOutput ScreenVertex(VertexInput input)
    54.             {
    55.                 VertexOutput output;
    56.              
    57.                 output.position = input.vertex;
    58.              
    59.                 return output;
    60.             }
    61.  
    62.             float4 ScreenColorFragment() : COLOR
    63.             {
    64.                 return float4(_shadowVolumeColor.x, _shadowVolumeColor.y, _shadowVolumeColor.z, 1.0f);
    65.             }
    66.             ENDCG
    67.         }
    68.      
    69.         GrabPass { }
    70.      
    71.         // Horizontal blur
    72.         Pass {
    73.             Stencil
    74.             {
    75.                 Ref 0
    76.                 Comp NotEqual
    77.             }
    78.          
    79.             Lighting Off
    80.             Cull Off
    81.             ZTest Always
    82.             ZWrite Off
    83.             Blend One Zero
    84.          
    85.             Fog
    86.             {
    87.                 Mode Off
    88.             }
    89.        
    90.             CGPROGRAM
    91.             #pragma vertex vert
    92.             #pragma fragment frag
    93.             #pragma fragmentoption ARB_precision_hint_fastest
    94.             #include "UnityCG.cginc"
    95.        
    96.             struct appdata_t {
    97.                 float4 vertex : POSITION;
    98.                 float2 texcoord: TEXCOORD0;
    99.             };
    100.        
    101.             struct v2f {
    102.                 float4 vertex : POSITION;
    103.                 float4 uvgrab : TEXCOORD0;
    104.             };
    105.        
    106.             v2f vert (appdata_t v) {
    107.                 v2f o;
    108.                 o.vertex = v.vertex;
    109.                 #if UNITY_UV_STARTS_AT_TOP
    110.                 float scale = -1.0;
    111.                 #else
    112.                 float scale = 1.0;
    113.                 #endif
    114.                 o.uvgrab.xy = (float2(o.vertex.x, 0.0015f+o.vertex.y*scale) + o.vertex.w) * 0.5;
    115.                 o.uvgrab.zw = o.vertex.zw;
    116.                 return o;
    117.             }
    118.        
    119.             sampler2D _GrabTexture;
    120.             float4 _GrabTexture_TexelSize;
    121.             float _Size;
    122.        
    123.             half4 frag( v2f i ) : COLOR {
    124.                 half4 sum = half4(0,0,0,0);
    125.  
    126.                 #define GRABPIXEL(weight,kernelx) tex2Dproj( _GrabTexture, UNITY_PROJ_COORD(float4(i.uvgrab.x + _GrabTexture_TexelSize.x * kernelx*_Size, i.uvgrab.y, i.uvgrab.z, i.uvgrab.w))) * weight
    127.  
    128.                 sum += GRABPIXEL(0.02, -5.0);
    129.                 sum += GRABPIXEL(0.05, -4.0);
    130.                 sum += GRABPIXEL(0.08, -3.0);
    131.                 sum += GRABPIXEL(0.11, -2.0);
    132.                 sum += GRABPIXEL(0.15, -1.0);
    133.                 sum += GRABPIXEL(0.18,  0.0);
    134.                 sum += GRABPIXEL(0.15, +1.0);
    135.                 sum += GRABPIXEL(0.11, +2.0);
    136.                 sum += GRABPIXEL(0.08, +3.0);
    137.                 sum += GRABPIXEL(0.05, +4.0);
    138.                 sum += GRABPIXEL(0.02, +5.0);
    139.            
    140.                 return sum;
    141.             }
    142.             ENDCG
    143.         }
    144.  
    145.         GrabPass { }
    146.  
    147.         // Vertical blur
    148.         Pass {
    149.             Stencil
    150.             {
    151.                 Ref 0
    152.                 Comp NotEqual
    153.             }
    154.          
    155.             Lighting Off
    156.             Cull Off
    157.             ZTest Always
    158.             ZWrite Off
    159.             Blend One Zero
    160.          
    161.             Fog
    162.             {
    163.                 Mode Off
    164.             }
    165.        
    166.             CGPROGRAM
    167.             #pragma vertex vert
    168.             #pragma fragment frag
    169.             #pragma fragmentoption ARB_precision_hint_fastest
    170.             #include "UnityCG.cginc"
    171.        
    172.             struct appdata_t {
    173.                 float4 vertex : POSITION;
    174.                 float2 texcoord: TEXCOORD0;
    175.             };
    176.        
    177.             struct v2f {
    178.                 float4 vertex : POSITION;
    179.                 float4 uvgrab : TEXCOORD0;
    180.             };
    181.        
    182.             v2f vert (appdata_t v) {
    183.                 v2f o;
    184.                 o.vertex = v.vertex;
    185.                 #if UNITY_UV_STARTS_AT_TOP
    186.                 float scale = -1.0;
    187.                 #else
    188.                 float scale = 1.0;
    189.                 #endif
    190.                 o.uvgrab.xy = (float2(o.vertex.x, 0.0015f+o.vertex.y*scale) + o.vertex.w) * 0.5;
    191.                 o.uvgrab.zw = o.vertex.zw;
    192.                 return o;
    193.             }
    194.        
    195.             sampler2D _GrabTexture;
    196.             float4 _GrabTexture_TexelSize;
    197.             float _Size;
    198.        
    199.             half4 frag( v2f i ) : COLOR {
    200.                 half4 sum = half4(0,0,0,0);
    201.  
    202.                 #define GRABPIXEL(weight,kernely) tex2Dproj( _GrabTexture, UNITY_PROJ_COORD(float4(i.uvgrab.x, i.uvgrab.y + _GrabTexture_TexelSize.y * kernely*_Size, i.uvgrab.z, i.uvgrab.w))) * weight
    203.            
    204.                 sum += GRABPIXEL(0.02, -5.0);
    205.                 sum += GRABPIXEL(0.05, -4.0);
    206.                 sum += GRABPIXEL(0.08, -3.0);
    207.                 sum += GRABPIXEL(0.11, -2.0);
    208.                 sum += GRABPIXEL(0.15, -1.0);
    209.                 sum += GRABPIXEL(0.18,  0.0);
    210.                 sum += GRABPIXEL(0.15, +1.0);
    211.                 sum += GRABPIXEL(0.11, +2.0);
    212.                 sum += GRABPIXEL(0.08, +3.0);
    213.                 sum += GRABPIXEL(0.05, +4.0);
    214.                 sum += GRABPIXEL(0.02, +5.0);
    215.            
    216.                 return sum;
    217.             }
    218.             ENDCG
    219.         }
    220.      
    221.         Pass {
    222.             Stencil
    223.             {
    224.                 Ref 0
    225.                 Comp NotEqual
    226.             }
    227.          
    228.             Lighting Off
    229.             Cull Off
    230.             ZTest Always
    231.             ZWrite Off
    232.             Blend SrcAlpha OneMinusSrcAlpha
    233.          
    234.             Fog
    235.             {
    236.                 Mode Off
    237.             }
    238.        
    239.             CGPROGRAM
    240.             #pragma vertex vert
    241.             #pragma fragment frag
    242.             #pragma fragmentoption ARB_precision_hint_fastest
    243.             #include "UnityCG.cginc"
    244.        
    245.             struct appdata_t {
    246.                 float4 vertex : POSITION;
    247.                 float2 texcoord: TEXCOORD0;
    248.             };
    249.        
    250.             struct v2f {
    251.                 float4 vertex : POSITION;
    252.                 float4 uvgrab : TEXCOORD0;
    253.             };
    254.        
    255.             v2f vert (appdata_t v) {
    256.                 v2f o;
    257.                 o.vertex = v.vertex;
    258.                 #if UNITY_UV_STARTS_AT_TOP
    259.                 float scale = -1.0;
    260.                 #else
    261.                 float scale = 1.0;
    262.                 #endif
    263.                 o.uvgrab.xy = (float2(o.vertex.x, 0.0015f+o.vertex.y*scale) + o.vertex.w) * 0.5;
    264.                 o.uvgrab.zw = o.vertex.zw;
    265.                 return o;
    266.             }
    267.  
    268.             float4 _shadowVolumeColor;
    269.             sampler2D _OriginalGrab;
    270.             float _Size;
    271.        
    272.             half4 frag( v2f i ) : COLOR {
    273.                 half4 col = tex2Dproj( _OriginalGrab, UNITY_PROJ_COORD(i.uvgrab));
    274.                 col.w = 1.0f - _shadowVolumeColor.w;
    275.                 return col;
    276.             }
    277.             ENDCG
    278.         }
    279.     }
    280. }
     
    Last edited: Jun 20, 2015
  38. gustavolsson

    gustavolsson

    Joined:
    Jan 14, 2011
    Posts:
    339
    Hi! Thanks for using the toolkit! I want to point out however, that even though the toolkit is free at the moment, I still retain the copyright to the toolkit and I do not want you to publish the code in any way. Please remove it.

    You can post just the part of the shader that is related to the actual blurring, but not the whole thing!

    Some months ago I spent some time making a depth-aware blur shader for the toolkit but it just wasn't good enough to be included in an update (and back then it would only work for Unity Pro users). If there is demand for it, I could work a bit more on it and publish it in an update.

    Best regards,
    Gustav
     
  39. scopigno

    scopigno

    Joined:
    Jul 10, 2014
    Posts:
    9
    Hi.

    The only thing I kept from the original file is the shader name, because I needed to replace the original one.
    I'm also calling the _shadowVolumeColor variable, because the shader needs its value.
    Thus I'm not using any logic or algorithm created by you, I'm not even including your cginc.
    Even the simple first pass was created by me and is returning a diff color.

    I can now see that this shader is all original, so I removed the copyright statement with your name from the comments.

    Btw, I also have a depth-aware solution.
    It is much better and faster than the one I posted.
    I'm getting max frame rate on my SGS4.

    Thanks.
     
    Last edited: Jun 20, 2015
  40. gustavolsson

    gustavolsson

    Joined:
    Jan 14, 2011
    Posts:
    339
    Sorry about that. I didn't read it through thoroughly enough. The start was so similar so I just assumed the rest was the same. My apologies.

    As a note on implementation, I would probably save the output of the shadow volumes toolkit to a render texture and then use a regular post-processing pass to sample the shadow and combine it with the rest of the framebuffer, as opposed to replacing the interpolation pass.

    Sounds good! I would be grateful if you wanted to post that so others can use it, and so that I can reference it for my implementation.

    Sorry again for the accusation!
     
  41. scopigno

    scopigno

    Joined:
    Jul 10, 2014
    Posts:
    9
    That is exactly how I'm doing it.
    I learned how to stencil the post-processing fx in the following thread:
    http://forum.unity3d.com/threads/leveraging-stencil-buffers-for-masked-post-effects.313466/
    Take a look at the last post for a downloadable example.

    No problem.
     
  42. scopigno

    scopigno

    Joined:
    Jul 10, 2014
    Posts:
    9
    OK, I'm sharing my files.
    But I'm not getting max frame rate on mobile as I though.
    Maybe you can show me a way on how to optimize it.

    INSTRUCTIONS / HOW TO USE IT:
    1. Set Shadows Volume Toolkit Renderer Layer to Shadow (create the layer if it doesn't exist).
    2. On your main camera set the Clear Flags to Depth Only and the culling mask to everything but Shadow.
    3. Create another camera as a child of your main Camera and name it Shadows Camera.
    4. On your shadows camera set the Clear Flags to Depth Only and the culling mask to Shadow.
    5. Create another camera as a child of your main Camera and name it Post Processing.
    6. On your post processing camera set the Clear Flags to Don't Clear and the culling mask to Nothing.
    7. Attach the PostProcessing script to the post processing camera and reference your Shadows Camera (drag-n-drop).
    You must also modify the ScreenColorFragment method on Screen.cginc file to return with no alpha.
    You may need to reimport all the other shaders from Shadow Volume Toolkit.
    Code (CSharp):
    1. float4 ScreenColorFragment() : COLOR
    2. {
    3.    return float4(_shadowVolumeColor.x, _shadowVolumeColor.y, _shadowVolumeColor.z, 1.0f);
    4. }
    By the way, I'm also modifying your asset so the caster does not receive its shadow but receives all others.
     

    Attached Files:

    Last edited: Jun 22, 2015
  43. scopigno

    scopigno

    Joined:
    Jul 10, 2014
    Posts:
    9
    Just found a way to blur the shadows outside the stencil area.
    I'm rendering a dummy black and white texture on ShadowsCamera.
    Then I apply the desired blur effect making white color transparent and blending the color and alpha selected on ShadowVolumeRenderer.cs.

    Performance is even better than the unitpackage in my last post (less blits).

    But unfortunately I can't post the code here for the following reasons:
    • I had to modify the Shadow Volume Toolkit asset and it is copyrighted.
    • The code has some assets that I'm using in my project, and getting them all out requires a lot of work.
    The attached image shows my final render.
     

    Attached Files:

  44. gustavolsson

    gustavolsson

    Joined:
    Jan 14, 2011
    Posts:
    339
  45. N00MKRAD

    N00MKRAD

    Joined:
    Dec 31, 2013
    Posts:
    210
    Cool!
    Shadow volumes have always impressed me, really interesting technique. Might give this a shot soon!
     
    gustavolsson likes this.
  46. Nyanpas

    Nyanpas

    Joined:
    Dec 29, 2016
    Posts:
    406
    Will this work for WebGL?

    Also are you using DrawMeshInstanced this time?
     
    gustavolsson likes this.