index.html页面中
editor.storage.init( function () {
editor.storage.get( function ( state ) {
if (isLoadingFromHash) return;
if (state !== undefined) {
editor.fromJSON( state );
}
var selected = editor.config.getKey( 'selected' );
if (selected !== undefined) {
editor.selectByUuid( selected );
}
} );
//
var timeout;
function saveState( scene ) {
if (editor.config.getKey( 'autosave' ) === false) {
return;
}
clearTimeout( timeout );
timeout = setTimeout( function () {
editor.signals.savingStarted.dispatch();
timeout = setTimeout( function () {
editor.storage.set( editor.toJSON() );
editor.signals.savingFinished.dispatch();
}, 100 );
}, 1000 );
};
var signals = editor.signals;
signals.geometryChanged.add( saveState );
signals.objectAdded.add( saveState );
signals.objectChanged.add( saveState );
signals.objectRemoved.add( saveState );
signals.materialChanged.add( saveState );
signals.sceneBackgroundChanged.add( saveState );
signals.sceneFogChanged.add( saveState );
signals.sceneGraphChanged.add( saveState );
signals.scriptChanged.add( saveState );
signals.historyChanged.add( saveState );
signals.showModal.add( function ( content ) {
modal.show( content );
} );
} );
eidtor原型中:
fromJSON: function ( json ) {
var loader = new THREE.ObjectLoader();
// backwards
if (json.scene === undefined) {
this.setScene( loader.parse( json ) );
return;
}
var camera = loader.parse( json.camera );
this.camera.copy( camera );
this.camera.aspect = this.DEFAULT_CAMERA.aspect;
this.camera.updateProjectionMatrix();
this.history.fromJSON( json.history );
this.scripts = json.scripts;
this.setScene( loader.parse( json.scene ) );
},
setScene: function ( scene ) {
this.scene.uuid = scene.uuid;
this.scene.name = scene.name;
if (scene.background !== null) this.scene.background = scene.background.clone();
if (scene.fog !== null) this.scene.fog = scene.fog.clone();
this.scene.userData = JSON.parse( JSON.stringify( scene.userData ) );
// avoid render per object
this.signals.sceneGraphChanged.active = false;
var hasCamern = false;
while (scene.children.length > 0) {
if (! hasCamern && scene.children[0].type === "PerspectiveCamera") {
hasCamern = true;
this.camera = scene.children[0];
}
this.addObject( scene.children[0] );
}
if (! hasCamern) {
this.execute( new AddObjectCommand( this.camera ) );
}
this.signals.sceneGraphChanged.active = true;
this.signals.sceneGraphChanged.dispatch();
},
toJSON: function () {
// scripts clean up
var scene = this.scene;
var scripts = this.scripts;
for (var key in scripts) {
var script = scripts[key];
if (script.length === 0 || scene.getObjectByProperty( 'uuid', key ) === undefined) {
delete scripts[key];
}
}
//
return {
metadata: {},
project: {
gammaInput: this.config.getKey( 'project/renderer/gammaInput' ),
gammaOutput: this.config.getKey( 'project/renderer/gammaOutput' ),
shadows: this.config.getKey( 'project/renderer/shadows' ),
vr: this.config.getKey( 'project/vr' )
},
camera: this.camera.toJSON(),
scene: this.scene.toJSON(),
scripts: this.scripts,
history: this.history.toJSON()
};
},
FileLoader
FR:徐海涛(hunk Xu)
QQ技术交流群:386476712