Unity verwendet eine Karte, um die Transparenz der Hauptkarte des Materials zu steuern

Vor kurzem gab es einen Bedarf: die Transparenz der Haupttextur des Materials durch den Farbwert einer Textur zu steuern.

Machen wir uns zunächst eine Anmerkung: Die Textur, die zur Steuerung der Transparenz der Haupttextur verwendet wird, wird vorübergehend als „Alpha-Textur“ bezeichnet; die Haupttextur wird als „Haupttextur“ bezeichnet.

Zunächst müssen wir den Farbwert der Alpha-Karte in Transparenz umwandeln. Nach vielen Tests haben wir schließlich die folgende Methode übernommen: 1) Vergleichen Sie die r, g, b der Pixel. 2) Nehmen Sie an, dass r ist der größte, dann float val = r *Weight+g+b; 3) alpha = val/(weight+2)

Gehen Sie direkt zum Code:

Shader "Custom/MainTexAlphaCtrl"
{
	Properties
	{
		_MainTex("Texture", 2D) = "white" {}//主贴图纹理
		_AlphaTex("AlphaTexture",2D) = "white"{}//Alpha纹理
		_BaseColor("Base Color",Color) = (1, 1, 1, 1)
		_Weight("Weight",float) = 0//Alpha计算时的权重
		[Toggle(_Reverse)] _Reverse("Reverse", float) = 0//是否取反,用于主贴图的透明度计算
	}
		SubShader
		{
		Tags { "RenderType" = "Transparent"}
			LOD 100
			Cull Off
			//如果使用Alpha混合,需要关闭深度缓冲写入
			ZWrite Off
			//添加混合命令
			Blend SrcAlpha OneMinusSrcAlpha

			Pass
			{
				CGPROGRAM
				#pragma vertex vert
				#pragma fragment frag
				// make fog work
				#pragma multi_compile_fog

				#include "UnityCG.cginc"

				struct appdata
				{
					float4 vertex : POSITION;
					float2 uv : TEXCOORD0;
				};

				struct v2f
				{
					float2 uv : TEXCOORD0;
					float4 vertex : SV_POSITION;
				};

				sampler2D _MainTex;
				sampler2D _AlphaTex;
				float4 _MainTex_ST;
				float4 _BaseColor;
				float _Weight;
				float _Reverse;

				
				v2f vert(appdata v)
				{
					v2f o;
					o.vertex = UnityObjectToClipPos(v.vertex);
					o.uv = TRANSFORM_TEX(v.uv, _MainTex);
					return o;
				}

				//将颜色值转化成透明度
				float color2Alpha(fixed3 col)
				{
					float a = 0;
					float val = 0;
					//比较r、g、b三个值,得到最大值,如果r最大,则val = 4*r+g+b;
					if (col.r > col.g)
					{
						if (col.r > col.b)
						{
							val = _Weight * col.r + col.g + col.b;
						}
						else
						{
							val = _Weight * col.b + col.g + col.r;
						}
					}
					else
					{
						if (col.g > col.b)
						{
							val = _Weight * col.g + col.r + col.b;
						}
						else
						{
							val = _Weight * col.b + col.g + col.r;
						}
					}
					//然后val除以(权重+2),得到最终alpha值
					return val / (_Weight+2);
					//return (col.r + col.g + col.b)/3;
				}

				fixed4 frag(v2f i) : SV_Target
				{
					// sample the texture
					fixed4 main_col = tex2D(_MainTex, i.uv) * _BaseColor;
					fixed3 alpha_col = tex2D(_AlphaTex, i.uv).rgb;
					if (_Reverse == 0)
					{
						return fixed4(main_col.rgb, main_col.a * color2Alpha(alpha_col));
					}
					else
					{
						return fixed4(main_col.rgb, main_col.a * (1 - color2Alpha(alpha_col)));
					}
				}
				ENDCG
			}
		}
}

Je suppose que tu aimes

Origine blog.csdn.net/humilezr/article/details/125736925
conseillé
Classement