Editor.js 8.9 KB
Newer Older
M
Mr.doob 已提交
1 2 3 4
/**
 * @author mrdoob / http://mrdoob.com/
 */

5 6 7 8 9 10
var Editor = function () {

	var SIGNALS = signals;

	this.signals = {

11 12 13 14
		// script

		editScript: new SIGNALS.Signal(),

M
Mr.doob 已提交
15 16 17 18 19
		// player

		startPlayer: new SIGNALS.Signal(),
		stopPlayer: new SIGNALS.Signal(),

20 21
		// actions

22
		// showDialog: new SIGNALS.Signal(),
23

24 25
		// notifications

26 27
		editorCleared: new SIGNALS.Signal(),

M
Mr.doob 已提交
28 29 30
		savingStarted: new SIGNALS.Signal(),
		savingFinished: new SIGNALS.Signal(),

31 32
		themeChanged: new SIGNALS.Signal(),

33 34
		transformModeChanged: new SIGNALS.Signal(),
		snapChanged: new SIGNALS.Signal(),
35
		spaceChanged: new SIGNALS.Signal(),
36
		rendererChanged: new SIGNALS.Signal(),
M
Mr.doob 已提交
37

38 39
		sceneGraphChanged: new SIGNALS.Signal(),

40 41
		cameraChanged: new SIGNALS.Signal(),

M
Mr.doob 已提交
42 43
		geometryChanged: new SIGNALS.Signal(),

M
Mr.doob 已提交
44
		objectSelected: new SIGNALS.Signal(),
45 46
		objectFocused: new SIGNALS.Signal(),

47 48
		objectAdded: new SIGNALS.Signal(),
		objectChanged: new SIGNALS.Signal(),
M
Mr.doob 已提交
49
		objectRemoved: new SIGNALS.Signal(),
M
Mr.doob 已提交
50 51

		helperAdded: new SIGNALS.Signal(),
M
Mr.doob 已提交
52
		helperRemoved: new SIGNALS.Signal(),
M
Mr.doob 已提交
53

54
		materialChanged: new SIGNALS.Signal(),
M
Mr.doob 已提交
55

56
		scriptAdded: new SIGNALS.Signal(),
M
Mr.doob 已提交
57
		scriptChanged: new SIGNALS.Signal(),
58
		scriptRemoved: new SIGNALS.Signal(),
M
Mr.doob 已提交
59

60 61 62
		fogTypeChanged: new SIGNALS.Signal(),
		fogColorChanged: new SIGNALS.Signal(),
		fogParametersChanged: new SIGNALS.Signal(),
C
Chris Jubb 已提交
63 64
		windowResize: new SIGNALS.Signal(),

65 66 67 68
		showGridChanged: new SIGNALS.Signal(),
		updateSidebar: new SIGNALS.Signal(),
		historyChanged: new SIGNALS.Signal(),
		refreshScriptEditor: new SIGNALS.Signal()
69 70

	};
M
Mr.doob 已提交
71

M
Mr.doob 已提交
72
	this.config = new Config();
73
	this.history = new History( this );
74
	this.storage = new Storage();
75 76
	this.loader = new Loader( this );

77
	this.camera = new THREE.PerspectiveCamera( 50, 1, 1, 100000 );
M
Mr.doob 已提交
78 79
	this.camera.position.set( 500, 250, 500 );
	this.camera.lookAt( new THREE.Vector3() );
80 81
	this.camera.name = 'Camera';

82
	this.scene = new THREE.Scene();
83
	this.scene.name = 'Scene';
M
Mr.doob 已提交
84

85
	this.sceneHelpers = new THREE.Scene();
86 87 88 89 90

	this.object = {};
	this.geometries = {};
	this.materials = {};
	this.textures = {};
M
Mr.doob 已提交
91
	this.scripts = {};
92

93 94 95
	this.selected = null;
	this.helpers = {};

96 97 98 99
};

Editor.prototype = {

100 101 102 103 104 105 106 107
	setTheme: function ( value ) {

		document.getElementById( 'theme' ).href = value;

		this.signals.themeChanged.dispatch( value );

	},

M
Mr.doob 已提交
108
	/*
109
	showDialog: function ( value ) {
M
Mr.doob 已提交
110

111
		this.signals.showDialog.dispatch( value );
M
Mr.doob 已提交
112

113
	},
M
Mr.doob 已提交
114
	*/
M
Mr.doob 已提交
115

116 117
	//

118 119
	setScene: function ( scene ) {

M
Mr.doob 已提交
120
		this.scene.uuid = scene.uuid;
121 122 123
		this.scene.name = scene.name;
		this.scene.userData = JSON.parse( JSON.stringify( scene.userData ) );

124 125 126 127
		// avoid render per object

		this.signals.sceneGraphChanged.active = false;

128 129 130 131 132 133
		while ( scene.children.length > 0 ) {

			this.addObject( scene.children[ 0 ] );

		}

134 135 136
		this.signals.sceneGraphChanged.active = true;
		this.signals.sceneGraphChanged.dispatch();

137 138 139 140
	},

	//

141 142
	addObject: function ( object ) {

143 144 145 146
		var scope = this;

		object.traverse( function ( child ) {

147 148 149
			if ( child.geometry !== undefined ) scope.addGeometry( child.geometry );
			if ( child.material !== undefined ) scope.addMaterial( child.material );

150 151 152 153
			scope.addHelper( child );

		} );

154 155 156
		this.scene.add( object );

		this.signals.objectAdded.dispatch( object );
157
		this.signals.sceneGraphChanged.dispatch();
158 159 160

	},

161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
	moveObject: function ( object, parent, before ) {

		if ( parent === undefined ) {

			parent = this.scene;

		}

		parent.add( object );

		// sort children array

		if ( before !== undefined ) {

			var index = parent.children.indexOf( before );
			parent.children.splice( index, 0, object );
			parent.children.pop();

		}

		this.signals.sceneGraphChanged.dispatch();

	},

185
	nameObject: function ( object, name ) {
186 187 188 189 190 191

		object.name = name;
		this.signals.sceneGraphChanged.dispatch();

	},

192 193
	removeObject: function ( object ) {

194
		if ( object.parent === null ) return; // avoid deleting the camera or scene
195

196 197 198 199 200 201 202 203
		var scope = this;

		object.traverse( function ( child ) {

			scope.removeHelper( child );

		} );

204
		object.parent.remove( object );
M
Mr.doob 已提交
205 206

		this.signals.objectRemoved.dispatch( object );
207
		this.signals.sceneGraphChanged.dispatch();
208 209 210

	},

211
	addGeometry: function ( geometry ) {
212

213
		this.geometries[ geometry.uuid ] = geometry;
214 215 216

	},

217 218 219 220 221 222 223
	setGeometryName: function ( geometry, name ) {

		geometry.name = name;
		this.signals.sceneGraphChanged.dispatch();

	},

224 225
	addMaterial: function ( material ) {

226
		this.materials[ material.uuid ] = material;
227 228 229

	},

230 231 232 233 234 235 236
	setMaterialName: function ( material, name ) {

		material.name = name;
		this.signals.sceneGraphChanged.dispatch();

	},

237 238
	addTexture: function ( texture ) {

239
		this.textures[ texture.uuid ] = texture;
240 241 242

	},

243 244
	//

M
Mr.doob 已提交
245
	addHelper: function () {
246

247
		var geometry = new THREE.SphereBufferGeometry( 20, 4, 2 );
M
Mr.doob 已提交
248
		var material = new THREE.MeshBasicMaterial( { color: 0xff0000, visible: false } );
249

M
Mr.doob 已提交
250
		return function ( object ) {
M
Mr.doob 已提交
251

M
Mr.doob 已提交
252
			var helper;
253

M
Mr.doob 已提交
254
			if ( object instanceof THREE.Camera ) {
255

M
Mr.doob 已提交
256
				helper = new THREE.CameraHelper( object, 10 );
257 258

			} else if ( object instanceof THREE.PointLight ) {
259

M
Mr.doob 已提交
260
				helper = new THREE.PointLightHelper( object, 10 );
261

M
Mr.doob 已提交
262
			} else if ( object instanceof THREE.DirectionalLight ) {
263

M
Mr.doob 已提交
264
				helper = new THREE.DirectionalLightHelper( object, 20 );
M
Mr.doob 已提交
265

M
Mr.doob 已提交
266
			} else if ( object instanceof THREE.SpotLight ) {
267

M
Mr.doob 已提交
268
				helper = new THREE.SpotLightHelper( object, 10 );
M
Mr.doob 已提交
269

M
Mr.doob 已提交
270
			} else if ( object instanceof THREE.HemisphereLight ) {
M
Mr.doob 已提交
271

M
Mr.doob 已提交
272
				helper = new THREE.HemisphereLightHelper( object, 10 );
M
Mr.doob 已提交
273

M
Mr.doob 已提交
274 275 276 277
			} else if ( object instanceof THREE.SkinnedMesh ) {

				helper = new THREE.SkeletonHelper( object );

M
Mr.doob 已提交
278
			} else {
M
Mr.doob 已提交
279

M
Mr.doob 已提交
280 281
				// no helper for this object type
				return;
M
Mr.doob 已提交
282

M
Mr.doob 已提交
283
			}
M
Mr.doob 已提交
284

M
Mr.doob 已提交
285 286 287 288
			var picker = new THREE.Mesh( geometry, material );
			picker.name = 'picker';
			picker.userData.object = object;
			helper.add( picker );
M
Mr.doob 已提交
289

M
Mr.doob 已提交
290 291
			this.sceneHelpers.add( helper );
			this.helpers[ object.id ] = helper;
M
Mr.doob 已提交
292

M
Mr.doob 已提交
293
			this.signals.helperAdded.dispatch( helper );
M
Mr.doob 已提交
294 295 296 297

		};

	}(),
298 299 300

	removeHelper: function ( object ) {

301 302
		if ( this.helpers[ object.id ] !== undefined ) {

303 304 305
			var helper = this.helpers[ object.id ];
			helper.parent.remove( helper );

306 307
			delete this.helpers[ object.id ];

M
Mr.doob 已提交
308 309
			this.signals.helperRemoved.dispatch( helper );

310 311
		}

312 313 314 315
	},

	//

316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
	addScript: function ( object, script ) {

		if ( this.scripts[ object.uuid ] === undefined ) {

			this.scripts[ object.uuid ] = [];

		}

		this.scripts[ object.uuid ].push( script );

		this.signals.scriptAdded.dispatch( script );

	},

	removeScript: function ( object, script ) {

		if ( this.scripts[ object.uuid ] === undefined ) return;

		var index = this.scripts[ object.uuid ].indexOf( script );

		if ( index !== - 1 ) {

			this.scripts[ object.uuid ].splice( index, 1 );

		}

		this.signals.scriptRemoved.dispatch( script );

	},

	//

348 349
	select: function ( object ) {

350
		if ( this.selected === object ) return;
351

352
		var uuid = null;
353

354
		if ( object !== null ) {
355

356
			uuid = object.uuid;
357 358 359

		}

360 361 362
		this.selected = object;

		this.config.setKey( 'selected', uuid );
363 364
		this.signals.objectSelected.dispatch( object );

365 366
	},

367 368
	selectById: function ( id ) {

369 370 371 372 373 374 375
		if ( id === this.camera.id ) {

			this.select( this.camera );
			return;

		}

376
		this.select( this.scene.getObjectById( id, true ) );
377

M
Mr.doob 已提交
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
	},

	selectByUuid: function ( uuid ) {

		var scope = this;

		this.scene.traverse( function ( child ) {

			if ( child.uuid === uuid ) {

				scope.select( child );

			}

		} );
393 394 395 396 397

	},

	deselect: function () {

M
Mr.doob 已提交
398
		this.select( null );
399

400 401 402 403 404 405 406 407 408 409 410 411
	},

	focus: function ( object ) {

		this.signals.objectFocused.dispatch( object );

	},

	focusById: function ( id ) {

		this.focus( this.scene.getObjectById( id, true ) );

M
Mr.doob 已提交
412 413
	},

M
Mr.doob 已提交
414 415
	clear: function () {

416 417
		this.history.clear();

M
Mr.doob 已提交
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
		this.camera.position.set( 500, 250, 500 );
		this.camera.lookAt( new THREE.Vector3() );

		var objects = this.scene.children;

		while ( objects.length > 0 ) {

			this.removeObject( objects[ 0 ] );

		}

		this.geometries = {};
		this.materials = {};
		this.textures = {};
		this.scripts = {};

		this.deselect();

436 437
		this.history.clear();

438 439
		this.signals.editorCleared.dispatch();

M
Mr.doob 已提交
440 441
	},

M
Mr.doob 已提交
442 443 444 445 446 447 448 449 450 451
	//

	fromJSON: function ( json ) {

		var loader = new THREE.ObjectLoader();

		// backwards

		if ( json.scene === undefined ) {

452
			this.setScene( loader.parse( json ) );
M
Mr.doob 已提交
453 454 455 456 457 458 459 460 461 462 463 464 465
			return;

		}

		// TODO: Clean this up somehow

		var camera = loader.parse( json.camera );

		this.camera.position.copy( camera.position );
		this.camera.rotation.copy( camera.rotation );
		this.camera.aspect = camera.aspect;
		this.camera.near = camera.near;
		this.camera.far = camera.far;
M
Mr.doob 已提交
466

M
Mr.doob 已提交
467 468
		this.setScene( loader.parse( json.scene ) );
		this.scripts = json.scripts;
469
		this.history.fromJSON( json.history );
M
Mr.doob 已提交
470 471 472 473 474 475 476

	},

	toJSON: function () {

		return {

M
Mr.doob 已提交
477 478 479
			project: {
				vr: this.config.getKey( 'project/vr' )
			},
M
Mr.doob 已提交
480 481
			camera: this.camera.toJSON(),
			scene: this.scene.toJSON(),
482 483
			scripts: this.scripts,
			history: this.history.toJSON()
M
Mr.doob 已提交
484 485 486

		};

487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
	},

	objectByUuid: function ( uuid ) {

		return this.scene.getObjectByProperty( 'uuid', uuid, true );

	},

	execute: function ( command ) {

		this.history.execute( command );

	},

	undo: function () {

		this.history.undo();

	},

	redo: function () {

		this.history.redo();

511 512 513
	}

}