Search Unity

Resolved Sprite (in atlas) flickers on sprite change

Discussion in 'Shaders' started by rioneye, Jul 23, 2021.

  1. rioneye

    rioneye

    Joined:
    Aug 10, 2015
    Posts:
    12
    Hi,

    I've been experimenting with the Amplify Shader package and have found it pretty intuitive overall. I've been trying to create a shader that will gradually fade out a sprite. The shader I made works for single sprites but starts to have problems when the sprite is packed in an atlas. Animating through a series of frames clearly shows the flickers.
    Here is the shader as I currently have it: ScreenshotASE.png

    Its divided into three sections:
    • The top is to add a transparency
    • The middle is the standard texture
    • The bottom is the gradient
    The bottom section is what is causing the problem. I calculate the position of the sprite in the atlas as well as its relative size and pass that into the shader via MaterialPropertyBlock. In the bottom left side of the diagram, I take those values (vScale and vShift) then modify the V value from the texture coordinates (this is a vertical gradient) such that it varies from [0,1]. This seems to work up until I switch the sprite. The order of events seems to be as follows:
    1. I update the renderer's sprite
    2. The sprite texture gets updated but there is a single frame where the modified V values are not applied thus resulting in an incorrect gradient on the texture.
    3. Then the modified V values are applied and the sprite looks like it should
    Here is what the result looks like:
    overworld_reflection_shader_flicker.gif
    Note: There are actually a few more flickering frames that the gif doesn't show.

    My assumption is that there is an update order issue somewhere but I cannot nail it down. If anyone has any suggestions, I'd greatly appreciate it.
    Also, feel free to critique the shader and post feedback. As I mentioned, I am new to shaders and am looking to improve.
     
  2. rioneye

    rioneye

    Joined:
    Aug 10, 2015
    Posts:
    12
    I managed to resolve this using the Vertex Position instead of the Texture UV coordinates. Brakeys tutorial on this helped me think about a different approach.