I am writing a shader to draw a frame around a shape geometry, I am passing the corners -2D points- to the shaderprojecting the position on to the edge to check the distance. Somehow the trigonometry is off, i cant see wher i got it wrong ( the points coordinates and uniforms check out).
vertexShader = `
//varying vec2 vUv;
out float isBorder;
uniform int shapeLen;
uniform vec2 shapeArr[shapeArrayLength];
uniform float uWidth;
void main(){
vec2 pos = position.xy ;
isBorder = 0.0;
//for each side
for (int i = 0; i < shapeLen ; i++){
//egde vector (AB)
highp vec2 AB;
highp vec2 B;
//edge vector
if( i < (shapeLen-1 )){ int next = i+1;
AB = shapeArr[next] - shapeArr[i] ; }
else{ AB = vec2( shapeArr[0] - shapeArr[i] ) ; } //close the shape
vec2 ABnorm = normalize(AB); //unit vector of the edge
float ABlen = length(AB);//distance( shapeArr[i], B );
//vector corner to position (AX)
vec2 AX = pos - shapeArr[i] ;
float AXlen = length(AX); //distance( shapeArr[i], pos );
vec2 AXnorm = normalize(AX); //unit vector
//test corners
float dist = distance(shapeArr[i], pos );
if(dist < .150){isBorder = 1.0;}
//dot product to project point to edge
float dotSide = dot(AB, AX);
if( dotSide >= 0.0 && dotSide <= 1.0){
//isBorder =0.15;
//angle of both vectors
float angle = acos(dot(ABnorm, AXnorm));
//length of projected vector ??
float APlen = cos(angle) * ABlen;
// distance to edge
float XPlen = sqrt( pow(AXlen, 2.0) - pow(APlen, 2.0));
// check distance to edge
if(XPlen < 2.0){isBorder = 1.0;}
}
}
vec4 modelPosition = modelMatrix * vec4(position, 1.0);
vec4 viewPosition = viewMatrix * modelPosition;
vec4 clipPosition = projectionMatrix * viewPosition;
gl_Position = clipPosition;
}
`;