Editor.js 8.0 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
		showDialog: new SIGNALS.Signal(),

14 15
		// notifications

16 17
		themeChanged: new SIGNALS.Signal(),

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

23 24
		sceneGraphChanged: new SIGNALS.Signal(),

25 26
		cameraChanged: new SIGNALS.Signal(),

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

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

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

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

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

	this.object = {};
	this.geometries = {};
	this.materials = {};
	this.textures = {};
M
Mr.doob 已提交
54 55
	
	this.scripts = {};
56

57 58 59
	this.selected = null;
	this.helpers = {};

60 61 62 63
};

Editor.prototype = {

64 65 66 67 68 69 70 71
	setTheme: function ( value ) {

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

		this.signals.themeChanged.dispatch( value );

	},

72 73 74 75 76 77 78 79
	showDialog: function ( value ) {
	
		this.signals.showDialog.dispatch( value );
	
	},
	
	//

80 81
	setScene: function ( scene ) {

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

85 86 87 88
		// avoid render per object

		this.signals.sceneGraphChanged.active = false;

89 90 91 92 93 94
		while ( scene.children.length > 0 ) {

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

		}

95 96 97
		this.signals.sceneGraphChanged.active = true;
		this.signals.sceneGraphChanged.dispatch();

98 99 100 101
	},

	//

102 103
	addObject: function ( object ) {

104 105 106 107
		var scope = this;

		object.traverse( function ( child ) {

108 109 110
			if ( child.geometry !== undefined ) scope.addGeometry( child.geometry );
			if ( child.material !== undefined ) scope.addMaterial( child.material );

111 112 113 114
			scope.addHelper( child );

		} );

115 116 117
		this.scene.add( object );

		this.signals.objectAdded.dispatch( object );
118
		this.signals.sceneGraphChanged.dispatch();
119 120 121

	},

122 123 124 125 126 127 128
	setObjectName: function ( object, name ) {

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

	},

129 130
	removeObject: function ( object ) {

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

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

135 136 137 138 139 140 141 142
		var scope = this;

		object.traverse( function ( child ) {

			scope.removeHelper( child );

		} );

143
		object.parent.remove( object );
M
Mr.doob 已提交
144 145

		this.signals.objectRemoved.dispatch( object );
146
		this.signals.sceneGraphChanged.dispatch();
147 148 149

	},

150
	addGeometry: function ( geometry ) {
151

152
		this.geometries[ geometry.uuid ] = geometry;
153 154 155

	},

156 157 158 159 160 161 162
	setGeometryName: function ( geometry, name ) {

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

	},

163 164
	addMaterial: function ( material ) {

165
		this.materials[ material.uuid ] = material;
166 167 168

	},

169 170 171 172 173 174 175
	setMaterialName: function ( material, name ) {

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

	},

176 177
	addTexture: function ( texture ) {

178
		this.textures[ texture.uuid ] = texture;
179 180 181

	},

182 183
	//

M
Mr.doob 已提交
184
	addHelper: function () {
185

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

M
Mr.doob 已提交
189
		return function ( object ) {
M
Mr.doob 已提交
190

M
Mr.doob 已提交
191
			var helper;
192

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

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

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

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

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

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

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

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

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

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

M
Mr.doob 已提交
213 214 215 216
			} else if ( object instanceof THREE.SkinnedMesh ) {

				helper = new THREE.SkeletonHelper( object );

M
Mr.doob 已提交
217
			} else {
M
Mr.doob 已提交
218

M
Mr.doob 已提交
219 220
				// no helper for this object type
				return;
M
Mr.doob 已提交
221

M
Mr.doob 已提交
222
			}
M
Mr.doob 已提交
223

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

M
Mr.doob 已提交
230 231
			this.sceneHelpers.add( helper );
			this.helpers[ object.id ] = helper;
M
Mr.doob 已提交
232

M
Mr.doob 已提交
233
			this.signals.helperAdded.dispatch( helper );
M
Mr.doob 已提交
234 235 236 237

		};

	}(),
238 239 240

	removeHelper: function ( object ) {

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

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

246 247
			delete this.helpers[ object.id ];

M
Mr.doob 已提交
248 249
			this.signals.helperRemoved.dispatch( helper );

250 251
		}

252 253 254 255
	},

	//

256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
	parent: function ( object, parent ) {

		if ( parent === undefined ) {

			parent = this.scene;

		}

		parent.add( object );

		this.signals.sceneGraphChanged.dispatch();

	},

	//

272 273
	select: function ( object ) {

274
		this.selected = object;
275 276 277 278 279 280 281 282 283 284 285

		if ( object !== null ) {

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

		} else {

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

		}

286 287
		this.signals.objectSelected.dispatch( object );

288 289
	},

290 291
	selectById: function ( id ) {

M
Mr.doob 已提交
292
		var scope = this;
293 294 295 296 297

		this.scene.traverse( function ( child ) {

			if ( child.id === id ) {

M
Mr.doob 已提交
298
				scope.select( child );
299 300 301 302 303

			}

		} );

M
Mr.doob 已提交
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
	},

	selectByUuid: function ( uuid ) {

		var scope = this;

		this.scene.traverse( function ( child ) {

			if ( child.uuid === uuid ) {

				scope.select( child );

			}

		} );
319 320 321 322 323

	},

	deselect: function () {

M
Mr.doob 已提交
324
		this.select( null );
325

M
Mr.doob 已提交
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
	},

	// 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,
341
			'SkinnedMesh': THREE.SkinnedMesh,
M
Mr.doob 已提交
342
			'Mesh': THREE.Mesh,
M
Mr.doob 已提交
343
			'Sprite': THREE.Sprite,
M
Mr.doob 已提交
344
			'Group': THREE.Group,
M
Mr.doob 已提交
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
			'Object3D': THREE.Object3D

		};

		for ( var type in types ) {

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

		}

	},

	getGeometryType: function ( geometry ) {

		var types = {

M
Mr.doob 已提交
361
			'BoxGeometry': THREE.BoxGeometry,
M
Mr.doob 已提交
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
			'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,
M
Mr.doob 已提交
403
			'PointCloudMaterial': THREE.PointCloudMaterial,
M
Mr.doob 已提交
404
			'ShaderMaterial': THREE.ShaderMaterial,
405
			'SpriteCanvasMaterial': THREE.SpriteCanvasMaterial,
M
Mr.doob 已提交
406
			'SpriteMaterial': THREE.SpriteMaterial,
M
Mr.doob 已提交
407 408 409 410 411 412 413 414 415 416
			'Material': THREE.Material

		};

		for ( var type in types ) {

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

		}

417 418 419
	}

}