Setting texture with setTexture() on custom material after creation has no effect?

See this playground: https://playground.babylonjs.com/#1OH09K#1873

The code in setTimeout has no effect, but it does if done before the setTimeout.

How can I set the texture after the material is created? Do I need to mark it dirty somehow?

I learned that if I call shaderMaterial.setTexture(...) first then call setTexture async, it sets the texture. So Babylon is dropping uniforms on the ground unless they’re immediately populated(?).

My original question still stands, how can I set a texture async?

Additionally, not in the playground, in my personal usage, I’m creating a PBRMaterial, not a ShaderMaterial, as in, I’m doing:

const shaderMaterial = new BABYLON.PBRMaterial(pbrName, scene);

And here, shaderMaterial.setTexture() isn’t a function. So I’m not sure how to set a custom uniform at material creation time. Later, in my scene, when I do mesh.material.getEffect().setTexture() - it has no effect - which I think is related to my original question.

The textures has to be listed in the samplers arguments:

samplers: ["textureSampler"]

Fixed PG:

It’s not in the playground, but in my own project, I’m making a PBRMaterial, and hijacking the source code using customShaderNameResolve / processFinalCode. Neither the PBRMaterial constructor nor the customShaderNameResolve appear to have a samplers option. How can I call setTexture async on a PBRMaterial with custom uniforms in the source code?

From what I know depending on your case, the regular options would be

  1. CustomPBRMaterial
  1. Material Plugin
  1. Node Material

But @Evgeni_Popov and @sebavan should know better than me, maybe they can help out if and how you can customize shader in PBRMaterial.

1 Like

All what @Takemura said :slight_smile:

I’m looking at the custom PBR material solution, and I see they set up an _afterbind property - Babylon.js/pbrCustomMaterial.ts at 52d30c49fffd2d9bd446668eb0b61337f7c167aa · BabylonJS/Babylon.js · GitHub

Is this a magic/undocumented material property, and is this what I also want to hook into to set custom uniforms on a PBRMaterial?

I could use PBRCustomMaterial directly but my code currently uses PBRMaterial heavily, so I want to try to get it working with that first.

Adding @nasimiasl the daddy of the PBR material :slight_smile:

1 Like

_afterBind does not seem to work, setting the texture in there still leaves the mesh as black, and for some reason is called in a loop. https://playground.babylonjs.com/#1OH09K#1878

Whoops this was all my own misunderstanding, all I had to do was push to samplers in customShaderNameResolve, as in

    const shaderMaterial = new BABYLON.PBRMaterial(pbrName, scene);
    shaderMaterial.customShaderNameResolve = (
      shaderName,
      uniforms,
      uniformBuffers,
      samplers,
      defines,
      attributes,
      options
    ) => {
        samplers.push('myUniformName');
    }

Thanks for the resources everyone!

2 Likes