Editor.js 8.6 KB
Newer Older
M
r59  
Mr.doob 已提交
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

M
r62  
Mr.doob 已提交
13 14
		themeChanged: new SIGNALS.Signal(),

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

		sceneGraphChanged: new SIGNALS.Signal(),

M
r62  
Mr.doob 已提交
22 23
		cameraChanged: new SIGNALS.Signal(),

M
r59  
Mr.doob 已提交
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
		objectSelected: new SIGNALS.Signal(),
		objectAdded: new SIGNALS.Signal(),
		objectChanged: new SIGNALS.Signal(),
		objectRemoved: new SIGNALS.Signal(),

		helperAdded: new SIGNALS.Signal(),
		helperRemoved: new SIGNALS.Signal(),

		materialChanged: new SIGNALS.Signal(),
		fogTypeChanged: new SIGNALS.Signal(),
		fogColorChanged: new SIGNALS.Signal(),
		fogParametersChanged: new SIGNALS.Signal(),
		windowResize: new SIGNALS.Signal()

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

M
r59  
Mr.doob 已提交
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
	this.scene = new THREE.Scene();
	this.sceneHelpers = new THREE.Scene();

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

	this.selected = null;
	this.helpers = {};

};

Editor.prototype = {

M
r62  
Mr.doob 已提交
59 60 61 62 63 64 65 66
	setTheme: function ( value ) {

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

		this.signals.themeChanged.dispatch( value );

	},

M
r59  
Mr.doob 已提交
67 68 69 70 71
	setScene: function ( scene ) {

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

M
r62  
Mr.doob 已提交
72 73 74 75
		// avoid render per object

		this.signals.sceneGraphChanged.active = false;

M
r59  
Mr.doob 已提交
76 77 78 79 80 81
		while ( scene.children.length > 0 ) {

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

		}

M
r62  
Mr.doob 已提交
82 83 84
		this.signals.sceneGraphChanged.active = true;
		this.signals.sceneGraphChanged.dispatch();

M
r59  
Mr.doob 已提交
85 86 87 88 89 90 91 92 93 94
	},

	//

	addObject: function ( object ) {

		var scope = this;

		object.traverse( function ( child ) {

M
r61  
Mr.doob 已提交
95 96 97
			if ( child.geometry !== undefined ) scope.addGeometry( child.geometry );
			if ( child.material !== undefined ) scope.addMaterial( child.material );

M
r59  
Mr.doob 已提交
98 99 100 101 102 103 104 105 106 107 108
			scope.addHelper( child );

		} );

		this.scene.add( object );

		this.signals.objectAdded.dispatch( object );
		this.signals.sceneGraphChanged.dispatch();

	},

M
r62  
Mr.doob 已提交
109 110 111 112 113 114 115
	setObjectName: function ( object, name ) {

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

	},

M
r59  
Mr.doob 已提交
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
	removeObject: function ( object ) {

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

		if ( confirm( 'Delete ' + object.name + '?' ) === false ) return;

		var scope = this;

		object.traverse( function ( child ) {

			scope.removeHelper( child );

		} );

		object.parent.remove( object );

		this.signals.objectRemoved.dispatch( object );
		this.signals.sceneGraphChanged.dispatch();

	},

M
r61  
Mr.doob 已提交
137
	addGeometry: function ( geometry ) {
M
r59  
Mr.doob 已提交
138

M
r61  
Mr.doob 已提交
139
		this.geometries[ geometry.uuid ] = geometry;
M
r59  
Mr.doob 已提交
140 141 142

	},

M
r62  
Mr.doob 已提交
143 144 145 146 147 148 149
	setGeometryName: function ( geometry, name ) {

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

	},

M
r59  
Mr.doob 已提交
150 151
	addMaterial: function ( material ) {

M
r61  
Mr.doob 已提交
152
		this.materials[ material.uuid ] = material;
M
r59  
Mr.doob 已提交
153 154 155

	},

M
r62  
Mr.doob 已提交
156 157 158 159 160 161 162
	setMaterialName: function ( material, name ) {

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

	},

M
r59  
Mr.doob 已提交
163 164
	addTexture: function ( texture ) {

M
r61  
Mr.doob 已提交
165
		this.textures[ texture.uuid ] = texture;
M
r59  
Mr.doob 已提交
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 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 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294

	},

	//

	addHelper: function () {

		var geometry = new THREE.SphereGeometry( 20, 4, 2 );
		var material = new THREE.MeshBasicMaterial( { color: 0xff0000 } );

		return function ( object ) {

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

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

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

				this.signals.helperAdded.dispatch( helper );

			} else if ( object instanceof THREE.DirectionalLight ) {

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

				var helper = new THREE.DirectionalLightHelper( object, 20 );
				helper.add( picker );

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

				this.signals.helperAdded.dispatch( helper );

			} else if ( object instanceof THREE.SpotLight ) {

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

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

			}

		};

	}(),

	removeHelper: function ( object ) {

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

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

			delete this.helpers[ object.id ];

			this.signals.helperRemoved.dispatch( helper );

		}

	},

	//

	parent: function ( object, parent ) {

		if ( parent === undefined ) {

			parent = this.scene;

		}

		parent.add( object );

		this.signals.sceneGraphChanged.dispatch();

	},

	//

	select: function ( object ) {

		this.selected = object;
M
r62  
Mr.doob 已提交
295
		this.config.setKey( 'selected', object.uuid );
M
r59  
Mr.doob 已提交
296 297 298 299 300 301
		this.signals.objectSelected.dispatch( object );

	},

	selectById: function ( id ) {

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

		this.scene.traverse( function ( child ) {

			if ( child.id === id ) {

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

			}

		} );

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

	selectByUuid: function ( uuid ) {

		var scope = this;

		this.scene.traverse( function ( child ) {

			if ( child.uuid === uuid ) {

				scope.select( child );

			}

		} );
M
r59  
Mr.doob 已提交
329 330 331 332 333 334 335

	},

	deselect: function () {

		this.select( null );

M
r61  
Mr.doob 已提交
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 409
	},

	// 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,
M
r62  
Mr.doob 已提交
410
			'ParticleSystemMaterial': THREE.ParticleSystemMaterial,
M
r61  
Mr.doob 已提交
411
			'ShaderMaterial': THREE.ShaderMaterial,
M
r62  
Mr.doob 已提交
412
			'SpriteCanvasMaterial': THREE.SpriteCanvasMaterial,
M
r61  
Mr.doob 已提交
413 414 415 416 417 418 419 420 421 422
			'Material': THREE.Material

		};

		for ( var type in types ) {

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

		}

M
r59  
Mr.doob 已提交
423 424 425
	}

}