Just got one of my extensions approved again and the extension reviewer suggested:
Note: You may want to scale down the preview lines drawn to the view by some percent towards the camera, to have them always drawn on top of coplanar faces with no Z fighting. SketchUp does something similar when drawing edges.
Any suggestions on how I would do that? I have a global (@edge) I highlight when acting with my extension.
def draw_edge(edge, view = edge.model.active_view)
# Get the two points that describe the edge:
points = edge.vertices.map(&:position)
# Get the view's camera object:
cam = view.camera
# Get the camera's eye point:
eye = cam.eye
# Calculate a smidgen of the camera's distance to target point:
distance = eye.target.distance(eye)
factor = 0.9996
smidgen = distance * ( 1.0 - factor )
# Get two vectors from the points to the camera eye point:
vec1 = points.first.vector_to(eye)
vec2 = points.last.vector_to(eye)
# Set vector lengths to the smidgen:
# Offset the drawing points by the vectors:
# Set drawing color & draw the line to the view:
view.drawing_color = 'Purple'
I don’t think you need to normalize the vectors before setting their lengths.
But yes, you are on the right track, and that is to use some point along an imaginary vector from the edge’s points toward the camera. (EDIT: I see you went the opposite along vectors from the eye to the points.)
The edge’s end points may not be the same distance from the camera eye, and therefore applying different offsets based on two differing distances can move one end of the draw line closer to the camera (and further from it’s edge point,) that the other.
This is why I suggested using the camera’s distance between it’s eye and it’s target point, hoping the draw points would be offset by the same amount.
However, OpenGL Z fighting is strange. I seem to remember that there is might be more bleed through for objects at a further distance than closer. You’ll need to experiment to find out which is best.
The range bins used by the z-buffer are usually larger when farther from the camera because details far away are not visually separable anyway.
Better explanation of what I was trying to say.
All the more reason to make the adjustment for more distant objects more pronounced, such that after the integral round-off the range to object is sure to fall into an different (in this case nearer) range bin.