SDF树的实现

Number of views 131

先画主干:

image1741760043394.png我们通过求取p点与vec3(0, p.y, 0)的距离并减去设置的半径值可以得到一根柱子:

float tree(vec3 pos, float h, float radius)
{
    float treeTrunk = length(pos-vec3(0., pos.y, 0.)) - radius;
    return treeTrunk;
}

image1741760998522.png

接着需要约束柱子的高度:

image1741761278434.png

float tree(vec3 pos, float h, float radius)
{
    float treeTrunk = length(pos-vec3(0., pos.y*(step(pos.y, h)-step(pos.y, 0.)), 0.)) - radius;
    return treeTrunk;
}

接着弯折下柱子,可以自己调整下弯折程度:

float tree(vec3 pos, float h, float radius)
{
    pos.x += sin(pos.y);
    float treeTrunk = length(pos-vec3(0., pos.y*(step(pos.y, h)-step(pos.y, 0.)), 0.)) - radius;
    return treeTrunk;
}

image1741761491272.png为柱子雕刻下纹路:

clamp(sin(y*8)*0.05, 0.01, 1)

image1741761640345.png

float tree(vec3 pos, float h, float radius)
{
    pos.x += sin(pos.y);
    float width = clamp(sin(pos.y * 8.0) * 0.05, 0.01, 1.);
    float treeTrunk = length(pos-vec3(0., pos.y*(step(pos.y, h)-step(pos.y, 0.)), 0.)) - radius+width;
    return treeTrunk;
}

image1741761738183.png

然后实现树冠,这里通过一个球体结合值噪声实现:

float leaf(vec3 pos)
{
    float h = length(pos) - 1.0;
    return h + valueNoise3(pos * 3.0) * 0.1;
}

image1741769015287.png

太小了,放大一下,通过乘上当前位置的y坐标,然后少量缩放下:

float leaf(vec3 pos)
{
    float h = length(pos) - 1.0;
    return h + valueNoise3(pos * 3.0) * 0.1 + pos.y * 0.8;
}

image1741769203817.png

0 Answers