How to write a MaterialPlugin regex that will find and replace multiple lines

I am currently experimenting with MaterialPlugins and would like to switch out a block of code using the regex.
I can follow the examples to switch out a single line without issue, but if I try to switch out multiple lines then I cannot get it to work.

For example, I have tried switching out the following block of code from pbrBlockFinalUnlitComponents:

vec3 finalDiffuse = diffuseBase;
finalDiffuse *= surfaceAlbedo.rgb;
finalDiffuse = max(finalDiffuse, 0.0);

using the following getCustomCode regex logic:

public override getCustomCode(shaderType: string) {
return shaderType === “vertex”
? null
: {
“!vec3\finalDiffuse\=\diffuseBase;\finalDiffuse\*=\surfaceAlbedo.rgb;\finalDiffuse\=\max(finalDiffuse,\0.0);”: vec3 finalDiffuse = vec3(0., 1., 0.); ,

but it doesn’t seem to do anything.

In addition:

  • when using these regexes, are we changing the code exactly as it is written within the checked in .ts files or are we changing some kind of parsed version?
  • is it possible to use the regex to change and ifdef statement or is the regex run after the ifdef is already applied?
  • is it possible to use the regex to change a #include?

Many thanks.

Regex in material plugins don’t support the multiline flag. We will update the code to add this support.

The code has already been parsed in some way. You can get it by doing material.getEffect().vertexSourceCodeBeforeMigration and material.getEffect().fragmentSourceCodeBeforeMigration.

Yes, the #ifdef / #define have not yet been parsed when the regex are processed.

No, the #include have already been parsed at that time.

1 Like

cool, thanks for the info :+1:

This PR will allow you to set the flags to use with the regexp:

The new supported syntax is: !flags!regex

The flags you can use: gimus

For example:


will look for diFFuseBase\\+=info\\.diffuse\\*shadow; in a case insensitive way.


Thanks @Evgeni_Popov :smiley: