Set parameters as texture or color in NodeMaterial via code


I want to change NodeMaterial texture or any value.
As I understood, it has to be done via NodeMaterial Block

I seen many examples where it is done like this:

block.texture = ...
block.value = ...

However, I cannot acces these values in my code, it says:

Property 'texture' does not exist on type 'NodeMaterialBlock'

Here is my code:

                    BABYLON.NodeMaterial.ParseFromFileAsync("lightmappedgeneric", "/master_materials/nodeMaterial.json", scene).then((nodeMaterial) => {
                        lightmappedgeneric = nodeMaterial;

                    var cloneMat: BABYLON.NodeMaterial = lightmappedgeneric.clone("CloneOfMaster");
                    var block = cloneMat.getBlockByPredicate((b) => === "diffuseTex");
                        block.texture = orgTex;

It works here:

ParseFromFileAsync is an async function so you need to await it or move the using code in the callback.

Thanks. I don’t think that is the problem.

You need to also cast the block to the correct block type in typescript to be sure the texture property is present as well as the value in the other cases.

1 Like

Thank you, that worked.

var property = nodeMaterial.getBlockByName("DiffuseTexture");
var textBlock = property as BABYLON.TextureBlock;

What about color, vector and float blocks, how do I access and modify them?
I tried this:

let blockInShader = cloneMat.getInputBlockByPredicate((b) => === matchedBlockName);
let colorBlock = blockInShader as BABYLON.Color4;

Second line complains:

Conversion of type ‘InputBlock’ to type ‘Color4’ may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to ‘unknown’ first.
Type ‘InputBlock’ is missing the following properties from type ‘Color4’: r, g, b, a, and 24 more
Then I add as suggested:
let colorBlock = blockInShader as unknown as BABYLON.Color4;

But I don’t think it is correct. Later I set the color via:

colorBlock = new BABYLON.Color4(1,0,0,0);

It does not work, but no errors either.

Found out:

let blockInShader = cloneMat.getInputBlockByPredicate((b) => === matchedBlockName);
if (blockInShader) {
   blockInShader.value = new BABYLON.Color3(1, 0, 0); 
1 Like