Editor.js 8.4 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 65
		windowResize: new SIGNALS.Signal(),

		showGridChanged: new SIGNALS.Signal()
66 67

	};
M
Mr.doob 已提交
68

M
Mr.doob 已提交
69
	this.config = new Config();
70
	this.history = new History( this );
71
	this.storage = new Storage();
72 73
	this.loader = new Loader( this );

74
	this.camera = new THREE.PerspectiveCamera( 50, 1, 1, 100000 );
M
Mr.doob 已提交
75 76
	this.camera.position.set( 500, 250, 500 );
	this.camera.lookAt( new THREE.Vector3() );
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 ) {

191
		if ( object.parent === null ) 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

244
		var geometry = new THREE.SphereBufferGeometry( 20, 4, 2 );
M
Mr.doob 已提交
245
		var material = new THREE.MeshBasicMaterial( { color: 0xff0000, visible: false } );
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
			var picker = new THREE.Mesh( geometry, material );
			picker.name = 'picker';
			picker.userData.object = object;
			helper.add( picker );
M
Mr.doob 已提交
286

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

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

		};

	}(),
295 296 297

	removeHelper: function ( object ) {

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

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

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

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

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

	},

	//

345 346
	select: function ( object ) {

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

349
		var uuid = null;
350

351
		if ( object !== null ) {
352

353
			uuid = object.uuid;
354 355 356

		}

357 358 359
		this.selected = object;

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

362 363
	},

364 365
	selectById: function ( id ) {

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

			this.select( this.camera );
			return;

		}

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

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

	selectByUuid: function ( uuid ) {

		var scope = this;

		this.scene.traverse( function ( child ) {

			if ( child.uuid === uuid ) {

				scope.select( child );

			}

		} );
390 391 392 393 394

	},

	deselect: function () {

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

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

	focus: function ( object ) {

		this.signals.objectFocused.dispatch( object );

	},

	focusById: function ( id ) {

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

M
Mr.doob 已提交
409 410
	},

M
Mr.doob 已提交
411 412
	clear: function () {

413 414
		this.history.clear();

M
Mr.doob 已提交
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
		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();

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

M
Mr.doob 已提交
435 436
	},

M
Mr.doob 已提交
437 438 439 440 441 442 443 444 445 446
	//

	fromJSON: function ( json ) {

		var loader = new THREE.ObjectLoader();

		// backwards

		if ( json.scene === undefined ) {

447
			this.setScene( loader.parse( json ) );
M
Mr.doob 已提交
448 449 450 451 452 453 454 455 456 457 458 459 460
			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 已提交
461

M
Mr.doob 已提交
462 463 464 465 466 467 468 469 470
		this.setScene( loader.parse( json.scene ) );
		this.scripts = json.scripts;

	},

	toJSON: function () {

		return {

M
Mr.doob 已提交
471 472 473
			project: {
				vr: this.config.getKey( 'project/vr' )
			},
M
Mr.doob 已提交
474 475 476 477 478 479
			camera: this.camera.toJSON(),
			scene: this.scene.toJSON(),
			scripts: this.scripts

		};

480 481 482
	}

}