ikaruga Posted February 19, 2022 Share Posted February 19, 2022 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; }, }); Quote Link to comment Share on other sites More sharing options...
ikaruga Posted February 19, 2022 Author Share Posted February 19, 2022 Wierd... if I apply the filter to the `app.stage` instead of the loaded sprite, it works ?♀️ Quote Link to comment Share on other sites More sharing options...
ikaruga Posted February 19, 2022 Author Share Posted February 19, 2022 OK, this *does* look like a React hook issue. This was adding the shader to the sprite's filter probably before it was added to the container, and that caused it not work? Because by delaying the filter assignment, it started to work as expected Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.