// Outline勾边渲染

uniform half _Outline = 0.35;
uniform half _OutlineStrength;
uniform half4 _OutlineColor;
v2f_basic OutlineVert(appdata v)
{
    v2f_basic o = (v2f_basic)0;
    VertexPositionInputs vertexInput = GetVertexPositionInputs(v.vertex.xyz);
    o.worldPos = vertexInput.positionWS;
    float4 Pos = half4(TransformObjectToWorld(v.vertex),vertexInput.positionCS.w);
    float dist = distance(_WorldSpaceCameraPos, Pos);
    #if defined(_Tangent)||defined(_TANGENT)
        half3 TNormal = v.tangent.xyz;
        half3 posWS = TransformObjectToWorld(v.vertex.xyz + half4(TNormal,0)* v.color.a * _Outline * 0.001*lerp(0.85,1,dist));
        o.pos = TransformWorldToHClip(posWS);
    #else
        // half3 normal = TransformObjectToWorldNormal(v.normal);
        // half2 offset = mul((float3x3)UNITY_MATRIX_VP, normal).xy;
        // o.pos = vertexInput.positionCS;
        // o.pos.xy += (offset * o.pos.z * _Outline * 0.001 * v.color.a) / o.pos.z;
        VertexNormalInputs normalInput = GetVertexNormalInputs(v.normal, v.tangent);
        float3x3 tangentTransform = float3x3(normalInput.tangentWS, normalInput.bitangentWS, normalInput.normalWS);	
        float3 UvNormal = UnpackNormalmapRGorAG(float4(v.uv3, 1.0, 1.0), 1.0);
        float3 worldSpaceUvNormal = normalize(mul(UvNormal, tangentTransform));
        float3 localSpaceNormal = mul((float3x3)unity_WorldToObject, worldSpaceUvNormal);
        float3 viewSpaceNormal = mul((float3x3)UNITY_MATRIX_IT_MV, localSpaceNormal);
        o.pos = vertexInput.positionCS;
        float2 ndcnormal = normalize(mul((float3x3)UNITY_MATRIX_P, viewSpaceNormal).xy);
        o.pos.xy += ndcnormal * 0.001 * _Outline * v.color.a * lerp(0.85,1,dist);
    #endif
    o.worldNormal = normalize(v.normal);
	o.shadowCoord = GetShadowCoord(vertexInput);
	o.color = v.color;
    o.uv = v.uv;
    o.uv2 = v.uv2;
    return o;
}
half4 OutlineFrag(v2f_basic i) : COLOR
{ 
    _RimWidth = 0;
    _BackRimWidth = 0;
    i.fogCoord.w = 0;
	half4 final = BasicFrag(i);

	final.rgb = _OutlineStrength * _OutlineColor.rgb*lerp(1,final.rgb,_OutlineColor.a);
	//final.a *= _BloomFactor;
	return final;
}