Automatic generator for ShadowCaster2D components based on sprite alpha channels. Suitable for creating complex 2D shadows in the Universal Render Pipeline.
- Generates
ShadowCaster2Dcomponents based on sprite outlines. - Determines shadow shape considering transparency (alpha channel).
- Saves created objects as prefabs to a specified folder.
- Cleans up temporary objects from the scene.
- Visualizes contours in the editor via Gizmos.
- Enhanced Efficiency & Advanced Automation:
- Dramatically boosts productivity by transforming 1-5 hours of manual effort into a swift 30-second to 2-minute automated process.
- Utilizes sophisticated algorithms for alpha channel-based pixel traversal and contour mask creation, followed by precise contour tracing and coordinate conversion. This solution was developed with AI guidance, demonstrating an innovative approach to complex automation challenges.
- Add the
ShadowCaster2DMakercomponent to aGameObjectin your scene. - Populate the sprites list, select the folder, and specify the
targetFolderName. - Call
GenerateShadows()from the Inspector or via an editor script. - If needed, call
ClearMadeGameObjects()to remove temporary objects.
To generate shadows without saving them to a folder, activate the DoNotSaveToFolder flag in the Advanced menu.
β οΈ Notes:
- If you enable DoNotSaveToFolder during generation and then disable it, the generated objects won't be saved to the folder, and
ClearMadeGameObjectswill no longer have a reference to them.ClearMadeGameObjectswill remove all objects created in the current session from the scene, provided it still holds references to them (see above).
A component for generating shadows from sprites in the editor.
Sprite[] spritesβ An array of sprites for generation.DefaultAsset folderβ The root folder for saving the results.string targetFolderNameβ The name of the subfolder for the output.- Advanced:
bool doNotSaveToFolderβ A flag to prevent saving as prefabs.
GenerateShadows()β The main method for generating shadows.ClearMadeGameObjects()β Cleans up created objects from the scene (if they were saved as prefabs or cleared manually).
A helper static class for processing sprites.
- GetPixels β Retrieves pixels from the sprite's texture.
- GetAlphaMask β Creates a 2D mask of opaque pixels.
- ExtractContourMask β Extracts the contour based on the mask.
- ExtractContourPoints β Forms a sequence of points along the contour.
- SimplifyPoints β Simplifies the geometry (removes redundant points).
- ConvertToLocalPoints β Converts points to local coordinates.
A Vector3[] array of points for the m_ShapePath field of the ShadowCaster2D component.
A Gizmos component for displaying the shadow contour in the editor.
ShadowCaster2D shadowCasterβ A reference to the caster.
- Draws Gizmos lines between the points of the
m_ShapePathprivate field of theShadowCaster2Dcomponent.
A helper static class for saving shadows as prefabs.
const string PREFAB_EXTENSION- The string".prefab"used to construct the path before saving.
- Determines the save path.
- Saves a
GameObjectas a.prefabfile.
- The scripts use reflection to access private fields and methods of
ShadowCaster2DandShadowUtility. - Tested with
Unity 2022.3.35f1andURP 14.0.11, may not work or might cause errors with other Unity versions. - Works only in the Unity Editor, as it utilizes
UnityEditor,Undo,EditorUtility, etc. - It is assumed that Universal Render Pipeline (URP) is already integrated into the project.
SerhiiStudio
Unity Tools & Games
GitHub: SerhiiStudio