Jump to content

pixi v5.3.3 shader problem ,please help me


AndyZhengLL
 Share

Recommended Posts

I have wrote my own mask fuction and I found a UV problem with shader

const sprite = PIXI.Sprite.from(slottexture)
const maskFrag = `
varying vec2 vTextureCoord;
uniform sampler2D uSampler;
uniform sampler2D maskTexture;

void main() {
vec2 uv = fract( vTextureCoord );
vec4 originalColor = texture2D(uSampler, uv);
vec4 maskColor = texture2D(maskTexture, uv);
gl_FragColor =  originalColor * maskColor.r;
}
`

const maskTexture = resources.mask.texture
maskTexture.baseTexture.mipmap = false;

const filter = new PIXI.Filter(null, maskFrag, {
  maskTexture: maskTexture,
});

sprite.filters = [filter];
root.addChild(sprite);

and it`s just render a quarter, I dont know why?

and  change the gl_FragColor = maskColor it`s still render a quarter, so I think it`s a uv problem!

boys.png

mask.png

demo.png

demo2.png

Link to comment
Share on other sites

There are different coordinate systems in filter. Input is actually bigger than filter area, so you need to multiply/divide on something to get correct result for your mask. Most probably

maskUV = vTextureCoord * inputSize.xy / outputFrame.zw

where inputSize / outputFrame are uniforms. For more tricks, see how DisplacementFilter works in source code of pixijs - it allows to overlay displacement mask, and then applies it. There are "calculateNormalCoord" or whatever, i dont remember exactly.

I'm sorry I cant post a long lecture here, but here are explanations:

https://github.com/pixijs/pixijs/wiki/v5-Creating-filters

https://github.com/pixijs/pixijs/wiki/v4-Creating-Filters

I can recommend to use mesh-shader instead, when possible: https://pixijs.io/examples/#/mesh-and-shaders/triangle-textured.js , just make a quad geometry and use two textures.

Link to comment
Share on other sites

16 hours ago, ivan.popelyshev said:

There are different coordinate systems in filter. Input is actually bigger than filter area, so you need to multiply/divide on something to get correct result for your mask. Most probably


maskUV = vTextureCoord * inputSize.xy / outputFrame.zw

where inputSize / outputFrame are uniforms. For more tricks, see how DisplacementFilter works in source code of pixijs - it allows to overlay displacement mask, and then applies it. There are "calculateNormalCoord" or whatever, i dont remember exactly.

I'm sorry I cant post a long lecture here, but here are explanations:

https://github.com/pixijs/pixijs/wiki/v5-Creating-filters

https://github.com/pixijs/pixijs/wiki/v4-Creating-Filters

I can recommend to use mesh-shader instead, when possible: https://pixijs.io/examples/#/mesh-and-shaders/triangle-textured.js , just make a quad geometry and use two textures.

can`t thank you enough

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...