In this short basic tutorial, we'll cover two different topics with a single practical example: how to position objects on two or more different View Layers in a Blender 4.1 scene, and how to blend them in Compositing using the Z-Combine node, in order to obtain, in the final rendering, from each View Layer, the pixels that are closest to the virtual camera.
Video Transcript
Hello everyone! In this short basic tutorial, we'll cover two different topics with a single practical example: how to position objects on two or more different View Layers in a Blender 4.1 scene, and how to blend them in Compositing using the Z-Combine node, in order to obtain, in the final rendering, from each View Layer, the pixels that are closest to the virtual camera.
View Layers allow us to render objects separately in a scene. It's also possible to choose which View Layers to render and which not to. This also allows us to optimize resources and rendering times, especially in very large or element-rich scenes.
The Z-Combine node allows us to sort objects from two or more View Layers, or even from different images with a Z channel or distance map, in order to obtain a single rendering in which the elements are displayed based on their distance from the virtual camera.
This is a basic tutorial, so we'll see step by step what the View Layers of a scene are, how to associate the Collections of objects with the various View Layers, how to determine which View Layers to render, and how to blend them in Compositing with the Z-Combine node.
In the scene I'm using for this tutorial, there are two mesh objects: a fence and a grass field. In particular, the grass of the field is present on both sides of the fence, so some parts are covered by the grass blades, and vice versa.

Initially, these two objects and the virtual camera are within a single Collection.
In the menu bar, we also notice the presence of only one Scene and one View Layer. View Layers, in particular, allow us to determine which elements of the scene to display and which not to, so it's possible to create groups of elements within the scene. They are similar to Collections for visualization and rendering.

By default, the present Collection is included in the base View Layer and, therefore, always displayed. This can be verified by looking at the Checkbox next to the Collection's name. The tooltip of this Checkbox informs us that we can decide whether to exclude the respective Collection from the View Layer.

Let's see how to put the grass and the fence in two different View Layers, to render and modify them separately in Compositing before merging them into a single image.
The first thing to do is rename the default View Layer to give it a more meaningful name; let's call it, for example, "Grass".
Similarly, let's rename the default Collection to Grass.

Now let's add another View Layer by clicking on the "Add View Layer" icon and choosing the "Blank" option, so we can create a new View Layer that will initially have all Collections disabled.

This operation immediately makes us understand one thing: the visibility of elements in the various View Layers is determined by the Collections they are in.
At this moment, the Grass Collection is not included in the new View Layer, and we cannot even see which elements are inside it.
Let's rename the new View Layer to Fence, then go back to the Grass View Layer.

Select the Fence object and press the M key to open the "Move to Collection" menu, then choose the "New Collection" option and create a new collection called Fence.

In the Outliner window, we can see that the new Collection has been created with the Scene Collection as its Root, just like Grass.

Since we don't want to display the fence in this View Layer, let's uncheck the checkbox next to the Fence box.

Now let's move to the Fence View Layer to perform the opposite operation, which is to include only the Fence Collection in the rendering, excluding Grass.
In this way, we have the objects separated into two different Collections, each of which is rendered in two distinct View Layers.
Before starting the rendering and moving on to Compositing, however, we need to activate the Z channel of the Fence View Layer so that we have the Depth information in the Render Layer during Compositing. This is done by switching to the Fence View Layer, then opening the View Layers tab in the Properties editor and selecting the Z checkbox.

This checkbox is enabled by default in the base View Layer of new projects, so it's generally not necessary to remember to activate it, but it's disabled for new View Layers. Like for all Render Passes, it needs to be activated before rendering, otherwise the information won't be available in Compositing.
After performing this operation, we can start the rendering.
As you may have noticed, Blender performs rendering for each View Layer.
Before moving on to Compositing the current scene, I must tell you that you can exclude View Layers from rendering, so you only render the View Layers where you are making changes, without wasting time rendering the others.
To decide whether to render a View Layer or not, select it and open the View Layers tab in the Properties editor, then select or deselect Use for Rendering.

In our case, however, we need the renderings of both View Layers, so we can move to a Compositing window and go on with our tutorial.
In the Render Layers node, we notice the presence of the View Layers selector at the bottom. Therefore, let's duplicate the default node and select the second View Layer of our project in the new node.

The blending of the two View Layers based on the distance from the camera is simply done by inserting a Z-Combine node in the editor and connecting the Image and Depth information of each Render Layer to the Image and Z inputs of the Z-Combine node.

The order in which we connect the two original images to the two Image inputs of the node is important because if two pixels have the same Z value, the one arriving at the second input will be shown. The first input is therefore called Background, and the second one Foreground.
You may have also noticed the presence of the "Use Alpha" option.
The Image inputs of the Z-Combine node also gather the Alpha information coming from Render Layers. The blending occurs based on the Z values, but if the "Use Alpha" option is selected and a pixel from the second Image input (Foreground) is semitransparent, then the corresponding pixel of the image present in the first Image input (Background) will be semi-visible, depending on the opacity of the foreground pixel.
Now it's possible to separately modify the information coming from the two Render Layers with nodes, while still examining the final Compositing correctly blended by the Z-Combine node.

The fact that the Image and Z inputs of the two images are separate suggests the possibility of using the Z information of a Render Layer to blend Textures or other images, using the Z information as a blending mask.

The fact that there is an overall Z output of the node suggests that it's possible to blend multiple Render Layers or images in succession, using multiple Z-Combine nodes connected one after the other.

Before concluding this tutorial, I want to point out that it's possible to parent objects belonging to different Collections and View Layers.
This operation is the basis of a tutorial that I will publish soon, and of which I am showing you a preview below...

… but, as I said, we'll talk about it another time! For this tutorial, that's all! See you soon!