Page 7: Handling Occlusion
Spring 2026 Sample Solution
The solidity of objects is one of the strongest cues for 3D perception. We cannot see through most objects: objects closer to the viewpoint occlude those behind them.
With rasterization, multiple objects can potentially cover a pixel. We need some mechanism for deciding which one gets to choose the color of the pixel. In 2D, we typically let the last object drawn cover over earlier objects. However, this doesn’t work in 3D, where we want the object in front to give the color, and we may not know which object is in front ahead of time (because it depends on viewpoint).
In practice: the hardware uses the Z-Buffer algorithm. The Painter’s Algorithm is useful to understand, and is often handy for simple software implementations. While you may not implement the Z-Buffer algorithm (unless you are designing hardware), it is important to understand its quirks.
The Painter’s Algorithm
The painter’s algorithm is a simple way to achieve occlusion. We paint the objects in the back first. Then, when we paint the closer objects, they cover over the objects in the back. This is just like how painters (especially those who work in non-translucent media) work.
The key to the painter’s algorithm is that we need to collect all of the triangles so that we can sort them in order of distance back to front. You should notice that there are some catches in this:
- We need to collect all the triangles before doing any drawing.
- The triangles need to be sortable: we need to be able to determine which one is closer. If the triangles overlap in depth (or intersect), there may be a problem.
- We need to sort the list of triangles. While sorting is fast (generally ), in graphics anything worse than linear is considered slow.
- The triangles must be drawn in order. We cannot draw them in parallel. The objects in back must be drawn first.
- It can be wasteful: we draw the objects in back, even if another object will cover them. We call this problem overdraw, when the same pixel is drawn (over-written) many times, even though only the final color will be seen.
The painter’s algorithm violates the independence assumption that we like to have for efficient rasterization. However, it is very easy to implement, so it is often preferred for simple situations where performance isn’t critical. The demo in the last workbook where I made a spinning cube using the Canvas 2D API used the painter’s algorithm.
The inability to deal with situations where the triangles have no clear ordering is problematic. It cannot handle cases where two triangles intersect: one of them must be drawn first, and it will cover over the other one - even when that one is in front in places. To handle intersecting triangles, painter’s algorithm implementations often cut the triangles into pieces, which adds complexity.
The Z-Buffer Algorithm
The Z-Buffer algorithm was designed to preserve the independence assumption of rasterization. Or, to be more precise, the independence assumption (and the performance benefits it provides) were made possible because of the invention of the Z-Buffer algorithm.
The Z-Buffer is generally credited to Ed Catmull, the Turing Award-winning computer scientist who was the founder of Pixar, and inventor of many other important things in computer graphics. However, another famous computer graphics researcher (Wolfgang Strasser) developed a similar idea around the same time in Europe.
The key idea behind the Z-Buffer algorithm is that it makes the occlusion decision on a per-pixel basis by storing extra information along with the resulting pixel colors. This contrasts with the Painter’s Algorithm, which makes decisions on a per-object basis.
The Z-Buffer extends the image that is being drawn to store depth information in addition to color information for each pixel in the resulting image. When a pixel is “filled” by writing a color into its memory location, the writing process also stores the depth information (the Z-value) associated with the point providing the color. The trick is that as part of writing the pixel, the drawing method first does a test to decide if the new color/depth value is closer than the one that is already there. This decision is made for every pixel write that the rasterizer does.
To describe the process in more detail:
- The image has memory for the color for each pixel, but also a depth value per pixel.
- When the image is initialized (i.e., cleared before drawing), the “maximally far” value is written to all pixels. Note that because we typically look down the Z-axis, the maximally far in front value is a large negative number.
- During the drawing process, the rasterization algorithm computes the color and the depth for each fragment. If it was not concerned about occlusion, this color would be written to memory.
- For each fragment, the “write color to memory” step is replaced with a read/test/write step:
- The existing Z-value is read from the image.
- If the existing value is closer than the new value, the fragment is rejected.
- If the fragment is not rejected, its color and Z-value are written into memory.
Notice that the Z-Buffer algorithm is (almost always) order independent. It doesn’t matter what order the triangles are drawn in, or that the fragments are generated. If the closer one is first, it gets stored in memory, and later, farther fragments are rejected. If the farther fragment is first, it can be over-written later by a fragment with a closer Z-value.
The Z-Buffer addresses most of the problems with the Painter’s Algorithm:
- We do not need to know the triangles beforehand: they can be generated on the fly, and processed in any order. This makes the Z-Buffer practical for immediate mode systems.
- The algorithm can handle overlapping and intersecting triangles. There is a potential problem in how ties are handled, but these only matter on a pixel-by-pixel basis (we will discuss this in a moment).
- We do not need to sort the triangles. There is no sorting step.
- There is no order dependency; the fragments can come in any order. Parallelism is possible.
- There is still the potential for overdraw.
The biggest cost of the Z-Buffer is that it is memory intensive. It replaces a simple writing of a color with a read/test/write cycle of both color and depth. In the old days, it required “graphics” memory for both color and depth.
A Small Detail…
Z-Buffers usually store 1/z rather than Z itself.
Remember from our discussion of the perspective transformation that it provides 1/z as its values. While a modern graphics system could take the reciprocal, there is no reason to: the ordering is preserved.
The reciprocal of depth turns out to be useful for other things. 1/z is linear in screen space, and therefore can be interpolated (for example, to get perspective-correct texture coordinates).
Z-Buffer Problems
The Z-Buffer is a key ingredient in enabling rasterization as a strategy for graphics, which in turn led to graphics hardware and interactive graphics applications.
However, the Z-Buffer does have some problems. These are all cases where, in some settings, there are (relatively minor) breaks in the independence assumption.
Z-Fighting
The depth test causes the nearest fragment to appear. But what happens if there is a tie (two fragments have the same depth)? In this case, the two fragments will “fight” to decide which one appears.
In practice, ties are handled by setting the comparison operator: do we include or exclude equality? If we make the comparison operator “>=”, then the newer fragment replaces the older (equal fragment); if the operator is “>” then the first fragment is kept. Notice that this creates an order dependence.
To combat Z-fighting, we need to avoid triangles that are close to being co-planar. Triangles that are truly coplanar are very problematic: they will violate the independence assumption, so the “winner” of the Z-fight will depend on the ordering (which is tricky, since the order the program draws the triangles may not be the order that the parallel hardware ultimately draws them). Also, small imprecisions in the depth computations may cause one triangle to be in front of the other sometimes, but not others.
In the old days, when there was limited resolution for the Z-Buffer, Z-fighting was a bigger problem. Rounding errors and imprecision would cause things that were close to have the same value, and z-fight. As things moved, small errors would compound and cause things to alternate unpredictably between the different elements.
From the demo, you can see that even a small amount of offset can cure Z-fighting problems. In the old days, we needed to use more offset to combat the low resolution of the Z-Buffer, and these larger offsets would sometimes make objects “float” when they were supposed to look coplanar.
In order to make the offset less visible, we can make it in the screen space Z direction. APIs offer a “polygon offset” parameter (this is part of three.js’s materials) that displace objects slightly in a screen space direction.
There are many techniques that help avoid Z-Fighting issues. Some are built into three.js. For example, the “logarithmic Z-Buffer” technique applies logarithmic scaling to the depth values. There is a performance cost, so this is not the default.
(Semi-) Transparency
The Z-Buffer assumes that the closer object occludes the farther one. If we want to make a transparent object we need to draw the farther object first so that the transparent object doesn’t block the thing behind it.
Transparent objects are typically drawn by blending them with the existing contents of the frame buffer (the pixel colors from other objects). They need to be drawn last: both because the existing colors need to be there, but also for the depth test (the transparent object will occlude the objects behind it if it is drawn first).
Transparent objects need to violate the independence assumption. However, often it is sufficient to draw all the “regular” objects first, and then draw all of the transparent objects.
Performance Issues
The Z-Buffer algorithm has a performance cost.
The algorithm requires all fragments to be computed - even if they will be discarded or overwritten. For scenes where many triangles cover the same pixel, this can be very wasteful.
We will discuss avoiding overdraw waste more in the future. But a short answer for now: the simplicity of the Z-Buffer and rasterization approach means it is often better to be efficient (because things are simple) and wasteful, than to give up the efficiency to avoid waste.
There is a quirk of performance that is unique to the Z-Buffer and represents a violation of the independence assumption: the number of memory writes depends on the triangle ordering. Consider a pixel that is covered by two triangles. If the front triangle is drawn first, it puts its depth value into the buffer, and the farther triangle is rejected. If the farther triangle is drawn first, both triangles will write to memory. The memory writes themselves are rarely a performance bottleneck. However, the memory write must complete before another fragment can read it (to perform its own test).
Looking ahead, the wastefulness of computing fragments that will never be seen, particularly when they involve complex and expensive lighting calculations, will turn out to be one of the keys to achieving high-performance interactive graphics.
Summary: Occlusion Methods
Rasterization approaches require some algorithm so that closer objects occlude farther objects. The Painter’s Algorithm provides a simple mechanism. The Z-Buffer algorithm is the method used in practice for most interactive graphics. Understanding its quirks allows us to work around its limitations.
Next: Page 8 - The Pipeline