我一直认为使用透明精灵会对性能产生影响,但在Unity中使用透支场景模式,如果我使用不透明精灵或alpha <1的精灵,我会看到有相同的重绘。
我错了吗?
I always thought that using transparent sprites will have performance impact, but using overdraw scene mode in Unity, I see that there is the same overdraw if I use an opaque sprite, or a sprite with alpha < 1.
Am I wrong?
最满意答案
Unity.2D和Unity.UI的默认着色器将所有内容(包括不透明的精灵)绘制为透明几何体,除非您使用具有不透明着色器的材质。
更新1:
这是我为减少透支而编写的不透明着色器,但不要指望它适用于所有情况。 不透明几何体将首先渲染,透明几何体将位于顶部。
Shader "Custom/Opaque UI" { Properties { _Color("Color", Color) = (1,1,1,1) _MainTex ("Texture", 2D) = "white" {} } SubShader { Tags { "RenderType" = "Opaque" } Pass { CGPROGRAM #pragma vertex vert #pragma fragment frag #include "UnityCG.cginc" sampler2D _MainTex; half3 _Color; struct v2f { float4 pos : SV_POSITION; float2 uv : TEXCOORD0; float4 color : COLOR; }; float4 _MainTex_ST; v2f vert (appdata_full v) { v2f o; o.pos = mul (UNITY_MATRIX_MVP, v.vertex); o.uv = TRANSFORM_TEX (v.texcoord, _MainTex); o.color = v.color; return o; } half4 frag (v2f i) : COLOR { half4 color_MainTex = tex2D (_MainTex, i.uv); return color_MainTex * i.color; } ENDCG } } FallBack "UI/Default" }The default shaders for Unity.2D and Unity.UI draws everything (including opaque sprites) as transparent geometry, unless you use a material with an opaque shader.
Update 1:
This is an opaque shader that I wrote for minimizing overdraw, but don't expect that it works in every situation. The opaque geometry will be renderer first and the transparent geometry on top.
Shader "Custom/Opaque UI" { Properties { _Color("Color", Color) = (1,1,1,1) _MainTex ("Texture", 2D) = "white" {} } SubShader { Tags { "RenderType" = "Opaque" } Pass { CGPROGRAM #pragma vertex vert #pragma fragment frag #include "UnityCG.cginc" sampler2D _MainTex; half3 _Color; struct v2f { float4 pos : SV_POSITION; float2 uv : TEXCOORD0; float4 color : COLOR; }; float4 _MainTex_ST; v2f vert (appdata_full v) { v2f o; o.pos = mul (UNITY_MATRIX_MVP, v.vertex); o.uv = TRANSFORM_TEX (v.texcoord, _MainTex); o.color = v.color; return o; } half4 frag (v2f i) : COLOR { half4 color_MainTex = tex2D (_MainTex, i.uv); return color_MainTex * i.color; } ENDCG } } FallBack "UI/Default" }更多推荐
发布评论