Node Material Editor - Custom Shader

Hey,

I’ve been using some code I found in a thread here.

Basically this.

BABYLON.Effect.IncludesShadersStore["lightFragment"] = BABYLON.Effect.IncludesShadersStore["lightFragment"].replace(
    "info.diffuse*=computeProjectionTextureDiffuseLighting(projectionLightSampler{X},textureProjectionMatrix{X});",
    `vec3 projCol = computeProjectionTextureDiffuseLighting(projectionLightSampler{X},textureProjectionMatrix{X});
    baseColor.rgb *= projCol;
    `
);

But now I need to make my material more complicated, and the standard shader is unlikely to work for what I’m trying to do. I’ve managed to create what I want in the Node Material Editor but I can’t get it to work with the code above.

Can I introduce a variable in the Node Material Editor that is editable in the light fragment?

I can’t find a way to amend the shader code that’s generated by the Node Material Editor. If I could work that out I think this is a pretty simple problem. (I could just add baseColor and apply it to the final function).

Thanks,
Chris

That’s not possible to do that, you can’t modify the ligthing inner computing from a node material.

I can’t see a way to do that, the “by hand” shader solution was already quite specific…

That would not work, the specific piece of shader code only works because baseColor is an existing variable in the standard material shader code, that is defined and filled before the lightFragment snippet is run.

The only possibility I can see is to pass a function to the processFinalCode parameter of the createEffect call, so that you can tamper with the shader code before it is compiled. However, the NodeMaterial class does not support setting this processFinalCode parameter, so you would need to overwrite it:

https://playground.babylonjs.com/#RCQVHG

See the customProcessFinalCode function in the snippet. You can change the code in any way you like and return the modified code.

1 Like

I had absolutely no idea you could do that. Thanks so much for replying.

I’ve just been messing around with it and I can make it do exactly what I need now.

Thanks again!
Chris.