Outline fragment shader

Started by
0 comments, last by Alundra 9 years, 1 month ago

Im trying to make an outline pixel shader that grows inwards depending on the specified thickness.

Here is what Ive tried:


	[unroll(MaxVal)]
	for (int i = -thickness; i < thickness; i++)
	{
		[unroll(MaxVal)]
		for (int j = -thickness; j < thickness; j++)
		{
			float2 pixelOffsetUV = uv.xy + float2( i / width, j / height );

			float pixelOffset = tex2D(input, pixelOffsetUV).a;

			if (pixelOffset <=0.5)
				return float4(0, 0, 0, color.a);
		}
	}
        return orginal;

It simply checks the neighbour pixels, and if it finds one whose alpha component is below a specified value, it returns black. It works fine, but it takes forever to compile. I need to have a max thickness of about 100-200, and the above implementation is obviously overkill for this.

What are some other more efficient outline algorithms?

By the way this is strictly 2d (WPF effects), no vertex shaders or geometry attributes,, just a pixel shader.

Advertisement

You can achieve this effect in 2 steps :

1) Render outline geometry in a stencil buffer, use a render targer and output an ID on each object pixel (unique ID).

2) Test a box of pixel of the size you want and check if it's an outline, you know it's an outline if the ID is not the same on the whole box.

If it's an outline output a pixel color which is your outline color. Blend the result using alpha blending, or discard on the second step.

EDIT : Apparently this is the kind of algorithm you already use.

This topic is closed to new replies.

Advertisement