src/__docs__/concepts/render-layers.mdx
RenderLayers let you control draw order independently of the scene graph hierarchy. An object keeps its parent's transform (position, scale, rotation) but gets drawn at the layer's position in the render order instead of its own.
Common use cases:
Independent Rendering Order:
Logical Parenting Stays Intact:
Explicit Object Management:
Dynamic Sorting:
zIndex and sortChildren for fine-grained control of rendering order.First let's create two items that we want to render, red guy and blue guy.
import { Sprite, Texture, RenderLayer } from 'pixi.js';
const texture = Texture.WHITE; // placeholder for your own texture
const redGuy = new Sprite(texture);
redGuy.tint = 0xff0000;
const blueGuy = new Sprite(texture);
blueGuy.tint = 0x0000ff;
stage.addChild(redGuy, blueGuy);
Now we know that red guy will be rendered first, then blue guy. Now in this simple example you could get away with just sorting the zIndex of the red guy and blue guy to help reorder.
But this is a guide about render layers, so lets create one of those.
Use renderLayer.attach to assign an object to a layer. This overrides the object’s default render order defined by its logical parent.
// a layer..
const layer = new RenderLayer();
stage.addChild(layer);
layer.attach(redGuy);
So now our scene graph order is:
|- stage
|-- redGuy
|-- blueGuy
|-- layer
And our render order is:
|- stage
|-- blueGuy
|-- layer
|-- redGuy
This happens because the layer is now the last child in the stage. Since the red guy is attached to the layer, it will be rendered at the layer's position in the scene graph. However, it still logically remains in the same place in the scene hierarchy.
Now let's remove the red guy from the layer. To stop an object from being rendered in a layer, use detach. Once removed from the layer, it's still going to be in the scene graph, and will be rendered in its scene graph order.
layer.detach(redGuy); // Stop rendering the rect via the layer
Removing an object from its logical parent (removeChild) automatically removes it from the layer.
stage.removeChild(redGuy); // if the red guy was removed from the stage, it will also be removed from the layer
However, if you remove the red guy from the stage and then add it back to the stage, it will not be added to the layer again.
// add red guy to his original position
stage.addChildAt(redGuy, 0);
You will need to reattach it to the layer yourself.
layer.attach(redGuy); // re attach it to the layer again!
This is intentional. Automatic re-attachment would cause subtle bugs: an object could silently rejoin a layer that was already removed from the scene. Explicit layer.attach() calls keep the render order predictable and debuggable.
The layer’s position in the scene graph determines its render priority relative to other layers and objects.
// reparent the layer to render first in the stage
stage.addChildAt(layer, 0);
Here's a real-world example that shows how to use RenderLayers to set a player UI on top of the world.
@stackblitz(rendering_render-layer)
Manual Reassignment:
Nested Children:
Sorting Within Layers:
zIndex property. This is useful for fine-grained control of render order.rect.zIndex = 10; // Higher values render later
layer.sortableChildren = true; // Enable sorting
layer.sortRenderLayerChildren(); // Apply the sorting
Layer Overlap:
By understanding and leveraging RenderLayers effectively, you can achieve precise control over your scene's visual presentation while maintaining a clean and logical hierarchy.