Unity中文字和图片混合

这几天遇到一个有趣的需求:一个旗子模型,要求在显示模型的时候将帮会Logo印在旗面上。

之前帮会Logo只存在于UI上,所以也用的是UI的实现方式,一个Label和一个Image底。模型上可能有动画,如旗子的随风飘扬什么的,所以不好在模型外单独加顶点。计划将Font纹理于原有旗子模型纹理混合。考虑到之前有规则,定义在帮会logo上的文字只能是一个,且只能是中文,感觉还是有得做的。

这里踩过一个坑是CharacterInfo返回的文字的朝向是不一致的,比如这样:

ttf生成的文字纹理

“金”和“帮”这两个字在动态生成时的朝向不一样,这种情况在UI上时可以调整顶点的位置来确定,在这里需要通过shader来处理。

fixed4 ChangeFontColor(fixed4 finalColor, float2 uv)
{
	float2 nUV = uv;
	nUV = (nUV - float2(0.5, 0.5)) * _FontTex_ST.xy + float2(0.5, 0.5);
	nUV = (nUV + _FontTex_ST.zw);
	if (nUV.x < 0 || nUV.y < 0 || nUV.x>1 || nUV.y>1)
	{
	}
	else
	{
	    if (_Flag == 0)
	    {
	        float x = nUV.x * (_FontTex_RG.z - _FontTex_RG.x) + _FontTex_RG.x; 
		float y = nUV.y * (_FontTex_RG.w - _FontTex_RG.y) + _FontTex_RG.y;
		float2 newUV = float2(x, y);
		fixed4 fontColor = tex2D(_FontTex, newUV);
		finalColor = finalColor * (1 - fontColor.a) + _FontColor * fontColor.a;
	    }
	    else
	    {
		float x = nUV.y * (_FontTex_RG.z - _FontTex_RG.x) + _FontTex_RG.x;
		float y = nUV.x * (_FontTex_RG.y - _FontTex_RG.w) + _FontTex_RG.w;
		float2 newUV = float2(x, y);
		fixed4 fontColor = tex2D(_FontTex, newUV);
		finalColor = finalColor * (1 - fontColor.a) + _FontColor * fontColor.a;
	    }
	}
	return finalColor;
}

  

原文地址:https://www.cnblogs.com/fastcam/p/14987558.html