Jump to content

Pixi6 shader fragment example?


ikaruga
 Share

Recommended Posts

Tried the official "hello shader" example with pixi6 but it no longer seems to animate: https://pixijs.io/examples/#/filters-advanced/custom.js

I inject PixiJs into a React app. (I don't think this is the issue, since it works fine for everything else I've been doing the past few months.)

In this code snippet, useEffect just ensures that the code runs outside the render loop. And the useLayoutEffect block ensures the "uniforms" object and animate callback don't trigger re-renders. 

The shader will load fine. The app.ticker will run the animation function. But the shader does not animate. ?‍♂️

export function usePixiShader(props: {
  comp?: Container;
  uniforms?: Record<string, unknown>;
  animate?: (filter: Filter, delta: number) => void;
}) {
  const { comp: parent, uniforms, animate } = props;
  // just gives you the instance of the app
  const app = useContext(PixiAppContext);

  const comp = parent ?? app?.stage;
  const uniformsRef = useRef(uniforms);
  const animateRef = useRef(animate);

  useLayoutEffect(() => {
    uniformsRef.current = uniforms;
    animateRef.current = animate;
  });

  useEffect(() => {
    if (!resources || !comp || !app) return;

    const _shader = `
precision mediump float;

varying vec2 vTextureCoord;
varying vec4 vColor;

uniform sampler2D uSampler;
uniform float customUniform;

void main(void)
{
    vec2 uvs = vTextureCoord.xy;

    vec4 fg = texture2D(uSampler, vTextureCoord);


    fg.r = uvs.y + sin(customUniform);

    //fg.r = clamp(fg.r,0.0,0.9);

    gl_FragColor = fg;

}    
`;

    const filter = new Filter(undefined, _shader, {
      ...uniformsRef.current,
    });

    comp.filters = [filter];

    let animate: undefined | ((delta: number) => void);

    if (animateRef.current) {
      animate = (delta: number) => {
        animateRef.current?.(filter, delta);
      };
      app.ticker.add(animate);
    }

    return () => {
      if (animate) app.ticker.remove(animate);
    };
  }, [app, comp, resources]);
    
  return null
}
// usage

        usePixiShader({
          comp: sprite?.sprite,
          uniforms: {
            customUniform: 0,
          },
          animate: (filter, delta) => {
            filter.uniforms.customUniform += 0.04 * delta;
          },
        });

 

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...