Save/Load Scene

0

I made a draft of the function of saving the scene to disk and opening the scene from the file.

function saveSceneOnDisk () {

	let link = document.createElement('a');
	link.download = 'Scene.txt';

	let blob = new Blob([JSON.stringify(ta_scene.scene.toJSON(), null, 2)], {type: 'text/plain'});

	link.href = URL.createObjectURL(blob);

	link.click();

	URL.revokeObjectURL(link.href);

}
loadFromDiskLabel.addEventListener( 'change', function loadSceneFromDisk( e ){

		let file = e.srcElement.files[0];

		let reader = new FileReader();

		reader.readAsText(file);

		reader.onload = function() {
		console.log( ta_scene.scene.children );

		let loader = new THREE.ObjectLoader();

		let loadedScene = loader.parse( JSON.parse( reader.result ) );

		let children = loadedScene.children;

		let elemToImport = [];

		children.forEach( element => {

			if ( element.userData.createdByUser ) {

				elemToImport.push( element );
				ta_scene.selectableObjects.push( element );

				Object.assign( ta_scene.scene.children, elemToImport );

			}

		});

		reader.onerror = function() {
			alert(reader.error);
		  };

};

Now only objects created by the user are loaded. To do this, I added the UserData to the object:
this.currentEntity.userData = {createdByUser: true, selectable: true};

TODO: There are errors in the console and many questions need to be worked out, such as clearing the scene before loading, loading cameras and setting the camera position, light sources, deselecting before saving …

0