# How to change textures of an imported mesh of an imported mesh on click

**URL:** <https://forum.babylonjs.com/t/how-to-change-textures-of-an-imported-mesh-of-an-imported-mesh-on-click/41498>\
**Category:** Questions\
**Created:** [June 9, 2023, 3:56am UTC](https://forum.babylonjs.com/t/how-to-change-textures-of-an-imported-mesh-of-an-imported-mesh-on-click/41498 "2023-06-09T03:56:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Akshat\_Agrawal](https://avatars.discourse-cdn.com/v4/letter/a/4af34b/32.png) [@Akshat\_Agrawal](https://forum.babylonjs.com/u/Akshat_Agrawal)\
**Post date:** [June 9, 2023, 3:56am UTC](https://forum.babylonjs.com/t/how-to-change-textures-of-an-imported-mesh-of-an-imported-mesh-on-click/41498/1 "2023-06-09T03:56:29Z")

</div>

So i’m trying to update the texels on click by the user. I know i can get the bu and bv coordinates from the pointerInfo. But i can’t figure out how do i place a new texel at that point in the texture. Ideally i would like to insert a image in the texture at that point.

---

<div class="post-metadata">

**Author:** ![david77](https://avatars.discourse-cdn.com/v4/letter/d/2bfe46/32.png) [@david77](https://forum.babylonjs.com/u/david77)\
**Post date:** [June 9, 2023, 6:40am UTC](https://forum.babylonjs.com/t/how-to-change-textures-of-an-imported-mesh-of-an-imported-mesh-on-click/41498/2 "2023-06-09T06:40:37Z")

</div>

To change textures of an imported mesh on click, you’ll need to implement a click event handler that detects interactions with the mesh. Upon click, you can programmatically switch the texture by accessing the material or shader associated with the mesh and updating the texture property. Make sure you have multiple textures available and cycle through them with each click. Refer to your platform’s documentation for specific implementation details.

---

<div class="post-metadata">

**Author:** ![Akshat\_Agrawal](https://avatars.discourse-cdn.com/v4/letter/a/4af34b/32.png) [@Akshat\_Agrawal](https://forum.babylonjs.com/u/Akshat_Agrawal)\
**Post date:** [June 9, 2023, 7:38am UTC](https://forum.babylonjs.com/t/how-to-change-textures-of-an-imported-mesh-of-an-imported-mesh-on-click/41498/3 "2023-06-09T07:38:46Z")

</div>

How do I access the shader programmatically. Also I do not want to replace the texture with another texture, I just want to update some region (small square shaped region) of the texture.

---

<div class="post-metadata">

**Author:** ![Joe\_Kerr](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/joe_kerr/32/39017_2.png) [@Joe\_Kerr](https://forum.babylonjs.com/u/Joe_Kerr)\
**Post date:** [June 9, 2023, 8:18am UTC](https://forum.babylonjs.com/t/how-to-change-textures-of-an-imported-mesh-of-an-imported-mesh-on-click/41498/4 "2023-06-09T08:18:42Z")

</div>

Wanted to get this to work myself. Couldnt find docs on Texture.getInternalTexture(). So I ended up converting the regular texture (ie Texture class) to a RawTexture. Like this

```auto
const buffer = await regularTexture.readPixels(); //raw RGBs <--- here!
//e.g.
    const [r,g,b,a] = this.getColorIndicesForCoord(x,y, width);
    buffer[r] = Math.floor( desiredCol.r * 255 );
    buffer[g] = Math.floor( desiredCol.g * 255 );
    buffer[b] = Math.floor( desiredCol.b * 255 );  

const format = BABYLON.Engine.TEXTUREFORMAT_RGBA;
const updatableTexture = new BABYLON.RawTexture(buffer, width, height, format);
//etc

```

Or use a DynamicTexture. Might make it easier copying sprites into it via the canvas context.

See also

- [Creating Raw RGB Textures | Babylon.js Documentation](https://doc.babylonjs.com/features/featuresDeepDive/materials/advanced/rawTexture)
- [Dynamic Textures | Babylon.js Documentation](https://doc.babylonjs.com/features/featuresDeepDive/materials/using/dynamicTexture)

---

<div class="post-metadata">

**Author:** ![Akshat\_Agrawal](https://avatars.discourse-cdn.com/v4/letter/a/4af34b/32.png) [@Akshat\_Agrawal](https://forum.babylonjs.com/u/Akshat_Agrawal)\
**Post date:** [June 11, 2023, 3:17am UTC](https://forum.babylonjs.com/t/how-to-change-textures-of-an-imported-mesh-of-an-imported-mesh-on-click/41498/5 "2023-06-11T03:17:20Z")

</div>

Thank You @Joe_Kerr. Using Dynamic Textures worked for my purpose 😃
