Skip to content

Adding full support for custom materials to GltfLoader#2725

Open
theMinka wants to merge 10 commits intojMonkeyEngine:masterfrom
theMinka:theMinka_Custom-material-support-in-GltfLoader
Open

Adding full support for custom materials to GltfLoader#2725
theMinka wants to merge 10 commits intojMonkeyEngine:masterfrom
theMinka:theMinka_Custom-material-support-in-GltfLoader

Conversation

@theMinka
Copy link
Copy Markdown
Contributor

Reworked the material creation process in GltfLoader, to allow for custom materials to be created.

Supporting a custom material definition

To support a new material definition, one simply needs to implement a new GltfMaterialFactory and register it with the GltfLoader. It is important to consider the overall order of all registered material factories, because only the first factory that accepts a given material data instance will be used. By default, there are already two material factories: one for the PBRLighting material and one for the Unshaded material.

One special use case would be when someone creates a copy of the standard PBRLighting material to add additional functionality. If none of the original material parameters have been changed (adding new ones is not a problem), one can simply extend PBRLightingMaterialFactory and modify the material definition path returned by the getMaterialDefPath() method. Finally, the original PBRLightingMaterialFactory just needs to be replaced in the GltfLoader with the custom one. This way, all loaded models will use the extended PBRLighting material.

Backward compatibility

As mentioned earlier, my new system is not backward compatible with regard to custom MaterialAdapters. Therefore, I have kept the old implementation but disabled it. To reactivate it, a specific flag must be set in the GltfModelKey. This will fully switch back to the old material creation process when loading the corresponding 3D model.

However, I believe that keeping the old MaterialAdapter system in the engine is not a good long-term solution. I suggest removing it in one or two future releases. To reflect this, I have already marked most classes, methods, and fields related to the old process as deprecated.

Forum discussion:
https://hub.jmonkeyengine.org/t/adding-full-support-for-custom-materials-to-gltfloader/49444

Also fixed some related bugs:

  • CustomContentManager can now handle multiple GLTF extensions per element
  • Wrong material parameter was set on unlit materials with vertex colors (UseVertexColor instead of VertexColor)
  • Materials using BlendMode.AlphaAdditive are now added to QueueBucket.Transparent

If multiple GLTF extensions are present on an element, the CustomContentManager now processes all supported ones instead of just the first one.
* Step 1: Collect all material data in the new GltfMaterialData object
* Step 1.1: GltfLoader reads all standard GLTF parameters
* Step 1.2: ExtensionLoader and ExtrasLoader can read additional GLTF parameters
* Step 2: Find a matching GltfMaterialFactory and use it to create the material
…ensionLoaders

* Retains the old MaterialAdapter handling for backward compatibility
…d material

* Added both material factories as the default ones to the GltfLoader
Reason: Setting default values directly in GltfMaterialData obscures whether a parameter is actually defined in the glTF material. The presence of material parameters may be relevant for some material factories. Therefore, default values should be set by the material factories themselves.
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a new material factory system for the GLTF loader, deprecating the legacy material adapter system. The new architecture uses GltfMaterialData and GltfMaterialFactory to provide a more extensible approach to material creation. Feedback was provided to correct a typo in the EMISSIV constant names within GltfMaterialData.java to improve code consistency.

Comment on lines +85 to +87
public static final String EMISSIV_TEXTURE_PARAM = "material.emissiveTexture";

public static final String EMISSIV_COLOR_PARAM = "material.emissiveFactor";
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

There appears to be a typo in the constant names. EMISSIV should likely be EMISSIVE. Correcting this would improve consistency and maintainability.

This change would also need to be propagated to GltfLoader.java, PBRLightingMaterialFactory.java, and UnshadedMaterialFactory.java where these constants are used.

Suggested change
public static final String EMISSIV_TEXTURE_PARAM = "material.emissiveTexture";
public static final String EMISSIV_COLOR_PARAM = "material.emissiveFactor";
public static final String EMISSIVE_TEXTURE_PARAM = "material.emissiveTexture";
public static final String EMISSIVE_COLOR_PARAM = "material.emissiveFactor";

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo was fixed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant