I am using the Graphics3D command in Mathematica to plot multiple 3 dimensional parallelotopes and two of them have overlapping faces. These two shapes have different colored faces, so the textures of the two seems to jump between the two colors. Is there any way to remove one or both of the textures in the overlapped area? Picture related
1 Answer
As I mentioned in the comments, this is called z-fighting. It's hard to avoid and it produces ugly artifacts like in this example:
Graphics3D[{Green, Cuboid[{0, 0, 0}, {1, 1, 1}], Red, Cuboid[{1/2, 1/2, 1/2}, {1, 1, 1}]}]`
Your graphics card determines whether a triangle is occluded by another triangle using a z-buffer. If the z-buffer has too low of a precision this means the outcome will be subject to floating point rounding error and as you rotate the object you'll experience flickering and one surface popping out over another like this.
You can eliminate the z-fighting by making one face very slightly offset in a way that is visually unnoticeable but distinct enough that the depth buffer will consistently produce the occlusion of one triangle by another as you move the object:
tiny = 2^-16;
Graphics3D[{Green, Cuboid[{0, 0, 0}, {1, 1, 1}], Red,
Cuboid[{1/2, 1/2, 1/2}, {1 - tiny, 1 - tiny, 1 - tiny}]}]
-
$\begingroup$ Very well explained. +1 $\endgroup$ Commented Jun 10, 2020 at 16:07
-
2$\begingroup$ I was also wondering about this and
Method -> {"RelieveDPZFighting" -> True}
doesn't fix the problem at least for sheared cubes. A slight offset of coplanar surfaces does it. $\endgroup$– kirmaCommented Jun 10, 2020 at 18:51 -
$\begingroup$ Thanks @kirma . I hadn't heard of that option. Maybe using BSP trees instead of z-buffering works? $\endgroup$– flintyCommented Jun 10, 2020 at 20:05
-
$\begingroup$ As @flinty points out, this is easily solved (so that the visible surface is the latter given to
Graphics3D
) by giving additional optionBaseStyle -> RenderingOptions -> {"Graphics3DRenderingEngine" -> "BSPTree"}
$\endgroup$ Commented Mar 6 at 20:19
Graphics3D[{Green, Cuboid[{0, 0, 0}, {1, 1, 1}], Red, Cuboid[{1/2, 1/2, 1/2}, {1, 1, 1}]}]
$\endgroup${1, 1, 1} + 0.0001
to fix the issue. $\endgroup$