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
![]()
![]()
![]()
Sprite used (sprite mesh type: full rect)
![]()
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 | |
| } | |
| } | |
| } |
Related Posts
1 Comment + Add Comment
Leave a comment
Recent posts
- Adding Real World Location to Ice Fishing Prototype
- [web tool] Image Channel Inspector : AlphaScope
- Editor Tool: Smart Recompile (don’t recompile if edit comments)
- Editor Tool: Scene Size Analyzer
- LudumDare 60: Old Tech
- uBasic Scripting Language for Unity
- GreaseMonkey+Editor Plugin: 1-Click Copy script from ChatGPT (Web) to Unity Editor (as script)
- Editor Tool: Property Watcher
- .unityignore : Hide files from Project window
- PostBuild Script to Create Single exe Standalone File
- Game Loop Designer
- Favorites in PackageManager
Recent Comments
Coin:
CUgDSbRqFcAumDSAcdKDvuXsw26VdkJe8C8WGUQHBAGS
An article by












poor UI performance, modify shader:
https://forum.unity3d.com/threads/extremely-poor-ui-performance-on-android.472747/#post-3088412