Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

2D gold shine/sparkle shader

Discussion in 'Shaders' started by Fissll, Apr 25, 2016.

  1. Fissll

    Fissll

    Joined:
    Jan 30, 2015
    Posts:
    70
    Hello everyone,
    I'm fairly new to hlsl and cg but my first shader turned out pretty good.
    Now however, I want to make a shader which imitates a shine or sparkle effect for the reward text the player gets when finishing a level in my game.
    I want it to look something like this

    Does anyone have an idea how I can do this?
     
  2. Fissll

    Fissll

    Joined:
    Jan 30, 2015
    Posts:
    70
    After a whole day of messing around, I finally managed to create the shine effect.
    If anyone is interested in it, here is the code (it is based on the standard UI shader)
    Code (CSharp):
    1. Shader "Custom/BlingShader"
    2. {
    3.     Properties
    4.     {
    5.         [PerRendererData] _MainTex("Sprite Texture", 2D) = "white" {}
    6.     _Color("Tint", Color) = (1,1,1,1)
    7.  
    8.         _StencilComp("Stencil Comparison", Float) = 8
    9.         _Stencil("Stencil ID", Float) = 0
    10.         _StencilOp("Stencil Operation", Float) = 0
    11.         _StencilWriteMask("Stencil Write Mask", Float) = 255
    12.         _StencilReadMask("Stencil Read Mask", Float) = 255
    13.  
    14.         _ColorMask("Color Mask", Float) = 15
    15.  
    16.         [Toggle(UNITY_UI_ALPHACLIP)] _UseUIAlphaClip("Use Alpha Clip", Float) = 0
    17.  
    18.         _Speed("Bling Speed", Float) = 2
    19.         _Thickness("Bling Thickness", Range(0, 1)) = 0.8
    20.     }
    21.  
    22.         SubShader
    23.     {
    24.         Tags
    25.     {
    26.         "Queue" = "Transparent"
    27.         "IgnoreProjector" = "True"
    28.         "RenderType" = "Transparent"
    29.         "PreviewType" = "Plane"
    30.         "CanUseSpriteAtlas" = "True"
    31.     }
    32.  
    33.         Stencil
    34.     {
    35.         Ref[_Stencil]
    36.         Comp[_StencilComp]
    37.         Pass[_StencilOp]
    38.         ReadMask[_StencilReadMask]
    39.         WriteMask[_StencilWriteMask]
    40.     }
    41.  
    42.         Cull Off
    43.         Lighting Off
    44.         ZWrite Off
    45.         ZTest[unity_GUIZTestMode]
    46.         Blend SrcAlpha OneMinusSrcAlpha
    47.         ColorMask[_ColorMask]
    48.  
    49.         Pass
    50.     {
    51.         CGPROGRAM
    52. #pragma vertex vert
    53. #pragma fragment frag
    54.  
    55. #include "UnityCG.cginc"
    56. #include "UnityUI.cginc"
    57.  
    58. #pragma multi_compile __ UNITY_UI_ALPHACLIP
    59.  
    60.     struct appdata_t
    61.     {
    62.         float4 vertex   : POSITION;
    63.         float4 color    : COLOR;
    64.         float2 texcoord : TEXCOORD0;
    65.     };
    66.  
    67.     struct v2f
    68.     {
    69.         float4 vertex   : SV_POSITION;
    70.         fixed4 color : COLOR;
    71.         half2 texcoord  : TEXCOORD0;
    72.         float4 worldPosition : TEXCOORD1;
    73.     };
    74.  
    75.     fixed4 _Color;
    76.     fixed4 _TextureSampleAdd;
    77.     float4 _ClipRect;
    78.  
    79.     v2f vert(appdata_t IN)
    80.     {
    81.         v2f OUT;
    82.         OUT.worldPosition = IN.vertex;
    83.         OUT.vertex = mul(UNITY_MATRIX_MVP, OUT.worldPosition);
    84.  
    85.         OUT.texcoord = IN.texcoord;
    86.  
    87. #ifdef UNITY_HALF_TEXEL_OFFSET
    88.         OUT.vertex.xy += (_ScreenParams.zw - 1.0)*float2(-1,1);
    89. #endif
    90.  
    91.         OUT.color = IN.color * _Color;
    92.         return OUT;
    93.     }
    94.  
    95.     sampler2D _MainTex;
    96.     float _Speed;
    97.     float _Thickness;
    98.  
    99.     fixed4 frag(v2f IN) : SV_Target
    100.     {
    101.         half4 color = (tex2D(_MainTex, IN.texcoord) + _TextureSampleAdd) * IN.color;
    102.  
    103.         float x = (IN.worldPosition.x + _ScreenParams[0] / 2) / _ScreenParams[0];
    104.         float y = (IN.worldPosition.y + _ScreenParams[1] / 2) / _ScreenParams[1];
    105.         float f = 1 + x - (_SinTime[3] * _Speed);
    106.         if (_CosTime[3] > 0)
    107.         {
    108.             if (y >= f && y <= f + _Thickness)
    109.                 color /= 1 - (f - y) - _Thickness;
    110.             if (y >= f - _Thickness && y <= f)
    111.                 color /= 1 + (f - y) - _Thickness;
    112.         }
    113.         color.a *= UnityGet2DClipping(IN.worldPosition.xy, _ClipRect);
    114.  
    115. #ifdef UNITY_UI_ALPHACLIP
    116.         clip(color.a - 0.001);
    117. #endif
    118.  
    119.         return color;
    120.     }
    121.         ENDCG
    122.     }
    123.     }
    124. }
    125.  
     
    pcg likes this.
  3. pcg

    pcg

    Joined:
    Nov 7, 2010
    Posts:
    292
    Just what I was looking for, thanks for sharing.

    I have a question. I recall reading that if's in a shader give a performance hit.
    Apparently its ok to use them sometimes and not others (I'm very new to shader coding).
    I was wondering if the "if's" in the shader code above are a problem with regards to performance?