Multi-compile shader from #macros


I’d like to compile two versions of a fragment shader (built via chunks), based on an #ifdef . Something similar to Unity’s Shader.SetKeyword() behaviour.

Whether or not this is defined is based on the user enabling a bool in the inspector.

My initial attempts result in undefined. I imagine it’s because I’m doing it in initialize, but I’m pretty new to js/PC, so it may be something else :

I’m basically trying to reproduce what I see here, but I’m not sure where to stick it!: engine/standard.js at f64bb6d28eeeac2598bc63ad6dae0b372c99e6ad · playcanvas/engine · GitHub

I’m wondering a couple of things:

  • Are shader macros the right solution for this in Playcanvas?
  • If so: Is there a way to make sure this hits the preprocessor?
  • If not: What’s the recommended approach? Any examples would be much appreciated.

My backup plan is to swap entire chunks in initialize, based on the bool’s condition.

Thanks for your time

Hi … PlayCanvas does not have a feature like this yet, the feature request is here for tracking: [Feature Request] Add shader recompile mechanism · Issue #2962 · playcanvas/engine · GitHub

In the meantime, I’d probably go with swapping of entire chunks I think.


Hey, good stuff - Swapping is working well for my purposes :+1:
For future context, here what I wound up going with:

    this.material.chunks.basePS = this.FlowMapping ? PlanetShader.basePSFlowMapping : PlanetShader.basePS;

Thanks for the info!