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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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
- Testing Unity AI Beta
- Ways to Support UnityCoder Development
- Using UI Slider to Create 5-Star Rating Element
- Game Music Library For Unity (editor plugin)
- Fontastic : Easily Test Fonts in Unity Editor!
- GeoTiff Importer & Terrain Generator for Unity
- Create Baked DropShadow for UI images
- .JP2 Ortho Image Converter to PNG/JPG/TIFF
- Convert LAS/LAZ/PLY pointclouds to GLTF (GLB) Point Meshes (standalone converter)
- Detect SRP (URP or HDRP) with Assembly Definition Version Defines
- [LudumDare57] Theme: Depths
- MotionVector Effect: Object “disappears” when paused
Recent Comments
- on [Asset Store] PolygonCollider2D Optimizer
- on Trajectory Test Scene 2.0
- on Vector3 maths for dummies!
- on UnityHub 3.6.0: Remove Version Control & Cloud Dashboard columns
- on Using RenderDoc with Unity (graphics debugger)
- on UI Scroll View automatic Content height
- on [Asset Store] Point Cloud Viewer & Tools
- on [Asset Store] Point Cloud Viewer & Tools
Coin:
CUgDSbRqFcAumDSAcdKDvuXsw26VdkJe8C8WGUQHBAGS
An article by












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