Search Unity

[RELEASED] Fast Shadows

Discussion in 'Assets and Asset Store' started by Phong, Sep 17, 2012.

  1. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085


    Version 1.2 now available in the asset store! Fast Shadows is faster than ever!

    Fast Shadows is a fast, easy to use substitute for the Unity3D Blob Shadow Projector. It is heavily optimized for mobile devices. Raycasting determines where shadows should appear. Shadows are a square mesh with a transparent material that hovers above a surface. Fast Shadows is VERY fast!

    Features
    • All shadows in a scene can share a single draw call
    • Works with the free indy version of Unity3D
    • A layer mask can be used to specify which surfaces show shadows
    • Can control the maximum projection distance. Shadows fade and disappear as they approach this distance
    • ‘Static’ shadows (shadows that don’t move) are calculated only once and have no overhead in Update. A scene can have hundreds of static shadows on a mobile device.
    • Different shadows can have different shapes and still share a single draw call using a texture atlas
    • Shadows can be cast by a light object (or any game object) or by specifying a light direction vector
    • Can use either the provided shadow material, or your own shadow material for interesting effects.
    • Shadow rotation matches object’s rotation
    • Perspective and orthographic projection
    • Easy to use. No Scripting required.
    • FAST, FAST, FAST! Did I mention it is fast?
    Links

    Easy To Use
    Add the FS_ShadowSimple script to your object and a shadow will appear when you press play. No scripting!

    Limitations
    These shadows are designed to be fast, not 100% accurate. There are some limitations:

    • Shadows are NOT volumetric; the size and the orientation of the shadows DO change, but the shape of the silhouette does not. Shadows use a light cookie (as does the blob shadow projector). The shape or the silhouette in the light cookie will not change as an object tumbles.
    • Shadows work best when cast on smooth surfaces. The shadow will not deform to match the contours of the surface. Spikes can penetrate the shadow and grooves are ignored.
    • Shadows only project onto one surface at a time. This creates a slight discontinuity as shadows pass from one object to another or over corners.
    • The edge of a shadow can extend beyond the object it is being projected upon.
    For moving objects, or transparent shadows with blurry edges, these oddities are hardly noticeable.

    Live Demo
    Try the live demo.

    Videos
    How To Use It:

    Fast Shadows On Uneven Terrain:


    Screenshots






    Update Fast Shadows 1.1 Released

    This update includes:
    Smoother shadow rotations
    Better documentation
    All fields can be modified dynamically
    Code cleanup
    Fixed a problem with one of the sample scenes
     
    Last edited: Jan 9, 2013
  2. ZJP

    ZJP

    Joined:
    Jan 22, 2010
    Posts:
    2,649
    For Unity "Free"?
     
  3. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    Yes, it was developed using the free version of Unity3d. definitely works.
     
    Last edited: Sep 17, 2012
  4. brilliantgames

    brilliantgames

    Joined:
    Jan 7, 2012
    Posts:
    1,937
    I see that it goes to the angle of the 'wall' or 'ground'. But what if half the shadow is on the floor and the other on the wall? Will the shadow project onto the 2 angles correctly?
     
  5. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    The shadow will not distribute across two objects correctly. It will appear on one surface, then jump to the other. Even so, the illusion can still be quite convincing. Check out the web demo as an example. The shadows work best if your game has a large reasonably smooth floor.
     
  6. unityasoft

    unityasoft

    Joined:
    Oct 28, 2011
    Posts:
    263
    is it a good idea to cast rays for shadows? i already own ramp brush
     
  7. rockysam888

    rockysam888

    Joined:
    Jul 28, 2009
    Posts:
    650
    (bookmarked)
     
  8. nukeD

    nukeD

    Joined:
    Feb 12, 2009
    Posts:
    411
    Purchased and loving it...



    When using a light source + perspective i achieve great results (almost PRO looking), there are places, when going around the track where the blob orientation suddenly switches direction... but its not a big deal.

    Great asset, exactly what Unity indie needs!!
    @everyone: Just buy it!!

    Thank you!
     
  9. I am da bawss

    I am da bawss

    Joined:
    Jun 2, 2011
    Posts:
    2,574
    Question, how is this different from Projector Shadow (Blob Shadow)?
    What does it bring to the table that's not offered in Projector Shadow (besides single draw call optimization)?
     
  10. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    Here is my understanding of how the blob shadow projector works:

    Unity first renders all objects in the scene. Unity then determines which objects are in the frustrum of the blob shadow projector. Unity re-renders these objects with a transparent blob shadow shader and overlays this on top of the other render.

    Advantages:
    • Shadows conform well to geometry.

    Disadvantages:
    • Lots of extra draw calls, 100 shadows on a terrain means the terrain mesh is sent to the graphics card 100 times!
    • shadows project through walls and onto backsides of objects,
    • performance sucks on mobile (one shadow will noticeably slow the framerate, several will kill it).
    Blob shadow projector is best choice for non-mobile platforms and when only a few shadows will be needed (not good for armies), or when shadow need to conform well to geometry.

    Here is how fast shadows works:

    A raycast is done to determine where a shadow should appear. Quad meshes are generated for each shadow and combined into a single draw call.
    Advantages:
    • Speed (mobile can support a dozen or so moving shadows and hundreds of static shadows. non-mobile can support thousands, try that with a blob shadow projector),
    • shadows don't project through objects.
    Disadvantages:
    • Shadows don't conform to geometry perfectly,
    • they can extend beyond edges of an object and as they pass from object to object they only render on one object at a time.
    Fast shadows is a good choice for smooth walls and floors, and where mobile devices and large numbers of shadows are needed. It works reasonably well on terrains provided the shadows are not much larger than the triangles making up the terrain mesh (view you terrain in wireframe to see).
     
    Last edited: Sep 24, 2012
  11. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    Yes, consider 1000 objects with shadows on a terrain.

    Using the blob shadow projector the terrain mesh will be sent to the graphics card 1001 times! This will be very slow even on a PC with a good graphics card.

    With Fast Shadows, there will be 1000 raycasts and a bit of math with each shadow to calculate the mesh. Then one call to draw them. If the shadows are static then there is raycast and math only happen once. This is MUCH faster!
     
    Last edited: Sep 24, 2012
  12. zappapa

    zappapa

    Joined:
    Dec 12, 2010
    Posts:
    57
    Could you please show me how your Fast Shadows will look like when cast (projected) onto non flat surfaces - such as terrains?
     
  13. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    I will whip an example up tonight and post it tomorrow.
     
  14. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    The shadow jumping may be due to the rotations in the model. Fast Shadows wants the model to have Y=up, Z=forward, X=right. When I import models from blender they have rotations which confuses Fast Shadows. This can be fixed by using a hierarchy like:

    • GameObject (Attach FS_SimpleShadow here and your character controller should move this. default rotation should be 0,0,0)
      • Model Game Object
     
  15. imtrobin

    imtrobin

    Joined:
    Nov 30, 2009
    Posts:
    1,548
    I like to see it on a terrain too. If you can raycast, get the surface normal and rotate the shadow, then it could be good replacement for blob
     
  16. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    Here is a video of Fast Shadows on a terrain. It works quite well when character is approximately the same size as the triangles in the terrain mesh. For very large game objects such as huge hovering space ships it does not work very well.

     
    Last edited: Sep 19, 2012
  17. imtrobin

    imtrobin

    Joined:
    Nov 30, 2009
    Posts:
    1,548
    can u post demo for terrain. Also, is this limited to 1 type of rendering or can it have mutiple different materials to use as the shadow. I'm thinking of replacing the projector for those projector fx on the ground.
     
  18. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    Here is a downloadable platform demo of Fast Shadows on terrain:


    Each shadow has a material field in the inspector. The material you assign to the shadow can be any material. You are not limited to the default material provided with Fast Shadows.
     
    Last edited: Sep 21, 2012
  19. tbelgrave

    tbelgrave

    Joined:
    Jul 29, 2006
    Posts:
    321
    Excellent! Just what I needed. Will definitely pick this up.

    Cheers!
     
  20. imtrobin

    imtrobin

    Joined:
    Nov 30, 2009
    Posts:
    1,548
    I found a problem. When an object is on top of another, the shadow is projected on the object. It should have a layer filter so it can cast on terrain only.
     
  21. cheezorg

    cheezorg

    Joined:
    Jun 5, 2008
    Posts:
    394
    @ imtrobin There is a layer filter.
     
  22. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    There is a "Layer Mask" field (first one in the inspector) that does exactly this.
     
  23. imtrobin

    imtrobin

    Joined:
    Nov 30, 2009
    Posts:
    1,548
    How did I missed that? That will teach me not to work while jetlagged. Thanks!
     
  24. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    Update Fast Shadows 1.1 Released

    This update includes:

    • Smoother shadow rotations
    • Better documentation
    • All fields can be modified dynamically
    • Code cleanup
    • Fixed a problem with one of the sample scenes
     
  25. Lars-Steenhoff

    Lars-Steenhoff

    Joined:
    Aug 7, 2007
    Posts:
    3,526
    You know what would be cool if there is a way to have a animated texture for the shadow.

    This way I could put a shadow under a bird that animates it wings.
     
  26. crafTDev

    crafTDev

    Joined:
    Nov 5, 2008
    Posts:
    1,820
    Do I have to use a light source. I don't use any light sources since I'm deving for mobile.
     
  27. Lars-Steenhoff

    Lars-Steenhoff

    Joined:
    Aug 7, 2007
    Posts:
    3,526
    no light source is needed from my experience
     
  28. Lars-Steenhoff

    Lars-Steenhoff

    Joined:
    Aug 7, 2007
    Posts:
    3,526
    just have to say smooth rotations is cool, but they could rotate a little faster, sometimes it's visible that there is a delay in the shadow.
     
  29. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    The light source field can be any game object even an empty game object. You can also use a light direction vector.
     
  30. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    I am surprised it is not fast enough. You can change this in the FS_ShadowSimple.cs script (line 152):

    shadowCaster.transform.rotation = Quaternion.Lerp(shadowCaster.transform.rotation, Quaternion.LookRotation(proj, -lightDirection), .1f);

    The .1f is the amount of time it takes to Lerp the rotation. By reducing this it should speed up the rotation. I will try to expose this in the inspector in the next version.
     
    Last edited: Oct 13, 2012
  31. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    I think this could be done with a shadow atlas and an animation to animate the UV Rect.

    • Put all the frames for the animation in a texture. (say a 3x3 image atlas with 9 frames)
    • Add an animation component to the object with the shadow script. (You may have one already)
    • In the Unity animation window you should be able to see the FS_SimpleShadow component and animate the UV Rect x, y coordinates
    • Be sure to set the animation curve type to "broken" so that it jumps from frame to frame instead of sliding smoothly.

    You may be able to use the same animation that you are already using for the bird flapping.
     
    Last edited: Oct 13, 2012
  32. Lars-Steenhoff

    Lars-Steenhoff

    Joined:
    Aug 7, 2007
    Posts:
    3,526
    Yes thats exactly what I was thinking :) Thanks for making the updates!
     
  33. tbelgrave

    tbelgrave

    Joined:
    Jul 29, 2006
    Posts:
    321
    Hiya Phong, are there any known issues with having fast shadows on a character with a rigid body component? Currently it appears to be fighting with an enemy character I attached the script to. Thanks for the update!
     
  34. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    I am not aware of any problems. Are you experiencing something strange?
     
  35. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    Hi Lars, I think this can be done without the need for a special script by using an animation component to update the UV Rect. I modified the previous post to describe the technique. You may be able to use your existing bird flapping animation.
     
  36. tbelgrave

    tbelgrave

    Joined:
    Jul 29, 2006
    Posts:
    321
    Hmm seems to be working after I adjusted the character collider, looking like the character sunk into the ground a bit causing it to get confused. Anyhoo all is good :)
     
  37. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    That could do it. If the pivot point of the character was below the floor then the raycast will always miss.
     
  38. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    I have an update for Fast Shadows that is almost ready and another asset store tool that I would like to put through a beta test before putting up in the asset store.

    The fast shadows update includes tools for animating the shadows using keyframes in the atlas and animating the UVs.

    My other product is called MeshBaker. It is a mesh combine tool that lets you "bake" a bunch of models into a new set of assets (mesh,material,atlas textures, prefab). Atlases are built automatically and the new mesh has adjusted uvs, normals, tangents etc.. The objects that were baked can be replaced by the new combined (1 draw call) object.

    Please PM me if you would be interested in testing these these out.
     
  39. angel_m

    angel_m

    Joined:
    Nov 4, 2005
    Posts:
    1,160
    The MeshBaker tool looks interesting but it is not the same solution as other systems already present in the Asset Store like "Batching Tools"and so?
     
    Last edited: Nov 7, 2012
  40. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    Thanks for the heads up. I did a search before developing this and couldn't find anything similar but didn't discover "Batching Tools". Looking at it, it is very similar although the interface workflow is different.
     
  41. Lars-Steenhoff

    Lars-Steenhoff

    Joined:
    Aug 7, 2007
    Posts:
    3,526
    I welcome new mesh batching options, as the current ones require special shaders, I hope this new tool works independent of what shaders is assigned.
     
  42. derkoi

    derkoi

    Joined:
    Jul 3, 2012
    Posts:
    2,260
    Bought!
     
  43. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    Just in time for Christmas! Fast Shadows is on sale in the Asset Store!

    What do you buy for that person who has everything? Looking for something unusual for that special someone? Give the gift of DARKNESS!

    Fast Shadows! 50% off for a limited time only!
     
    Last edited: Dec 23, 2012
  44. I am da bawss

    I am da bawss

    Joined:
    Jun 2, 2011
    Posts:
    2,574
    "Shadows only project onto one surface at a time. This creates a slight discontinuity as shadows pass from one object to another or over corners."

    I took another look at the Live Demo - and it strikes me as this "discontinuity" problem can easily be solved - since you are saying its inexpensive to project fast shadow to surface - why not project Fast Shadow to multiple surfaces?? eg. When an object flies over the staircase - in Fast Shadow it would look very unnatural and jittery as the shadow is "switching" between surface. What I am thinking is to allow Fast Shadow to project on any surface until it leaves. So if you project the Fast Shadow on both the step 1 and 2 of the staircase - instead of deleting shadow on step 1 when it hits step 2, why not keep both (until the shadow completely leaves step 1's surface) since they occupy the same "space"?

    Or is it due to the limitation of the system you cannot do this? Because that "jump" is quite glaring and would destroy any illusion of the shadow if it strikes any discontinous surface - which means you have to design the game around that limitation (making sure no discountinous surface in the object's path) which can be quite limiting.
     
    Last edited: Dec 26, 2012
  45. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    Hello, sorry for the slow reply, we have been celebrating Christmas for the past few days.

    I have thought a lot about your proposed solutions, but I think there would still be quite a few visual artifacts. Each shadow would still protrude beyond the edges of the surface it is being projected upon and the shadows would overlap sometimes creating dark spots. It would also be very difficult to line up parts of the shadow if the user was using a light cookie. I am not optimistic it would be an improvement.

    The problem boils down to attempting to conform to the geometry. At one extreme there is fast shadows with only attempts to match the normal of the geometry, at the other extreme is the blob shadow projector which matches the geometry perfectly. Fast shadows is much (100 times) faster because it makes a huge assumption about the geometry that lets it take a colossal short cut. Unfortunately, stairs are a very tough problem.

    I think an approach like the following would produce the best results:

    • Add a fast shadow and a blob shadow projector to your object
    • Disable the blob shadow projector
    • Put a collider box around the stairs
    • OnColliderEnter enable the blob shadow and disable the fast shadow
    • OnColliderExit enable the fast shadow and disable the blob shadow
    Most of the time you would see the performance benefit of fast shadows, for complicated geometry the shadow would behave properly.
     
  46. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    A new version Fast Shadows is available in the asset store. This version includes numerous performance improvements:

    • Optional culling of shadows not visible to the main camera
    • Better memory management, mesh buffers grow and shrink in blocks
    • Uses the new mesh API available in Unity 4 for dynamic meshes

    If you like Fast Shadows try my new asset Mesh Baker
     
  47. Lars-Steenhoff

    Lars-Steenhoff

    Joined:
    Aug 7, 2007
    Posts:
    3,526
    When I try to compile the new version for iOS I get this warning:

    Assets/FastShadows/Scenes/FS_InspectorFunctions.cs(2,7): error CS0246: The type or namespace name `UnityEditor' could not be found. Are you missing a using directive or an assembly reference?


    Probably have to exclude some part of the code to be editor only, how to do this?

    Thanks
     
  48. Phong

    Phong

    Joined:
    Apr 12, 2010
    Posts:
    2,085
    Hi Lars,

    Hmmm, looks like that file accidentally got dragged out of the Editor folder at some point. Move the file: FastShadows/Scenes/FS_InspectorFunctions.cs to FastShadows/Editor

    That should fix the problem.
     
  49. Lars-Steenhoff

    Lars-Steenhoff

    Joined:
    Aug 7, 2007
    Posts:
    3,526
    Yes confirmed that fixed it, Thanks!!
     
  50. SteveJ

    SteveJ

    Joined:
    Mar 26, 2010
    Posts:
    3,085
    Having a weird problem with this asset all of a sudden. Whenever I try to build my project (for iOS), it stops almost immediately and prints the following errors to the console:


    Any thoughts?