The Depth Buffer
Daniel Ilett · Apress eBooks · 2022
How do you make sure objects get rendered in the correct order if they overlap in the viewport? What if an object is only partially obscured by another one? When attempting to render a given pixel, we need a system in place that allows us to check whether a pixel has already been drawn at the same position and determine whether it is in front of or behind the one we are currently drawing. This requires us to store additional data every time we draw a pixel. We can’t just store the color of the pixel; we also need to store the depth of the last pixel drawn to that position. Unity stores the color data inside a 2D array the size of the screen called the color buffer , and similarly, the depth information is stored in the depth buffer , which has the same dimensions as the color buffer. In this chapter, we will learn how the depth buffer works and create some shader effects that interact with it in different ways.