Hi guys. I am trying to write a shader that will draw silhouette edges around my mesh. I have written the following code which in my mind should work however I am not getting the desired results. Basically, I want to see if the angle between verticies’ normal and the vector from the vertex to the camera is greater than 90 degrees. If it is, I want to treat that vertex like an edge. The following is my implementation.
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct v2f
{
float4 position : POSITION;
float3 color : COLOR0;
};
v2f vert(appdata_base v)
{
v2f o;
o.position = mul(UNITY_MATRIX_MVP, v.vertex);
float3 vecDistance = _WorldSpaceCameraPos - (mul(v.vertex, _Object2World));
vecDistance = normalize(vecDistance);
float3 vecWorldSpaceNormal = float3(mul(float4(v.normal, 1), _Object2World));
float f = dot(vecDistance, vecWorldSpaceNormal);
if(f < 0)
{
o.color = float3(0,0,0);
}
else
{
o.color = float3(1,1,1);
}
return o;
}
half4 frag(v2f i) : COLOR
{
return half4(i.color, 1);
}
ENDCG
It seems however that only the bottom half of my mesh is colored black. Any obvious ideas regarding what I may be doing wrong? Thanks!