shinzou
shinzou

Reputation: 6232

Save and load files with three.js

Is there a way to save and load stringified objects with three.js?

I found this: https://github.com/josdirksen/learning-threejs/blob/master/chapter-08/03-load-save-json-object.html

but they use localstorage to save and load, which won't work between sessions or different computers.

Is there a way to load files just like the model is loaded? This should be like loading data files for a game.

I run the webgl client with Autodesk viewer locally with http-server.

Upvotes: 0

Views: 2169

Answers (2)

Augusto Goncalves
Augusto Goncalves

Reputation: 8604

Adding to Martin's answer, the Autodesk Viewer uses files translated and hosted by Model Derivative API. It's possible to show multiple files into the same scene. The Viewer is read-only. There is a getState and loadState functions to get the objects that represents the current zoom/explode/view information, and that can be serialized and stored somewhere.

There are some samples showing how to move a geometry on the model, for instance, move the geometry of a wall (from a building model). But that is not persistent, meaning you need to implement a JavaScript (client) + back-end infrastructure to save and restore those transformations.

Upvotes: 1

Martin Schuhfuß
Martin Schuhfuß

Reputation: 6986

If the Object can be written to localstorage it can just as well be exported as a file. You can send them to a server and store them there (maybe something like firebase would be useful here), or you can intiate a "download" directly from the browser. This is explained in Create a file in memory for user to download, not through server.

For loading a file, you can use the file-api, which is shown here: How to open a local disk file with Javascript?.

You just need to replace the localstorage-parts in your example accordingly.

Upvotes: 1

Related Questions