Apply shader to a sprite.

Is it possible to apply shader to a unity4.3 sprite somehow?
Want to make some basic color correction.

I was finally able to use my own shader with a SpriteRenderer. Here what I did,

I created a new shader (i.e. called TextureBlend), then I edited the shader to have:

Shader "2D/Texture Blend"
{  
    Properties
    {
       _MainTex ("Sprite Texture", 2D) = "white" {}
       _Color ("Alpha Color Key", Color) = (0,0,0,1)
    }
    SubShader
    {
        Tags 
        { 
            "RenderType" = "Opaque" 
            "Queue" = "Transparent+1" 
        }

        Pass
        {
            ZWrite Off
            Blend SrcAlpha OneMinusSrcAlpha 
 
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #pragma multi_compile DUMMY PIXELSNAP_ON
 
            sampler2D _MainTex;
            float4 _Color;

            struct Vertex
            {
                float4 vertex : POSITION;
                float2 uv_MainTex : TEXCOORD0;
                float2 uv2 : TEXCOORD1;
            };
    
            struct Fragment
            {
                float4 vertex : POSITION;
                float2 uv_MainTex : TEXCOORD0;
                float2 uv2 : TEXCOORD1;
            };
 
            Fragment vert(Vertex v)
            {
                Fragment o;
    
                o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
                o.uv_MainTex = v.uv_MainTex;
                o.uv2 = v.uv2;
    
                return o;
            }
                                                    
            float4 frag(Fragment IN) : COLOR
            {
                float4 o = float4(1, 0, 0, 0.2);

                half4 c = tex2D (_MainTex, IN.uv_MainTex);
                o.rgb = c.rgb;
                if(c.r == _Color.r && c.g == _Color.g && c.b == _Color.b)
                {
                    o.a = 0;
                }
                else
                {
                    o.a = 1;
                }
                    
                return o;
            }

            ENDCG
        }
    }
}

Then I created a new Material (i.e. called OverlayMaterial) and attached the above shader to it.

Finally on the SpriteRenderer component I selected the OverlayMaterial for the Material attribute.

The important part that did make it work for me was to set the tags such as

Tags 
{ 
    "RenderType" = "Opaque"
    "Queue" = "Transparent+1" 
}

And by defining the vert program input as

    struct Vertex
    {
        float4 vertex : POSITION;
        float2 uv_MainTex : TEXCOORD0;
        float2 uv2 : TEXCOORD1;
    };

After that I was able to apply the effect I wanted. Here a shot of the result for the sprite renderer component [21486-sprite+renderer+custom+material.jpg|21486]

See this link:

I haven’t tried this myself, but look down at the shaders that can be replaced. Maybe you can use Camera.SetReplacementShader(). This functionality may not be built in for 2D sprites yet though.

This is my solution - a lot simpler and more straight forward :

  • create a material

  • add your sprite to the material ( you can change this later from the sprite itself )

  • add the shader to the material

  • in the sprite properties, drag in the shader.

  • List item

That should do it :slight_smile: .