Editor.js 6.3 KB
Newer Older
M
Mr.doob 已提交
1 2 3 4
/**
 * @author mrdoob / http://mrdoob.com/
 */

5 6 7 8 9 10
var Editor = function () {

	var SIGNALS = signals;

	this.signals = {

M
Mr.doob 已提交
11 12 13 14 15
		// player

		startPlayer: new SIGNALS.Signal(),
		stopPlayer: new SIGNALS.Signal(),

16 17
		// actions

18 19
		playAnimation: new SIGNALS.Signal(),
		stopAnimation: new SIGNALS.Signal(),
20

21 22
		showDialog: new SIGNALS.Signal(),

23 24
		// notifications

M
Mr.doob 已提交
25 26 27
		savingStarted: new SIGNALS.Signal(),
		savingFinished: new SIGNALS.Signal(),

28 29
		themeChanged: new SIGNALS.Signal(),

30 31
		transformModeChanged: new SIGNALS.Signal(),
		snapChanged: new SIGNALS.Signal(),
32
		spaceChanged: new SIGNALS.Signal(),
33
		rendererChanged: new SIGNALS.Signal(),
M
Mr.doob 已提交
34

35 36
		sceneGraphChanged: new SIGNALS.Signal(),

37 38
		cameraChanged: new SIGNALS.Signal(),

M
Mr.doob 已提交
39 40
		geometryChanged: new SIGNALS.Signal(),

M
Mr.doob 已提交
41
		objectSelected: new SIGNALS.Signal(),
42 43
		objectFocused: new SIGNALS.Signal(),

44 45
		objectAdded: new SIGNALS.Signal(),
		objectChanged: new SIGNALS.Signal(),
M
Mr.doob 已提交
46
		objectRemoved: new SIGNALS.Signal(),
M
Mr.doob 已提交
47 48

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

51 52 53 54
		materialChanged: new SIGNALS.Signal(),
		fogTypeChanged: new SIGNALS.Signal(),
		fogColorChanged: new SIGNALS.Signal(),
		fogParametersChanged: new SIGNALS.Signal(),
C
Chris Jubb 已提交
55 56 57
		windowResize: new SIGNALS.Signal(),

		showGridChanged: new SIGNALS.Signal()
58 59

	};
M
Mr.doob 已提交
60

M
Mr.doob 已提交
61
	this.config = new Config();
62
	this.storage = new Storage();
63 64
	this.loader = new Loader( this );

M
Mr.doob 已提交
65
	this.camera = new THREE.PerspectiveCamera( 50, 1, 0.1, 100000 );
66 67
	this.camera.name = 'Camera';

68
	this.scene = new THREE.Scene();
69
	this.scene.name = 'Scene';
M
Mr.doob 已提交
70

71
	this.sceneHelpers = new THREE.Scene();
72 73 74 75 76

	this.object = {};
	this.geometries = {};
	this.materials = {};
	this.textures = {};
M
Mr.doob 已提交
77
	// this.scripts = {};
78

79 80 81
	this.selected = null;
	this.helpers = {};

82 83 84 85
};

Editor.prototype = {

86 87 88 89 90 91 92 93
	setTheme: function ( value ) {

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

		this.signals.themeChanged.dispatch( value );

	},

94
	showDialog: function ( value ) {
M
Mr.doob 已提交
95

96
		this.signals.showDialog.dispatch( value );
M
Mr.doob 已提交
97

98
	},
M
Mr.doob 已提交
99

100 101
	//

102 103
	setScene: function ( scene ) {

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

107 108 109 110
		// avoid render per object

		this.signals.sceneGraphChanged.active = false;

111 112 113 114 115 116
		while ( scene.children.length > 0 ) {

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

		}

117 118 119
		this.signals.sceneGraphChanged.active = true;
		this.signals.sceneGraphChanged.dispatch();

120 121 122 123
	},

	//

124 125
	addObject: function ( object ) {

126 127 128 129
		var scope = this;

		object.traverse( function ( child ) {

130 131 132
			if ( child.geometry !== undefined ) scope.addGeometry( child.geometry );
			if ( child.material !== undefined ) scope.addMaterial( child.material );

133 134 135 136
			scope.addHelper( child );

		} );

137 138 139
		this.scene.add( object );

		this.signals.objectAdded.dispatch( object );
140
		this.signals.sceneGraphChanged.dispatch();
141 142 143

	},

144 145 146 147 148 149 150
	setObjectName: function ( object, name ) {

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

	},

151 152
	removeObject: function ( object ) {

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

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

157 158 159 160 161 162 163 164
		var scope = this;

		object.traverse( function ( child ) {

			scope.removeHelper( child );

		} );

165
		object.parent.remove( object );
M
Mr.doob 已提交
166 167

		this.signals.objectRemoved.dispatch( object );
168
		this.signals.sceneGraphChanged.dispatch();
169 170 171

	},

172
	addGeometry: function ( geometry ) {
173

174
		this.geometries[ geometry.uuid ] = geometry;
175 176 177

	},

178 179 180 181 182 183 184
	setGeometryName: function ( geometry, name ) {

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

	},

185 186
	addMaterial: function ( material ) {

187
		this.materials[ material.uuid ] = material;
188 189 190

	},

191 192 193 194 195 196 197
	setMaterialName: function ( material, name ) {

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

	},

198 199
	addTexture: function ( texture ) {

200
		this.textures[ texture.uuid ] = texture;
201 202 203

	},

204 205
	//

M
Mr.doob 已提交
206
	addHelper: function () {
207

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

M
Mr.doob 已提交
211
		return function ( object ) {
M
Mr.doob 已提交
212

M
Mr.doob 已提交
213
			var helper;
214

M
Mr.doob 已提交
215
			if ( object instanceof THREE.Camera ) {
216

M
Mr.doob 已提交
217
				helper = new THREE.CameraHelper( object, 10 );
218 219

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

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

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

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

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

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

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

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

M
Mr.doob 已提交
235 236 237 238
			} else if ( object instanceof THREE.SkinnedMesh ) {

				helper = new THREE.SkeletonHelper( object );

M
Mr.doob 已提交
239
			} else {
M
Mr.doob 已提交
240

M
Mr.doob 已提交
241 242
				// no helper for this object type
				return;
M
Mr.doob 已提交
243

M
Mr.doob 已提交
244
			}
M
Mr.doob 已提交
245

M
Mr.doob 已提交
246 247 248 249 250
			var picker = new THREE.Mesh( geometry, material );
			picker.name = 'picker';
			picker.userData.object = object;
			picker.visible = false;
			helper.add( picker );
M
Mr.doob 已提交
251

M
Mr.doob 已提交
252 253
			this.sceneHelpers.add( helper );
			this.helpers[ object.id ] = helper;
M
Mr.doob 已提交
254

M
Mr.doob 已提交
255
			this.signals.helperAdded.dispatch( helper );
M
Mr.doob 已提交
256 257 258 259

		};

	}(),
260 261 262

	removeHelper: function ( object ) {

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

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

268 269
			delete this.helpers[ object.id ];

M
Mr.doob 已提交
270 271
			this.signals.helperRemoved.dispatch( helper );

272 273
		}

274 275 276 277
	},

	//

278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
	parent: function ( object, parent ) {

		if ( parent === undefined ) {

			parent = this.scene;

		}

		parent.add( object );

		this.signals.sceneGraphChanged.dispatch();

	},

	//

294 295
	select: function ( object ) {

296
		if ( this.selected === object ) return;
297

298
		var uuid = null;
299

300
		if ( object !== null ) {
301

302
			uuid = object.uuid;
303 304 305

		}

306 307 308
		this.selected = object;

		this.config.setKey( 'selected', uuid );
309 310
		this.signals.objectSelected.dispatch( object );

311 312
	},

313 314
	selectById: function ( id ) {

315 316 317 318 319 320 321
		if ( id === this.camera.id ) {

			this.select( this.camera );
			return;

		}

322
		this.select( this.scene.getObjectById( id, true ) );
323

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

	selectByUuid: function ( uuid ) {

		var scope = this;

		this.scene.traverse( function ( child ) {

			if ( child.uuid === uuid ) {

				scope.select( child );

			}

		} );
339 340 341 342 343

	},

	deselect: function () {

M
Mr.doob 已提交
344
		this.select( null );
345

346 347 348 349 350 351 352 353 354 355 356 357
	},

	focus: function ( object ) {

		this.signals.objectFocused.dispatch( object );

	},

	focusById: function ( id ) {

		this.focus( this.scene.getObjectById( id, true ) );

358 359 360
	}

}