Tweaking the StandardMaterial shader

I am exploring shaders and how to tweak existing materials like StandardMaterial. There are multiple options:

  • NodeMaterial: there is a large node material which is pretty close to StandardMaterial. But it seems almost too large to edit in the graphical editor (on a notebook) and is very verbose in code.
  • ShaderMaterial: is well documented, and seems capable to support anything. Is there an example which aims at StandardMaterial (which would require additional uniforms), also see Shadows on a ShaderMaterial?
  • CustomMaterial: is really a way to customize StandardMaterial: CustomMaterial Samples, is main documentation; seems a bit unsupported but would fit well.
  • derive from SimpleMaterial.ts or similar: requires translation/bundling, not really tweaking but complete definition

Are there other options ? I thought I have seen injection of shader code before shader compilation into existing shaders ?

My planned experiment is to flip the normal so it is always pointing towards the camera, probably in the vertex shader. Probably by just checking the sign of normal.z in view space. That could be a cheap substitute for DOUBLESIDE if the back has the same appearance as the front.

CustomMaterial seems to have a hook in the right place for that. I may try that in a PG.

Edit: I found material.twoSidedLighting which sounds like the option I was looking for. Interestingly, I found it by looking at the NodeMaterial which suggests that tweaking the NodeMaterial would be the way to go (on a larger screen) after all.

Any feedback or comment very welcome.

Hi andreasplesch,

Babylon is committed to backward compatibility, so I believe all these things are fair to use. That said, we recently have been moving away from StandardMaterial (which is older) and toward NodeMaterial and PBRMaterial for new development. NodeMaterial in particular is very powerful and flexible. It is definitely designed with NME in mind, but you can also use it with code alone if you’d rather not use the editor. Hope this helps, and best of luck!

2 Likes

Are there other options ? I thought I have seen injection of shader code before shader compilation into existing shaders ?

A PR is in the works to allow exactly this :slight_smile: https://github.com/BabylonJS/Babylon.js/pull/11410

6 Likes

You can also extend the StandardMaterial, then add/change/remove uniforms and attributes, as well as supplying your own shader, like I’ve done in the past:
https://playground.babylonjs.com/#NTCAMV#2

1 Like

Yes @brunobg is killing it honestly! That will be huge!

4 Likes

https://www.babylonjs-playground.com/#EZPPH7

i have shaderBuilder more than 6 years in babylon and still people look them as complicated
but that old system support any request because that “IS exact shader code” and perpared that for making shader Code in javascript

what i think after 6 years

** i think the Node material is good and enough for people don’t wanna work on code

** custommaterial can be fix some limitation of standard material and PBR material
they still useful i don’t understand when we say that is not useful but don’t explain how

** Shader Material is the best choice for advance users at all

:slight_smile: ** i always can make better performance in ShaderBuilder

3 Likes

Thank you for the rich response. Clearly, that topic strikes a nerve and it is great that there are so many options !

1 Like