Sign in to follow this  

How do I draw edge lines using the z-buffer?

This topic is 2352 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi! I want to draw edge lines around edges over which the z-coordinate makes a sudden jump, how can I accomplish this? I want to implement it as follows

After the image has been rendered, I look at the z-buffer value for each pixel. I loop though all of the eight neighboring pixels and look at their z-buffer values too, and if they differ by more than, say, 1 % or so of the z-buffer value in the middle (this value is multiplied by the square root of two for the diagonal pixels on the diagonal), then I add one to a counter. For the counter value 0 the pixel should be left unchanged, for 1 it should 1/3 darkened, for 2 it should be 2/3 darkened and for 3 or higher it should be completely black.

How do I implement this? I know that I first have to render the entire scene to get the z-buffer I want to use, but then how can I process all the pixels again? And how do I access the z-buffer?

Share this post


Link to post
Share on other sites
[quote name='TriKri' timestamp='1310169248' post='4832963']
Hi! I want to draw edge lines around edges over which the z-coordinate makes a sudden jump, how can I accomplish this? I want to implement it as follows

After the image has been rendered, I look at the z-buffer value for each pixel. I loop though all of the eight neighboring pixels and look at their z-buffer values too, and if they differ by more than, say, 1 % or so of the z-buffer value in the middle (this value is multiplied by the square root of two for the diagonal pixels on the diagonal), then I add one to a counter. For the counter value 0 the pixel should be left unchanged, for 1 it should 1/3 darkened, for 2 it should be 2/3 darkened and for 3 or higher it should be completely black.

How do I implement this? I know that I first have to render the entire scene to get the z-buffer I want to use, but then how can I process all the pixels again? And how do I access the z-buffer?
[/quote]

You have to render the depth buffer to a texture first in order to access it in the shader for the 2nd render pass (which can draw both the image and the darkened lines)

Share this post


Link to post
Share on other sites
did exactly this kind of effect a while back as a tiny crysis [url="http://www.pcgames.de/Crysis-PC-119380/Downloads/Crysis-So-sieht-der-Ego-Shooter-mit-Comic-Style-aus-763804/"]mod[/url].
And there are some part of this effect which make it a bit more complicated as it seem in the beginning.
My code is probably far from perfect, also i probably would have done it differently if I had the freedom which u have when writing your own renderer but here it comes:

directly from the mod with some simplifications
[code]
//tweakables
const float pow = 0.5f;
const float strength = 5.f;
const float delta= 4.f;
const float width = 2.f.;//I choosed 2 pixel wide border

half sceneDepth = tex2D(zMap, IN.baseTC.xy).r; //depth from texture;

half scDepth = pow(sceneDepth, pow);

half2 edgeOffset = width / ScreenSize.xy;

half4 sampels;
sampels.x = tex2D(zMap, IN.baseTC.xy - edgeOffset).r;
sampels.y = tex2D(zMap, IN.baseTC.xy + edgeOffset).r;
sampels.z = tex2D(zMap, IN.baseTC.xy + half2(1. , -1.) * edgeOffset).r;
sampels.w = tex2D(zMap, IN.baseTC.xy + half2(-1. , 1.) * edgeOffset).r;
sampels = pow(sampels, pow);

half dDelta = dot(sampels, 1) - scDepth * delta.; // not so good in hlsl but should be the same as "sampels.x + sampels.y + sampels.z + sampels.w"

dDelta *= 1.0 - PS_NearFarClipDist.x; //sorry don't remember exactly anymore what this var was about

half edgeAmount = dDelta / sceneDepth * strength;
Color.rgb *= saturate(1.0-edgeAmount);[/code]


ps: the crymod link seems to be down cause of some site restructuring or so, u can download it at [url="http://www.moddb.com/mods/cel-shader"]moddb[/url]

Share this post


Link to post
Share on other sites

This topic is 2352 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this