UnityRef is currently in early development. Some features may be incomplete and/or not functioning.

UNITYREF

Your Pit Stop For Solving ANYTHING in Unity

graphics

[VFX Graph] Randomly Select Sprite Sheet Frames in Shader Graph

Solution

shadersgraphicstexturesvfx graphvisuals

Unity 2021.x - Unity 6.3.x

Published 28 days ago

Issue

 Developers often encounter limitations when synchronizing per-particle data from VFX Graph to a custom Shader Graph for texture atlas indexing. Without manual UV manipulation, particles cannot independently select unique frames from your sprite sheet.

Explanation

To achieve random texture selection, UV coordinates must be mapped to a specific grid index.

  1. Create a new Shader Graph and add a Flipbook node.
  2. Define the Width and Height parameters to match the grid of your sprite sheet.
  3. Create a new Float property named FrameIndex and ensure it is exposed in the Blackboard.
  4. Connect the FrameIndex property to the Tile input of the Flipbook node.
  5. Within the VFX Graph, locate the exposed FrameIndex on the Output Particle block.
  6. Assign a Random Number node or use the texIndex attribute to drive the FrameIndex value.

Additional Tips

  • Set the texture Wrap Mode to Clamp to avoid sampling pixels from adjacent tiles.
  • Use a Floor node before the Tile input if the incoming value is a continuous float to ensure discrete frame selection.
  • For high-performance requirements, consider using Texture2DArray to simplify the sampling logic and avoid UV math.

TL;DR

Utilize the Flipbook node and drive the Tile input with an exposed FrameIndex property linked to per-particle attributes.


Related Posts Haven't quite found a solution to your problem? We think these posts might help you.

Content inspired by a Unity discussion post.