I am implementing an edge outline cg shader.
The basic idea is simple. First, I get the depth buffer using a depth shader.
The output color is the depth.
void vshader(float4 vtx_position : POSITION,
uniform float4x4 mat_modelproj,
uniform float4x4 mat_modelview,
out float4 l_position : POSITION,
out float4 l_color0: COLOR0) {
l_position = mul(mat_modelproj, vtx_position);
float depth = mul(mat_modelview, l_position).w*.15;
l_color0 = float4(depth, depth, depth, 1);
}void fshader(float4 l_color0: COLOR0,
out float4 o_color : COLOR) {
o_color = l_color0;
}
Then, I use another shader to do post-processing.
The edges are extracted and bolded.
void vshader(float4 vtx_position : POSITION,
float2 vtx_texcoord0 : TEXCOORD0,
out float4 l_position : POSITION,
out float2 l_texcoord0 : TEXCOORD0,
uniform float4x4 mat_modelproj)
{
l_position = mul(mat_modelproj, vtx_position);
l_texcoord0 = vtx_texcoord0;
}void fshader(float2 l_texcoord0 : TEXCOORD0,
uniform sampler2D tex_0 : TEXUNIT0,
uniform float2 windowsize,
out float4 o_color : COLOR,
uniform float4x4 mat_modelproj)
{
float sepx = 1/windowsize.x;
float sepy = 1/windowsize.y;
float4 color0 = tex2D(tex_0, l_texcoord0);
float2 texcoord1 = l_texcoord0+float2(sepx, 0);
float4 color1 = tex2D(tex_0, texcoord1);
float2 texcoord2 = l_texcoord0+float2(0, sepy);
float4 color2 = tex2D(tex_0, texcoord2);
float2 texcoord3 = l_texcoord0+float2(-sepx, 0);
float4 color3 = tex2D(tex_0, texcoord3);
float2 texcoord4 = l_texcoord0+float2(0, -sepy);
float4 color4 = tex2D(tex_0, texcoord4);
o_color = (color0-color1).z > .007||(color0-color2).z > .007||(color0-color3).z > .007||(color0-color4).z > .007?
float4(0, 0, 0, 1) : float4(0, 0, 0, 0);
}
Parameters in the snippets are hard coded for quick test.
My problem is there is always a discontinuity at an arrow tip, as is shown in the following figure.
I also tested adding dark pixels in 8 directions and different texel step sizes, but the results look the same. I would appreciate your help to denote my mistake.