Skip to content

Instantly share code, notes, and snippets.

@unitycoder
Last active February 14, 2024 01:36

Revisions

  1. unitycoder revised this gist Apr 8, 2022. 1 changed file with 163 additions and 0 deletions.
    163 changes: 163 additions & 0 deletions Sprites_ScrollFX-ScreenSpace.shader
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,163 @@
    // Unity buit-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt)

    // modified to add scrolling lines fx, use black and white mask texture for scrollingtex. - unitycoder.com

    Shader "Sprites/ScrollFX (ScreenSpace)"
    {
    Properties
    {
    [PerRendererData] _MainTex("Sprite Texture", 2D) = "white" {}
    _ScrollTex("Scroll Texture", 2D) = "white" {}
    _Color("Tint", Color) = (1,1,1,1)
    _ScrollColor1("ScrollColor1", Color) = (1,1,1,1)
    _ScrollColor2("ScrollColor2", Color) = (0,0,0,0)
    [MaterialToggle] PixelSnap("Pixel snap", Float) = 0
    //[PerRendererData]
    _FXScale("FXScale", Float) = 1
    _ScrollSpeed("Scroll Speed", Float) = 1
    [HideInInspector] _RendererColor("RendererColor", Color) = (1,1,1,1)
    [HideInInspector] _Flip("Flip", Vector) = (1,1,1,1)
    [PerRendererData] _AlphaTex("External Alpha", 2D) = "white" {}
    [PerRendererData] _EnableExternalAlpha("Enable External Alpha", Float) = 0
    }

    SubShader
    {
    Tags
    {
    "Queue" = "Transparent"
    "IgnoreProjector" = "True"
    "RenderType" = "Transparent"
    "PreviewType" = "Plane"
    "CanUseSpriteAtlas" = "True"
    }

    Cull Off
    Lighting Off
    ZWrite Off
    Blend One OneMinusSrcAlpha

    Pass
    {
    CGPROGRAM
    #pragma vertex SpriteVert
    #pragma fragment SpriteFrag
    #pragma target 2.0
    #pragma multi_compile_instancing
    #pragma multi_compile_local _ PIXELSNAP_ON
    #pragma multi_compile _ ETC1_EXTERNAL_ALPHA
    #include "UnityCG.cginc"
    //#include "UnitySprites.cginc"

    // Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt)

    //#ifndef UNITY_SPRITES_INCLUDED
    //#define UNITY_SPRITES_INCLUDED
    //#include "UnityCG.cginc"
    #ifdef UNITY_INSTANCING_ENABLED

    UNITY_INSTANCING_BUFFER_START(PerDrawSprite)

    // SpriteRenderer.Color while Non-Batched/Instanced.
    UNITY_DEFINE_INSTANCED_PROP(fixed4, unity_SpriteRendererColorArray)
    // this could be smaller but that's how bit each entry is regardless of type
    UNITY_DEFINE_INSTANCED_PROP(fixed2, unity_SpriteFlipArray)
    UNITY_INSTANCING_BUFFER_END(PerDrawSprite)

    #define _RendererColor UNITY_ACCESS_INSTANCED_PROP(PerDrawSprite, unity_SpriteRendererColorArray)
    #define _Flip UNITY_ACCESS_INSTANCED_PROP(PerDrawSprite, unity_SpriteFlipArray)

    #endif // instancing

    CBUFFER_START(UnityPerDrawSprite)
    #ifndef UNITY_INSTANCING_ENABLED
    fixed4 _RendererColor;
    fixed2 _Flip;
    #endif
    float _EnableExternalAlpha;
    CBUFFER_END

    // Material Color.
    fixed4 _Color;

    struct appdata_t
    {
    float4 vertex : POSITION;
    float4 color : COLOR;
    float2 texcoord : TEXCOORD0;
    float4 screenPos : TEXCOORD1;
    UNITY_VERTEX_INPUT_INSTANCE_ID
    };

    struct v2f
    {
    float4 vertex : SV_POSITION;
    fixed4 color : COLOR;
    float2 texcoord : TEXCOORD0;
    float4 screenPos : TEXCOORD1;
    UNITY_VERTEX_OUTPUT_STEREO
    };

    inline float4 UnityFlipSprite(in float3 pos, in fixed2 flip)
    {
    return float4(pos.xy * flip, pos.z, 1.0);
    }

    v2f SpriteVert(appdata_t IN)
    {
    v2f OUT;

    UNITY_SETUP_INSTANCE_ID(IN);
    UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(OUT);

    OUT.vertex = UnityFlipSprite(IN.vertex, _Flip);
    OUT.vertex = UnityObjectToClipPos(OUT.vertex);
    OUT.texcoord = IN.texcoord;
    OUT.screenPos = ComputeScreenPos(OUT.vertex);
    OUT.color = IN.color * _Color * _RendererColor;

    #ifdef PIXELSNAP_ON
    OUT.vertex = UnityPixelSnap(OUT.vertex);
    #endif

    return OUT;
    }

    sampler2D _MainTex;
    sampler2D _ScrollTex;
    sampler2D _AlphaTex;
    float _ScrollSpeed;
    fixed4 _ScrollColor1;
    fixed4 _ScrollColor2;
    float _FXScale;

    fixed4 SampleSpriteTexture(float2 uv)
    {
    fixed4 color = tex2D(_MainTex, uv);

    #if ETC1_EXTERNAL_ALPHA
    fixed4 alpha = tex2D(_AlphaTex, uv);
    color.a = lerp(color.a, alpha.r, _EnableExternalAlpha);
    #endif

    return color;
    }

    fixed4 SpriteFrag(v2f IN) : SV_Target
    {
    fixed4 c = SampleSpriteTexture(IN.texcoord) * IN.color;
    //fixed4 fx = tex2D(_ScrollTex, IN.texcoord2+float2(_Time.x* _ScrollSpeed,0));
    fixed4 fx = tex2D(_ScrollTex, _FXScale*IN.screenPos +float2(_Time.x* _ScrollSpeed,0));
    c.rgb *= c.a;

    fx = lerp(_ScrollColor1, _ScrollColor2,fx.r);
    c.rgb = lerp(fx.rgb,c.rgb,1-fx.a)*c.a;

    return c;
    }

    // #endif // UNITY_SPRITES_INCLUDED
    ENDCG
    }
    }
    }
  2. unitycoder created this gist Apr 8, 2022.
    157 changes: 157 additions & 0 deletions Sprites_ScrollFX.shader
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,157 @@
    // Unity buit-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt)

    // modified to add scrolling lines fx, use black and white mask texture for scrollingtex. - unitycoder.com
    // forum topic: https://forum.unity.com/threads/can-this-2d-effect-be-created-using-shaders.1264700/

    Shader "Sprites/ScrollFX"
    {
    Properties
    {
    [PerRendererData] _MainTex("Sprite Texture", 2D) = "white" {}
    _ScrollTex("Scroll Texture", 2D) = "white" {}
    _Color("Tint", Color) = (1,1,1,1)
    _ScrollColor1("ScrollColor1", Color) = (1,1,1,1)
    _ScrollColor2("ScrollColor2", Color) = (0,0,0,0)
    [MaterialToggle] PixelSnap("Pixel snap", Float) = 0
    _ScrollSpeed("Scroll Speed", Float) = 1
    [HideInInspector] _RendererColor("RendererColor", Color) = (1,1,1,1)
    [HideInInspector] _Flip("Flip", Vector) = (1,1,1,1)
    [PerRendererData] _AlphaTex("External Alpha", 2D) = "white" {}
    [PerRendererData] _EnableExternalAlpha("Enable External Alpha", Float) = 0
    }

    SubShader
    {
    Tags
    {
    "Queue" = "Transparent"
    "IgnoreProjector" = "True"
    "RenderType" = "Transparent"
    "PreviewType" = "Plane"
    "CanUseSpriteAtlas" = "True"
    }

    Cull Off
    Lighting Off
    ZWrite Off
    Blend One OneMinusSrcAlpha

    Pass
    {
    CGPROGRAM
    #pragma vertex SpriteVert
    #pragma fragment SpriteFrag
    #pragma target 2.0
    #pragma multi_compile_instancing
    #pragma multi_compile_local _ PIXELSNAP_ON
    #pragma multi_compile _ ETC1_EXTERNAL_ALPHA
    #include "UnityCG.cginc"
    //#include "UnitySprites.cginc"

    // Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt)

    //#ifndef UNITY_SPRITES_INCLUDED
    //#define UNITY_SPRITES_INCLUDED
    //#include "UnityCG.cginc"
    #ifdef UNITY_INSTANCING_ENABLED

    UNITY_INSTANCING_BUFFER_START(PerDrawSprite)

    // SpriteRenderer.Color while Non-Batched/Instanced.
    UNITY_DEFINE_INSTANCED_PROP(fixed4, unity_SpriteRendererColorArray)
    // this could be smaller but that's how bit each entry is regardless of type
    UNITY_DEFINE_INSTANCED_PROP(fixed2, unity_SpriteFlipArray)
    UNITY_INSTANCING_BUFFER_END(PerDrawSprite)

    #define _RendererColor UNITY_ACCESS_INSTANCED_PROP(PerDrawSprite, unity_SpriteRendererColorArray)
    #define _Flip UNITY_ACCESS_INSTANCED_PROP(PerDrawSprite, unity_SpriteFlipArray)

    #endif // instancing

    CBUFFER_START(UnityPerDrawSprite)
    #ifndef UNITY_INSTANCING_ENABLED
    fixed4 _RendererColor;
    fixed2 _Flip;
    #endif
    float _EnableExternalAlpha;
    CBUFFER_END

    // Material Color.
    fixed4 _Color;

    struct appdata_t
    {
    float4 vertex : POSITION;
    float4 color : COLOR;
    float2 texcoord : TEXCOORD0;
    UNITY_VERTEX_INPUT_INSTANCE_ID
    };

    struct v2f
    {
    float4 vertex : SV_POSITION;
    fixed4 color : COLOR;
    float2 texcoord : TEXCOORD0;
    UNITY_VERTEX_OUTPUT_STEREO
    };

    inline float4 UnityFlipSprite(in float3 pos, in fixed2 flip)
    {
    return float4(pos.xy * flip, pos.z, 1.0);
    }

    v2f SpriteVert(appdata_t IN)
    {
    v2f OUT;

    UNITY_SETUP_INSTANCE_ID(IN);
    UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(OUT);

    OUT.vertex = UnityFlipSprite(IN.vertex, _Flip);
    OUT.vertex = UnityObjectToClipPos(OUT.vertex);
    OUT.texcoord = IN.texcoord;
    OUT.color = IN.color * _Color * _RendererColor;

    #ifdef PIXELSNAP_ON
    OUT.vertex = UnityPixelSnap(OUT.vertex);
    #endif

    return OUT;
    }

    sampler2D _MainTex;
    sampler2D _ScrollTex;
    sampler2D _AlphaTex;
    float _ScrollSpeed;
    fixed4 _ScrollColor1;
    fixed4 _ScrollColor2;

    fixed4 SampleSpriteTexture(float2 uv)
    {
    fixed4 color = tex2D(_MainTex, uv);

    #if ETC1_EXTERNAL_ALPHA
    fixed4 alpha = tex2D(_AlphaTex, uv);
    color.a = lerp(color.a, alpha.r, _EnableExternalAlpha);
    #endif

    return color;
    }

    fixed4 SpriteFrag(v2f IN) : SV_Target
    {
    fixed4 c = SampleSpriteTexture(IN.texcoord) * IN.color;
    fixed4 fx = tex2D(_ScrollTex, IN.texcoord+float2(_Time.x* _ScrollSpeed,0));
    c.rgb *= c.a;

    fx = lerp(_ScrollColor1, _ScrollColor2,fx.r);
    c.rgb = lerp(fx.rgb,c.rgb,1-fx.a)*c.a;

    return c;
    }

    // #endif // UNITY_SPRITES_INCLUDED
    ENDCG
    }
    }
    }