我正在玩一个带有菱形十二面体体素的光线行军引擎,因为它们可以像六边形对 2D 空间一样完美地镶嵌 3D 空间。问题是我只设法想出了那个实体的有符号距离函数的近似值。可以渲染体素,但它会提供非常清晰的边缘。我想使用精确的 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
}
}
这是一个快速草图,展示我的方法
其余的案件变得更加疯狂。如果有任何突破,我会更新帖子。