在我的(类似Minecraft)3D体素世界中,我希望平滑形状以获得更自然的视觉效果。让我们首先在2D中看一下这个例子。
左派是没有平滑的世界。地形数据是二进制的,每个体素都呈现为单位大小的立方体。
在中心,您可以看到天真的圆形平滑。它只考虑四个直接相邻的块。它仍然不是很自然。而且,我想要出现平坦的45度斜坡。
在右侧,您可以看到我提出的平滑算法。它需要考虑八个直接和对角线的邻居才能得出一个块的形状。我在线the C++ code。下面是用于绘制贝塞尔曲线的控制点的代码。
#include <iostream>
using namespace std;
using namespace glm;
list<list<dvec2>> Points::find(ivec2 block)
{
// Control points
list<list<ivec2>> lines;
list<ivec2> *line = nullptr;
// Fetch blocks, neighbours start top left and count
// around the center block clock wise
int center = m_blocks->get(block);
int neighs[8];
for (int i = 0; i < 8; i++) {
auto coord = blockFromIndex(i);
neighs[i] = m_blocks->get(block + coord);
}
// Iterate over neighbour blocks
for (int i = 0; i < 8; i++) {
int current = neighs[i];
int next = neighs[(i + 1) % 8];
bool is_side = (((i + 1) % 2) == 0);
bool is_corner = (((i + 1) % 2) == 1);
if (line) {
// Border between air and ground needs a line
if (current != center) {
// Sides are cool, but corners get skipped when they don't
// stop a line
if (is_side || next == center)
line->push_back(blockFromIndex(i));
} else if (center || is_side || next == center) {
// Stop line since we found an end of the border. Always
// stop for ground blocks here, since they connect over
// corners so there must be open docking sites
line = nullptr;
}
} else {
// Start a new line for the border between air and ground that
// just appeared. However, corners get skipped if they don't
// end a line.
if (current != center) {
lines.emplace_back();
line = &lines.back();
line->push_back(blockFromIndex(i));
}
}
}
// Merge last line with first if touching. Only close around a differing corner for air
// blocks.
if (neighs[7] != center && (neighs[0] != center || (!center && neighs[1] != center))) {
// Skip first corner if enclosed
if (neighs[0] != center && neighs[1] != center)
lines.front().pop_front();
if (lines.size() == 1) {
// Close circle
auto first_point = lines.front().front();
lines.front().push_back(first_point);
} else {
// Insert last line into first one
lines.front().insert(lines.front().begin(), line->begin(), line->end());
lines.pop_back();
}
}
// Discard lines with too few points
auto i = lines.begin();
while (i != lines.end()) {
if (i->size() < 2)
lines.erase(i++);
else
++i;
}
// Convert to concrete points for output
list<list<dvec2>> points;
for (auto &line : lines) {
points.emplace_back();
for (auto &neighbour : line)
points.back().push_back(pointTowards(neighbour));
}
return points;
}
glm::ivec2 Points::blockFromIndex(int i)
{
// Returns first positive representant, we need this so that the
// conditions below "wrap around"
auto modulo = [](int i, int n) { return (i % n + n) % n; };
ivec2 block(0, 0);
// For two indices, zero is right so skip
if (modulo(i - 1, 4))
// The others are either 1 or -1
block.x = modulo(i - 1, 8) / 4 ? -1 : 1;
// Other axis is same sequence but shifted
if (modulo(i - 3, 4))
block.y = modulo(i - 3, 8) / 4 ? -1 : 1;
return block;
}
dvec2 Points::pointTowards(ivec2 neighbour)
{
dvec2 point;
point.x = static_cast<double>(neighbour.x);
point.y = static_cast<double>(neighbour.y);
// Convert from neighbour space into
// drawing space of the block
point *= 0.5;
point += dvec2(.5);
return point;
}
然而,这仍然是2D。如何将此算法转换为三维?
答案 0 :(得分:3)
您应该查看marching cubes algorithm并从那里开始工作。您可以轻松控制生成的blob的平滑度:
为了更改外观/平滑度,您需要更改密度函数和行进立方体算法的阈值。行进立方体以创建更平滑的网格的可能扩展是以下想法:假设您在立方体的边缘遇到两个点,其中一个点位于您的体积内(高于阈值)而另一个点位于外部(低于阈值)。在这种情况下,许多行进立方体算法将边界精确地放置在边缘的中间。人们可以计算出精确的边界点 - 这可以消除锯齿。
此外,我建议您在此之后运行网格简化算法。使用行进立方体会产生具有许多不必要三角形的网格。
答案 1 :(得分:2)
作为上述答案的替代方案:您还可以使用NURBS或subdivision surfaces的任何算法。特别是细分曲面算法被特化以平滑网格。根据算法及其配置,您可以使用
获得原始网格的更平滑版本等等。
答案 2 :(得分:0)