Saving, Loading and Manipulating.
Improved the function of saving the scene. The scene file created in the TertiusAxis editor has the extension “.trxs”. Now it is a text file with data in the form of an array with elements in the JSON format, which are scene objects. Only objects created by the user are saved. To do this, the elements of the children array of the scene are checked in which userData = true.
function saveSceneToDisk () { // Savig scene, created in TertiusAxis let ta_entities = new TA_Entities(); if ( ta_scene.currentSelection.object ) { ta_entities.removeSelection(ta_scene.currentSelection); } let children = ta_scene.scene.children; let elemToExport = []; children.forEach( element => { if ( element.userData.createdByUser ) { elemToExport.push( element.toJSON() ); if ( element.userData.selectable ){ ta_scene.selectableObjects.push( element ); } } }); let blob = new Blob([JSON.stringify( elemToExport, null, 2)], {type: 'text/plain'}); saveFile( blob , 'Scene', 'trxs' ); }
By the same principle, the scene was cleared in the TA_Scene module:
clearScene(){ let taEntities = new TA_Entities(); if (this.currentSelection.object) { this.transformControls.detach(this.currentSelection.object); taEntities.removeSelection(this.currentSelection); } let children = this.scene.children; let elementsToRemove = []; children.forEach( element => { if ( element.userData.createdByUser ) { elementsToRemove.push( element ); this.selectableObjects.splice( this.selectableObjects.indexOf( element )); } }); this.scene.remove( ...elementsToRemove ); }
Saving a file is a separate function
function saveFile( blob, name, fileExtention ){ let fileName = name + '.' + fileExtention; let link = document.createElement('a'); link.download = fileName; if (window.navigator && window.navigator.msSaveOrOpenBlob) { window.navigator.msSaveOrOpenBlob( blob, fileName ); } else { link.href = URL.createObjectURL( blob ); link.click(); URL.revokeObjectURL( link.href ); } }
Added function “Merge with scene from disk”, which adds objects from another scene to an existing scene.
function loadSceneFromDisk( e ) { // Loading scene, created in TertiusAxis ta_scene.clearScene(); loadScene( e ); } function mergeScenes ( e ) { // Merge scenes, created in TertiusAxis loadScene( e ); } function clearScene () { if( confirm("Al objects will be deleted. Are you shure?")){ ta_scene.clearScene(); } } function loadScene( e ){ let file = e.srcElement.files[0]; if( !file.name.endsWith( '.trxs') ){ alert( 'File is not a TertiusAxis Scene'); return; } let reader = new FileReader(); reader.readAsText(file); reader.onload = function() { let loader = new THREE.ObjectLoader(); let loadedObjectsJSON = JSON.parse( reader.result); loadedObjectsJSON.forEach( element => { let loadedObject = loader.parse( element); ta_scene.scene.add( loadedObject ); if ( loadedObject.userData.selectable ){ ta_scene.selectableObjects.push( loadedObject ); } }); }; reader.onerror = function() { alert(reader.error); }; }
When loading a scene, only the “.trsx” file extension is checked, but in the future, when the file format changes, the file structure will be checked.
The whole scene can be exported to GLtf format
function exportGLTF(){ let gltfExporter = new GLTFExporter(); gltfExporter.parse( ta_scene.scene, function ( result ) { let gltf = JSON.stringify( result, null, 2 ); let blob = new Blob( [gltf], {type: 'text/plain'}); saveFile( blob , 'Scene', 'gltf' ); } ); }
Implemented the functionality of the object manipulation buttons: move, rotate, scale and drag, by connecting the appropriate modules of the three.js library.
Cloning of an object is implemented by pressing the “c” key, as well as by selecting the corresponding menu item on the Edit tab. At the same time, the geometry of the clones is independent, and the material is the same, that is, changing the color of one object – the color of everyone changes.
cloneObject( ta_scene ) { let copiedObjectID = ta_scene.currentSelection.object.id; this.removeSelection( ta_scene.currentSelection ); let copiedObject = ta_scene.scene.getObjectById( copiedObjectID ); let newObject = copiedObject.clone( false ); ta_scene.selectableObjects.push( newObject ); ta_scene.scene.add( newObject ); this.selectEntity( copiedObject, ta_scene.currentSelection ); }
TODO:
- Make selection of several objects and their manipulation, as well as cloning of all selected objects. To do this, create a multiselect: new Group () field in the currentSelection object and add the selected objects to the group.
- implement or use the existing library for monitoring application states.