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
Asked
Active
Viewed 184 times
3
Matt Roberts
- 31
- 1
1 Answers
7
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}]}]
flinty
- 25,147
- 2
- 20
- 86
-
-
2I 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. – kirma Jun 10 '20 at 18:51 -
Thanks @kirma . I hadn't heard of that option. Maybe using BSP trees instead of z-buffering works? – flinty Jun 10 '20 at 20:05
-
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"}– Anti Earth Mar 06 '24 at 20:19


Graphics3D[{Green, Cuboid[{0, 0, 0}, {1, 1, 1}], Red, Cuboid[{1/2, 1/2, 1/2}, {1, 1, 1}]}]– flinty Jun 10 '20 at 15:14{1, 1, 1} + 0.0001to fix the issue. – C. E. Jun 10 '20 at 15:19