title: How to find correct way to project point on triangle for drawing line on mesh?
body: <p>I have mesh and 2 points on it - <strong>A</strong> and <strong>B</strong>. Each point lies on some triangle of mesh. The main goal is - draw <em>correct</em> line on mesh using 2 points. When points lies on triangles with different planes - I have problems with line drawing. </p>
  
  <p>What I do: </p>
  
  <blockquote>
    <p>CurrentTriangle = triangle on which the point A lies. </p>
    
    <p>While CurrentTriangle != triangle with point B:</p>
    
    <p>Get B projected(<strong>Bp</strong>) to CurrentTriangle: moving <strong>B</strong> by
    -CurrentTriangle.normal * distance to plane. </p>
    
    <p>Find the exit point from the triangle - the intersection of <strong>ABp</strong> with the side of the triangle(converting 3d coords to 2d and find intersection point, then using barycentric coordinates gets 3d intersection point).</p>
    
    <p>Move the resulting position towards position <strong>B</strong> to find a new CurrentTriangle.</p>
  </blockquote>
  
  <p>The problem is to project position <strong>B</strong> correctly onto the plane of the CurrentTriangle. Actual result:</p>
  
  <p><a href="https://i.stack.imgur.com/FTRGl.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/FTRGl.png" alt="Problem example"></a></p>
  
  <p>Expected result (red line):
  <a href="https://i.stack.imgur.com/KxAae.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/KxAae.png" alt="enter image description here"></a></p>