Editor.js 8.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
var Editor = function () {

	var SIGNALS = signals;

	this.signals = {

		// actions

		playAnimations: new SIGNALS.Signal(),

		// notifications

13 14
		themeChanged: new SIGNALS.Signal(),

15 16
		transformModeChanged: new SIGNALS.Signal(),
		snapChanged: new SIGNALS.Signal(),
17
		spaceChanged: new SIGNALS.Signal(),
18
		rendererChanged: new SIGNALS.Signal(),
M
Mr.doob 已提交
19

20 21
		sceneGraphChanged: new SIGNALS.Signal(),

22 23
		cameraChanged: new SIGNALS.Signal(),

M
Mr.doob 已提交
24
		objectSelected: new SIGNALS.Signal(),
25 26
		objectAdded: new SIGNALS.Signal(),
		objectChanged: new SIGNALS.Signal(),
M
Mr.doob 已提交
27
		objectRemoved: new SIGNALS.Signal(),
M
Mr.doob 已提交
28 29

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

32 33 34 35 36 37 38
		materialChanged: new SIGNALS.Signal(),
		fogTypeChanged: new SIGNALS.Signal(),
		fogColorChanged: new SIGNALS.Signal(),
		fogParametersChanged: new SIGNALS.Signal(),
		windowResize: new SIGNALS.Signal()

	};
M
Mr.doob 已提交
39 40
	
	this.config = new Config();
41 42
	this.loader = new Loader( this );

43
	this.scene = new THREE.Scene();
44
	this.sceneHelpers = new THREE.Scene();
45 46 47 48 49 50

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

51 52 53
	this.selected = null;
	this.helpers = {};

54 55 56 57
};

Editor.prototype = {

58 59 60 61 62 63 64 65
	setTheme: function ( value ) {

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

		this.signals.themeChanged.dispatch( value );

	},

66 67
	setScene: function ( scene ) {

68 69 70
		this.scene.name = scene.name;
		this.scene.userData = JSON.parse( JSON.stringify( scene.userData ) );

71 72 73 74
		// avoid render per object

		this.signals.sceneGraphChanged.active = false;

75 76 77 78 79 80
		while ( scene.children.length > 0 ) {

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

		}

81 82 83
		this.signals.sceneGraphChanged.active = true;
		this.signals.sceneGraphChanged.dispatch();

84 85 86 87
	},

	//

88 89
	addObject: function ( object ) {

90 91 92 93
		var scope = this;

		object.traverse( function ( child ) {

94 95 96
			if ( child.geometry !== undefined ) scope.addGeometry( child.geometry );
			if ( child.material !== undefined ) scope.addMaterial( child.material );

97 98 99 100
			scope.addHelper( child );

		} );

101 102 103
		this.scene.add( object );

		this.signals.objectAdded.dispatch( object );
104
		this.signals.sceneGraphChanged.dispatch();
105 106 107

	},

108 109 110 111 112 113 114
	setObjectName: function ( object, name ) {

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

	},

115 116
	removeObject: function ( object ) {

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

M
Mr.doob 已提交
119
		if ( confirm( 'Delete ' + object.name + '?' ) === false ) return;
120

121 122 123 124 125 126 127 128
		var scope = this;

		object.traverse( function ( child ) {

			scope.removeHelper( child );

		} );

129
		object.parent.remove( object );
M
Mr.doob 已提交
130 131

		this.signals.objectRemoved.dispatch( object );
132
		this.signals.sceneGraphChanged.dispatch();
133 134 135

	},

136
	addGeometry: function ( geometry ) {
137

138
		this.geometries[ geometry.uuid ] = geometry;
139 140 141

	},

142 143 144 145 146 147 148
	setGeometryName: function ( geometry, name ) {

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

	},

149 150
	addMaterial: function ( material ) {

151
		this.materials[ material.uuid ] = material;
152 153 154

	},

155 156 157 158 159 160 161
	setMaterialName: function ( material, name ) {

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

	},

162 163
	addTexture: function ( texture ) {

164
		this.textures[ texture.uuid ] = texture;
165 166 167

	},

168 169
	//

M
Mr.doob 已提交
170
	addHelper: function () {
171

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

M
Mr.doob 已提交
175
		return function ( object ) {
M
Mr.doob 已提交
176

177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
			if ( object instanceof THREE.Camera ) {

				var picker = new THREE.Mesh( geometry, material );
				picker.name = 'picker';
				picker.userData.object = object;
				picker.visible = false;

				var helper = new THREE.CameraHelper( object, 10 );
				helper.add( picker );
			
				this.sceneHelpers.add( helper );
				this.helpers[ object.id ] = helper;

				this.signals.helperAdded.dispatch( helper );

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

M
Mr.doob 已提交
194 195 196 197
				var picker = new THREE.Mesh( geometry, material );
				picker.name = 'picker';
				picker.userData.object = object;
				picker.visible = false;
198

M
Mr.doob 已提交
199 200 201 202 203
				var helper = new THREE.PointLightHelper( object, 10 );
				helper.add( picker );
			
				this.sceneHelpers.add( helper );
				this.helpers[ object.id ] = helper;
M
Mr.doob 已提交
204

M
Mr.doob 已提交
205
				this.signals.helperAdded.dispatch( helper );
206

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

M
Mr.doob 已提交
209 210 211 212
				var picker = new THREE.Mesh( geometry, material );
				picker.name = 'picker';
				picker.userData.object = object;
				picker.visible = false;
M
Mr.doob 已提交
213

M
Mr.doob 已提交
214 215
				var helper = new THREE.DirectionalLightHelper( object, 20 );
				helper.add( picker );
216

M
Mr.doob 已提交
217 218
				this.sceneHelpers.add( helper );
				this.helpers[ object.id ] = helper;
219

M
Mr.doob 已提交
220
				this.signals.helperAdded.dispatch( helper );
M
Mr.doob 已提交
221

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

M
Mr.doob 已提交
224 225 226 227
				var picker = new THREE.Mesh( geometry, material );
				picker.name = 'picker';
				picker.userData.object = object;
				picker.visible = false;
228

M
Mr.doob 已提交
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
				var helper = new THREE.SpotLightHelper( object, 10 );
				helper.add( picker );

				this.sceneHelpers.add( helper );
				this.helpers[ object.id ] = helper;

				this.signals.helperAdded.dispatch( helper );

			} else if ( object instanceof THREE.HemisphereLight ) {

				var picker = new THREE.Mesh( geometry, material );
				picker.name = 'picker';
				picker.userData.object = object;
				picker.visible = false;

				var helper = new THREE.HemisphereLightHelper( object, 10 );
				helper.add( picker );

				this.sceneHelpers.add( helper );
				this.helpers[ object.id ] = helper;

				this.signals.helperAdded.dispatch( helper );

			}

		};

	}(),
257 258 259

	removeHelper: function ( object ) {

260 261
		if ( this.helpers[ object.id ] !== undefined ) {

262 263 264
			var helper = this.helpers[ object.id ];
			helper.parent.remove( helper );

265 266
			delete this.helpers[ object.id ];

M
Mr.doob 已提交
267 268
			this.signals.helperRemoved.dispatch( helper );

269 270
		}

271 272 273 274
	},

	//

275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
	parent: function ( object, parent ) {

		if ( parent === undefined ) {

			parent = this.scene;

		}

		parent.add( object );

		this.signals.sceneGraphChanged.dispatch();

	},

	//

291 292
	select: function ( object ) {

293
		this.selected = object;
M
Mr.doob 已提交
294
		this.config.setKey( 'selected', object.uuid );
295 296
		this.signals.objectSelected.dispatch( object );

297 298
	},

299 300
	selectById: function ( id ) {

M
Mr.doob 已提交
301
		var scope = this;
302 303 304 305 306

		this.scene.traverse( function ( child ) {

			if ( child.id === id ) {

M
Mr.doob 已提交
307
				scope.select( child );
308 309 310 311 312

			}

		} );

M
Mr.doob 已提交
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
	},

	selectByUuid: function ( uuid ) {

		var scope = this;

		this.scene.traverse( function ( child ) {

			if ( child.uuid === uuid ) {

				scope.select( child );

			}

		} );
328 329 330 331 332

	},

	deselect: function () {

M
Mr.doob 已提交
333
		this.select( null );
334

M
Mr.doob 已提交
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
	},

	// utils

	getObjectType: function ( object ) {

		var types = {

			'Scene': THREE.Scene,
			'PerspectiveCamera': THREE.PerspectiveCamera,
			'AmbientLight': THREE.AmbientLight,
			'DirectionalLight': THREE.DirectionalLight,
			'HemisphereLight': THREE.HemisphereLight,
			'PointLight': THREE.PointLight,
			'SpotLight': THREE.SpotLight,
			'Mesh': THREE.Mesh,
			'Object3D': THREE.Object3D

		};

		for ( var type in types ) {

			if ( object instanceof types[ type ] ) return type;

		}

	},

	getGeometryType: function ( geometry ) {

		var types = {

			'CircleGeometry': THREE.CircleGeometry,
			'CubeGeometry': THREE.CubeGeometry,
			'CylinderGeometry': THREE.CylinderGeometry,
			'ExtrudeGeometry': THREE.ExtrudeGeometry,
			'IcosahedronGeometry': THREE.IcosahedronGeometry,
			'LatheGeometry': THREE.LatheGeometry,
			'OctahedronGeometry': THREE.OctahedronGeometry,
			'ParametricGeometry': THREE.ParametricGeometry,
			'PlaneGeometry': THREE.PlaneGeometry,
			'PolyhedronGeometry': THREE.PolyhedronGeometry,
			'ShapeGeometry': THREE.ShapeGeometry,
			'SphereGeometry': THREE.SphereGeometry,
			'TetrahedronGeometry': THREE.TetrahedronGeometry,
			'TextGeometry': THREE.TextGeometry,
			'TorusGeometry': THREE.TorusGeometry,
			'TorusKnotGeometry': THREE.TorusKnotGeometry,
			'TubeGeometry': THREE.TubeGeometry,
			'Geometry': THREE.Geometry,
			'BufferGeometry': THREE.BufferGeometry

		};

		for ( var type in types ) {

			if ( geometry instanceof types[ type ] ) return type;

		}

	},

	getMaterialType: function ( material ) {

		var types = {

			'LineBasicMaterial': THREE.LineBasicMaterial,
			'LineDashedMaterial': THREE.LineDashedMaterial,
			'MeshBasicMaterial': THREE.MeshBasicMaterial,
			'MeshDepthMaterial': THREE.MeshDepthMaterial,
			'MeshFaceMaterial': THREE.MeshFaceMaterial,
			'MeshLambertMaterial': THREE.MeshLambertMaterial,
			'MeshNormalMaterial': THREE.MeshNormalMaterial,
			'MeshPhongMaterial': THREE.MeshPhongMaterial,
409
			'ParticleSystemMaterial': THREE.ParticleSystemMaterial,
M
Mr.doob 已提交
410
			'ShaderMaterial': THREE.ShaderMaterial,
411
			'SpriteCanvasMaterial': THREE.SpriteCanvasMaterial,
M
Mr.doob 已提交
412 413 414 415 416 417 418 419 420 421
			'Material': THREE.Material

		};

		for ( var type in types ) {

			if ( material instanceof types[ type ] ) return type;

		}

422 423 424
	}

}