// #include "res://addons/rokojori_action_library/Runtime/Shading/Library/Line3.gdshaderinc"

struct Line3
{
  vec3 start;
  vec3 end;
};

vec3 Line3_getAt( inout Line3 line, float t )
{
  return line.start + ( line.end - line.start ) * t;
} 

float Line3_closestParameterToPoint( inout Line3 line, vec3 point )
{
  vec3 startP = point - line.start;
  vec3 startEnd = line.end - line.start;

  float startEnd2 = dot( startEnd, startEnd );
  float startEnd_startP = dot( startEnd, startP );

  if ( startEnd2 == 0.0 )
  {
    return 0.5;
  }

  float t = startEnd_startP / startEnd2;

  return t;        
}

vec3 Line3_closestPointToPoint( inout Line3 line, vec3 point )
{
  float parameter = Line3_closestParameterToPoint( line, point );
  return Line3_getAt( line, parameter );
}

float Line3_getDistance( inout Line3 line, vec3 point )
{
  float parameter = clamp( Line3_closestParameterToPoint( line, point ), 0, 1 );
  
  vec3 p = Line3_getAt( line, parameter );

  return length( point - p );
}