# aniketrajnish/pixelperfectshadowcaster2d-unity architecture

> Architecture diagram of the GitHub repository aniketrajnish/pixelperfectshadowcaster2d-unity, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-30). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/aniketrajnish/pixelperfectshadowcaster2d-unity
- Repository: https://github.com/aniketrajnish/pixelperfectshadowcaster2d-unity

## Overview

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

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Unity Components
    subgraph "Unity Components"
        A("Sprite (Read/Write Texture)"):::input
        B("SpriteRenderer"):::unity
        C("PolygonCollider2D"):::unity
        D("ShadowCaster2D"):::unity
    end

    %% Custom Scripts
    subgraph "Custom Scripts"
        E("PixelPerfectSpritePathCalculator"):::custom
        F("PixelPerfectShadowCaster2D"):::custom
    end

    %% Unity Rendering Pipeline
    G("Unity Rendering Pipeline"):::unity

    %% Relationships
    A -->|"providesData"| B
    A -->|"sendsTexture"| E
    E -->|"computedOutline"| F
    F -->|"updatesPolygon"| C
    F -->|"updatesShadow"| D
    D -->|"rendersThrough"| G

    %% Click Events
    click F "https://github.com/aniketrajnish/pixelperfectshadowcaster2d-unity/blob/main/PixelPerfectShadowCaster2D.cs"
    click E "https://github.com/aniketrajnish/pixelperfectshadowcaster2d-unity/blob/main/PixelPerfectSpritePathCalculator.cs"

    %% Style Definitions
    classDef unity fill:#ffcc00,stroke:#000,stroke-width:2px;
    classDef custom fill:#99ccff,stroke:#000,stroke-width:2px;
    classDef input fill:#e6ffe6,stroke:#008000,stroke-width:2px;
```
