Pixel Perfect Shadow Caster (2D) for Unity using Contour Tracing!
Comparison between the following shape providers:
SpriteRenderer
that usesSprite.vertices
PolygonCollider2D
that usesSprite.GetPhysicsShape
PixelPerfectShadowCaster2D
that uses contour tracing (ours)
- Download either:
PixelPerfectShadowCaster2D.cs
&PixelPerfectSpritePathCalculator.cs
- latest
.unitypackage
from Releases
- Import scripts into project
- Attach this script to a shadow-casting sprite
- Make sure the sprite has
Read/Write Enabled
in texture import settings - Change the
Casting Source
ofShadowCaster2D
toPolygon Collider 2D
- Change the
Trim Edges
ofShadowCaster2D
to 0 - Set the
Alpha Threshold
to the desired value, any pixel with alpha < threshold will be trimmed - Remove the script from the sprite after using it
Note
Using the PolygonCollider2D
as a mediator to set the path of the ShadowCaster2D
instead of directly applying it as Unity recommends leaving the code within UnityEngine.Rendering.Universal
untouched!
px_perf_shadow_cast_2d_how_to.mp4
Contributions to the project are welcome.
MIT
Thankful to RandomiaGaming for Contour Tracing Algorithm.