我在 Viewport3D 中有一些 GeometryModel3D 球,其中一些是可见的,其中一些被蓝色立方体隐藏。(尽管下面的图像是 2d 的,让我们假设所有对象都是 3D)
我想确定哪些红球是可见的,哪些是隐藏的。
我怎样才能做到这一点 ?
这个问题也称为Occlusion Culling,尽管您对计算被遮挡的图元感兴趣。鉴于您的场景条件,解决此问题的蛮力方法(假设您正在使用透视投影)是以下伪代码:
occludedSpheresCount = 0
spheres = {Set of spheres}
cubes = {Set of cubes}
normalizedCubes = {}
# First, build the set of normalized cubes (it means,
# take the cubes that are free in space and transform their
# coordinates to values between [-1, -1, -1] and [1, 1, 1], they are the same
# cubes but now the coordinates are laying in that range
# To do that, use the
投影矩阵
projectionMatrix = GetProjectionMatrix(perspectiveCamera)
for each cube in cubes do
Rect3D boundingBox = cube.Bounds()
Rect3D normalizedBBox = projectionMatrix.transform(boundingBox)
cubes_normalized.add(normalizedBBox)
end for
# Now search every sphere, normalize it's bounding box
# and check if it's been occluded by some normalized cube
for each sphere in spheres do
Rect3D sphereBBox = sphere.Bounds()
Rect3D normalizedSphere = projectionMatrix.transform(sphereBBox)
for each normalizedCube in normalizedCubes do
x0 = normalizedCube.Location.X - (normalizedCube.Location.SizeX / 2)
y0 = normalizedCube.Location.Y - (normalizedCube.Location.SizeY / 2)
z0 = normalizedCube.Location.Z - (normalizedCube.Location.SizeZ / 2)
xf = normalizedCube.Location.X + (normalizedCube.Location.SizeX / 2)
yf = normalizedCube.Location.Y + (normalizedCube.Location.SizeY / 2)
sx0 <- normalizedSphere.Location.X - (normalizedSphere.Location.SizeX / 2)
sy0 <- normalizedSphere.Location.X - (normalizedSphere.Location.SizeY / 2)
sz0 <- normalizedSphere.Location.X - (normalizedSphere.Location.SizeZ / 2)
sxf <- normalizedSphere.Location.X + (normalizedSphere.Location.SizeX / 2)
syf <- normalizedSphere.Location.X + (normalizedSphere.Location.SizeY / 2)
# First, let's check that the normalized-sphere is behind the
# normalized-cube, to do that, let's compare their z-front values
if z0 > sz0 then
# Now that we know that the sphere is behind the frontface of the cube
# lets check if it is fully contained inside the
# the normalized-cube, in that case, it is occluded
if sx0 >= x0 and sxf <= xf and sy0 >= y0 and syf >= yf then
occludedSpheresCount++
# Here you can even avoid rendering the sphere altogether
end if
end if
end for
end for
获取 projectionMatrix 的一种方法是使用以下代码(从此处提取):
private static Matrix3D GetProjectionMatrix(PerspectiveCamera camera, double aspectRatio)
{
// This math is identical to what you find documented for
// D3DXMatrixPerspectiveFovRH with the exception that in
// WPF the camera's horizontal rather the vertical
// field-of-view is specified.
double hFoV = MathUtils.DegreesToRadians(camera.FieldOfView);
double zn = camera.NearPlaneDistance;
double zf = camera.FarPlaneDistance;
double xScale = 1 / Math.Tan(hFoV / 2);
double yScale = aspectRatio * xScale;
double m33 = (zf == double.PositiveInfinity) ? -1 : (zf / (zn - zf));
double m43 = zn * m33;
return new Matrix3D(
xScale, 0, 0, 0,
0, yScale, 0, 0,
0, 0, m33, -1,
0, 0, m43, 0);
}
这种方法的唯一缺点是在以下情况下:
+---------------+--------------+ | -|- | | / | \ | | | | | | | \ | / | | -|- | +---------------+--------------+ 或者 在这里拦截 | v +----------+--+--------------+ | | -|- | | /| | \ | | | | | | | | \| | / | | | -|- | +----------+--+--------------+
Set{ Set{ cube1, cube2}, Set{cube3, cube4}, ... }
其中两个截断立方体遮挡球体,在这种情况下,当两个或多个立方体区域截断(可以在第一个循环中完成)时,您必须构建一组归一化立方体( )并且争用测试会更多复杂的。不知道您的程序中是否允许这样做(多维数据集拦截)
该算法是O(n^2)
因为是一种蛮力方法,希望这可以为您提供最终解决方案的提示,如果您正在寻找一个更有效的更通用的解决方案,请使用类似Hierarchical Z Buffering