# Material plugins: only the last-added plugin's MATERIALPLUGIN\_n define reaches the effect key, so materials with different plugin sets can share one shader program

**URL:** <https://forum.babylonjs.com/t/material-plugins-only-the-last-added-plugins-materialplugin-n-define-reaches-the-effect-key-so-materials-with-different-plugin-sets-can-share-one-shader-program/64383>\
**Category:** Bugs\
**Created:** [October 23, 2026, 12:59am UTC](https://forum.babylonjs.com/t/material-plugins-only-the-last-added-plugins-materialplugin-n-define-reaches-the-effect-key-so-materials-with-different-plugin-sets-can-share-one-shader-program/64383 "2026-10-23T00:59:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ctabone](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/ctabone/32/52182_2.png) [@ctabone](https://forum.babylonjs.com/u/ctabone)\
**Post date:** [October 23, 2026, 12:59am UTC](https://forum.babylonjs.com/t/material-plugins-only-the-last-added-plugins-materialplugin-n-define-reaches-the-effect-key-so-materials-with-different-plugin-sets-can-share-one-shader-program/64383/1 "2026-10-23T00:59:46Z")

</div>

Hi all! We found a shader-cache mix-up with material plugins. When a material has two plugins, two materials whose plugin sets differ can end up drawing with the same compiled program. Depending on which one compiles first, a mesh draws in the wrong colour (black in the repro) or is not drawn at all, with a WebGL “uniform buffer too small” error. We found it while digging into shader recompiles in our game.

**In short:** `MaterialPluginManager` gives each plugin class a marker define, `MATERIALPLUGIN_n`, so that a material with a plugin never shares a program with one without it. That was the fix for [PluginMaterial Bug](https://forum.babylonjs.com/t/pluginmaterial-bug/30098) (PR #12502). But `_addPlugin` rebuilds the define list from scratch each time and puts back only the marker of the plugin being added. With two plugins, only the last one’s marker reaches the effect key. A disabled plugin still adds its uniforms to the material’s UBO, so two materials whose plugin sets differ only by a disabled plugin get the same key, and so the same program, but different UBO layouts.

The fix is a one-line move inside `_addPlugin`. Details, measurements and a suggested patch are below.

**Repro (Playground):** [Babylon.js Playground](https://playground.babylonjs.com/#JCUGDF)

Both boxes are StandardMaterials with the same green “Tint” plugin, so both should be green. The left box also has the Colorify plugin from the material plugins docs page, attached but switched off (its default state). The on-screen text shows each box’s effect key, whether the two boxes share a program, the centre pixel of each box and the WebGL error state. One button chooses which material compiles first. An “Apply fix” button patches `_addPlugin` as suggested below and rebuilds both boxes.

**Measured** (WebGL2, Chrome 156, Intel Iris Xe on Linux; the same results on Babylon 9.31.0 and 9.32.0):

| Compiled first | Same program? | Left box (Colorify off + Tint) | Right box (Tint only) | WebGL error |
| --- | --- | --- | --- | --- |
| “Tint only” | yes | **rgb(0,0,0), black** | rgb(39,196,59), green | none |
| “Colorify + Tint” | yes | rgb(39,196,59), green | **not drawn** (background) | **GL\_INVALID\_OPERATION: uniform buffer too small** |
| either, with the fix | no | green | green | none |

Both keys are `MATERIALPLUGIN_n TINT`. With the fix, the left key gains Colorify’s marker, and both keys gain the built-in DetailMap plugin’s `MATERIALPLUGIN_1`.

Why black: the left material’s UBO holds `..., myColor, tintColor`. The program it borrowed was compiled for `..., tintColor`, so it reads `tintColor` from the slot where the left buffer holds `myColor`, which is zero because Colorify is off. If we write (1, 0.5, 0) into the left material’s `myColor` slot, the left box turns orange. On WebGPU the two boxes share one program too, but this particular scene happens to draw correctly there.

(Screenshots attached.)

**Even smaller repro** (console only):

```js
const scene = new BABYLON.Scene(engine);
class A extends BABYLON.MaterialPluginBase { constructor(m) { super(m, "A", 200, {}); } getClassName() { return "A"; } }
class B extends BABYLON.MaterialPluginBase { constructor(m) { super(m, "B", 300, {}); } getClassName() { return "B"; } }
const m = new BABYLON.StandardMaterial("m", scene);
new A(m); new B(m);
console.log(Object.keys(m.pluginManager._defineNamesFromPlugins).filter((k) => k.startsWith("MATERIALPLUGIN")));
// ["MATERIALPLUGIN_3"]: B's marker only; A's (MATERIALPLUGIN_2) and the built-in DetailMap's (MATERIALPLUGIN_1) are gone

```

**Where:** `materialPluginManager.pure.ts`, `_addPlugin`, unchanged on master:

```ts
const pluginClassName = plugin.getClassName();
if (!MaterialPluginManager._MaterialPluginClassToMainDefine[pluginClassName]) {
    MaterialPluginManager._MaterialPluginClassToMainDefine[pluginClassName] = "MATERIALPLUGIN_" + ++MaterialPluginManager._MaterialPluginCounter;
}
// ...
const defineNamesFromPlugins: { [name: string]: { type: string; default: any } } = {};
defineNamesFromPlugins[MaterialPluginManager._MaterialPluginClassToMainDefine[pluginClassName]] = { // only the plugin being added
    type: "boolean",
    default: true,
};

for (const plugin of this._plugins) { // every plugin's own defines are collected...
    plugin.collectDefines(defineNamesFromPlugins);
    this._collectPointNames("vertex", plugin.getCustomCode("vertex", this._material.shaderLanguage));
    this._collectPointNames("fragment", plugin.getCustomCode("fragment", this._material.shaderLanguage));
}

this._defineNamesFromPlugins = defineNamesFromPlugins; // ...but only one marker survives

```

Meanwhile the `PrepareUniformBuffer` handler adds the UBO uniforms of every attached plugin (`for (const plugin of this._plugins)`), whether it is enabled or not. So the UBO layout depends on the full plugin set, but the key only sees the last plugin.

**Suggested fix:** add every plugin’s marker inside the loop that already visits every plugin:

```ts
const defineNamesFromPlugins: { [name: string]: { type: string; default: any } } = {};

for (const plugin of this._plugins) {
    defineNamesFromPlugins[MaterialPluginManager._MaterialPluginClassToMainDefine[plugin.getClassName()]] = {
        type: "boolean",
        default: true,
    };
    plugin.collectDefines(defineNamesFromPlugins);
    this._collectPointNames("vertex", plugin.getCustomCode("vertex", this._material.shaderLanguage));
    this._collectPointNames("fragment", plugin.getCustomCode("fragment", this._material.shaderLanguage));
}

```

Every plugin in `_plugins` already has an entry in `_MaterialPluginClassToMainDefine`, because it went through `_addPlugin`. The only side effect we can see is that materials with plugins get a slightly longer key (for example, every StandardMaterial with a user plugin gains DetailMap’s marker), so they compile under a new key once.

Our workaround in the meantime: give each plugin a define whose default is `true`, e.g. `super(material, "Colorify", 200, { COLORIFY: false, COLORIFY_ATTACHED: true })`. This puts the plugin’s presence in the key even while it is disabled.

Happy to open a PR with this fix and a unit test if that helps. Thanks!

 ![image](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/4/9/49e0bfbe8f1404f4a5dce9cea486ea08833af119.png)

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [October 25, 2026, 3:15pm UTC](https://forum.babylonjs.com/t/material-plugins-only-the-last-added-plugins-materialplugin-n-define-reaches-the-effect-key-so-materials-with-different-plugin-sets-can-share-one-shader-program/64383/2 "2026-10-25T15:15:01Z")

</div>

Thanks for the detailed report and repro! Your analysis is correct: `_addPlugin()` rebuilds the define map each time, but only inserts the newly added plugin’s `MATERIALPLUGIN_n` marker. Different plugin sets can therefore end up sharing the same effect key.

Moving the marker insertion inside the loop over all registered plugins is the right fix. Including disabled plugins is important too, since they can still contribute to the uniform-buffer layout.

Please go ahead with a PR, and thanks for offering to contribute the fix!

---

<div class="post-metadata">

**Author:** ![ctabone](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/ctabone/32/52182_2.png) [@ctabone](https://forum.babylonjs.com/u/ctabone)\
**Post date:** [October 25, 2026, 7:24pm UTC](https://forum.babylonjs.com/t/material-plugins-only-the-last-added-plugins-materialplugin-n-define-reaches-the-effect-key-so-materials-with-different-plugin-sets-can-share-one-shader-program/64383/3 "2026-10-25T19:24:51Z")

</div>

Thanks for confirming this so quickly, @Evgeni_Popov, and for the pointer about the disabled plugins!

The PR is up: [Add the marker define of every material plugin, not only the last one by christabone · Pull Request #19053 · BabylonJS/Babylon.js · GitHub](https://github.com/BabylonJS/Babylon.js/pull/19053)

`_addPlugin()` now adds the `MATERIALPLUGIN_n` marker of every registered plugin, enabled or not, inside the loop that already collects each plugin’s defines. A new unit test checks that a material with two plugins gets both markers (plus the built-in DetailMap marker), and that two StandardMaterials whose plugin sets differ only by a disabled plugin get different defines and different effects. Both tests fail without the fix.
