Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dose pixijs support reverse mask feature ? #8207

Open
liujinxiao opened this issue Mar 3, 2022 · 7 comments
Open

dose pixijs support reverse mask feature ? #8207

liujinxiao opened this issue Mar 3, 2022 · 7 comments

Comments

@liujinxiao
Copy link

liujinxiao commented Mar 3, 2022

is there any plan to achieve mask reverse feature ? thanks !

now our team is using pixi.js to achieve mask feature and mask reverse feature, like this

1、this is the original sprite
image

2、this is the sprite with circle mask
image

3、this is the sprite with circle reverse mask
image

@carl-jin
Copy link

carl-jin commented Mar 4, 2022

check this demo below,看下这个例子老铁
https://pixijs.io/examples/#/masks/filter.js

@liujinxiao
Copy link
Author

check this demo below,看下这个例子老铁 https://pixijs.io/examples/#/masks/filter.js

this example just show mask feature, but does not support mask reverse feature

@liujinxiao liujinxiao changed the title reverse mask dose pixijs support reverse mask feature ? Mar 4, 2022
@liujinxiao
Copy link
Author

https://github.com/liujinxiao/pixijs/pull/1/files

edit packages/core/src/filters/spriteMask/spriteMaskFilter.frag file, maybe resolve the issue?
image

@dev7355608
Copy link
Collaborator

You can use custom sprite mask filters like so:

object.mask = new PIXI.MaskData(sprite);
object.mask.filter = new MyCustomSpriteMaskFilter();

But you shouldn't use a mask for reverse masking. That doesn't work. Use a filter:

object.filters = [new MyReverseSpriteMaskFilter()];
object.filters[0].maskSprite = sprite;

@liujinxiao
Copy link
Author

You can use custom sprite mask filters like so:

object.mask = new PIXI.MaskData(sprite);
object.mask.filter = new MyCustomSpriteMaskFilter();

But you shouldn't use a mask for reverse masking. That doesn't work. Use a filter:

object.filters = [new MyReverseSpriteMaskFilter()];
object.filters[0].maskSprite = sprite;

mask is actually a filter, so i want to edit mask filter to achieve mask reverse feature。this does work.

@liujinxiao liujinxiao reopened this Mar 12, 2022
@dev7355608
Copy link
Collaborator

Try this:

object.filters = [new PIXI.SpriteMaskFilter(undefined, `\
varying vec2 vMaskCoord;
varying vec2 vTextureCoord;

uniform sampler2D uSampler;
uniform sampler2D mask;
uniform float alpha;
uniform float npmAlpha;
uniform vec4 maskClamp;

void main(void)
{
    float clip = step(3.5,
        step(maskClamp.x, vMaskCoord.x) +
        step(maskClamp.y, vMaskCoord.y) +
        step(vMaskCoord.x, maskClamp.z) +
        step(vMaskCoord.y, maskClamp.w));

    vec4 original = texture2D(uSampler, vTextureCoord);
    vec4 masky = texture2D(mask, vMaskCoord);
    float alphaMul = 1.0 - npmAlpha * (1.0 - masky.a);

    original *= 1.0 - (alphaMul * masky.r * alpha * clip);

    gl_FragColor = original;
}
`)];
object.filters[0].maskSprite = sprite;

@liujinxiao
Copy link
Author

this is same as my method, and it does work. but i think support reverse mask feature may be easy for users, thanks.
anyway, we can achieve this feature :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants