1

I want to draw a line where an object intersects a plane, in the fragment shader.

I pass the plane equation to the fragment shader (a, b, c, d) and want to draw in red the intersection between my object and this uniform plane.

I want the width of the intersection to be always the same no matter how much we zoom in.

Right now I get the distance from my object to the plane in the fragment shader as:

  // uPlane: vec4 where values are a.b.c.d (plane equation)
  // vPos: wodlPosition of the fragment
  vec4 prod = uPlane*vPos;
  float distance = (prod.r + prod.g + prod.g + prod.a) / length(uPlane.xyz);

  if(abs(distance) < 1.) {
    gl_FragColor = vec4(1., 0., 0., .5);
  } else {
    gl_FragColor = vec4(0., 1., 0., .5);
  }

Problem with this approach is that as I zoom in/out, the "width" of the intersection changes (see screenshots below).

Which would be the best approach to draw a "2px width" line at the intersection between my object and the plane uniform?

enter image description here enter image description here

Nicolas
  • 2,191
  • 3
  • 29
  • 49
  • One solution could be somehow to project the fragment position onto the plane, then compute the projection screen coordinates somehow, then compare the projection's screen coordinate to the fragment screen coordinate and use that info to draw the line – Nicolas Feb 08 '17 at 16:38

1 Answers1

1

You have to account for the "zoom factor" / scaling of your view. For the 2D case (your pictures look pretty much 2D), uniform scaling and no rotation this is pretty easy. Pass the model-view matrix and extract the [0][0]th element (or pass that value to the shader). Divide your distance value by it. For extracting the proper scaling value from a model-view matrix with rotations and translations, see this.

Bim
  • 1,008
  • 1
  • 10
  • 29