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

Custom sprite shader Broke Unity 5

Discussion in 'Shaders' started by Thom Sip, Mar 4, 2015.

  1. Thom Sip

    Thom Sip

    Joined:
    Apr 27, 2013
    Posts:
    26
    Hey guys,

    In a 2d game I'm working on I use a custom shader I found online to give the enemies and players a white flash effect when they get hit.

    The shader worked fine in Unity 4.6 but now In Unity 5 the shader acts weird. I've attached a screenshot of the problem.

    Code (CSharp):
    1. Shader "Sprites/Diffuse Flash"
    2. {
    3.     Properties
    4.     {
    5.         [PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
    6.         _SelfIllum ("Self Illumination",Range(0.0,1.0)) = 0.0
    7.         _FlashAmount ("Flash Amount",Range(0.0,1.0)) = 0.0
    8.         _Color ("Tint", Color) = (1,1,1,1)
    9.         _FlashColor ("FlashColor", Color) = (1,1,1,1)
    10.         [MaterialToggle] PixelSnap ("Pixel snap", Float) = 0
    11.     }
    12.  
    13.     SubShader
    14.     {
    15.         Tags
    16.         {
    17.             "Queue"="Transparent"
    18.             "IgnoreProjector"="True"
    19.             "RenderType"="Transparent"
    20.             "PreviewType"="Plane"
    21.             "CanUseSpriteAtlas"="True"
    22.         }
    23.  
    24.         Cull Off
    25.         Lighting Off
    26.         ZWrite Off
    27.         Fog { Mode Off }
    28.         Blend SrcAlpha OneMinusSrcAlpha
    29.  
    30.         CGPROGRAM
    31.         #pragma surface surf Lambert alpha vertex:vert
    32.         #pragma multi_compile DUMMY PIXELSNAP_ON
    33.  
    34.         sampler2D _MainTex;
    35.         fixed4 _Color;
    36.         fixed4 _FlashColor;
    37.         float _FlashAmount,_SelfIllum;
    38.        
    39.         struct Input
    40.         {
    41.             float2 uv_MainTex;
    42.             fixed4 color;
    43.         };
    44.        
    45.         void vert (inout appdata_full v, out Input o)
    46.         {
    47.             #if defined(PIXELSNAP_ON) && !defined(SHADER_API_FLASH)
    48.             v.vertex = UnityPixelSnap (v.vertex);
    49.             #endif
    50.             v.normal = float3(0,0,-1);
    51.            
    52.             UNITY_INITIALIZE_OUTPUT(Input, o);
    53.             o.color = _Color;
    54.         }
    55.  
    56.         void surf (Input IN, inout SurfaceOutput o)
    57.         {
    58.             fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * IN.color;
    59.             o.Albedo = lerp(c.rgb,float3(_FlashColor.r,_FlashColor.g,_FlashColor.b),_FlashAmount);
    60.             o.Emission = lerp(c.rgb,float3(_FlashColor.r,_FlashColor.g,_FlashColor.b),_FlashAmount) * _SelfIllum;
    61.             o.Alpha = c.a;
    62.         }
    63.         ENDCG
    64.     }
    65.  
    66. Fallback "Transparent/VertexLit"
    67. }
    68.  
    Here's the script. Is there anyone who could help me out?
     

    Attached Files:

  2. drjamiearron

    drjamiearron

    Joined:
    Jun 8, 2012
    Posts:
    132
    I too am using a modified version of this shader and am having the same issue. If you manage to fix it, could you post it here? Thanks :)
     
  3. jakkovanhunen

    jakkovanhunen

    Joined:
    Oct 20, 2008
    Posts:
    79
    You now need to multiply the o.Albedo by the alpha:
    o.Albedo = {your fancy color stuff} * c.a;
     
  4. drjamiearron

    drjamiearron

    Joined:
    Jun 8, 2012
    Posts:
    132
    Hi jakkovanhunen,

    I updated the line, based on your suggestion, to:

    Code (CSharp):
    1. o.Albedo = lerp(c.rgb,float3(_FlashColor.r,_FlashColor.g,_FlashColor.b),_FlashAmount) * c.a;
    This didn't fix the issue, unfortunately.
     
  5. jackson31

    jackson31

    Joined:
    Aug 25, 2010
    Posts:
    28
    I have to agree, this suggestion doesn't work
     
  6. jakkovanhunen

    jakkovanhunen

    Joined:
    Oct 20, 2008
    Posts:
    79
    Ah, well it solved some shader problems for me that looked similar. Guess they were not similar after all.
     
  7. Jonny-Roy

    Jonny-Roy

    Joined:
    May 29, 2013
    Posts:
    666
    In 5 there is different types of alpha mode in the shader, change this:

    #pragma surface surf Lambert alpha vertex:vert

    To this:


    #pragma surface surf Lambert alpha:blend vertex:vert

    It should fix it!
     
  8. Thom Sip

    Thom Sip

    Joined:
    Apr 27, 2013
    Posts:
    26
    HERO!

    This works perfectly, thanks a lot!

     
    Jonny-Roy likes this.
  9. Jonny-Roy

    Jonny-Roy

    Joined:
    May 29, 2013
    Posts:
    666
    No problem Thom!
     
  10. Getfreeze

    Getfreeze

    Joined:
    Jul 28, 2014
    Posts:
    4
    Thanks alot for the solution! ;D
     
    Jonny-Roy likes this.