将Sprite alpha颜色分量设置为值

编程入门 行业动态 更新时间:2024-10-28 02:26:23
将Sprite alpha颜色分量设置为值<1会对性能产生影响吗?(Does setting Sprite alpha color component to a value < 1 have a performance impact?)

我一直认为使用透明精灵会对性能产生影响,但在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" }

更多推荐

本文发布于:2023-08-06 06:52:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1445301.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:设置为   分量   颜色   Sprite   alpha

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!