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 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 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
- MotionVector Effect: Object “disappears” when paused
- [GreaseMonkey] Unity Forum Fixer
- UnityHub: Make Hub application background Translucent
- Customize SpriteShapeRenderer quality (but has issues)
- Editor tool: Copy selected gameobject’s names into clipboard as rows (for Excel)
- Editor tool: Replace string in selected gameobject’s names
- UnityHub: Enable built-in Login Dialog (no more browser login/logout issues!)
- Use TikTok-TTS in Unity (with WebRequest)
- Create Scene Thumbnail Image using OnSceneSaved & OnPreviewGUI
- webgl+javascript TTS
- Using Moonsharp (LUA) + Unity Webgl
- Using 3D gameobject prefabs with Unity Tilemap + NavMesh Surface
Recent Comments
- [Asset Store] Point Cloud Viewer & Tools on
- [Asset Store] Point Cloud Viewer & Tools on
- Vector3 maths for dummies! on
- UnityHub: Make Hub application background Translucent on
- UnityHub: Make Hub application background Translucent on
- Install Android SDK+JDK+NDK for Unity (without AndroidStudio or Unity Hub) on
- Install Android SDK+JDK+NDK for Unity (without AndroidStudio or Unity Hub) on
- [Asset Store] Point Cloud Viewer & Tools on
poor UI performance, modify shader:
https://forum.unity3d.com/threads/extremely-poor-ui-performance-on-android.472747/#post-3088412