- Home /
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 am looking to do that also. I've tried to create a new material and bind it a custom shader, but I can't seem to figure out how to write the .shader file exactly for the Sprite Renderer.
Answer by schmidj42 · Feb 01, 2014 at 12:46 AM
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
So what exactly was the parts necessary for sprite renderer to accept it? The vertex struct and the tags?
Thanks for giving a detailed description of your solution! Not often you see that from people :)
Hi. I'm trying to apply your solution. But I can't. Is it possible for you to give me the resources for me to test it? $$anonymous$$aybe, the the texture and the shader file? That would really help me. Thanks!
OP_toss, This properties and tags are vital i think
Properties
{
[PerRendererData] _$$anonymous$$ainTex ("Sprite Texture", 2D) = "white" {}
[$$anonymous$$aterialToggle] PixelSnap ("Pixel snap", Float) = 0
}
SubShader
{
Tags
{
"Queue"="Transparent"
"IgnoreProjector"="True"
"RenderType"="Transparent"
"PreviewType"="Plane"
"CanUseSpriteAtlas"="True"
}
Cull Off
Lighting Off
ZWrite Off
Fog { $$anonymous$$ode Off }
Blend SrcAlpha One$$anonymous$$inusSrcAlpha
also you can always check builin shader source for details. You can get them here http://unity3d.com/unity/download/archive
Answer by supernat · Jan 31, 2014 at 11:30 PM
See this link: http://docs.unity3d.com/Documentation/Components/SL-ShaderReplacement.html
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.
Answer by DTek · Mar 31, 2015 at 10:28 PM
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 :) .