Skip to content
Marc Flerackers edited this page Sep 1, 2024 · 7 revisions

Layers are coming back soon in Kaplay. Though there are a few things which need to be discussed. Layers works as follows:

// Define the layers as well as the default layer
layers(["bg", "game", "ui"], "game");

// bg layer
add([
    rect(width(), height()),
    // Draw this object in the bg layer
    layer("bg"),
    color(rgb(64, 128, 255))
])

Currently it only works locally, like z does. The plan is to change it to work globally. This means all visible nodes are collected in tree order, then sorted on layer and z.

There are a few known caveats:

  • The mask component works on the children of the node it is attached to. With local sorting there is no problem. With global sorting, the children may be in another layer and not drawn right after the mask is set. So the children need to know that they need to set the mask first, before drawing. This adds quite some complexity. Unity has mask as a component, but on the object which is masked.
  • If the mask component's draw method can be called multiple times during one frame because it masks nodes which are drawn in different layers, the draw method needs to be deterministic, it shouldn't change during a single frame. This may be fixed by drawing into a picture, and reusing the picture.
  • The drawon component is similar. It switches the current framebuffer for its owner and children.
Root
 | Mask
    | First masked node
    | Second masked node

Right now Mask is drawn, then First, and finally Second. If we would sort all three nodes first, and the layer and/or z would place First and Second before Mask, we need to know that we need to make the mask active for First and Second. Also, if First and Second are still bunched together, we would want to make the mask active only once to draw both.

Clone this wiki locally