Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Damage full-screen red color with burn blend effect?

Discussion in 'General Graphics' started by Zebbi, Nov 16, 2020.

  1. Zebbi

    Zebbi

    Joined:
    Jan 17, 2017
    Posts:
    521
    I'm looking to add a full-screen red damage effect to my game when the first-person player gets hit, and currently I'm using a simple UI element that adjusts it's opacity depending on the amount of damage, but it's a solid, linear color effect and I'm hoping to make it so the luminance is affected, similar to a photoshop effect. So for instance, instead of:



    Something more like (darken):

    Or (overlay):


    I presume this could be done with a shader, but I'm not sure how. I'm also not sure if this could (or should!) be done as a shader on a UI element, or if it would be a fullscreen blit efffect (I'm using built-in render pipeline, so I can't really use any special pipeline tricks).
     
  2. arkano22

    arkano22

    Joined:
    Sep 20, 2012
    Posts:
    1,991
    Can be easily done with a shader. The default shader created when you ask for a image effect shader (Create->Shader->Image Effect Shader) will invert the entire screen's colors:

    Code (CSharp):
    1. fixed4 frag (v2f i) : SV_Target
    2.             {
    3.                 fixed4 col = tex2D(_MainTex, i.uv);
    4.                 // just invert the colors
    5.                 col.rgb = 1 - col.rgb;
    6.                 return col;
    7.             }

    so instead of inverting colors, multiply them with red:

    Code (CSharp):
    1.  
    2. fixed4 frag (v2f i) : SV_Target
    3.             {
    4.                 fixed4 col = tex2D(_MainTex, i.uv);
    5.                 // tint red:
    6.                 col.rgb *= half3(1,0,0);
    7.                 return col;
    8.             }
    To apply the shader, use the OnRenderImage callback and call Graphics.Blit inside of it.
    https://docs.unity3d.com/550/Documentation/Manual/WritingImageEffects.html
     
    Zebbi likes this.
  3. Zebbi

    Zebbi

    Joined:
    Jan 17, 2017
    Posts:
    521
    Thankyou! If I'm already using graphics blit for another effect (such as underwater distortion) would this add to an existing graphics blit or override it?
     
  4. arkano22

    arkano22

    Joined:
    Sep 20, 2012
    Posts:
    1,991
    It entirely depends on what you want it to look like. I'd assume you need the red tinting to happen after underwater distortion, so probably you want to add a new Graphics.Blit() at the end that takes the distorted image as input and outputs a red-tinted distorted image :).