opengl 菱形十二面体的精确符号距离函数

wf82jlnq  于 9个月前  发布在  其他
关注(0)|答案(1)|浏览(111)

我正在使用一个带有rhombic dodecahedra形状的体素的光线行进引擎,因为它们可以像2D空间的六边形一样完美地测试3D空间。问题是,我只得到了该实体的signed distance function的近似值。渲染体素是可以的,但它会给出非常尖锐的边缘。我想使用精确的SDF。
"有人知道菱形十二角体的SDF吗?"
以下是我的SDF(不准确):

float Sdf(Voxel voxel, vec3 position) {
    position = position - voxel.Position;
    // Exploit the solid's symmetries
    position = vec3(abs(position.x), sign(position.z) * position.y, abs(position.z));

    // distance to each face
    float a = dot(vec3(1.  ,   0.           , 0.          ), position) - voxelSize;
    float b = dot(vec3(0.5 ,   0.           , sqrt3 / 2.  ), position) - voxelSize;
    float c = dot(vec3(0.  , - sqrt2 / sqrt3, 1. / sqrt3  ), position) - voxelSize;
    float d = dot(vec3(0.5 ,   sqrt2 / sqrt3, sqrt3 / 6.  ), position) - voxelSize;
    float e = dot(vec3(0.5 , - sqrt2 / sqrt3, - sqrt3 / 6.), position) - voxelSize;

    return max(a, max(b, max(c, max(d, e))));
}

字符串
它看起来是这样的:

编辑:

因此,我对如何处理这个问题有了一些新的想法。在这个新技术中,我试图找到不同的区域,它们将有不同的距离公式(取决于离实体最近的点是在面上、边上还是顶点上)并分别计算它们的距离。到目前为止,我只有最近点在面上或边上的区域的正确间距和距离。代码如下所示:

float Sdf(Voxel voxel, vec3 position){
    position = position - voxel.Position;

    vec3 normals[6];
    float dists[6];
    float signs[6];

    // The rhombic dodecahedron has 6 pairs of opposite faces.
    // Assign a normal to each pair of faces.
    normals[0] = vec3(1.  ,   0.           , 0.          );
    normals[1] = vec3(0.5 ,   0.           , sqrt3 / 2.  );
    normals[2] = vec3(0.5 ,   0.           , - sqrt3 / 2.);
    normals[3] = vec3(0.  , - sqrt2 / sqrt3, 1. / sqrt3  );
    normals[4] = vec3(0.5 ,   sqrt2 / sqrt3, sqrt3 / 6.  );
    normals[5] = vec3(0.5 , - sqrt2 / sqrt3, - sqrt3 / 6.);

    // Compute the distance to each face (the sign tells which face of the pair of faces is closest)
    for (int i = 0; i < 6; i++){
        dists[i] = dot(normals[i], position);
        signs[i] = sign(dists[i]);
        dists[i] = max(0., abs(dists[i]) - voxelSize);
    }

    bool sorted = false;
    while(!sorted){
        sorted = true;
        for (int i = 0; i < 5; i++){
            if (dists[i] < dists[i+1]){
                vec3 n = normals[i];
                float d = dists[i];
                float s = signs[i];
                normals[i] = normals[i+1];
                dists[i] = dists[i+1];
                signs[i] = signs[i+1];
                normals[i+1] = n;
                dists[i+1] = d;
                signs[i+1] = s;
                sorted = false;
            }
        }
    }
    
    if (dists[1] < dists[0] * 0.5){ //0.5 comes from sin(pi/6)
        // case where the closest point is on a face
        return dists[0];
    }
    else if (dists[2] == 0.){
        // case where the closest point is on an edge
        float a = 0.5 * dists[0];
        float b = 0.5 * (dists[1] - a);
        return length((dists[0] - b) * signs[0] * normals[0]
                    + (dists[1] - a) * signs[1] * normals[1]);
    }
    else {
        //dunno
    }
}


这里有一个简单的草图,展示了我的方法

剩下的案子越来越疯狂了,如果有突破,我会更新帖子的。

js4nwp54

js4nwp541#

前言

假设菱形十二面体的方向为seen here
此方向使其相对于沿着所有三个坐标平面的镜像对称。
然后,您需要的是一个适用于(+,+,+)八分区中this shape的sdf。

替代近似值

形状可以被看作是三个平面的联合。利用曼哈顿距离,可以定义近似sdf的替代方案:

float Sdf(float3 position, float size)
{
    position = abs(position);

    float halfsqrt2 = sqrt(2) / 2;

    float side1 = (position.x + position.y - size) * halfsqrt2;
    float side2 = (position.x + position.z - size) * halfsqrt2;
    float side3 = (position.y + position.z - size) * halfsqrt2;

    return max(max(side1, side2), side3);
}

字符串
这可以缩写为:

float Sdf(float3 position, float size)
{
    position = abs(position);

    return (max(max(position.x + position.y, position.x + position.z), position.y + position.z) - size) * sqrt(2) / 2;
}

准确的sdf

形状也可以看作是transformed cube。从一个矩阵转换到另一个矩阵是

0 1 1
1 0 1
1 1 0


它是逆的

-0.5  0.5  0.5
 0.5 -0.5  0.5
 0.5  0.5 -0.5


(有多个矩阵可以工作,但这是我使用的一个)
转换后,可以使用圆框的sdf。下面是一个代码示例:

float SdfRoundRhombicDodecahedron(float3 position, float size, float edgeSize)
{
    float3x3 w2r = float3x3(
        0, 1, 1,
        1, 0, 1,
        1, 1, 0
    );

    position = abs(position);
    position = mul(w2r, position);
    position -= clamp(position, 0, size - edgeSize);
    return length(position) - edgeSize;
}


变换和镜像的圆形立方体然后具有菱形十二面体的形状。Here is a visualisation of it made in Unity

相关问题