Use custom material for loaded models

Hi,

What would be the best way to make the loaded geometries use my own material?

In my game I load textured models from .obj files provided by user. The OBJLoader by default create materials based on Lighting.j3md definition.
I’m creating my custom extension of Lighting.j3md (it will contain all the parameters of Lighting.j3md but I want to introduce some modifications into the fragment shaders used by this material definition).

I can probably iterate all geometries, and replace their materials with instances of my material class, and then copy all the values from materials created by OBJLoader. I believe such approach was taken by someone succesfully here [Solved] How to change a Material's MatDef with code? (although I’m not yet sure what is the render-state, so I’m kinda reluctant, until I find out :wink: )

The MTLLoader class doesn’t seem to provide any nice way for me to define which Material class I want to use, but I could extend this class and override one method to fix that. However, I’m not sure how to alter the default behaviour of AssetManager so that for all .mtl files it is my custom MTLLoader derived class that loads them? Is there a good way to do it?

In the future I might also want to do similar thing for .gltf models, which use yet another material by default.

Any advice how to proceed? :slight_smile:

cheers, Elg’

The usual way is to use a scene graph visitor and just replace the materials on the relevant geometries. Bonus: it works with every format.

Even if you were using a more formal asset pipeline with .j3o files, that’s usually the way… which is one of the reasons why JMEC supports running groovy scripts against the models after load.

The additional render state is what holds things like blend mode, face cull mode, etc… and so depending on the model it might be useful to copy those settings to the new material.

I guess I will replace materials after load then.
Thanks :slight_smile:

Normally games bundle the j3o that is already setup. The models are more compact, load quicker, etc…

So this only comes up for games that (for whatever reason, good or bad) insist on loading the bulkier and slower formats at runtime.

I see. I cannot distribute the models. I’m creating a remake of an old game. It will load models from folder pointed by the user (where the original game installation resides).
I may implement some caching mechanism in the future, so that once the files are loaded for the first time I will store them as .j3o somewhere to be used at next executions. In any case, I must be able to load the models in their original format at least once.

3 Likes