Editor.js 8.3 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 23
		playAnimation: new SIGNALS.Signal(),
		stopAnimation: new SIGNALS.Signal(),
24

25
		// showDialog: new SIGNALS.Signal(),
26

27 28
		// notifications

29 30
		editorCleared: new SIGNALS.Signal(),

M
Mr.doob 已提交
31 32 33
		savingStarted: new SIGNALS.Signal(),
		savingFinished: new SIGNALS.Signal(),

34 35
		themeChanged: new SIGNALS.Signal(),

36 37
		transformModeChanged: new SIGNALS.Signal(),
		snapChanged: new SIGNALS.Signal(),
38
		spaceChanged: new SIGNALS.Signal(),
39
		rendererChanged: new SIGNALS.Signal(),
M
Mr.doob 已提交
40

41 42
		sceneGraphChanged: new SIGNALS.Signal(),

43 44
		cameraChanged: new SIGNALS.Signal(),

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

M
Mr.doob 已提交
47
		objectSelected: new SIGNALS.Signal(),
48 49
		objectFocused: new SIGNALS.Signal(),

50 51
		objectAdded: new SIGNALS.Signal(),
		objectChanged: new SIGNALS.Signal(),
M
Mr.doob 已提交
52
		objectRemoved: new SIGNALS.Signal(),
M
Mr.doob 已提交
53 54

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

57
		materialChanged: new SIGNALS.Signal(),
M
Mr.doob 已提交
58

59
		scriptAdded: new SIGNALS.Signal(),
M
Mr.doob 已提交
60
		scriptChanged: new SIGNALS.Signal(),
61
		scriptRemoved: new SIGNALS.Signal(),
M
Mr.doob 已提交
62

63 64 65
		fogTypeChanged: new SIGNALS.Signal(),
		fogColorChanged: new SIGNALS.Signal(),
		fogParametersChanged: new SIGNALS.Signal(),
C
Chris Jubb 已提交
66 67 68
		windowResize: new SIGNALS.Signal(),

		showGridChanged: new SIGNALS.Signal()
69 70

	};
M
Mr.doob 已提交
71

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

76
	this.camera = new THREE.PerspectiveCamera( 50, 1, 1, 100000 );
77 78
	this.camera.name = 'Camera';

79
	this.scene = new THREE.Scene();
80
	this.scene.name = 'Scene';
M
Mr.doob 已提交
81

82
	this.sceneHelpers = new THREE.Scene();
83 84 85 86 87

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

90 91 92
	this.selected = null;
	this.helpers = {};

93 94 95 96
};

Editor.prototype = {

97 98 99 100 101 102 103 104
	setTheme: function ( value ) {

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

		this.signals.themeChanged.dispatch( value );

	},

M
Mr.doob 已提交
105
	/*
106
	showDialog: function ( value ) {
M
Mr.doob 已提交
107

108
		this.signals.showDialog.dispatch( value );
M
Mr.doob 已提交
109

110
	},
M
Mr.doob 已提交
111
	*/
M
Mr.doob 已提交
112

113 114
	//

115 116
	setScene: function ( scene ) {

M
Mr.doob 已提交
117
		this.scene.uuid = scene.uuid;
118 119 120
		this.scene.name = scene.name;
		this.scene.userData = JSON.parse( JSON.stringify( scene.userData ) );

121 122 123 124
		// avoid render per object

		this.signals.sceneGraphChanged.active = false;

125 126 127 128 129 130
		while ( scene.children.length > 0 ) {

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

		}

131 132 133
		this.signals.sceneGraphChanged.active = true;
		this.signals.sceneGraphChanged.dispatch();

134 135 136 137
	},

	//

138 139
	addObject: function ( object ) {

140 141 142 143
		var scope = this;

		object.traverse( function ( child ) {

144 145 146
			if ( child.geometry !== undefined ) scope.addGeometry( child.geometry );
			if ( child.material !== undefined ) scope.addMaterial( child.material );

147 148 149 150
			scope.addHelper( child );

		} );

151 152 153
		this.scene.add( object );

		this.signals.objectAdded.dispatch( object );
154
		this.signals.sceneGraphChanged.dispatch();
155 156 157

	},

158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
	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();

	},

182
	nameObject: function ( object, name ) {
183 184 185 186 187 188

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

	},

189 190
	removeObject: function ( object ) {

M
Mr.doob 已提交
191
		if ( object.parent === undefined ) return; // avoid deleting the camera or scene
192

193 194 195 196 197 198 199 200
		var scope = this;

		object.traverse( function ( child ) {

			scope.removeHelper( child );

		} );

201
		object.parent.remove( object );
M
Mr.doob 已提交
202 203

		this.signals.objectRemoved.dispatch( object );
204
		this.signals.sceneGraphChanged.dispatch();
205 206 207

	},

208
	addGeometry: function ( geometry ) {
209

210
		this.geometries[ geometry.uuid ] = geometry;
211 212 213

	},

214 215 216 217 218 219 220
	setGeometryName: function ( geometry, name ) {

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

	},

221 222
	addMaterial: function ( material ) {

223
		this.materials[ material.uuid ] = material;
224 225 226

	},

227 228 229 230 231 232 233
	setMaterialName: function ( material, name ) {

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

	},

234 235
	addTexture: function ( texture ) {

236
		this.textures[ texture.uuid ] = texture;
237 238 239

	},

240 241
	//

M
Mr.doob 已提交
242
	addHelper: function () {
243

M
Mr.doob 已提交
244 245
		var geometry = new THREE.SphereGeometry( 20, 4, 2 );
		var material = new THREE.MeshBasicMaterial( { color: 0xff0000 } );
246

M
Mr.doob 已提交
247
		return function ( object ) {
M
Mr.doob 已提交
248

M
Mr.doob 已提交
249
			var helper;
250

M
Mr.doob 已提交
251
			if ( object instanceof THREE.Camera ) {
252

M
Mr.doob 已提交
253
				helper = new THREE.CameraHelper( object, 10 );
254 255

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

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

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

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

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

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

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

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

M
Mr.doob 已提交
271 272 273 274
			} else if ( object instanceof THREE.SkinnedMesh ) {

				helper = new THREE.SkeletonHelper( object );

M
Mr.doob 已提交
275
			} else {
M
Mr.doob 已提交
276

M
Mr.doob 已提交
277 278
				// no helper for this object type
				return;
M
Mr.doob 已提交
279

M
Mr.doob 已提交
280
			}
M
Mr.doob 已提交
281

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

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

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

		};

	}(),
296 297 298

	removeHelper: function ( object ) {

299 300
		if ( this.helpers[ object.id ] !== undefined ) {

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

304 305
			delete this.helpers[ object.id ];

M
Mr.doob 已提交
306 307
			this.signals.helperRemoved.dispatch( helper );

308 309
		}

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
	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 );

	},

	//

346 347
	select: function ( object ) {

348
		if ( this.selected === object ) return;
349

350
		var uuid = null;
351

352
		if ( object !== null ) {
353

354
			uuid = object.uuid;
355 356 357

		}

358 359 360
		this.selected = object;

		this.config.setKey( 'selected', uuid );
361 362
		this.signals.objectSelected.dispatch( object );

363 364
	},

365 366
	selectById: function ( id ) {

367 368 369 370 371 372 373
		if ( id === this.camera.id ) {

			this.select( this.camera );
			return;

		}

374
		this.select( this.scene.getObjectById( id, true ) );
375

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

	selectByUuid: function ( uuid ) {

		var scope = this;

		this.scene.traverse( function ( child ) {

			if ( child.uuid === uuid ) {

				scope.select( child );

			}

		} );
391 392 393 394 395

	},

	deselect: function () {

M
Mr.doob 已提交
396
		this.select( null );
397

398 399 400 401 402 403 404 405 406 407 408 409
	},

	focus: function ( object ) {

		this.signals.objectFocused.dispatch( object );

	},

	focusById: function ( id ) {

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

M
Mr.doob 已提交
410 411
	},

M
Mr.doob 已提交
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
	clear: function () {

		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();

432 433
		this.signals.editorCleared.dispatch();

M
Mr.doob 已提交
434 435
	},

M
Mr.doob 已提交
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
	//

	fromJSON: function ( json ) {

		var loader = new THREE.ObjectLoader();

		// backwards

		if ( json.scene === undefined ) {

			var scene = loader.parse( json );

			this.setScene( scene );

			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 已提交
463

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

	},

	toJSON: function () {

		return {

			camera: this.camera.toJSON(),
			scene: this.scene.toJSON(),
			scripts: this.scripts

		};

479 480 481
	}

}