Raymarching坐标裁剪会产生奇怪的拉伸伪影。

huangapple go评论56阅读模式
英文:

Raymarching Clamping coordinate makes weird streched artifacts

问题

I am trying to render a grid of spheres using ray marching. The SDF function looks like:

float sq_layer(vec3 p, vec3 bounding_box_min, vec3 bounding_box_max)
{
    float cell_size = 4.0 / 16.0;
    vec3 p0 = clamp(p.xyz, bounding_box_min, bounding_box_max);
    p0 = mod(p0, cell_size) - cell_size * 0.5f;
    float d_pt = sphereSDF(vec3(p0.x, p0.y, p0.z), 0.05f);
    return d_pt;
}

But I am getting something like this:

Raymarching坐标裁剪会产生奇怪的拉伸伪影。

For some more relevance, I tried changing the code up a bit:

float sq_layer(vec3 p, vec3 bounding_box_min, vec3 bounding_box_max)
{
    vec3 cell_size = (bounding_box_max - bounding_box_min)  / 4.0;
    vec3 p0 = clamp(p.xyz, bounding_box_min, bounding_box_max);
    p0 = mod(p0, cell_size) - cell_size * 0.5f;
    float d_pt = boxSDF(p0, cell_size * 0.35);
    return d_pt;
}

Still I get,

Raymarching坐标裁剪会产生奇怪的拉伸伪影。

英文:

I am trying to render a grid of spheres using ray marching. The SDF function looks like:

float sq_layer(vec3 p, vec3 bounding_box_min, vec3 bounding_box_max)
{
         float cell_size = 4.0 / 16.0;
         vec3 p0 = clamp(p.xyz, bounding_box_min, bounding_box_max);
         p0 = mod(p0, cell_size) - cell_size * 0.5f;
         float d_pt = sphereSDF(vec3(p0.x, p0.y, p0.z), 0.05f)
         return d_pt;
}

But I am getting something like this:
Raymarching坐标裁剪会产生奇怪的拉伸伪影。

For some more relevance, I tried changing the code up a bit:

float sq_layer(vec3 p, vec3 bounding_box_min, vec3 bounding_box_max)
{
         vec3 cell_size = (bounding_box_max - bounding_box_min)  / 4.0;
         vec3 p0 = clamp(p.xyz, bounding_box_min, bounding_box_max);
         p0 = mod(p0, cell_size) - cell_size * 0.5f;
         float d_pt = boxSDF(p0, cell_size * 0.35);
         return d_pt;
}

Still I get,
Raymarching坐标裁剪会产生奇怪的拉伸伪影。

答案1

得分: 5

使用您目前的坐标截断方式导致您的 SDF 返回一个接近表面的常数距离,因此您迭代次数用尽,导致看到的伪影。
Raymarching坐标裁剪会产生奇怪的拉伸伪影。

与其截断,不如与边界框大小相同的盒形 SDF 进行交集:

float sq_layer(vec3 p, vec3 bounding_box_min, vec3 bounding_box_max)
{
         float cell_size = 4.0 / 16.0;
         vec3 p0 = mod(p, cell_size) - cell_size * 0.5f;
         float d_pt = sphereSDF(p0, 0.05f);

         // 以下是一个简化的盒形 SDF
         vec3 bb_extents = (bounding_box_max - bounding_box_min) * .5; 
         vec3 bb_center = bounding_box_min + bb_extents;
         vec3 bb_p = max(abs(p-bb_center)-bb_extents,0.);
         float d_bb = max(max(bb_p.x,bb_p.y),bb_p.z);

         // 与边界框相交的“球场”
         return max(d_pt,d_bb);
}

Raymarching坐标裁剪会产生奇怪的拉伸伪影。

英文:

Clamping the coordinates the way you do results in your sdf returning a constant distance very close to the surface, hence you're running out of iterations resulting in the artifacts you see.
Raymarching坐标裁剪会产生奇怪的拉伸伪影。

Instead of clamping you want to do an intersection with a box sdf the size of your bounding box:

float sq_layer(vec3 p, vec3 bounding_box_min, vec3 bounding_box_max)
{
         float cell_size = 4.0 / 16.0;
         vec3 p0 = mod(p, cell_size) - cell_size * 0.5f;
         float d_pt = sphereSDF(p0, 0.05f);

         // the following is just a simplified box sdf
         vec3 bb_extents = (bounding_box_max - bounding_box_min) * .5; 
         vec3 bb_center = bounding_box_min + bb_extents;
         vec3 bb_p = max(abs(p-bb_center)-bb_extents,0.);
         float d_bb = max(max(bb_p.x,bb_p.y),bb_p.z);

         // intersect "sphere-field" with bounding box
         return max(d_pt,d_bb);
}

Raymarching坐标裁剪会产生奇怪的拉伸伪影。

huangapple
  • 本文由 发表于 2023年5月14日 05:14:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76244905.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定