# Neighbor Pixels in HLSL

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

## 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 on other sites
I'd assume you'd want an offU, and an offV. They should be 1.0/texturewidth and 1.0/textureheight to give you neighbour pixels.

##### Share on other sites
Quote:
 Original post by NamethatnobodyelsetookThey 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.

1. 1
Rutin
24
2. 2
3. 3
4. 4
JoeJ
18
5. 5

• 14
• 14
• 11
• 11
• 9
• ### Forum Statistics

• Total Topics
631757
• Total Posts
3002142
×