2D SDF推导4: 通用正多边形

最开始想要看一看SDF,是因为我在网上找到下面一段代码,我发现我根本看不懂,但是挺好用的

glsl 复制代码
float sdf_polygon(vec2 pt, float radius, int N) {
    float a = atan(pt.x, pt.y);
    float r =  PI * 2.0 / float(N);
    if (a < 0.) {
        a += PI * 2.0;
    }
    float d = length(pt) * (1. + f4 -  radius );
    float a2 = floor(f1 * .5 + a /r)*r;
    return cos(a2 - a) *d - f2 / 2.0;
}

到现在我还是看不懂这段代码....但是通过推导,我写出了上面代码更优雅,更好理解的Shader :)

实现通用正多边形的SDF的秘诀,就在于坐标系的转换,通过极坐标可以很好的处理正多边形的每一条边。极坐标有以下性质

  • 基于一个固定点(极点)和从这个点发出的射线来定义点的位置。
  • 任何平面上的点都通过一对坐标(r, θ)来表示:r是点到极点的距离(半径),θ是从参考方向(通常是x轴正方向)到点的连线与参考方向之间的角度。

设正N边形中心为点 <math xmlns="http://www.w3.org/1998/Math/MathML"> O ( 0 , 0 ) O(0, 0) </math>O(0,0) 半径为r,根据N的大小,将圆等分成N块。每块占据的角度为 <math xmlns="http://www.w3.org/1998/Math/MathML"> δ \delta </math>δ 任意点 <math xmlns="http://www.w3.org/1998/Math/MathML"> P ( r 1 , θ ) P(r1, \theta) </math>P(r1,θ) 根据 <math xmlns="http://www.w3.org/1998/Math/MathML"> θ \theta </math>θ 都可以归属到某一块中。如下图,圆角块开始边角度为2PI / N * 0,结束边角度为2PI / N * 1. 假设开始点为 <math xmlns="http://www.w3.org/1998/Math/MathML"> A A </math>A ,结束点为 <math xmlns="http://www.w3.org/1998/Math/MathML"> A ′ A^\prime </math>A′ . 过 <math xmlns="http://www.w3.org/1998/Math/MathML"> O O </math>O 做 <math xmlns="http://www.w3.org/1998/Math/MathML"> A A ′ AA^\prime </math>AA′ 的垂线有交点 <math xmlns="http://www.w3.org/1998/Math/MathML"> D D </math>D . 点 <math xmlns="http://www.w3.org/1998/Math/MathML"> P P </math>P 到边的距离就是 <math xmlns="http://www.w3.org/1998/Math/MathML"> P O PO </math>PO 在 <math xmlns="http://www.w3.org/1998/Math/MathML"> P D PD </math>PD 上的投影长度减去 <math xmlns="http://www.w3.org/1998/Math/MathML"> P D PD </math>PD 的长度。于是我们可以写出以下的shader

glsl 复制代码
float sdf_polygon1(vec2 P, float radius, int sides) {
    float angle = atan(P.y, P.x);
    // Translate the value of angle into the range (0, 2 * PI).
    angle = P.y > 0. ? angle: angle + PI * 2.;
    
    float delta = 2. * PI / float(sides);
    float areaNumber = ceil(angle / delta);
    
    
    float theta1 = delta * areaNumber;
    float theta2 = delta * (areaNumber + 1.0);
   
    
    vec2 PA       = vec2(radius * cos(theta1), radius * sin(theta1) );
    vec2 PA_Prime = vec2(radius * cos(theta2), radius * sin(theta2) );
    vec2 PD       = (PA - PA_Prime)/2.0;
        
    float projectLength = abs(dot(PD, P)) / length(PD);
    return projectLength - length(PD) ;
}

实际上我们只需要知道两个长度即可, <math xmlns="http://www.w3.org/1998/Math/MathML"> P O PO </math>PO 在 <math xmlns="http://www.w3.org/1998/Math/MathML"> P D PD </math>PD 上的投影长度ProjectLength 和 <math xmlns="http://www.w3.org/1998/Math/MathML"> P D PD </math>PD 的长度。PD的长度可以直接通过 <math xmlns="http://www.w3.org/1998/Math/MathML"> r × c o s ( δ / 2 ) r \times cos(\delta / 2) </math>r×cos(δ/2) 直接计算出。 投影长度可以通过 <math xmlns="http://www.w3.org/1998/Math/MathML"> P O PO </math>PO 与 <math xmlns="http://www.w3.org/1998/Math/MathML"> P D PD </math>PD 的夹角求得。 整体代码可以简化为

glsl 复制代码
float sdf_polygon3(vec2 P, float radius, int sides) {
    float angle = atan(P.y, P.x);
    float delta = 2. * PI / float(sides);
    float theta = mod(angle, delta) - delta / 2.0;
  
    float lengthPD         = radius * cos(delta / 2.0);
    float lengthProjection = length(P) * cos(theta)

    return lengthProjection - lengthPD;
}

最后我们就可以得到以下漂亮的图形了

相关推荐
l***O52017 小时前
免费的WebGL性能优化,纹理压缩与实例化
性能优化·webgl
二川bro1 天前
第38节:WebGL 2.0与Three.js新特性
开发语言·javascript·webgl
qiao若huan喜4 天前
9、webgl 基本概念 + 复合变换 + 平面内容复习
前端·javascript·信息可视化·webgl
qiao若huan喜7 天前
10、webgl 基本概念 + 坐标系统 + 立方体
前端·javascript·信息可视化·webgl
GisCoder7 天前
Cesium教程(9)---编辑Entity图形控制点、拖拽Entity移动、删除Entity
webgl·cesium
qiao若huan喜11 天前
7、webgl 基本概念 + 前置数学知识点(向量 + 矩阵)
线性代数·矩阵·webgl
qiao若huan喜13 天前
6、webgl 基本概念 + 四边形纹理
前端·javascript·信息可视化·webgl
爱看书的小沐14 天前
【小沐杂货铺】基于Three.js绘制三维管道Pipe(WebGL、vue、react)
javascript·vue.js·webgl·three.js·管道·pipe·三维管道
梦凡尘15 天前
webgl 变换矩阵:旋转、平移、缩放
webgl
倚剑仙17 天前
Unity-WebGL开发——用IIS(Internet Information Services)部署webGL工程
unity·游戏引擎·webgl