Shaders: Checking if the current fragment is at the border of the geometry

Hello,

I need to draw a border around a quad. The quads can be differently sized and scalled.

My first approach was to use the texturecoordinate:

[java]

if(texCoord1.x<0.1||texCoord1.x>0.9||texCoord1.y<0.1||texCoord1.y>0.9){

gl_FragColor=m_Color2;

}else{

gl_FragColor=m_Color1;

}

[/java]



This basically works, but of course the size of the border scales with the geometry scale. I would like to have the border 1px thick/wide.

Is the a nicer/good way of getting the information needed?

Approach 1)

A separate texture with a single pixel border using alpha to knock out everything but the border. You can use color ramping to change the border color without screwing up the alpha mapping.



Approach 2)

Do the same thing you’re doing… however… make the check a LOT smaller. i.e.

texCoord1 < 0.0004

Or something similar until you get the results you looking for. Even though the border will scale… It will be so minimal, you won’t see it rendered.

1 Like

If it always has to be 1px and we are only talking quads, I would think about abandoning shaders and using a simple custom quad mesh made up of 4 lines.

@thetoucher I am betting they are texture and he is wanting to ad the border above the texture.

@t0neg0d said:
@thetoucher I am betting they are texture and he is wanting to ad the border above the texture.


Nope, no textures. Just plain quads and only two colors, one for the body and one for the border.
Actually @thetoucher 's method sounds valid and quite easy to implement. I was hoping for a 'magic' shader command :D
I will try to decrease the texCoord treshold. If that does not make the needed results i will make the line-quad mesh

There are a few border shaders around (inner glow, toon, etc) that you could check out for ideas.

The CartoonEdgeFilter does the computation based on depth and normal of the object.

But it’s a post process effect and will be applied on every object of the scene.



If you are using just a quad then 'id go for @thetoucher 's way of doing it, it’s straightforward and gives the expected result.

Also you may have to add some poly offset on the material to avoid z-fighting with the real object

@nehon said:
The CartoonEdgeFilter does the computation based on depth and normal of the object.
But it's a post process effect and will be applied on every object of the scene.

If you are using just a quad then 'id go for @thetoucher 's way of doing it, it's straightforward and gives the expected result.
Also you may have to add some poly offset on the material to avoid z-fighting with the real object


ToonShader is not an option since AFAIK i cannot set the 'bordercolor' individually for each object.
Decreasing the texCoor1 treshold is also not an options since the result is not predictable(Sometimes a border get's drawn, sometimes not. (Depeding on the scale and position of the quad)

I have implemented @thetoucher 's way. polyoffset is not a problem because i am dealing on the GuiNode.

Good.


@zzuegg said:
ToonShader is not an option since AFAIK i cannot set the 'bordercolor' individually for each object.

Can't you? i thought I added this feature?