Sign in to follow this  
Mr Furious

Neighbor Pixels in HLSL

Recommended Posts

I've been working on doing post processing effects lately and wanted to start doing filters. However, filters needs to sample their neighboring pixels. I know i could hack it by playing with arbitrary float2's until it looks ok but i'd rather be legit about it. For reference, i was looking at some of humus' code. Here's an example filter: (Edge detection using Sobel's method)
const float off;

float4 main(float2 texCoord: TEXCOORD) : COLOR {
	// Sobel filter
	float4 s00 = tex2D(Image, texCoord + float2(-off, -off));
	float4 s01 = tex2D(Image, texCoord + float2( 0,   -off));
	float4 s02 = tex2D(Image, texCoord + float2( off, -off));

	float4 s10 = tex2D(Image, texCoord + float2(-off,  0));
	float4 s12 = tex2D(Image, texCoord + float2( off,  0));

	float4 s20 = tex2D(Image, texCoord + float2(-off,  off));
	float4 s21 = tex2D(Image, texCoord + float2( 0,    off));
	float4 s22 = tex2D(Image, texCoord + float2( off,  off));

	float4 sobelX = s00 + 2 * s10 + s20 - s02 - 2 * s12 - s22;
	float4 sobelY = s00 + 2 * s01 + s02 - s20 - 2 * s21 - s22;

	float4 edgeSqr = sobelX * sobelX + sobelY * sobelY;
	return 1 - dot(edgeSqr, 80);
So i guess my question is how to properly compute the "off". He does it like this: renderer->changeShaderConstant3f("off", 0.13f / length(position)); But I don't understanding how he gets this.

Share this post

Link to post
Share on other sites
Original post by Namethatnobodyelsetook
They should be 1.0/texturewidth and 1.0/textureheight to give you neighbour pixels.

Thanks for the feedback, that seems a really intuitive way to go about it but it's deconstructing humus's work that's so elusive.

Share this post

Link to post
Share on other sites

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