View Full Version : Distance to lines results in inlets at line crossings

06-01-2014, 06:53 AM

I have a problem with the result that I get when calculating distances to lines in a shader program, specifically distances to multiple lines.


As you can see from the screenshots there is no problem with single lines, but when combined in the calculation inlets are visible at the line crossing that I do not understand and would like to get rid of. If this is some sort of optical illusion, then I still would like to know how to avoid this.
(background: I want to create a soft shadow effect from a polygon silhouette that gets passed to the fragment shader)

Part of the fragment shader code that is used to calculate the combined distance:

void main(void) {
outputColor = vec4(1f, 1f, 0f, 1f);
bool bands = false;

float len = 0.2;
float distHorizontalLine = getDistanceToLine(vec3(0f, 0f, 0f), vec3(len, 0f, 0f), currentPoint);
float distVerticalLine = getDistanceToLine(vec3(0f, 0f, 0f), vec3(0f, len, 0f), currentPoint);

float dist = distHorizontalLine; // [A] distance to horizontal line
dist = distVerticalLine; // [B] distance to vertical line
// dist = min(distVerticalLine, distHorizontalLine); // [C] distance to cross

float radius = 0.25f;
if (dist<radius) {
float ratio = dist/radius;
vec4 distColor = vec4(0f, 0f, 1f, 1f);
outputColor = mix(distColor, outputColor, ratio);

if (bands) {
// debug: display bands
float band = (ceil(fract(ratio)*10f))/10f;
outputColor = vec4(band, band, 1-band, 1f);

The complete project (java (lwjgl) + shaders sources):

06-02-2014, 06:25 AM
Replace min() with something else, e.g.

dist = min(min(distVerticalLine, distHorizontalLine), distVerticalLine*distHorizontalLine/radius);

06-14-2014, 06:06 PM
GClements thanx for the hint, I got the asked result, however I have tried to come up with a proper result (without unwanted markings), but so far I have had not much luck. I think this problem actually consist of 2 problems:
1] the calculated distances are not exactly the same for both lines, so the shader outputs a slightly different color which is visible as a discontinuous region at the 'shadow' intersection

2] the optical illusion thing. Even when solving [1] the inlets are disturbingly visible. It just doesn't make sense though, they should not appear in the first place.