unity 多重纹理 绘制 shader处理

最近想把多个mesh上的纹理的绘制合并到一个mesh上,在处理shader时遇到了问题


使用纹理渲染模式公式实现正常的纹理叠加效果(SrcAlpha OneMinusSrcAlpha)并不能得到正常的效果

 公式为  

Factor    = SrcTextureA_Alpha

DstTexture  = SrcTextureA*Factor +SrcTextureB*(1 - Factor );

(A混合到B上)

得到的效果边缘会有黑色不能重合

在网上找了很多公式实现都不能很好处理

自己想了想使用了下面的公式 


Factor    = SrcTextureA_Alpha^3;(使用3次方,越高边缘处理的越多 )

DstTexture  = SrcTextureA*Factor +SrcTextureB*(1 - Factor *);

透明越小地方权重B越大 这样A透出效果越明显 反之 

这样解决了边缘黑边的问图


上一shader源码


Shader "Unlit/Transparent MultiTexture Vertex Colored"
{
	Properties
	{
		_MainTex  ("_MainTex", 2D)  = "white" {}
		_MainTex1 ("_MainTex1", 2D) = "white" {}
	}

	SubShader
	{
		LOD 200

		Tags
		{
			"Queue" = "Transparent"
			"IgnoreProjector" = "True"
			"RenderType" = "Transparent"
		}
		
		Pass
		{
	
			Lighting Off
			Blend SrcAlpha OneMinusSrcAlpha
			ZWrite Off
			Fog { Mode Off }
			Offset -1, -1

			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag

			#include "UnityCG.cginc"

			sampler2D _MainTex;
			sampler2D _MainTex1;
			float4 _Color;

			struct appdata_t
			{
				float4 vertex    : POSITION;
				half4  color     : COLOR;
				float2 texcoord0 : TEXCOORD0;
				float2 texcoord1 : TEXCOORD1;
			};

			struct v2f
			{
				float4 vertex    : POSITION;
				half4  color     : COLOR;
				float2 texcoord0 : TEXCOORD0;
				float2 texcoord1 : TEXCOORD1;
			};

			v2f vert (appdata_t v)
			{
				v2f o;
				o.vertex    = mul(UNITY_MATRIX_MVP, v.vertex);
				o.color     = v.color;
				o.texcoord0 = v.texcoord0;
				o.texcoord1 = v.texcoord1;
				return o;
			}

			half4 frag (v2f IN) : SV_Target
			{
				half4 col0   = tex2D(_MainTex, IN.texcoord0);
				half4 col1   = tex2D(_MainTex1,IN.texcoord1);
				
				half4 col    = half4(0,0,0,0);
			
				float factor = pow(col0.a,4);

				col.r        = col0.r*factor + col1.r*(1-factor);
				col.g        = col0.g*factor + col1.g*(1-factor);
				col.b        = col0.b*factor + col1.b*(1-factor);
				col.a        = col0.a*factor + col1.a*(1-factor);

				col          = col*IN.color;
				
				return col;
			}
			ENDCG
		}
	}
	Fallback Off
}






你可能感兴趣的:(unity 多重纹理 绘制 shader处理)