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
	setObjectName: function ( object, name ) {

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

	},

165 166
	removeObject: function ( object ) {

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

169 170 171 172 173 174 175 176
		var scope = this;

		object.traverse( function ( child ) {

			scope.removeHelper( child );

		} );

177
		object.parent.remove( object );
M
Mr.doob 已提交
178 179

		this.signals.objectRemoved.dispatch( object );
180
		this.signals.sceneGraphChanged.dispatch();
181 182 183

	},

184
	addGeometry: function ( geometry ) {
185

186
		this.geometries[ geometry.uuid ] = geometry;
187 188 189

	},

190 191 192 193 194 195 196
	setGeometryName: function ( geometry, name ) {

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

	},

197 198
	addMaterial: function ( material ) {

199
		this.materials[ material.uuid ] = material;
200 201 202

	},

203 204 205 206 207 208 209
	setMaterialName: function ( material, name ) {

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

	},

210 211
	addTexture: function ( texture ) {

212
		this.textures[ texture.uuid ] = texture;
213 214 215

	},

216 217
	//

M
Mr.doob 已提交
218
	addHelper: function () {
219

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

M
Mr.doob 已提交
223
		return function ( object ) {
M
Mr.doob 已提交
224

M
Mr.doob 已提交
225
			var helper;
226

M
Mr.doob 已提交
227
			if ( object instanceof THREE.Camera ) {
228

M
Mr.doob 已提交
229
				helper = new THREE.CameraHelper( object, 10 );
230 231

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

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

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

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

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

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

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

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

M
Mr.doob 已提交
247 248 249 250
			} else if ( object instanceof THREE.SkinnedMesh ) {

				helper = new THREE.SkeletonHelper( object );

M
Mr.doob 已提交
251
			} else {
M
Mr.doob 已提交
252

M
Mr.doob 已提交
253 254
				// no helper for this object type
				return;
M
Mr.doob 已提交
255

M
Mr.doob 已提交
256
			}
M
Mr.doob 已提交
257

M
Mr.doob 已提交
258 259 260 261 262
			var picker = new THREE.Mesh( geometry, material );
			picker.name = 'picker';
			picker.userData.object = object;
			picker.visible = false;
			helper.add( picker );
M
Mr.doob 已提交
263

M
Mr.doob 已提交
264 265
			this.sceneHelpers.add( helper );
			this.helpers[ object.id ] = helper;
M
Mr.doob 已提交
266

M
Mr.doob 已提交
267
			this.signals.helperAdded.dispatch( helper );
M
Mr.doob 已提交
268 269 270 271

		};

	}(),
272 273 274

	removeHelper: function ( object ) {

275 276
		if ( this.helpers[ object.id ] !== undefined ) {

277 278 279
			var helper = this.helpers[ object.id ];
			helper.parent.remove( helper );

280 281
			delete this.helpers[ object.id ];

M
Mr.doob 已提交
282 283
			this.signals.helperRemoved.dispatch( helper );

284 285
		}

286 287 288 289
	},

	//

290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
	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 );

	},

	//

M
Mr.doob 已提交
322
	parent: function ( object, parent, before ) {
323 324 325 326 327 328 329 330 331

		if ( parent === undefined ) {

			parent = this.scene;

		}

		parent.add( object );

M
Mr.doob 已提交
332
		// sort children array
M
Mr.doob 已提交
333

M
Mr.doob 已提交
334 335 336 337
		if ( before !== undefined ) {

			var index = parent.children.indexOf( before );
			parent.children.splice( index, 0, object );
M
Mr.doob 已提交
338 339 340 341
			parent.children.pop();

		}

342 343 344 345 346 347
		this.signals.sceneGraphChanged.dispatch();

	},

	//

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 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
	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();

434 435
		this.signals.editorCleared.dispatch();

M
Mr.doob 已提交
436 437
	},

M
Mr.doob 已提交
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 463 464
	//

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

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

	},

	toJSON: function () {

		return {

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

		};

481 482 483
	}

}