SpaceToast

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?

Share this post


Link to post
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.

Share this post


Link to post
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.
`;

 

Share this post


Link to post
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. 😔

Share this post


Link to post
Share on other sites

You just want a  custom uniform I got you.

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

for some reason the added uniforms are non-accessible as far as I can tell. as demonstrated by:
https://www.babylonjs-playground.com/#LRGH45#2

so what you need to do is bind the uniform  to a varying on the vertex Shader and pass it that to the fragment.
https://www.babylonjs-playground.com/#LRGH45#4
❤️

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.