ABOUT PORTFOLIO GAMES DEVLOG DOCS TOOLS CONTACT
HOME > DOCS > Shader / URP > 피격 시 스프라이트를 순간적으로 발광시키는 2D 플래시(Flash) 셰이더2D Sprite Hit-Flash Shader Using RGB Color Interpolation
Shader / URP Beginner Unity 2021.3+ / Unity 6

피격 시 스프라이트를 순간적으로 발광시키는 2D 플래시(Flash) 셰이더2D Sprite Hit-Flash Shader Using RGB Color Interpolation

스프라이트 원본 알파 채널을 보존하면서 RGB를 단색(화이트/레드)으로 보간해 타격 피드백을 강조하는 2D 스프라이트 셰이더와 MaterialPropertyBlock 연동.A 2D sprite shader that preserves the source alpha channel while interpolating RGB toward a solid flash color to emphasize hit feedback.

#Shader #2D #Sprite #Combat #Game Feel
핵심 개념2D 액션 게임에서 적이 피격되었을 때 0.05~0.1초 동안 하얗게 점멸하는 효과는 직관적인 타격 피드백을 제공합니다. 원본 텍스처의 알파 채널은 그대로 유지한 채 RGB 채널만 _FlashColor와 선형 보간(lerp)하고, _FlashAmount를 1에서 0으로 되돌리는 방식으로 동작합니다.
Core ConceptBriefly flashing an enemy sprite solid white for 0.05 to 0.1 seconds upon impact provides clear combat feedback in 2D action games. The shader preserves the original texture alpha while linearly interpolating (lerp) the RGB channels toward _FlashColor using a _FlashAmount parameter.

1. 2D 플래시 셰이더 코드1. 2D Flash Shader Code

SpriteFlash.shader
Shader "Custom/2D/SpriteFlash"
{
    Properties
    {
        [PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
        _Color ("Tint", Color) = (1,1,1,1)
        _FlashColor ("Flash Color", Color) = (1,1,1,1)
        _FlashAmount ("Flash Amount", Range(0, 1)) = 0.0
    }
    SubShader
    {
        Tags { "Queue"="Transparent" "RenderType"="Transparent" }
        Blend SrcAlpha OneMinusSrcAlpha
        Cull Off
        ZWrite Off

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"

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

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

            sampler2D _MainTex;
            fixed4 _FlashColor;
            float _FlashAmount;

            v2f vert(appdata_t IN)
            {
                v2f OUT;
                OUT.vertex = UnityObjectToClipPos(IN.vertex);
                OUT.texcoord = IN.texcoord;
                OUT.color = IN.color;
                return OUT;
            }

            fixed4 frag(v2f IN) : SV_Target
            {
                fixed4 c = tex2D(_MainTex, IN.texcoord) * IN.color;
                c.rgb = lerp(c.rgb, _FlashColor.rgb, _FlashAmount);
                c.rgb *= c.a; // 프리멀티플라이드 알파 대응
                return c;
            }
            ENDCG
        }
    }
}
SpriteFlash.shader
Shader "Custom/2D/SpriteFlash"
{
    Properties
    {
        [PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
        _Color ("Tint", Color) = (1,1,1,1)
        _FlashColor ("Flash Color", Color) = (1,1,1,1)
        _FlashAmount ("Flash Amount", Range(0, 1)) = 0.0
    }
    SubShader
    {
        Tags { "Queue"="Transparent" "RenderType"="Transparent" }
        Blend SrcAlpha OneMinusSrcAlpha
        Cull Off
        ZWrite Off

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"

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

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

            sampler2D _MainTex;
            fixed4 _FlashColor;
            float _FlashAmount;

            v2f vert(appdata_t IN)
            {
                v2f OUT;
                OUT.vertex = UnityObjectToClipPos(IN.vertex);
                OUT.texcoord = IN.texcoord;
                OUT.color = IN.color;
                return OUT;
            }

            fixed4 frag(v2f IN) : SV_Target
            {
                fixed4 c = tex2D(_MainTex, IN.texcoord) * IN.color;
                c.rgb = lerp(c.rgb, _FlashColor.rgb, _FlashAmount);
                c.rgb *= c.a; // Premultiplied alpha support
                return c;
            }
            ENDCG
        }
    }
}

2. 실무 적용 및 주의사항2. Practical Considerations and Caveats

  • MaterialPropertyBlock 또는 SRP Batcher 활용: spriteRenderer.material.SetFloat를 호출하면 머티리얼 사본이 생성되어 스프라이트 배칭이 깨질 수 있으므로, Built-in 렌더러에서는 MaterialPropertyBlock을 사용하고 URP에서는 CBUFFER_START(UnityPerMaterial)에 프로퍼티를 선언해 SRP Batcher 호환성을 유지하는 것이 좋습니다.
  • 프리멀티플라이드 알파(Premultiplied Alpha) 처리: 스프라이트 셰이더에서 c.rgb를 먼저 보간한 뒤 마지막에 c.rgb *= c.a;를 곱해주지 않으면 투명한 여백 사각형까지 하얗게 칠해지는 현상이 발생합니다.
  • Using MaterialPropertyBlock or SRP Batcher: Calling spriteRenderer.material.SetFloat instantiates a duplicate material and breaks sprite batching; use MaterialPropertyBlock in the Built-in pipeline or declare properties inside CBUFFER_START(UnityPerMaterial) in URP to maintain SRP Batcher compatibility.
  • Premultiplied Alpha Multiplication: Always multiply c.rgb *= c.a; after interpolating the flash color so that transparent quad borders do not turn opaque white during the flash.

알파 채널 보존 후 RGB 보간을 수행하는 플래시 셰이더는 추가 텍스처 제작 없이 모든 2D 스프라이트에 일관된 피격 피드백을 부여합니다.

Interpolating RGB while preserving the source alpha channel delivers responsive hit-flash feedback across 2D sprites without requiring duplicate hit-frame textures.

참고자료 & 관련 링크