Editor.js 7.8 KB
Newer Older
1 2 3 4 5 6 7 8
var Editor = function () {

	var SIGNALS = signals;

	this.signals = {

		// actions

9 10
		playAnimation: new SIGNALS.Signal(),
		stopAnimation: new SIGNALS.Signal(),
11 12 13

		// notifications

14 15
		themeChanged: new SIGNALS.Signal(),

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

21 22
		sceneGraphChanged: new SIGNALS.Signal(),

23 24
		cameraChanged: new SIGNALS.Signal(),

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

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

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

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

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

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

53 54 55
	this.selected = null;
	this.helpers = {};

56 57 58 59
};

Editor.prototype = {

60 61 62 63 64 65 66 67
	setTheme: function ( value ) {

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

		this.signals.themeChanged.dispatch( value );

	},

68 69
	setScene: function ( scene ) {

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

73 74 75 76
		// avoid render per object

		this.signals.sceneGraphChanged.active = false;

77 78 79 80 81 82
		while ( scene.children.length > 0 ) {

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

		}

83 84 85
		this.signals.sceneGraphChanged.active = true;
		this.signals.sceneGraphChanged.dispatch();

86 87 88 89
	},

	//

90 91
	addObject: function ( object ) {

92 93 94 95
		var scope = this;

		object.traverse( function ( child ) {

96 97 98
			if ( child.geometry !== undefined ) scope.addGeometry( child.geometry );
			if ( child.material !== undefined ) scope.addMaterial( child.material );

99 100 101 102
			scope.addHelper( child );

		} );

103 104 105
		this.scene.add( object );

		this.signals.objectAdded.dispatch( object );
106
		this.signals.sceneGraphChanged.dispatch();
107 108 109

	},

110 111 112 113 114 115 116
	setObjectName: function ( object, name ) {

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

	},

117 118
	removeObject: function ( object ) {

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

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

123 124 125 126 127 128 129 130
		var scope = this;

		object.traverse( function ( child ) {

			scope.removeHelper( child );

		} );

131
		object.parent.remove( object );
M
Mr.doob 已提交
132 133

		this.signals.objectRemoved.dispatch( object );
134
		this.signals.sceneGraphChanged.dispatch();
135 136 137

	},

138
	addGeometry: function ( geometry ) {
139

140
		this.geometries[ geometry.uuid ] = geometry;
141 142 143

	},

144 145 146 147 148 149 150
	setGeometryName: function ( geometry, name ) {

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

	},

151 152
	addMaterial: function ( material ) {

153
		this.materials[ material.uuid ] = material;
154 155 156

	},

157 158 159 160 161 162 163
	setMaterialName: function ( material, name ) {

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

	},

164 165
	addTexture: function ( texture ) {

166
		this.textures[ texture.uuid ] = texture;
167 168 169

	},

170 171
	//

M
Mr.doob 已提交
172
	addHelper: function () {
173

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

M
Mr.doob 已提交
177
		return function ( object ) {
M
Mr.doob 已提交
178

M
Mr.doob 已提交
179
			var helper;
180

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

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

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

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

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

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

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

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

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

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

M
Mr.doob 已提交
201 202 203 204
			} else if ( object instanceof THREE.SkinnedMesh ) {

				helper = new THREE.SkeletonHelper( object );

M
Mr.doob 已提交
205
			} else {
M
Mr.doob 已提交
206

M
Mr.doob 已提交
207 208
				// no helper for this object type
				return;
M
Mr.doob 已提交
209

M
Mr.doob 已提交
210
			}
M
Mr.doob 已提交
211

M
Mr.doob 已提交
212 213 214 215 216
			var picker = new THREE.Mesh( geometry, material );
			picker.name = 'picker';
			picker.userData.object = object;
			picker.visible = false;
			helper.add( picker );
M
Mr.doob 已提交
217

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

M
Mr.doob 已提交
221
			this.signals.helperAdded.dispatch( helper );
M
Mr.doob 已提交
222 223 224 225

		};

	}(),
226 227 228

	removeHelper: function ( object ) {

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

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

234 235
			delete this.helpers[ object.id ];

M
Mr.doob 已提交
236 237
			this.signals.helperRemoved.dispatch( helper );

238 239
		}

240 241 242 243
	},

	//

244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
	parent: function ( object, parent ) {

		if ( parent === undefined ) {

			parent = this.scene;

		}

		parent.add( object );

		this.signals.sceneGraphChanged.dispatch();

	},

	//

260 261
	select: function ( object ) {

262
		this.selected = object;
263 264 265 266 267 268 269 270 271 272 273

		if ( object !== null ) {

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

		} else {

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

		}

274 275
		this.signals.objectSelected.dispatch( object );

276 277
	},

278 279
	selectById: function ( id ) {

M
Mr.doob 已提交
280
		var scope = this;
281 282 283 284 285

		this.scene.traverse( function ( child ) {

			if ( child.id === id ) {

M
Mr.doob 已提交
286
				scope.select( child );
287 288 289 290 291

			}

		} );

M
Mr.doob 已提交
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
	},

	selectByUuid: function ( uuid ) {

		var scope = this;

		this.scene.traverse( function ( child ) {

			if ( child.uuid === uuid ) {

				scope.select( child );

			}

		} );
307 308 309 310 311

	},

	deselect: function () {

M
Mr.doob 已提交
312
		this.select( null );
313

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

	// 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,
329
			'SkinnedMesh': THREE.SkinnedMesh,
M
Mr.doob 已提交
330
			'Mesh': THREE.Mesh,
M
Mr.doob 已提交
331
			'Sprite': THREE.Sprite,
M
Mr.doob 已提交
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
			'Object3D': THREE.Object3D

		};

		for ( var type in types ) {

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

		}

	},

	getGeometryType: function ( geometry ) {

		var types = {

M
Mr.doob 已提交
348
			'BoxGeometry': THREE.BoxGeometry,
M
Mr.doob 已提交
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
			'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,
			'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,
390
			'ParticleSystemMaterial': THREE.ParticleSystemMaterial,
M
Mr.doob 已提交
391
			'ShaderMaterial': THREE.ShaderMaterial,
392
			'SpriteCanvasMaterial': THREE.SpriteCanvasMaterial,
M
Mr.doob 已提交
393
			'SpriteMaterial': THREE.SpriteMaterial,
M
Mr.doob 已提交
394 395 396 397 398 399 400 401 402 403
			'Material': THREE.Material

		};

		for ( var type in types ) {

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

		}

404 405 406
	}

}