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. MABManZ

    MABManZ

    Joined:
    Sep 29, 2011
    Posts:
    144
    Looking at manno's post, if he doesn't need a true shadow that deforms to light sources, he could get away with just using a flat quad w/ a transparent shadow texture.
     
  2. PixelEnvision

    PixelEnvision

    Joined:
    Feb 7, 2012
    Posts:
    513
    gustavolsson,

    Just purchased, I'm curious if the current version (1.2) fully compatible (or tested) with Unity 3.5?
     
  3. carking1996

    carking1996

    Joined:
    Jun 15, 2010
    Posts:
    2,609
    I'm using it with 3.5 with no problems.
     
  4. goat

    goat

    Joined:
    Aug 24, 2009
    Posts:
    5,182

    Thsat can be done with Unity Basic, check the Lerpz tutorial.
     
  5. Velvety

    Velvety

    Joined:
    May 4, 2009
    Posts:
    153
    If all you want is a simple "fake shadow" that is always below your ball, you can easily do this by putting a plane below the ball that follows the ball. The plane would be textured with a blob-like shadow texture. This is easy and much more efficient than a projector, but it only really works with flat surfaces (which is fine for most cases). I just added it to my game for a similar reason. Here is an example of a script you can use on the "shadow plane" gameobject to make it follow your ball:

    http://answers.unity3d.com/questions/38587/ios-plane-shadow-that-follows-object-on-flat-terra.html
     
  6. duke

    duke

    Joined:
    Jan 10, 2007
    Posts:
    763
    This is great for the niche of visualisation. As long as I can render stuff at HD at a frame per second with crisp shadows i'm happy so these are great!
     
  7. gustavolsson

    gustavolsson

    Joined:
    Jan 14, 2011
    Posts:
    339
    Hi everyone!

    I'm going to start working on a new update in a week or two. It should increase performance slightly and simplify a lot of the scene setup, so stay tuned :)

    manno sent me an email too so I answered the question that way. It's nice with more opinions though, thanks!

    Cool, thanks!
     
  8. Tiles

    Tiles

    Joined:
    Feb 5, 2010
    Posts:
    2,481
    Awesome. Looking forward to it =)

    Will it be 3.5 only then?
     
  9. PixelEnvision

    PixelEnvision

    Joined:
    Feb 7, 2012
    Posts:
    513
    Great news, can't wait...
     
  10. God-at-play

    God-at-play

    Joined:
    Nov 3, 2006
    Posts:
    330
    BLACK SHADOWS ARE SO UGLY. How can you people tolerate this?! I sure can't!

    That's why I made the following changes to use shadow strength as an ambient term:

    Screen.cginc line 3
    Code (csharp):
    1. float4 _strength;
    Screen.cginc lines 29+
    Code (csharp):
    1. float4 ScreenStrengthFragment() : COLOR
    2. {
    3.     return _strength;
    4. }
    ScreenFinal.shader line 7
    Code (csharp):
    1. _strength ("Shadow Color (RGB) Strength (A)", Color) = (1.0, 1.0, 1.0, 0.5)
    ScreenFinal.shader line 58
    Code (csharp):
    1. Blend DstColor OneMinusDstAlpha
    ShadowVolumeRenderer.cs lines 11-21
    Code (csharp):
    1. [SerializeField]
    2.     private Color shadowStrength;
    3.    
    4.     /// <summary>
    5.     /// Gets or sets the shadow strength of all shadow volumes.
    6.     /// </summary>
    7.     public Color ShadowStrength
    8.     {
    9.         get { return shadowStrength; }
    10.         set { shadowStrength = value; }
    11.     }
    ShadowVolumeRenderer.cs line 84
    Code (csharp):
    1. materials[4].SetColor(strengthPropertyName, shadowStrength);
    ShadowVolumeRendererEditor.cs line 16
    Code (csharp):
    1. source.ShadowStrength = EditorGUILayout.ColorField(new GUIContent("Shadow Color (RGB) Strength (A)", shadowStrengthTooltip), source.ShadowStrength);
     
    Last edited: Feb 27, 2012
  11. God-at-play

    God-at-play

    Joined:
    Nov 3, 2006
    Posts:
    330
    It looks like there's bugs in the logic here as well. I'm not sure what's causing this (possibly my camera with Layer masking and bringing in a volume prefab that doesn't match the renderer's layer?), but at various times - like after quitting Unity and opening it again - my material references for shadow volumes get null.

    Part of your code neither does anything about it nor even checks for null refs, only that the arrays have entries:
    Code (csharp):
    1. if (renderer.sharedMaterials.Length == 0)
    2. if (materials.Length == 5)
    and so on. You should check to see if the material array is not only filled, but that it is filled with references. That way this line doesn't throw a null ref:
    Code (csharp):
    1. materials[4].SetFloat
    My materials[4] gets null at some point, so I get exceptions.
     
  12. God-at-play

    God-at-play

    Joined:
    Nov 3, 2006
    Posts:
    330
    I fixed a couple problems by adding better checks. A specific area I had to add some code to was ShadowVolume in onder to make sure my materials were assigned properly. The code below could be overdoing it, but I don't really have time to figure out exactly what's going on and fix it "right." That's why I bought this actually, short on time.

    Code (csharp):
    1. if (setMaterials)
    2. {
    3.     renderer.sharedMaterials = source.SharedMaterials;
    4.                    
    5.     setMaterials = false;
    6. }
    7. else
    8. {
    9.     if (renderer.sharedMaterials == null || renderer.sharedMaterials.Length == 0 || renderer.sharedMaterials[0] == null)
    10.     {
    11.         renderer.sharedMaterials = source.SharedMaterials;
    12.     }
    13. }

    If it helps any, my camera is in a prefab, and when I apply my changes I get this 2 times:
    Code (csharp):
    1. transform.position assign attempt for 'PreRenderCamera' is not valid. Input position is { NaN, NaN, -Infinity }.
    2. UnityEditor.DockArea:OnGUI()
    3.  
     
    Last edited: Feb 27, 2012
  13. God-at-play

    God-at-play

    Joined:
    Nov 3, 2006
    Posts:
    330
    EDIT: Found the problem, though I'll leave this here for future reference:

    I had to change the Bounds Margin to a higher value than 2, click Create again to regenerate the mesh, and then apply my prefab change. When I tested it before I didn't apply changes so I didn't see that fix the issue.

    Maybe add the Bounds Margin note in the README?

    ----------------------------

    My woes continue. It seems some shadows disappear when the camera is inside them? Or something like that. Please note that I'm using a Light prefab and NOT a Light Simple prefab. Also note that the viewport shows the shadows correctly, so it's not an issue of extrude length.

    My camera is in the desired location, but no shadows from the clouds:


    A series of images showing the clouds disappearing after the camera comes in toward the desired location:


     
    Last edited: Feb 27, 2012
  14. Xellinus

    Xellinus

    Joined:
    Jan 28, 2012
    Posts:
    44
    Oh my god I love your terrain! I want to do just that! =) How!
     
  15. gustavolsson

    gustavolsson

    Joined:
    Jan 14, 2011
    Posts:
    339
    Passing materials around is an ugly workaround for supporting multiple shadow sources, in the next version it will only be possible to have one enabled shadow source at any time, but the code will be completely stable. (The material references are sometimes lost at the moment)

    Yeah, the shadows of the clouds disappears because of Unity's built-in frustum culling; change the Bounds Margin property to fix the problem. I'll update the tooltip/read me to clearly state that the mesh needs to be recreated for the setting to take effect.

    Also, I plan to add support for shadow color in the next version :)
     
  16. JanHelleman

    JanHelleman

    Joined:
    Oct 11, 2009
    Posts:
    116
    Hi,

    using AR from qualcomm and this doesn't work. I think it has to do with the fact that the clear value of the camera is black + alpha 0, and maybe your post rendering affects this situation. Do you have any idea why this is?

    ios 5 is not working at all (with latest unity). I can test anything you want on both ipad 2 and iphone 4. PM me if you need me.
     
    Last edited: Feb 28, 2012
  17. God-at-play

    God-at-play

    Joined:
    Nov 3, 2006
    Posts:
    330
    Thanks Gustav, I look forward to the next version then. :)
     
  18. rockysam888

    rockysam888

    Joined:
    Jul 28, 2009
    Posts:
    650
    (bookmarked)
     
  19. jujunosuke

    jujunosuke

    Joined:
    Apr 30, 2011
    Posts:
    190
    gustavolsson, it is simply awesome !
    I was looking for a real time shadow solution with Indie !

    The demo run very smoothly on my iOS Lion ! Congratulation.

    I have one question though, All the demo i have seen was used on very simple objects.
    Can you make more demos with a little bit more complicated meshes to see the performance ?

    I want to use it on a car but i am afraid of performance i would get on my computer (Mac book pro).

    Is it possible to cast shadow of a invisible mesh ?
    My thinking was to create an invisible simplified mesh of my car, so the geometry would be easier to cast.
    What do you think ?
     
  20. HolBol

    HolBol

    Joined:
    Feb 9, 2010
    Posts:
    2,887
    DID you just say "iOS Lion"?

    Sweet jumping jellyfish.
    Ever heard of Mac OS? Yeah, that thing you're using right now.
     
  21. jujunosuke

    jujunosuke

    Joined:
    Apr 30, 2011
    Posts:
    190
    Who care ?

    If you can't answer my question just don't say anything.
     
  22. Tiles

    Tiles

    Joined:
    Feb 5, 2010
    Posts:
    2,481
    That`s in fact the way Gustav`s plugin works. And yes, you can define any mesh to be your shadow thrower you want for that.
     
  23. jujunosuke

    jujunosuke

    Joined:
    Apr 30, 2011
    Posts:
    190
    Tiles, thank you for your answer !
    This really look very interesting.
     
  24. gustavolsson

    gustavolsson

    Joined:
    Jan 14, 2011
    Posts:
    339
    So, I've spent the last couple of days rewriting the shadow volume toolkit and I believe it's a lot simpler and easier to use now. Here are the important updates:

    • Shadow meshes are now created as assets using a separate dialog (easier to store and share between objects)
    • There are no longer any prefabs, only one script per object type (drag&drop into empty game objects)
    • Added the ability to change the shadow color (not true modulation, but good enough)
    • Shadow object materials no longer have distracting public properties (no more inspector clutter)
    • Extremely stable (materials are no longer lost)

    The new workflow:

    1. Click on Window -> Shadow Mesh Creator to open up the mesh creator dialog. Select a reference mesh and click to create a shadow mesh asset
    2. Drag drop the ShadowVolumeRenderer script onto an empty game object (preferably called "Shadow Renderer")
    3. Drag drop the ShadowVolumeSource script onto a light game object
    4. For each shadow caster - create an empty game object (preferably called "Shadow") and put it as a child to the shadow caster. Drag drop the ShadowVolume script onto the child object. Choose which shadow mesh the child object should use.
    5. Done!

    I also lowered the full screen alpha-channel passes from 15 to 11 using a new Unity 3.5 feature (BlendOp RevSub), but here is where I got stuck. Even though the OpenGL ES 2.0 standard requires GPUs to support the feature, my android device does not. It works fine on my Mac and in-editor but not on the actual device. Apparently, android driver makers sometimes "forget" to support parts of the standard..

    So, I could either release the new version with all the updates above but with 15 full screen passes (as before), or I could spend more time with it and add the option to let you choose which method to use. (If you're NOT targeting android, it should work fine with 11 passes)

    I'll probably release the 15 pass version as soon as I can and start working on the ability to choose which version to use when I have more free time. :)
     
    Last edited: Mar 16, 2012
  25. Tiles

    Tiles

    Joined:
    Feb 5, 2010
    Posts:
    2,481
    That sounds really nice. Can`t wait to get my hands on it. What about simply having two versions? One for non android devices, one for android devices?
     
  26. gustavolsson

    gustavolsson

    Joined:
    Jan 14, 2011
    Posts:
    339
    I really want it all to be one package, and it should be easy to switch between the "modes" if someone decides to target android after the game is made for example. It won't take a lot of time to implement, but I would like to release the current version as soon as possible (this weekend hopefully).
     
    Last edited: Mar 16, 2012
  27. carking1996

    carking1996

    Joined:
    Jun 15, 2010
    Posts:
    2,609
    Looks good! Great updates. Cant wait to try the new updates! :)
     
  28. PixelEnvision

    PixelEnvision

    Joined:
    Feb 7, 2012
    Posts:
    513
    Thanks for the update, can't wait to try it...

    That sounds perfectly fine for me...
     
  29. gustavolsson

    gustavolsson

    Joined:
    Jan 14, 2011
    Posts:
    339
    I just submitted the new version (2.0) to the asset store! I had quite a lot of free time this weekend so I managed to implement the ability to choose compatibility mode. This means that you'll not have to wait for the performance boost :)

    The compatibility mode is set to Standard by default; Only use the NoBlendOp mode when targeting platforms that don't reliably support the BlendOp shader function, such as the Android platform. (The new version has been tested on my Android device and it works great in NoBlendOp mode)

    I've also raised the price from 20$ to 30$ because of the time I spent on the update and because I believe the new toolkit is very user friendly (as opposed to the last version). If you haven't bought it yet but plan to, I suggest buying it before Unity approves it and the price change takes effect :)

    If you've already purchased the toolkit you will be able to download the update for free as usual, stay tuned!
     
    Last edited: Mar 18, 2012
  30. carking1996

    carking1996

    Joined:
    Jun 15, 2010
    Posts:
    2,609
    Thanks! I'm about to redownload it! :)
     
  31. PixelEnvision

    PixelEnvision

    Joined:
    Feb 7, 2012
    Posts:
    513
    Great news, thank you...

    One Q... Do you know if Kindle Fire can support standard (BlendOp) mode? I don't have that device and it's the only android device I'm targeting...
     
  32. gustavolsson

    gustavolsson

    Joined:
    Jan 14, 2011
    Posts:
    339
    Nope, the only way to really tell is to try it on the actual device, and I don't have one either.
     
  33. AaronC

    AaronC

    Joined:
    Mar 6, 2006
    Posts:
    3,552
    Any thoughts whats happening here folks?
    $Screen shot 2012-03-20 at 3.51.30 PM.jpg
    This is in editor..
    Cheers
    AaronC

    EDIT I'm using a different mesh for shadow casting if that makes any difference, the wheels are separate on the car but attached on the shadow casting mesh
     
    Last edited: Mar 20, 2012
  34. AaronC

    AaronC

    Joined:
    Mar 6, 2006
    Posts:
    3,552
    Ok well sadly it looks like I cant use this as we're at shipping point right now, and we cant have these pinstripes..

    Also Gustav, I too have found the "everything turns pink and lots of errors" issue.

    Try this - see if you can reproduce:

    Make a scene or scenes, using your tools. Select scene files >rightclick> select dependencies then (remember to reselect the scene file if its become unselected) > export as package. Make a new Project and import said package. Thats how I ran in to problems.

    Reimporting the original asset store package doesnt fix it either
     
  35. PixelEnvision

    PixelEnvision

    Joined:
    Feb 7, 2012
    Posts:
    513
    Thanks, I'll try to get one soon to test and report back the results...
     
  36. gustavolsson

    gustavolsson

    Joined:
    Jan 14, 2011
    Posts:
    339
    Just to be clear, the new version (2.0) is NOT YET released! I still await approval from Unity.

    The pinstripes appear when you switch to a mobile platform in Unity, right? The shadow and the visible mesh are very close and on platforms with low quality z-buffers z-fighting will occur (the stripes). To fix the problem, just increase the Extrude Bias of the light's ShadowVolumeSource script (Open up the closed Advanced tab to access the properties) or force a depth buffer with a higher bit count in the build settings!

    Also, the new version should fix the pink missing materials.

    Hope this helps :)
     
    Last edited: Mar 20, 2012
  37. PolyMad

    PolyMad

    Joined:
    Mar 19, 2009
    Posts:
    2,350
    Just to know: I have Unity Pro, what is the advantage of this plugin?
     
  38. Zen-Davis

    Zen-Davis

    Joined:
    Aug 14, 2009
    Posts:
    285
    And how well does this work on mobile?
     
  39. escpodgames

    escpodgames

    Joined:
    Jan 9, 2012
    Posts:
    9
    I also have Unity Pro - the only reason you would purchase this is to use shadows on a mobile platform.

    I just tested it on an iPad 2 with IOS 4.3.1 (haven't tested IOS 5 yet) and it works a treat.
     
  40. PolyMad

    PolyMad

    Joined:
    Mar 19, 2009
    Posts:
    2,350
    Wow... can you give a word or two about performance on iPad? :)
     
  41. escpodgames

    escpodgames

    Joined:
    Jan 9, 2012
    Posts:
    9
    I'm waiting for the update before I look at how it performs - I used the test scene that comes with the plugin (not exactly mobile friendly)
     
  42. hthth

    hthth

    Joined:
    Mar 21, 2012
    Posts:
    5
    Same here—waiting for the update. Thanks for confirming that it runs! Couple of us here who hope the SVT can provide shadows on iPad 2+iOS 5. It's likely you'll beat us to it, so we're joining the growing group of people who'd appreciate hearing results of your testing! =)
     
  43. Enzign

    Enzign

    Joined:
    Aug 20, 2010
    Posts:
    169
    Has anyone tried this on Unity 3.5? I just bought it from the asset store, but can't really get it showing anything. And all the assets are pink in the testing scenes. Will do some more testing and research, just wanted to check if it's supposed to work out of the box on 3.5. Thanks.
     
  44. Tiles

    Tiles

    Joined:
    Feb 5, 2010
    Posts:
    2,481
    Haven`t had problems to convert my 3.42 project to 3.5. The shadows still worked. It`s the drawcall issues that forced me to go back to 3.42 then though. Hopefully Unity will release the patch soon :)
     
  45. escpodgames

    escpodgames

    Joined:
    Jan 9, 2012
    Posts:
    9
    Worked for me using 3.5 Unity Pro (on mac)
     
  46. Enzign

    Enzign

    Joined:
    Aug 20, 2010
    Posts:
    169
    Seems like it works fine if we start a new project, but if we import the package in our current project we get some shader errors for some reason.
     
  47. Enzign

    Enzign

    Joined:
    Aug 20, 2010
    Posts:
    169
    The graphics settings for the editor was set too low in our current project. Sorry for the disturbance. Maybe somebody will run into the same issue.
     
  48. Wolfos

    Wolfos

    Joined:
    Mar 17, 2011
    Posts:
    951
    Do these work as bad as the implementation on the Wiki? That one didn't work with most meshes.
     
  49. Enzign

    Enzign

    Joined:
    Aug 20, 2010
    Posts:
    169
    Seems to be working fine so far. Your mesh obviously can't be crazy, since everything will cast a shadow, but it does what it's supposed to.
     
  50. gustavolsson

    gustavolsson

    Joined:
    Jan 14, 2011
    Posts:
    339
    The new version (2.0) is now released on the asset store!
    Note that Unity 3.5 is required, see posts below.

    Here is a video of the basic workflow: (Annotations coming soon)



    Please let me know if you run into any problems with the update! :)
     
    Last edited: Mar 24, 2012