Shader control

Is there a way to turn a shader on and off? I’m using it for calculations and not rendering and I’d rather turn it off when not needed.

Can I turn the shader off with existing methods? Setting active to false does not work.

Can I use existing methods to trigger shader rendering? Can I use flush for that?

I’m creating a shader by using this.add.shader. If I wan’t to add additional uniforms - do I need to create my own object? I’m not sure from the documentation on how to add a uniform using base shader.

this.add.shader creates a game object that renders with the given shader. To prevent it from rendering, you should be able to take it off the display list, which is this.children or this.sys.displayList.

Game objects have an internal renderWebGL method that is used to render them. Given the right arguments, you can call it manually, but I don’t know how well that would work outside the render loop.

Shader game objects should be able to use any uniforms. I’d look through the examples to see how.

Considering that shader game objects are meant to be used to render graphics, I’m not convinced they’re the best solution if you want to compute something. You can access the WebGL context with game.renderer.gl, which could make it easier to control the shader programs yourself. For a more complicated shader handled outside Phaser, I’d also recommend looking into the clearPipeline and rebindPipeline methods on the renderer.

1 Like

Thank you, especially for the examples page which (for some reason) I couldn’t find directly.
After playing with the shaders for a few days, it seems I can’t do much in WebGL 1. Especially not being able to access items in arrays by index. So the only thing I can think of is having calculations done by rendering to texture and feeding it back as input for multiple passes. I think it’s refereed to as a ping-pong technique. For this to be useful I need to work separated from the rendering loop.
So I guess I’ll try to work out how to do this outside of phaser provided objects. If you have any helpful links on anyone trying out such things on WebGL, please let me know.