Obtaining diffuseTexture in Typescript

In Javascript I can do

if(mesh.material.diffuseTexture) {
     mesh.material.diffuseTexture.onLoadObservable.add(function () {

as in lines 21 to 25 of

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 () {

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:



@Adam Thank you that works :slightly_smiling_face:

1 Like