Sprite shaders
Sprite shaders are simple legacy shaders that do not use a complex lighting model.
The default shader is simply applies a tinted color output based on an input value; sprite sheet animation and other features are done at a higher level.
2f vert(appdata_t IN)
{
v2f OUT;
OUT.vertex = mul(UNITY_MATRIX_MVP, IN.vertex);
OUT.texcoord = IN.texcoord;
OUT.color = IN.color * _Color;
#ifdef PIXELSNAP_ON
OUT.vertex = UnityPixelSnap (OUT.vertex);
#endif
return OUT;
}
fixed4 SampleSpriteTexture (float2 uv)
{
fixed4 color = tex2D (_MainTex, uv);
#if ETC1_EXTERNAL_ALPHA
color.a = tex2D (_AlphaTex, uv).r;
#endif
return color;
}
fixed4 frag(v2f IN) : SV_Target
{
fixed4 c = SampleSpriteTexture (IN.texcoord) * IN.color;
c.rgb *= c.a;
return c;
}
Customization
It is therefore simple to modify the default sprite renderer to customize the sprite behaviour.
For example, a sprite renderer that animates across it's texture sheet can be created in a similar manner to the Animated
material shader example:
fixed4 SampleSpriteTexture (float2 uv, float2 uv2)
{
// Sample the offset texture
fixed4 offset = tex2D (_Offset, uv);
// Rotate the uv offset in a circle over the target texture
float v = _Amount * offset[0];
float x = uv[0] + v * lerp(-1.0, 1.0, sin(_Time[1] * _Speed));
float y = uv[1] + v * lerp(-1.0, 1.0, cos(_Time[1] * _Speed));
x = IF(x < 0, 1.0 - x, IF(x > 1, x - 1.0, x));
y = IF(y < 0, 1.0 - y, IF(y > 1, y - 1.0, y));
// Normal sprite shader continues...
fixed4 color = tex2D (_MainTex, float2(x, y));
return color;
}
Notice that the [PerRendererData]
is attached to the sprite texture; this is assigned by the sprite internals.
Similarly, the geometry for the sprite is procedurally driven, and cannot be manually assigned; this complicates the use of vertex offset shaders and an offset mask texture should be used instead.