- Home /
Constant border width in shader, ignore view distance
I have a 2d mesh (all vertices` z coordinate is 0). I use mesh normals to create line border. So every normal is looking inside the mesh. Shader use 2 passes. On first pass full mesh is drawn in black. Then I draw inner mesh on the second pass. It looks fine but when i scale line width is scaling as well but I want it to be constant. What I want is to make line constant width. I tried using fwidth but get no results. Here is shader code
Shader "Custom/Outline" {
Properties
{
_Color("Color", Color) = (1, 1, 1, 1)
_OutlineExtrusion("Outline Extrusion", float) = 0
}
SubShader
{
//inner polygon
Pass
{
Blend SrcAlpha OneMinusSrcAlpha
Stencil
{
Ref 4
Pass replace
}
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
// Properties
uniform float _OutlineExtrusion;
struct vertexInput
{
float4 vertex : POSITION;
float3 normal : NORMAL;
float4 color : COLOR;
};
struct vertexOutput
{
float4 pos : SV_POSITION;
float4 color : COLOR;
};
vertexOutput vert(vertexInput input)
{
vertexOutput output;
float4 newPos = input.vertex;
float3 normal = normalize(input.normal);
newPos += float4(normal, 0.0) * _OutlineExtrusion;
output.pos = UnityObjectToClipPos(newPos);
output.color = input.color;
return output;
}
float4 frag(vertexOutput input) : COLOR
{
return input.color;
}
ENDCG
}
//bigger polygon
Pass
{
Stencil
{
Ref 4
Comp notequal
}
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
float4 _Color;
struct vertexInput
{
float4 vertex : POSITION;
};
struct vertexOutput
{
float4 pos : SV_POSITION;
};
vertexOutput vert(vertexInput input)
{
vertexOutput output;
output.pos = UnityObjectToClipPos(input.vertex);
return output;
}
float4 frag(vertexOutput input) : COLOR
{
return _Color;
}
ENDCG
}
}
}
notbig.png
(31.1 kB)
capture.png
(19.2 kB)
Comment