Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serialization of ParametricGeometry, ParametricBufferGeometry contains unserializable function #17381

Closed
jsantell opened this issue Aug 29, 2019 · 3 comments · Fixed by #17739 or #22559

Comments

@jsantell
Copy link
Contributor

Description of the problem

The parametric function in a ParametricBufferGeometry ultimately gets serialized as { func } on the geometry, and since it is a function, the object representation is no longer serializable, at least in terms of the web's structured clone algorithm. In the context of other serialization issues (#17328, #16816, #16764), maybe this issue would be better framed as, "What is the serialization strategy for three.js objects?"

@Mugen87
Copy link
Collaborator

Mugen87 commented Sep 4, 2019

"What is the serialization strategy for three.js objects?"

Some classes in three.js were developed without serialization/deserialization in mind. ParametricBufferGeometry is one of these for sure.

I guess we can solve this issue by implementing toJSON() in ParametricGeometry and ParametricBufferGeometry like so:

toJSON() {

    var data = super.toJSON();

    data.func = this.parameters.func.toString();

    return data;

}

Deserializing in ObjectLoader would look like so:

var func = new Function( 'return ' + data.func )();

We consider the parametric functions as self-contained (like ParametricGeometries.klein) so this approach should work.

Better suggestions?

@jsantell
Copy link
Contributor Author

jsantell commented Sep 4, 2019

@Mugen87 that should work if the function contains only local variables and globals (e.g. Math) like you mention; using a pure function is not a requirement of ParametricGeometry, so the unserialization could always fail, but at least serialization is possible. I'll check out a PR for this -- thanks!

@Mugen87
Copy link
Collaborator

Mugen87 commented Oct 24, 2019

As mentioned by @mrdoob in the linked PR, the proposed code is not safe enough since malicious code could be injected into 3D scenes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants