How aniketrajnish/pixelperfectshadowcaster2d-unity works
Project's Purpose and Type
• The project is a Unity plugin aimed at providing “pixel perfect” shadow casting for 2D sprites.
• The purpose is to use an algorithm (credited from RandomiaGaming) to modify the path used by Unity’s built-in ShadowCaster2D.
File Structure
• The file tree contains only two main C# files:
• PixelPerfectShadowCaster2D.cs
• PixelPerfectSpritePathCalculator.cs
• These files, along with the README and license, indicate a minimalist architecture where the logic is entirely driven by these two scripts.
• No separate directories for backend or frontend are present because this is a script-based plugin meant to be imported into Unity projects.
Main Components
• Unity Engine Core: Represent the Unity framework as a base component.
• Sprite with Readable Texture: Indicate that the sprite must have “Read/Write Enabled” for the algorithm to work.
• Unity Components: Include the default components attached to the GameObject:
• SpriteRenderer (providing Sprite.vertices)
• ShadowCaster2D (which normally uses data from PolygonCollider2D)
• Custom Scripts:
• PixelPerfectShadowCaster2D: The main script that uses the custom contour tracing algorithm.
• PixelPerfectSpritePathCalculator: A helper script or utility that calculates the sprite path using pixel data.
Out Relationships and Interactions
• The custom PixelPerfectShadowCaster2D script is integrated with the GameObject’s existing components.
• The PixelPerfectSpritePathCalculator is used internally by the shadow caster script to compute the desired outline based on the sprite’s pixel data.
• The Sprite (input image texture) feeding into the PixelPerfectSpritePathCalculator.
• The output from the calculator linking into the PixelPerfectShadowCaster2D.
• The PixelPerfectShadowCaster2D then updates or “mediates” the PolygonCollider2D / ShadowCaster2D setup.
• The final lighting and shadow system handled by Unity’s rendering engine.
Architectural Patterns or Design Principles
• Single Responsibility: Each script has a clear, distinct role. PixelPerfectSpritePathCalculator performs the path calculation while PixelPerfectShadowCaster2D manages how that path is applied.
• Modularity: The plugin is designed to be used optionally by attaching and then later removing the script from the GameObject. This indicates a non-intrusive, plug-and-play approach.
• Extension of Existing Systems: Instead of replacing Unity’s internal rendering pipelines, the plugin merely acts as an intermediary to adjust component properties at runtime.