我的方法:
与其使用贝塞尔曲线(这样会使厚度更加复杂),我试图走捷径。基本上,我只是使用一个平滑的步骤来在水平段之间进行过渡,这给出了一个不错的曲线。为了计算曲线的厚度,对于任何给定的片段 x,我计算 y 并最终计算出我们应该在哪条线上的坐标(x,y)。不幸的是,这并没有计算到曲线的最短距离,如下所示。
以下是一张图表,可以帮助理解我遇到的问题所涉及的功能。// Start is a 2D point where the line will start
// End is a 2d point where the line will end
// transition_x is the "x" position where we're use a smoothstep to transition between points
float CurvedLine(vec2 start, vec2 end, float transition_x) {
// Setup variables for positioning the line
float curve_width_frac = bendWidth; // How wide should we make the S bend
float thickness = abs(end.x - start.x) * curve_width_frac; // normalize
float start_blend = transition_x - thickness;
float end_blend = transition_x + thickness;
// for the current fragment, if you draw a line straight up, what's the first point it hits?
float progress_along_line = smoothstep(start_blend, end_blend, frag_coord.x);
vec2 point_on_line_from_x = vec2(frag_coord.x, mix(start.y,end.y, progress_along_line)); // given an x, this is the Y
// Convert to application specific stuff since units are a little odd
vec2 nearest_coord = point_on_line_from_x * dimensions;
vec2 rad_as_coord = rad * dimensions;
// return pseudo distance function where 1 is inside and 0 is outside
return 1.0 - smoothstep(lineWidth * dimensions.y, lineWidth * 1.2 * dimensions.y, distance(nearest_coord, rad_as_coord));
// return mix(vec4(1.0), vec4(0.0), s));
}
我熟悉如何计算线段到直线的最短距离,但对于曲线段,不太确定该如何处理。如有建议,敬请赐教。