doNotSerialize for material plugins

Version: git master (d4c5968)
Engine: WebGL2

Background:

I’m making a material plugin and do not want it to be serialized, since it is designed to be attached at runtime by code.
In Material#_serializePlugins, which is called every time a material being serialized, it just serialize every plugin in _plugins.

Proposal:

Add a public doNotSerialize to MaterialPluginBase, and skip serialization when it’s true. Existing material plugins would implicitly have this field as they extends MaterialPluginBase.


    protected _serializePlugins(serializationObject: any) {
        serializationObject.plugins = {};

        if (this.pluginManager) {
            for (const plugin of this.pluginManager._plugins) {
                if (!plugin.doNotSerialize) // the proposed change
                    serializationObject.plugins[plugin.getClassName()] = plugin.serialize();
            }
        }
    }

Risks:

Material plugins that does not extend MaterialPluginBase will not have a doNotSerialize after this, but when using if to check, it would have the same result as false, which should be the default value, so backward compatibility for js projects would not be broken after this.
In typescript projects, type checking ensures that plugins extending MaterialPluginBase.

Alternatives

Users can iterate the serialized json for material plugin and remove what they do not want, but it’s complex to do that.
Js users can patch Material#_serializePlugins in prototype and make the change without upstreaming it.

I think it is an easy change that we should do. Wanna give a shot at a PR?

Why not simply implementing an empty serialize method in your plugin?

empty serialize can not be easily changed at runtime, and would emit a error at Material._ParsePlugins

You should return an object with a name property, not null:

[…] Ah, my bad, you don’t want the plugin to be serialized so that it’s not recreated at parse time! A new doNotSerialize property should do it indeed.

PR Here:

1 Like