Shadow Volumes Toolkit

   

Available now in the Unity Asset Store

Unity forum thread | Webplayer demo | Readme

Videos

Features

  • Dynamic, pixel-perfect shadows based on the Shadow Volume algorithm
  • Stencil buffer support in Unity 4.2
  • Supports all three Unity light types
  • Supports skinned meshes
  • Works even when the camera is in shadow
  • Requires no scripting

The toolkit is not...

  • a complete renderer
  • a replacement for Unity Pro lighting/shadows
  • using the patented depth-fail technique

Graphics Hardware Requirements

  • 8bit alpha channel (Unity Free) or 8bit stencil buffer (Unity Pro)
  • Shader Model 2.0

(Supported on all modern computer graphics cards)

Platform support & Unity license requirements

See the Readme

Limitations

  • Antialiasing is not garaunteed to work in conjunction with the toolkit
  • Only closed meshes can cast shadows (see the first question in the FAQ)
  • Only a single light can cast shadows at any given moment
  • Custom vertex shaders won't affect shadows

FAQ

Are skinned meshes supported?

Yes, but there might be small artifacts along the edge of the shadow if the character's bones rotate a lot. (The normals used to create the shadow volume will not be entirely correct when they're transformed by Unity) Some models are more prone to the artifacts than others and the best way to determine how they look is testing them.

See the Quick workflow video at 3:15 for an example of using a skinned mesh with the toolkit.

How do the shadows interact with lightmaps?

The shadows affect the scene by linearly interpolating the scene color towards the shadow color by the amount specified in the shadow alpha value. If the shadow is black this is equivalent to multiplying the scene by the shadow alpha value. In this case, the light maps will be darkened wherever there is a shadow.

Is it possible for runtime generated meshes to cast shadows?

This is not supported out of the box. One of the drawbacks to using GPU-based shadow volumes is that it requires a special shadow mesh for each game object (mesh) that should cast a shadow. Shadow mesh assets are created and assigned automatically using the Quick Shadow Setup dialog but it can't be used during runtime, mainly because it's not a good idea to create assets then. The shadow mesh can be created manually using the ShadowMeshCreator script but it is probably too slow to be used during runtime, at least for more advanced source meshes. If you're interested in trying it out, watch the manual workflow example video at the top of this page to see how the game object hierarchy should be set up and then replicate the behavior in the script that generates the source geometry.

My game uses the alpha channel/stencil buffer for other tasks, will this corrupt the shadows?

The toolkit uses the alpha channel/stencil buffer for a limited time only (at render queue AlphaTest+~500), so anything using the buffer before or after this will work fine.

However, if something is written to the alpha channel/stencil buffer before this time it'll be gone after the shadow pass. For example, if the scene geometry writes to the alpha channel to mark where a selective glow effect should blur the screen in a post process pass, the toolkit will overwrite the values (if the alpha channel backend is used) and the selective glow will not function properly. (Note: Normal alpha blending does not require the alpha channel so there will be no conflicts with transparent shaders)

Stencil Buffer backend: Why is the screen black when I use the toolkit on mobile devices?

Make sure that you have a valid Unity iOS Pro license and that you have enabled a 24bit depth buffer in the build settings.

Alpha Channel backend: Why is the screen black when I use the toolkit on mobile devices?

Make sure you have enabled a 32bit display buffer in the build settings.

Alpha Channel backend: Why is the screen still black when I use the toolkit on Android devices?

See the answer to the question below.

Alpha Channel backend: Why are the shadow volumes pink when I use the toolkit on Android devices?

Even though the OpenGL ES 2.0 specification requires all devices to support subtractive blending, the drivers on many Android devices often do not. In these cases, the screen may turn black or the shadow volumes may show up as pink. In order to avoid this, it's recommended to set the "Compatibility mode" of the ShadowVolume, SkinnedShadowVolume and ShadowVolumeRenderer scripts to "No Blend Op".

Known problematic devices: Galaxy S (Fails on 2.3 Gingerbread; works fine on CyanogenMod 10)