Aug
20
2016

Sprite Shader with Clip() *Discard pixels

Testing what happens if add clip(discard), remove transparency and remove color tint from default Sprite Shader..
Just wanted to see if there is big difference or not,
but seems like no huge difference on PC (asus laptop, gtx970m), unless you have thousands of sprites on screen (*will test on mobile later)

This thread says in mobile devices it might be worse performance to use Clip()..

Stats for test scene (20000 sprites overlapping, sprite texture: 128×128) *Using Unity 5.4.0p2

default-sprite-shader

sprite-shader-clip

sprite-shader-clip-no_transparency

Sprite used (sprite mesh type: full rect)

sprite 1

Shader sources

Sprites/Clip


// removed ColorTint, Added clip() based on alpha
Shader "Sprites/Clip"
{
Properties
{
[PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
[MaterialToggle] PixelSnap ("Pixel snap", Float) = 0
_Cutoff ("AlphaCutoff", Range(0,1)) = 1
}
SubShader
{
Tags
{
"Queue"="Transparent"
"IgnoreProjector"="True"
"RenderType"="Transparent"
"PreviewType"="Plane"
"CanUseSpriteAtlas"="True"
}
Cull Off
Lighting Off
ZWrite Off
Blend One OneMinusSrcAlpha
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma target 2.0
#pragma multi_compile _ PIXELSNAP_ON
#include "UnityCG.cginc"
struct appdata_t
{
float4 vertex : POSITION;
float2 texcoord : TEXCOORD0;
};
struct v2f
{
float4 vertex : SV_POSITION;
float2 texcoord : TEXCOORD0;
};
v2f vert(appdata_t IN)
{
v2f OUT;
OUT.vertex = UnityObjectToClipPos(IN.vertex);
OUT.texcoord = IN.texcoord;
#ifdef PIXELSNAP_ON
OUT.vertex = UnityPixelSnap (OUT.vertex);
#endif
return OUT;
}
sampler2D _MainTex;
fixed _Cutoff;
fixed4 frag(v2f IN) : SV_Target
{
fixed4 c = tex2D (_MainTex,IN.texcoord);
clip(c.a-_Cutoff);
c.rgb *= c.a;
return c;
}
ENDCG
}
}
}

Sprites/Clip-NoTransparency


// removed ColorTint, Transparency, Added clip() based on alpha
Shader "Sprites/Clip-NoTransparency"
{
Properties
{
[PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
[MaterialToggle] PixelSnap ("Pixel snap", Float) = 0
_Cutoff ("AlphaCutoff", Range(0,1)) = 1
}
SubShader
{
Tags
{
"Queue"="Geometry"
"IgnoreProjector"="True"
"RenderType"="Opaque"
"PreviewType"="Plane"
"CanUseSpriteAtlas"="True"
}
Cull Off
Lighting Off
ZWrite Off
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma target 2.0
#pragma multi_compile _ PIXELSNAP_ON
#include "UnityCG.cginc"
struct appdata_t
{
float4 vertex : POSITION;
float2 texcoord : TEXCOORD0;
};
struct v2f
{
float4 vertex : SV_POSITION;
float2 texcoord : TEXCOORD0;
};
v2f vert(appdata_t IN)
{
v2f OUT;
OUT.vertex = UnityObjectToClipPos(IN.vertex);
OUT.texcoord = IN.texcoord;
#ifdef PIXELSNAP_ON
OUT.vertex = UnityPixelSnap (OUT.vertex);
#endif
return OUT;
}
sampler2D _MainTex;
fixed _Cutoff;
fixed4 frag(v2f IN) : SV_Target
{
fixed4 c = tex2D (_MainTex, IN.texcoord);
clip(c.a-_Cutoff);
//if (c.a<_Cutoff) discard;
return c;
}
ENDCG
}
}
}


1 Comment + Add Comment

Leave a comment

Connect

Twitter View LinkedIn profile Youtube Youtube Join Discord Twitch

UnityLauncherPro

Get UnityLauncherPRO and work faster with Unity Projects!
*free unity hub alternative

@unitycoder_com

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.