pixijs / layers

Separate the z-hierarchy of your scene tree from its canonical structure.
https://pixijs.io/layers/docs/
MIT License
223 stars 57 forks source link

Layers cannot be masked ? #75

Closed gsotirov closed 2 years ago

gsotirov commented 3 years ago

When setting layer.mask = <some_mask> all display objects on the layer disappear. Can layers be masked ?

Example code:

const stage = new display.Stage();

const group = new Group(0, true);
const layer = new Layer(group);

stage.addChild(layer);

const mask = new Graphics();
mask.beginFill(0x000000).drawRect(0, 0, 200, 200);

layer.mask = mask; // Here the whole layer disappears.
ivanpopelyshev commented 3 years ago

you should add mask to stage too, there are no guarantees for stage-less masks.

It should work, need complete example

gsotirov commented 3 years ago

Thanks for the swift response, Ivan. Yes, when I added it directly to the stage it worked, But if the display object used as a mask is added to a Container that is rendered on a specific layer, the above-mentioned problem happened. That was the case with my initial implementation.

const stage = new display.Stage();
const group = new Group(0, true);
const layer = new Layer(group);

stage.addChild(layer);

const container = new Container(); // The container has some children inside, 1 of which will be used as a mask.
container.parentGroup = group;

layer.mask = container.children[0];
ivanpopelyshev commented 3 years ago

Oh, now i know whats wrong and i think i can reproduce it. thank you!

gsotirov commented 3 years ago

You are welcome. @ivanpopelyshev Since I guess it could take a while before the eventual fix is added, can you share where the issue is so I can try a temp solution from my end ?

ivanpopelyshev commented 3 years ago

most possibly something with if (this._activeParentLayer) . It should be ignored if element is a mask

ivanpopelyshev commented 3 years ago

@gsotirov actually, if your layer has only dynamic objects - they wont affect bounds, and bounds are required for sprite masks. You may add filterArea to that layer, like layer.filterArea = renderer.screen , or override calculateBounds or add a graphics with a rect that has alpha=0.

Please confirm that was the issue!

ivanpopelyshev commented 3 years ago

Here's important notice for you: https://github.com/pixijs/layers#important-notice-about-filters-and-masks

ivanpopelyshev commented 3 years ago

Nope, I failed to reproduce your case. Can you please put it on jsfiddle or pixi-playground or codesandbox?

gsotirov commented 3 years ago

Thanks @ivanpopelyshev, for the notice.

Sorry for the late response, I've been busy. While making the example, I was able to make it work, but only if the mask is rendered on the same layer I wanted to mask with it. If I want to use the mask to mask two layers, it won't work.

Here's what I did: https://codesandbox.io/s/pixi-playground-forked-8lwo1?file=/src/index.js

Comment/Uncomment lines 49 & 50 to reproduce.