Jump to content

Ideas: Best Way to Extend the StandardMaterial?


Recommended Posts

I'm picking up an older project again, and moving it to the current version of BJS. This is a pain, though, because I'd been extending the StandardMaterial shader within a copy of the BJS framework itself. The project made extensive use of noise and shape functions to simulate textures within the GPU by altering the diffuse, specular and normal values of the StandardMaterial, based on a flag hacked into the framework.

The advantage of this method vs. ShaderMaterial was getting unlimited resolution textures at no bandwidth cost without having to reimplement all the goodies in the StandardMaterial--SSAO2, fog, shadows, etc. The disadvantage: Lack of portability, and having to find a way to re-minify everything myself before deployment. (My kingdom for uglify.js to support the `` multiline literal...)

Before I start migrating my hacks, I wanted to ask this of the smart people around the water cooler: Can anyone suggest a more elegent way to do this, without modifying BJS itself?

Link to comment
Share on other sites

Thanks @Pryme8, @Sebavan and @NasimiAsl.

CustomMaterial looks like exactly what I need. Unfortunately, it doesn't seem to be production-ready just yet, based on the other forum threads.

I could see saving a bit of trouble by replacing the StandardMaterial object with a custom version at runtime--one containing my changes. I'm not really familiar with JavaScript modules, however: They seem to be designed specifically to prevent code from tinkering around inside their scope.

Link to comment
Share on other sites

I was 100% wrong about that last comment. Replacing the StandardMaterial's shaders is as simple as:

BABYLON.Effect.ShadersStore["defaultVertexShader"] = `
   //Copy the StandardMaterial's vertex shader code in here, and modify away.
   //Template literals are your friend.
BABYLON.Effect.ShadersStore["defaultPixelShader"] = `
   //Copy the StandardMaterial's pixel shader code in here, and modify away.


Link to comment
Share on other sites

On 8/13/2018 at 8:17 AM, Sebavan said:

Yup this would work as well, the only drawback is that you will apply the change to all the standard materials.

That's not a drawback in this case. Unfortunately, after 6 hours trying to understand the source code, I'm no closer to figuring how to inject a new uniform into the StandardMaterial object at runtime. It's just too amazingly complicated.

Afraid I'll have to go back to my previous method. ?

Link to comment
Share on other sites

You just want a  custom uniform I got you.


for some reason the added uniforms are non-accessible as far as I can tell. as demonstrated by:

so what you need to do is bind the uniform  to a varying on the vertex Shader and pass it that to the fragment.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...