StandardMaterial needs to be imported

Hi

I’m working on BabylonJS v 4.2 and I do not want to use StandardMaterial at all in my project. To limit bundle size. But I’m starting to have this error.

The surprise is that I need to import StandardMaterial to not use it at all cause All is done using NodeMaterial.

import '@babylonjs/core/Materials/standardMaterial';

This import sorts the problem out but I will not use this material at all.

Also, I can import it rebuild and after the error is gone, remove this import, and app works just fine for some time. And then it’s again starting to show up.

How to fix the fact that I will not use it on the Ground with a displacement and primitive meshes created with a MeshBuilder.

Refgards
Peter

Hi Peter,

this is explained here - Babylon.js ES6 support with Tree Shaking | Babylon.js Documentation , and is sadly part of the side effects of porting from UMD to es6.

The standard material is the scene’s default material, so by importing the scene you are actually using it.

But if I do not want to use it?

Can I remove it during the build? so have like lazy loading if it’s NODE_DEV=‘development’ and then removed it from the final build? Or will I have an error?

Regards
Peter

And also if I do not need it as it said here:

I’m not importing it fine. But I got an Error that stops my code from execution.

What to do with this?

Regards
Peter

1 Like

You will need to make sure all meshes have a material right after they are created (or pre-render), otherwise the standard material will be initialized. Technically you would need to make sure that defaultMaterial is never accessed.

OK. A gotcha moment.

This:

const box = MeshBuilder.CreateBox('Box',{size: 1}, scene);
box.position.x = 100;
box.position.y = 100;
box.position.z = 100;
[...]
box.material = material;

should be like this:

const box = MeshBuilder.CreateBox('Box',{size: 1}, scene);
box.material = material;
box.position.x = 100;
box.position.y = 100;
box.position.z = 100;
[...]

and the error is gone. Would be nice to put it in that FAQ URL for reference in the future for other lost souls :slight_smile:

Regards
Peter

1 Like