Making the base material shader customizable

I’d like to open a discussion on making a base material shader customizable, from the idea that came from discussions in GitHub during the VAT implementation. I’m offering myself to implement this.

What I propose that would work for me as an user:

  1. All the Vertex_* and Fragment_* methods currently in CustomMaterial/PBRCustomMaterial would be moved to a CustomShaderCode class.
  2. Instead of storing a single string in the class for each of these methods, a string[] is used.
  3. Possibly each of the code fragments also could have a priority (number) so that basic trivial sorting of code can be done for the most simple and common cases.
  4. Make class Material have a customShaderCode: CustomShaderCode field. Another possibility is that Material implements all the methods directly, but composition seems cleaner here. It also allows to change the CustomShaderCode class easily for a more specialized version.
  5. Pretty much the same #define/replace system from CustomMaterial is implemented in Material, calling CustomShaderCode.Builder().
  6. CustomMaterial and PBRCustomMaterial become essentially aliases for StandardMaterial and PBRMaterial, with all its methods just calling this.customShaderCode.xxx. This keeps backwards compatibility.

What do you think? This is purely my “works as an user for my problems” view, but apparently this feature could have an impact on other parts of Babylonjs and simplify other parts of its code.

I guess we d need to see a draft PR to better understand it (even if not fully implemented).

The restriction would be to have a nice a simple API to use without complexifying the current code :slight_smile:

I’ll send the PR soon. The API should be pretty much like CustomMaterial, but under a field. so something like myMaterial.customShaderCode.Vertex_AfterBegin(myCode);. In fact I’d rather make it a bit nicer, like myMaterial.customShaderCode.addCode(MaterialCode.AFTER_BEGIN, myCode);.

1 Like