先画主干:
我们通过求取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;
}
接着需要约束柱子的高度:
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;
}
为柱子雕刻下纹路:
clamp(sin(y*8)*0.05, 0.01, 1)
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;
}
然后实现树冠,这里通过一个球体结合值噪声实现:
float leaf(vec3 pos)
{
float h = length(pos) - 1.0;
return h + valueNoise3(pos * 3.0) * 0.1;
}
太小了,放大一下,通过乘上当前位置的y坐标,然后少量缩放下:
float leaf(vec3 pos)
{
float h = length(pos) - 1.0;
return h + valueNoise3(pos * 3.0) * 0.1 + pos.y * 0.8;
}