Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Flash a 2d sprite to white?

Discussion in '2D' started by Rick74, May 21, 2014.

  1. Rick74

    Rick74

    Joined:
    Jun 7, 2013
    Posts:
    40
    Gonna try my luck on these boards.

    I've made a 2d game (but on an older unity 4.2.1f)

    Does anyone know of a shader that can change a 2d sprite texture to white? The default has 255 values just giving you the regular color. Surely their has to be some way to "bloom" out the texture to white through the shader?

    I've seen plenty of 2d games do this very trick, and theonly way I can think of doing them is to have a "white" version of every texture I want to possibly flash, which seems like over kill.

    help!
     
  2. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    1. blow out vertex colour by multiplying rgb in vertex shader by whichever amount you want to flash. You could simply multiply it by twice in vert shader then just halve the input vertex colour to compensate. Then to flash the sprite, you go from 0.5 rgb to 1.0 rgb in sprite renderer (or whatever middleware).

    or

    2. overlay the same sprite - just draw it again, same anim frame but new shader which only returns white + alpha in frag. Use input vertex alpha to fade the flash.
     
  3. Rick74

    Rick74

    Joined:
    Jun 7, 2013
    Posts:
    40
    Sorry I need a trick that works with mobile shaders.

    I noticed that the default particles alpha blend has an ability to slightly overbright the texture, (color value stars at 130,130,130) but it's been ripped out of the mobile version. I've been trying to tinker myself with making a mobile shader myself, but i've never written one before and havn't a clue what it is I'm doing.
     
  4. Kellyrayj

    Kellyrayj

    Joined:
    Aug 29, 2011
    Posts:
    933
    The second one would work without having to mess much with shaders.

    You could also switch textures when you want it to flash. Duplicate the sprite texture, fill it with white and when you want it to flash, swap the sprite texture with a completely white version and then back again.
     
  5. Rick74

    Rick74

    Joined:
    Jun 7, 2013
    Posts:
    40
    Thanks for your help on this! I've asked in a bunch of places and your the first and only response!
     
  6. TomasJ

    TomasJ

    Joined:
    Sep 26, 2010
    Posts:
    256
    You can duplicate Sprites/Default shader and make it output white instead of the actual RGB values from the texture. Alpha should still come from the texture in order to keep the original shape of the sprite. Then swap the material of the SpriteRenderer when flashing.
    Or even make one material capable of switching logic.

    Will work on mobile.
     
  7. Rick74

    Rick74

    Joined:
    Jun 7, 2013
    Posts:
    40
    Thanks Tomas! Any chance we could get a default shader that does the same trick? I'd wager most mobile 2d games would want to express damage or hits taken with a white flash.