Unity--使用shader给角色描边异常

unity版本:Unity 2018.2.0f2 (64-bit)

shader实现如下:

// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'

#pragma fragmentoption ARB_precision_hint_fastest

#include "UnityCG.cginc"

half _Outline;
fixed4 _OutlineColor;

struct appdata {
	half4 vertex : POSITION;
	half3 normal : NORMAL;
};

struct v2f_outline {
	half4 pos : SV_POSITION;
};

v2f_outline vert_outline(appdata v) {
	v2f_outline o;		
	o.pos = UnityObjectToClipPos(v.vertex);
	half3 norm = mul ((float3x3)UNITY_MATRIX_IT_MV, v.normal);
	half2 offset = TransformViewToProjection(norm.xy);
	
	o.pos.xy += offset * o.pos.z * _Outline;
	
	return o;
}

fixed4 frag_outline(v2f_outline i) : COLOR
{
	return _OutlineColor;
}

ios真机上描边显示正常,android真机上描边显示得很粗,(项目保密,截图不便公开,找了个图替代)

Unity--使用shader给角色描边异常Unity--使用shader给角色描边异常

左边是预期的情况,右边是android真机上显示出来的效果

 

造成这个问题的原因是代码中用到o.pos.z,这个值在不同平台上的值的范围不同,即

o.pos.xy += offset * o.pos.z * _Outline;

 

官方的解释(https://docs.unity3d.com/Manual/SL-PlatformDifferences.html  

Unity--使用shader给角色描边异常

github上找到解决方案(https://github.com/Shrimpey/Outlined-Diffuse-Shader-Fixed

最终的实现如下

// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'

#pragma fragmentoption ARB_precision_hint_fastest

#include "UnityCG.cginc"

half _Outline;
fixed4 _OutlineColor;

struct appdata {
	half4 vertex : POSITION;
	half3 normal : NORMAL;
};

struct v2f_outline {
	half4 pos : SV_POSITION;
};

v2f_outline vert_outline(appdata v) {
	v2f_outline o;		
	v.vertex *= (1 + _Outline);
	o.pos = UnityObjectToClipPos(v.vertex);
	return o;
}

fixed4 frag_outline(v2f_outline i) : COLOR
{
	return _OutlineColor;
}