Need ideas for an GLSL Shader

I need to figure out a Shader (vert, frag) that as further away from edges creates more white color. So it it’s like a IN gradient. Shapes and angles can be different, even unregular forms. Any ideas?

Ah and in one pass I must do this :confused:

First, OpenGL only supports triangles. Quads and polygons (which only exist in the compatibility profile) are tessellated into triangles, essentially the same as triangle fans. You’ll need to take that into account when assigning values to vertex attributes. You may be better off performing tessellation yourself. If you need to handle non-convex polygons, you’ll have to handle tessellation yourself.

Once a shape has been tessellated into triangles, you’ll have exterior (boundary) edges and interior edges. For each exterior edge, you need a scalar attribute (or one component of a vector attribute) whose value is zero for the two vertices which form that edge and one for the vertex opposite that edge. You don’t need a distinct attribute for each edge; they only need to be distinct within each triangle.

Colouring each fragment according to the minimum of the interpolated values will give you something like you describe. However, the shading will be based upon “distance” as a proportion of the triangle’s size, so larger triangles will get a thicker boundary. If you want to use actual screen-space distance, you’ll need to use partial derivatives (dFdx and dFdy) to determine the gradient and divide by its magnitude to obtain a value which is proportional to screen-space distance.

Could you draw some sketches? I have hard time understanding your approach from text. Please!

Start with a single triangle. Define a vec3 vertex attribute whose values for the three vertices are (1,0,0), (0,1,0) and (0,0,1) (if the attribute was a colour, the vertices would be red, green and blue). The vertex shader just copies the attribute from input to output:

in vec3 a_dist;
out vec3 dist;

void main() {
    dist = a_dist;
    gl_Position = /* whatever */;

In the fragment shader:

in vec3 dist;

void main() {
    float d = min(dist.x, min(dist.y, dist.z));
    gl_FragColor = vec4(d, d, d, 1);

From there, figure out how to assign values for arbitrary triangle meshes so that exterior edges are shaded but interior edges aren’t.

Once you have that working, follow up if you need advice on tweaking the shading.

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.