I am trying to tap into serialization to add my own behavior to GPUParticle system
https://www.babylonjs-playground.com/#PJDB1J#1
Open up console, call data = particleSystem.serialize();
– It works. Now try to create a new GPUParticleSystem (the subclassed system):
data = particleSystem.serialize();
newSystem = GPUParticleSystem.parse(data, particleSystem._scene)
Blows up, because BABYLON.GPUParticleSystem.Parse, is doing
var name = parsedParticleSystem.name;
var particleSystem = new GPUParticleSystem(name, { capacity: parsedParticleSystem.capacity, randomTextureSize: parsedParticleSystem.randomTextureSize }, scene);
The easy fix is:
var name = parsedParticleSystem.name;
var particleSystem = new this(name, { capacity: parsedParticleSystem.capacity, randomTextureSize: parsedParticleSystem.randomTextureSize }, scene);
Which should be done for really all the serializable objects, in order to allow people to subclass things.
Beyond that however: It would be nice to have an easy/simple api to hook into to extend serialization, i.e.
BabylonObject {
static Parse() {
//do parsing
if(this.additional_serializable_fields) { //parse these fields also }
}
}
– The other fix would be to separate “Parsing” from initializing the object altogether. I.e. parse just returns the javascript object, and instead of
Class.Parse()
to initialize,
Class.FromJSON = function(data) {
let result = this.Parse(data);
let instance = new this(result);
}
(or Class.Load, or whatever)
Which IMO would be the better way to handle it, however, would be a breaking API change – Reason I think it’s better way to handle is Parse implies you are parsing something, while in reality it is parsing and instantiating. Separating the behavior and adding a Load method would be more intuitive but more importantly, allow for subclassing. Additionally if every class defined the array of fields it needs to serialize in a static method, then that could be used in place of suggestion #2, as I could just call
FieldsToSerialize() {
[...super.FieldsToSerialize(), 'additional_field1', 'additional_field_2']
}
I only looked in a few other places but seems like it’s broken (or rather, isn’t currently possible without overwriting the serialization entirely) for basically all classes.