Editor.js 7.7 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
			} else {
M
Mr.doob 已提交
202

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

M
Mr.doob 已提交
206
			}
M
Mr.doob 已提交
207

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

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

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

		};

	}(),
222 223 224

	removeHelper: function ( object ) {

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

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

230 231
			delete this.helpers[ object.id ];

M
Mr.doob 已提交
232 233
			this.signals.helperRemoved.dispatch( helper );

234 235
		}

236 237 238 239
	},

	//

240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
	parent: function ( object, parent ) {

		if ( parent === undefined ) {

			parent = this.scene;

		}

		parent.add( object );

		this.signals.sceneGraphChanged.dispatch();

	},

	//

256 257
	select: function ( object ) {

258
		this.selected = object;
259 260 261 262 263 264 265 266 267 268 269

		if ( object !== null ) {

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

		} else {

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

		}

270 271
		this.signals.objectSelected.dispatch( object );

272 273
	},

274 275
	selectById: function ( id ) {

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

		this.scene.traverse( function ( child ) {

			if ( child.id === id ) {

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

			}

		} );

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

	selectByUuid: function ( uuid ) {

		var scope = this;

		this.scene.traverse( function ( child ) {

			if ( child.uuid === uuid ) {

				scope.select( child );

			}

		} );
303 304 305 306 307

	},

	deselect: function () {

M
Mr.doob 已提交
308
		this.select( null );
309

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

		};

		for ( var type in types ) {

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

		}

	},

	getGeometryType: function ( geometry ) {

		var types = {

M
Mr.doob 已提交
344
			'BoxGeometry': THREE.BoxGeometry,
M
Mr.doob 已提交
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
			'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,
386
			'ParticleSystemMaterial': THREE.ParticleSystemMaterial,
M
Mr.doob 已提交
387
			'ShaderMaterial': THREE.ShaderMaterial,
388
			'SpriteCanvasMaterial': THREE.SpriteCanvasMaterial,
M
Mr.doob 已提交
389
			'SpriteMaterial': THREE.SpriteMaterial,
M
Mr.doob 已提交
390 391 392 393 394 395 396 397 398 399
			'Material': THREE.Material

		};

		for ( var type in types ) {

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

		}

400 401 402
	}

}