# How to add a custom shader to the standard or PBR material shaders?

**URL:** <https://forum.babylonjs.com/t/how-to-add-a-custom-shader-to-the-standard-or-pbr-material-shaders/39526>\
**Category:** Questions\
**Tags:** material, shader\
**Created:** [April 1, 2023, 7:51am UTC](https://forum.babylonjs.com/t/how-to-add-a-custom-shader-to-the-standard-or-pbr-material-shaders/39526 "2023-04-01T07:51:10Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![heyefeng229](https://avatars.discourse-cdn.com/v4/letter/h/f04885/32.png) [@heyefeng229](https://forum.babylonjs.com/u/heyefeng229)\
**Post date:** [April 1, 2023, 7:51am UTC](https://forum.babylonjs.com/t/how-to-add-a-custom-shader-to-the-standard-or-pbr-material-shaders/39526/1 "2023-04-01T07:51:10Z")

</div>

I want to konw how to add the effect of HeapMap based on existing pbr material?  
Now I have completed the effect of HeapMap,but I don’t want to change the existing PBR effect of the model.

> **[Babylon.js Playground](https://playground.babylonjs.com/#1OH09K%231955)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

> **[Make the Best Use of the Forum | Babylon.js Documentation](https://doc.babylonjs.com/journey/forum)**
>
> Learn about the best place to get help, the Babylon.js forum.

---

<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:** [April 1, 2023, 10:29am UTC](https://forum.babylonjs.com/t/how-to-add-a-custom-shader-to-the-standard-or-pbr-material-shaders/39526/2 "2023-04-01T10:29:07Z")

</div>

You can create a material plugin for your code:

> **[Material Plugins | Babylon.js Documentation](https://doc.babylonjs.com/features/featuresDeepDive/materials/using/materialPlugins)**
>
> Create plugins to customize shaders of existing materials

---

<div class="post-metadata">

**Author:** ![heyefeng229](https://avatars.discourse-cdn.com/v4/letter/h/f04885/32.png) [@heyefeng229](https://forum.babylonjs.com/u/heyefeng229)\
**Post date:** [April 6, 2023, 8:27am UTC](https://forum.babylonjs.com/t/how-to-add-a-custom-shader-to-the-standard-or-pbr-material-shaders/39526/3 "2023-04-06T08:27:19Z")

</div>

Hello,according to the case you gave me, I have completed customization of PBR material with custom shader code.

However, I encountered a new difficulty. I want to add heat maps to the model where the pick occurred when the mouse clicks on the model ,but now heat maps appear at random places in the model after Material Plugins.

> **[Babylon.js Playground](https://playground.babylonjs.com/#22HT5Z%2370)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

When I use the custom material alone, the position of heat maps is correct,just appeared where I clicked.

> **[Babylon.js Playground](https://playground.babylonjs.com/#1OH09K%231955)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

---

<div class="post-metadata">

**Author:** ![heyefeng229](https://avatars.discourse-cdn.com/v4/letter/h/f04885/32.png) [@heyefeng229](https://forum.babylonjs.com/u/heyefeng229)\
**Post date:** [April 6, 2023, 8:34am UTC](https://forum.babylonjs.com/t/how-to-add-a-custom-shader-to-the-standard-or-pbr-material-shaders/39526/4 "2023-04-06T08:34:25Z")

</div>

I can’t find a case for transferring float arrays in Material Plugins. Did I make an error in the array processing?

var mPoints = new Float32Array(32 \* 3);  
HeatMapPluginMaterial.\_Marks=mPoints;

static \_Marks = new Float32Array(32 \* 3);  
{ name: “\_Marks”, size: 3 \* 32, type: “float[24]” },  
uniform float[3 \* 32] \_Marks;  
uniformBuffer.updateFloatArray(“\_Marks”, HeatMapPluginMaterial.\_Marks);

---

<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:** [April 6, 2023, 9:58am UTC](https://forum.babylonjs.com/t/how-to-add-a-custom-shader-to-the-standard-or-pbr-material-shaders/39526/5 "2023-04-06T09:58:59Z")

</div>

What you are doing seems to work, but it would be better to declare it as such in `getUniforms` :

```auto
    { name: "_Marks", size: 1, type: "float", arraySize: 96 },

```

If I click 3 or 4 times on the same spot, the circle appears there. Maybe there is a problem somewhere in the picking or updating of the uniforms?

You can see what is going on better with [Spector](https://spector.babylonjs.com/), as you will be able to see what values are being transferred for your uniforms, as well as update the shader code and see the result in real time.

---

<div class="post-metadata">

**Author:** ![heyefeng229](https://avatars.discourse-cdn.com/v4/letter/h/f04885/32.png) [@heyefeng229](https://forum.babylonjs.com/u/heyefeng229)\
**Post date:** [April 10, 2023, 6:33am UTC](https://forum.babylonjs.com/t/how-to-add-a-custom-shader-to-the-standard-or-pbr-material-shaders/39526/6 "2023-04-10T06:33:59Z")

</div>

I set three datas to be transmitted by clicking once, and the uniform printed out by Spector also displays three datas. However, in the actual calculation of shader, only one data is transmitted by clicking once, which leads to problems in clicking display.  
I don’t know how to solve this problem. I hope to get your help.

 ![image](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/4/d/4d57cf44b5cbba68eb33f201cd638dbc4f12970b.png)  
 ![image](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/1/0/10ce9d0dc0b2aeb2102b9bdb48596675d7f5fa58.png)  
 ![image](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/7/4/74937a61e7f76b911af25d9661890af989fe11fd.png)

> **[Babylon.js Playground](https://playground.babylonjs.com/#22HT5Z%2393)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

---

<div class="post-metadata">

**Author:** ![heyefeng229](https://avatars.discourse-cdn.com/v4/letter/h/f04885/32.png) [@heyefeng229](https://forum.babylonjs.com/u/heyefeng229)\
**Post date:** [April 10, 2023, 6:36am UTC](https://forum.babylonjs.com/t/how-to-add-a-custom-shader-to-the-standard-or-pbr-material-shaders/39526/7 "2023-04-10T06:36:51Z")

</div>

Follow this link:

> **[Babylon.js Playground](https://playground.babylonjs.com/#22HT5Z%2394)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

---

<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:** [April 10, 2023, 10:40am UTC](https://forum.babylonjs.com/t/how-to-add-a-custom-shader-to-the-standard-or-pbr-material-shaders/39526/8 "2023-04-10T10:40:24Z")

</div>

You should not mess with the `UniformBuffer._dynamic` property, it’s an internal property that is not meant to be modified by external code:

> **[Babylon.js Playground](https://playground.babylonjs.com/#22HT5Z%23101)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

---

<div class="post-metadata">

**Author:** ![heyefeng229](https://avatars.discourse-cdn.com/v4/letter/h/f04885/32.png) [@heyefeng229](https://forum.babylonjs.com/u/heyefeng229)\
**Post date:** [April 11, 2023, 5:11am UTC](https://forum.babylonjs.com/t/how-to-add-a-custom-shader-to-the-standard-or-pbr-material-shaders/39526/9 "2023-04-11T05:11:50Z")

</div>

It’s been very helpful.Thank you very much for your help.
