Editor.js 7.7 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

	},

	//

	addHelper: function () {

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

		return function ( object ) {

M
r66  
Mr.doob 已提交
178
			var helper;
M
r59  
Mr.doob 已提交
179

M
r66  
Mr.doob 已提交
180
			if ( object instanceof THREE.Camera ) {
M
r59  
Mr.doob 已提交
181

M
r66  
Mr.doob 已提交
182
				helper = new THREE.CameraHelper( object, 10 );
M
r59  
Mr.doob 已提交
183 184 185

			} else if ( object instanceof THREE.PointLight ) {

M
r66  
Mr.doob 已提交
186
				helper = new THREE.PointLightHelper( object, 10 );
M
r59  
Mr.doob 已提交
187 188 189

			} else if ( object instanceof THREE.DirectionalLight ) {

M
r66  
Mr.doob 已提交
190
				helper = new THREE.DirectionalLightHelper( object, 20 );
M
r59  
Mr.doob 已提交
191 192 193

			} else if ( object instanceof THREE.SpotLight ) {

M
r66  
Mr.doob 已提交
194
				helper = new THREE.SpotLightHelper( object, 10 );
M
r59  
Mr.doob 已提交
195

M
r66  
Mr.doob 已提交
196
			} else if ( object instanceof THREE.HemisphereLight ) {
M
r59  
Mr.doob 已提交
197

M
r66  
Mr.doob 已提交
198
				helper = new THREE.HemisphereLightHelper( object, 10 );
M
r59  
Mr.doob 已提交
199

M
r66  
Mr.doob 已提交
200
			} else {
M
r59  
Mr.doob 已提交
201

M
r66  
Mr.doob 已提交
202 203
				// no helper for this object type
				return;
M
r59  
Mr.doob 已提交
204

M
r66  
Mr.doob 已提交
205
			}
M
r59  
Mr.doob 已提交
206

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

M
r66  
Mr.doob 已提交
213 214
			this.sceneHelpers.add( helper );
			this.helpers[ object.id ] = helper;
M
r59  
Mr.doob 已提交
215

M
r66  
Mr.doob 已提交
216
			this.signals.helperAdded.dispatch( helper );
M
r59  
Mr.doob 已提交
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

		};

	}(),

	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
r65  
Mr.doob 已提交
258 259 260 261 262 263 264 265 266 267 268

		if ( object !== null ) {

			this.config.setKey( 'selected', object.uuid );

		} else {

			this.config.setKey( 'selected', null );

		}

M
r59  
Mr.doob 已提交
269 270 271 272 273 274
		this.signals.objectSelected.dispatch( object );

	},

	selectById: function ( id ) {

M
r62  
Mr.doob 已提交
275
		var scope = this;
M
r60  
Mr.doob 已提交
276 277 278 279 280

		this.scene.traverse( function ( child ) {

			if ( child.id === id ) {

M
r62  
Mr.doob 已提交
281
				scope.select( child );
M
r60  
Mr.doob 已提交
282 283 284 285 286

			}

		} );

M
r62  
Mr.doob 已提交
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
	},

	selectByUuid: function ( uuid ) {

		var scope = this;

		this.scene.traverse( function ( child ) {

			if ( child.uuid === uuid ) {

				scope.select( child );

			}

		} );
M
r59  
Mr.doob 已提交
302 303 304 305 306 307 308

	},

	deselect: function () {

		this.select( null );

M
r61  
Mr.doob 已提交
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
	},

	// 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,
M
r63  
Mr.doob 已提交
325
			'Sprite': THREE.Sprite,
M
r61  
Mr.doob 已提交
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
			'Object3D': THREE.Object3D

		};

		for ( var type in types ) {

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

		}

	},

	getGeometryType: function ( geometry ) {

		var types = {

M
r66  
Mr.doob 已提交
342
			'BoxGeometry': THREE.BoxGeometry,
M
r61  
Mr.doob 已提交
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
			'CircleGeometry': THREE.CircleGeometry,
			'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,
M
r66  
Mr.doob 已提交
360
			'Geometry2': THREE.Geometry2,
M
r61  
Mr.doob 已提交
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
			'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 已提交
385
			'ParticleSystemMaterial': THREE.ParticleSystemMaterial,
M
r61  
Mr.doob 已提交
386
			'ShaderMaterial': THREE.ShaderMaterial,
M
r62  
Mr.doob 已提交
387
			'SpriteCanvasMaterial': THREE.SpriteCanvasMaterial,
M
r63  
Mr.doob 已提交
388
			'SpriteMaterial': THREE.SpriteMaterial,
M
r61  
Mr.doob 已提交
389 390 391 392 393 394 395 396 397 398
			'Material': THREE.Material

		};

		for ( var type in types ) {

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

		}

M
r59  
Mr.doob 已提交
399 400 401
	}

}