TheNewbie
TheNewbie

Reputation: 353

Unity 2D: Area Color Inverse Effect

I am trying to create a GameObejct that if active reverses the colors of anything behind it. Ideally, this GameObject expands from its centre point to a certain size radius when it appears/becomes active.

So imagine a circle whose Scale increases from x=0 and y=0 to x=5 and y=5 gradually over 1 or 2 seconds and reverses the colors of anything behind. Doesn't exactly matter how long or how big the actual circle is, just an example of the idea. I tried just creating a GameObect with a mask and a shader I found online, but t didn't work at all and I still wasn't even sure how to animate the expansion.

Does anyone have any idea on how I could make this dream effect of mine a reality?

Upvotes: 2

Views: 3591

Answers (2)

Reasurria
Reasurria

Reputation: 1848

Here is a shader that does nothing except invert the color.

The important part is Alpha:Blend and Blend OneMinusDstColor Zero.

Shader "Custom/Invert" 
{
    Properties 
    {
        _Color ("Color", Color) = (1,1,1,1)
        _MainTex ("Albedo (RGB)", 2D) = "white" {}
    }
    SubShader 
    {
        Tags { "RenderType"="Transparent" "Queue"="Transparent" }
        LOD 200
        Blend OneMinusDstColor Zero

        PASS
        {
            CGPROGRAM

            #pragma vertex vert
            #pragma fragment frag
            #pragma target 3.0 Alpha:Blend

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
            };

            fixed4 _Color;

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = float2(0,0);
                return o;
            }
        
            fixed4 frag (v2f i) : SV_Target
            {
                return _Color;
            }

            ENDCG
        }
    }
    FallBack "Diffuse"
}

You can put this on any sprite or 3D object to get this effect:

As for the animation, King's answer is nice and simple. Alternatively you could animate the scale by code:

public class Scale : MonoBehaviour 
{
    void Update ()
    {
        var scale = Mathf.Sin(Time.timeSinceLevelLoad / 4f) * 20;
        this.transform.localScale = new Vector3(scale, scale, 1);
    }
}

This particular code will make the sprite 'pulse'. Like this:

Pulse

I think you should be able to make your own scaling script from this example.

Upvotes: 7

King
King

Reputation: 56

Select your GameObject->Window->Animation->Animation->Create->Save->Add Property->Transform->Scale. At your 0:00 keyframe set your scale for x,y,z to 1. Click over to your 1:00 keyframe and set your scale for x,y,z to 2. Press play and your animation should loop from scale of 1 to 2.

Upvotes: 1

Related Questions