1 | var Editor = function () {
|
---|
2 |
|
---|
3 | var SIGNALS = signals;
|
---|
4 |
|
---|
5 | this.signals = {
|
---|
6 |
|
---|
7 | // actions
|
---|
8 |
|
---|
9 | playAnimations: new SIGNALS.Signal(),
|
---|
10 |
|
---|
11 | // notifications
|
---|
12 |
|
---|
13 | themeChanged: new SIGNALS.Signal(),
|
---|
14 |
|
---|
15 | transformModeChanged: new SIGNALS.Signal(),
|
---|
16 | snapChanged: new SIGNALS.Signal(),
|
---|
17 | spaceChanged: new SIGNALS.Signal(),
|
---|
18 | rendererChanged: new SIGNALS.Signal(),
|
---|
19 |
|
---|
20 | sceneGraphChanged: new SIGNALS.Signal(),
|
---|
21 |
|
---|
22 | cameraChanged: new SIGNALS.Signal(),
|
---|
23 |
|
---|
24 | objectSelected: new SIGNALS.Signal(),
|
---|
25 | objectAdded: new SIGNALS.Signal(),
|
---|
26 | objectChanged: new SIGNALS.Signal(),
|
---|
27 | objectRemoved: new SIGNALS.Signal(),
|
---|
28 |
|
---|
29 | helperAdded: new SIGNALS.Signal(),
|
---|
30 | helperRemoved: new SIGNALS.Signal(),
|
---|
31 |
|
---|
32 | materialChanged: new SIGNALS.Signal(),
|
---|
33 | fogTypeChanged: new SIGNALS.Signal(),
|
---|
34 | fogColorChanged: new SIGNALS.Signal(),
|
---|
35 | fogParametersChanged: new SIGNALS.Signal(),
|
---|
36 | windowResize: new SIGNALS.Signal()
|
---|
37 |
|
---|
38 | };
|
---|
39 |
|
---|
40 | this.config = new Config();
|
---|
41 | this.storage = new Storage();
|
---|
42 | this.loader = new Loader( this );
|
---|
43 |
|
---|
44 | this.scene = new THREE.Scene();
|
---|
45 | this.sceneHelpers = new THREE.Scene();
|
---|
46 |
|
---|
47 | this.object = {};
|
---|
48 | this.geometries = {};
|
---|
49 | this.materials = {};
|
---|
50 | this.textures = {};
|
---|
51 |
|
---|
52 | this.selected = null;
|
---|
53 | this.helpers = {};
|
---|
54 |
|
---|
55 | };
|
---|
56 |
|
---|
57 | Editor.prototype = {
|
---|
58 |
|
---|
59 | setTheme: function ( value ) {
|
---|
60 |
|
---|
61 | document.getElementById( 'theme' ).href = value;
|
---|
62 |
|
---|
63 | this.signals.themeChanged.dispatch( value );
|
---|
64 |
|
---|
65 | },
|
---|
66 |
|
---|
67 | setScene: function ( scene ) {
|
---|
68 |
|
---|
69 | this.scene.name = scene.name;
|
---|
70 | this.scene.userData = JSON.parse( JSON.stringify( scene.userData ) );
|
---|
71 |
|
---|
72 | // avoid render per object
|
---|
73 |
|
---|
74 | this.signals.sceneGraphChanged.active = false;
|
---|
75 |
|
---|
76 | while ( scene.children.length > 0 ) {
|
---|
77 |
|
---|
78 | this.addObject( scene.children[ 0 ] );
|
---|
79 |
|
---|
80 | }
|
---|
81 |
|
---|
82 | this.signals.sceneGraphChanged.active = true;
|
---|
83 | this.signals.sceneGraphChanged.dispatch();
|
---|
84 |
|
---|
85 | },
|
---|
86 |
|
---|
87 | //
|
---|
88 |
|
---|
89 | addObject: function ( object ) {
|
---|
90 |
|
---|
91 | var scope = this;
|
---|
92 |
|
---|
93 | object.traverse( function ( child ) {
|
---|
94 |
|
---|
95 | if ( child.geometry !== undefined ) scope.addGeometry( child.geometry );
|
---|
96 | if ( child.material !== undefined ) scope.addMaterial( child.material );
|
---|
97 |
|
---|
98 | scope.addHelper( child );
|
---|
99 |
|
---|
100 | } );
|
---|
101 |
|
---|
102 | this.scene.add( object );
|
---|
103 |
|
---|
104 | this.signals.objectAdded.dispatch( object );
|
---|
105 | this.signals.sceneGraphChanged.dispatch();
|
---|
106 |
|
---|
107 | },
|
---|
108 |
|
---|
109 | setObjectName: function ( object, name ) {
|
---|
110 |
|
---|
111 | object.name = name;
|
---|
112 | this.signals.sceneGraphChanged.dispatch();
|
---|
113 |
|
---|
114 | },
|
---|
115 |
|
---|
116 | removeObject: function ( object ) {
|
---|
117 |
|
---|
118 | if ( object.parent === undefined ) return; // avoid deleting the camera or scene
|
---|
119 |
|
---|
120 | if ( confirm( 'Delete ' + object.name + '?' ) === false ) return;
|
---|
121 |
|
---|
122 | var scope = this;
|
---|
123 |
|
---|
124 | object.traverse( function ( child ) {
|
---|
125 |
|
---|
126 | scope.removeHelper( child );
|
---|
127 |
|
---|
128 | } );
|
---|
129 |
|
---|
130 | object.parent.remove( object );
|
---|
131 |
|
---|
132 | this.signals.objectRemoved.dispatch( object );
|
---|
133 | this.signals.sceneGraphChanged.dispatch();
|
---|
134 |
|
---|
135 | },
|
---|
136 |
|
---|
137 | addGeometry: function ( geometry ) {
|
---|
138 |
|
---|
139 | this.geometries[ geometry.uuid ] = geometry;
|
---|
140 |
|
---|
141 | },
|
---|
142 |
|
---|
143 | setGeometryName: function ( geometry, name ) {
|
---|
144 |
|
---|
145 | geometry.name = name;
|
---|
146 | this.signals.sceneGraphChanged.dispatch();
|
---|
147 |
|
---|
148 | },
|
---|
149 |
|
---|
150 | addMaterial: function ( material ) {
|
---|
151 |
|
---|
152 | this.materials[ material.uuid ] = material;
|
---|
153 |
|
---|
154 | },
|
---|
155 |
|
---|
156 | setMaterialName: function ( material, name ) {
|
---|
157 |
|
---|
158 | material.name = name;
|
---|
159 | this.signals.sceneGraphChanged.dispatch();
|
---|
160 |
|
---|
161 | },
|
---|
162 |
|
---|
163 | addTexture: function ( texture ) {
|
---|
164 |
|
---|
165 | this.textures[ texture.uuid ] = texture;
|
---|
166 |
|
---|
167 | },
|
---|
168 |
|
---|
169 | //
|
---|
170 |
|
---|
171 | addHelper: function () {
|
---|
172 |
|
---|
173 | var geometry = new THREE.SphereGeometry( 20, 4, 2 );
|
---|
174 | var material = new THREE.MeshBasicMaterial( { color: 0xff0000 } );
|
---|
175 |
|
---|
176 | return function ( object ) {
|
---|
177 |
|
---|
178 | if ( object instanceof THREE.Camera ) {
|
---|
179 |
|
---|
180 | var picker = new THREE.Mesh( geometry, material );
|
---|
181 | picker.name = 'picker';
|
---|
182 | picker.userData.object = object;
|
---|
183 | picker.visible = false;
|
---|
184 |
|
---|
185 | var helper = new THREE.CameraHelper( object, 10 );
|
---|
186 | helper.add( picker );
|
---|
187 |
|
---|
188 | this.sceneHelpers.add( helper );
|
---|
189 | this.helpers[ object.id ] = helper;
|
---|
190 |
|
---|
191 | this.signals.helperAdded.dispatch( helper );
|
---|
192 |
|
---|
193 | } else if ( object instanceof THREE.PointLight ) {
|
---|
194 |
|
---|
195 | var picker = new THREE.Mesh( geometry, material );
|
---|
196 | picker.name = 'picker';
|
---|
197 | picker.userData.object = object;
|
---|
198 | picker.visible = false;
|
---|
199 |
|
---|
200 | var helper = new THREE.PointLightHelper( object, 10 );
|
---|
201 | helper.add( picker );
|
---|
202 |
|
---|
203 | this.sceneHelpers.add( helper );
|
---|
204 | this.helpers[ object.id ] = helper;
|
---|
205 |
|
---|
206 | this.signals.helperAdded.dispatch( helper );
|
---|
207 |
|
---|
208 | } else if ( object instanceof THREE.DirectionalLight ) {
|
---|
209 |
|
---|
210 | var picker = new THREE.Mesh( geometry, material );
|
---|
211 | picker.name = 'picker';
|
---|
212 | picker.userData.object = object;
|
---|
213 | picker.visible = false;
|
---|
214 |
|
---|
215 | var helper = new THREE.DirectionalLightHelper( object, 20 );
|
---|
216 | helper.add( picker );
|
---|
217 |
|
---|
218 | this.sceneHelpers.add( helper );
|
---|
219 | this.helpers[ object.id ] = helper;
|
---|
220 |
|
---|
221 | this.signals.helperAdded.dispatch( helper );
|
---|
222 |
|
---|
223 | } else if ( object instanceof THREE.SpotLight ) {
|
---|
224 |
|
---|
225 | var picker = new THREE.Mesh( geometry, material );
|
---|
226 | picker.name = 'picker';
|
---|
227 | picker.userData.object = object;
|
---|
228 | picker.visible = false;
|
---|
229 |
|
---|
230 | var helper = new THREE.SpotLightHelper( object, 10 );
|
---|
231 | helper.add( picker );
|
---|
232 |
|
---|
233 | this.sceneHelpers.add( helper );
|
---|
234 | this.helpers[ object.id ] = helper;
|
---|
235 |
|
---|
236 | this.signals.helperAdded.dispatch( helper );
|
---|
237 |
|
---|
238 | } else if ( object instanceof THREE.HemisphereLight ) {
|
---|
239 |
|
---|
240 | var picker = new THREE.Mesh( geometry, material );
|
---|
241 | picker.name = 'picker';
|
---|
242 | picker.userData.object = object;
|
---|
243 | picker.visible = false;
|
---|
244 |
|
---|
245 | var helper = new THREE.HemisphereLightHelper( object, 10 );
|
---|
246 | helper.add( picker );
|
---|
247 |
|
---|
248 | this.sceneHelpers.add( helper );
|
---|
249 | this.helpers[ object.id ] = helper;
|
---|
250 |
|
---|
251 | this.signals.helperAdded.dispatch( helper );
|
---|
252 |
|
---|
253 | }
|
---|
254 |
|
---|
255 | };
|
---|
256 |
|
---|
257 | }(),
|
---|
258 |
|
---|
259 | removeHelper: function ( object ) {
|
---|
260 |
|
---|
261 | if ( this.helpers[ object.id ] !== undefined ) {
|
---|
262 |
|
---|
263 | var helper = this.helpers[ object.id ];
|
---|
264 | helper.parent.remove( helper );
|
---|
265 |
|
---|
266 | delete this.helpers[ object.id ];
|
---|
267 |
|
---|
268 | this.signals.helperRemoved.dispatch( helper );
|
---|
269 |
|
---|
270 | }
|
---|
271 |
|
---|
272 | },
|
---|
273 |
|
---|
274 | //
|
---|
275 |
|
---|
276 | parent: function ( object, parent ) {
|
---|
277 |
|
---|
278 | if ( parent === undefined ) {
|
---|
279 |
|
---|
280 | parent = this.scene;
|
---|
281 |
|
---|
282 | }
|
---|
283 |
|
---|
284 | parent.add( object );
|
---|
285 |
|
---|
286 | this.signals.sceneGraphChanged.dispatch();
|
---|
287 |
|
---|
288 | },
|
---|
289 |
|
---|
290 | //
|
---|
291 |
|
---|
292 | select: function ( object ) {
|
---|
293 |
|
---|
294 | this.selected = object;
|
---|
295 |
|
---|
296 | if ( object !== null ) {
|
---|
297 |
|
---|
298 | this.config.setKey( 'selected', object.uuid );
|
---|
299 |
|
---|
300 | } else {
|
---|
301 |
|
---|
302 | this.config.setKey( 'selected', null );
|
---|
303 |
|
---|
304 | }
|
---|
305 |
|
---|
306 | this.signals.objectSelected.dispatch( object );
|
---|
307 |
|
---|
308 | },
|
---|
309 |
|
---|
310 | selectById: function ( id ) {
|
---|
311 |
|
---|
312 | var scope = this;
|
---|
313 |
|
---|
314 | this.scene.traverse( function ( child ) {
|
---|
315 |
|
---|
316 | if ( child.id === id ) {
|
---|
317 |
|
---|
318 | scope.select( child );
|
---|
319 |
|
---|
320 | }
|
---|
321 |
|
---|
322 | } );
|
---|
323 |
|
---|
324 | },
|
---|
325 |
|
---|
326 | selectByUuid: function ( uuid ) {
|
---|
327 |
|
---|
328 | var scope = this;
|
---|
329 |
|
---|
330 | this.scene.traverse( function ( child ) {
|
---|
331 |
|
---|
332 | if ( child.uuid === uuid ) {
|
---|
333 |
|
---|
334 | scope.select( child );
|
---|
335 |
|
---|
336 | }
|
---|
337 |
|
---|
338 | } );
|
---|
339 |
|
---|
340 | },
|
---|
341 |
|
---|
342 | deselect: function () {
|
---|
343 |
|
---|
344 | this.select( null );
|
---|
345 |
|
---|
346 | },
|
---|
347 |
|
---|
348 | // utils
|
---|
349 |
|
---|
350 | getObjectType: function ( object ) {
|
---|
351 |
|
---|
352 | var types = {
|
---|
353 |
|
---|
354 | 'Scene': THREE.Scene,
|
---|
355 | 'PerspectiveCamera': THREE.PerspectiveCamera,
|
---|
356 | 'AmbientLight': THREE.AmbientLight,
|
---|
357 | 'DirectionalLight': THREE.DirectionalLight,
|
---|
358 | 'HemisphereLight': THREE.HemisphereLight,
|
---|
359 | 'PointLight': THREE.PointLight,
|
---|
360 | 'SpotLight': THREE.SpotLight,
|
---|
361 | 'Mesh': THREE.Mesh,
|
---|
362 | 'Sprite': THREE.Sprite,
|
---|
363 | 'Object3D': THREE.Object3D
|
---|
364 |
|
---|
365 | };
|
---|
366 |
|
---|
367 | for ( var type in types ) {
|
---|
368 |
|
---|
369 | if ( object instanceof types[ type ] ) return type;
|
---|
370 |
|
---|
371 | }
|
---|
372 |
|
---|
373 | },
|
---|
374 |
|
---|
375 | getGeometryType: function ( geometry ) {
|
---|
376 |
|
---|
377 | var types = {
|
---|
378 |
|
---|
379 | 'CircleGeometry': THREE.CircleGeometry,
|
---|
380 | 'CubeGeometry': THREE.CubeGeometry,
|
---|
381 | 'CylinderGeometry': THREE.CylinderGeometry,
|
---|
382 | 'ExtrudeGeometry': THREE.ExtrudeGeometry,
|
---|
383 | 'IcosahedronGeometry': THREE.IcosahedronGeometry,
|
---|
384 | 'LatheGeometry': THREE.LatheGeometry,
|
---|
385 | 'OctahedronGeometry': THREE.OctahedronGeometry,
|
---|
386 | 'ParametricGeometry': THREE.ParametricGeometry,
|
---|
387 | 'PlaneGeometry': THREE.PlaneGeometry,
|
---|
388 | 'PolyhedronGeometry': THREE.PolyhedronGeometry,
|
---|
389 | 'ShapeGeometry': THREE.ShapeGeometry,
|
---|
390 | 'SphereGeometry': THREE.SphereGeometry,
|
---|
391 | 'TetrahedronGeometry': THREE.TetrahedronGeometry,
|
---|
392 | 'TextGeometry': THREE.TextGeometry,
|
---|
393 | 'TorusGeometry': THREE.TorusGeometry,
|
---|
394 | 'TorusKnotGeometry': THREE.TorusKnotGeometry,
|
---|
395 | 'TubeGeometry': THREE.TubeGeometry,
|
---|
396 | 'Geometry': THREE.Geometry,
|
---|
397 | 'BufferGeometry': THREE.BufferGeometry
|
---|
398 |
|
---|
399 | };
|
---|
400 |
|
---|
401 | for ( var type in types ) {
|
---|
402 |
|
---|
403 | if ( geometry instanceof types[ type ] ) return type;
|
---|
404 |
|
---|
405 | }
|
---|
406 |
|
---|
407 | },
|
---|
408 |
|
---|
409 | getMaterialType: function ( material ) {
|
---|
410 |
|
---|
411 | var types = {
|
---|
412 |
|
---|
413 | 'LineBasicMaterial': THREE.LineBasicMaterial,
|
---|
414 | 'LineDashedMaterial': THREE.LineDashedMaterial,
|
---|
415 | 'MeshBasicMaterial': THREE.MeshBasicMaterial,
|
---|
416 | 'MeshDepthMaterial': THREE.MeshDepthMaterial,
|
---|
417 | 'MeshFaceMaterial': THREE.MeshFaceMaterial,
|
---|
418 | 'MeshLambertMaterial': THREE.MeshLambertMaterial,
|
---|
419 | 'MeshNormalMaterial': THREE.MeshNormalMaterial,
|
---|
420 | 'MeshPhongMaterial': THREE.MeshPhongMaterial,
|
---|
421 | 'ParticleSystemMaterial': THREE.ParticleSystemMaterial,
|
---|
422 | 'ShaderMaterial': THREE.ShaderMaterial,
|
---|
423 | 'SpriteCanvasMaterial': THREE.SpriteCanvasMaterial,
|
---|
424 | 'SpriteMaterial': THREE.SpriteMaterial,
|
---|
425 | 'Material': THREE.Material
|
---|
426 |
|
---|
427 | };
|
---|
428 |
|
---|
429 | for ( var type in types ) {
|
---|
430 |
|
---|
431 | if ( material instanceof types[ type ] ) return type;
|
---|
432 |
|
---|
433 | }
|
---|
434 |
|
---|
435 | }
|
---|
436 |
|
---|
437 | }
|
---|