float cross2d(vec2 v, vec2 w){ return v.x * w.y - w.x * v.y; } mat3 get_xy_to_uv(vec2 b0, vec2 b1){ mat3 shift = mat3( 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, -b0.x, -b0.y, 1.0 ); float sf = length(b1 - b0); vec2 I = (b1 - b0) / sf; vec2 J = vec2(-I.y, I.x); mat3 rotate = mat3( I.x, J.x, 0.0, I.y, J.y, 0.0, 0.0, 0.0, 1.0 ); return (1.0 / sf) * rotate * shift; } // Orthogonal matrix to convert to a uv space defined so that // b0 goes to [0, 0] and b1 goes to [1, 0] mat4 get_xyz_to_uv(vec3 b0, vec3 b1, vec3 unit_normal){ mat4 shift = mat4( 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, -b0.x, -b0.y, -b0.z, 1 ); float scale_factor = length(b1 - b0); vec3 I = (b1 - b0) / scale_factor; vec3 K = unit_normal; vec3 J = cross(K, I); // Transpose (hence inverse) of matrix taking // i-hat to I, k-hat to unit_normal, and j-hat to their cross mat4 rotate = mat4( I.x, J.x, K.x, 0.0, I.y, J.y, K.y, 0.0, I.z, J.z, K.z, 0.0, 0.0, 0.0, 0.0, 1.0 ); return (1.0 / scale_factor) * rotate * shift; }