Obtaining diffuseTexture in Typescript

In Javascript I can do

if(mesh.material.diffuseTexture) {
     mesh.material.diffuseTexture.onLoadObservable.add(function () {
         console.log(mesh.material.diffuseTexture.readPixels())
     })
}

as in lines 21 to 25 of
https://www.babylonjs-playground.com/#Z537PR

However adding code in babylon.js cloned repo in typescript, Visual Studio Code gives an error with

diffuseTexture is not a property of material

I tried surround it with

if ('diffuseTexture' in mesh.material) {
     
}

but still get the same message.

This works in typescript

if(mesh.material.getActiveTextures().length > 0) {
        mesh.material.getActiveTextures()[0].onLoadObservable.add(function () {
            console.log(mesh.material.getActiveTextures()[0].readPixels())
   })
}

but cannot know if texture with index 0 is a diffuse texture or not.

Can I override typescripts objections to diffuseTexture not being a property of material? If so how?

EDIT Also tried

if ('diffuseTexture' in mesh.material) {
      var  a = mesh.material['diffuseTexture'].readPixels();
}

message is _ Property 'readPixels' does not exist on type 'never'
1 Like

You have to cast mesh.material to StandardMaterial:

https://www.babylonjs-playground.com/ts.html#0XC1MH

2 Likes

@Adam Thank you that works :slightly_smiling_face:

1 Like