Editor.js 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100
  1. var Editor = function ( scene ) {
  2. this.geometries = {};
  3. this.materials = {};
  4. this.textures = {};
  5. this.objects = {};
  6. this.selected = {};
  7. this.helpers = {};
  8. this.scene = new THREE.Scene();
  9. this.scene.name = ( scene && scene.name ) ? scene.name : 'Scene';
  10. this.addObject( this.scene );
  11. this.sceneHelpers = new THREE.Scene();
  12. this.defaultMaterial = new THREE.MeshPhongMaterial();
  13. this.defaultMaterial.name = 'Default Material';
  14. this.addMaterial( this.defaultMaterial );
  15. }
  16. Editor.prototype = {
  17. // Assets
  18. setScene: function( scene ) {
  19. this.deleteAll(); // WARNING! deletes everything
  20. if ( scene ) {
  21. this.scene.name = scene.name;
  22. this.scene.userData = JSON.parse( JSON.stringify( scene.userData ) );
  23. if ( scene.children.length ) this.addObject( scene.children );
  24. }
  25. signals.sceneChanged.dispatch( this.scene );
  26. return this.scene
  27. },
  28. createObject: function( type, parameters, material ) {
  29. type = type ? type : 'Group';
  30. var object;
  31. var geometry;
  32. material = material ? material : this.defaultMaterial;
  33. parameters = parameters ? parameters : {};
  34. var name = parameters.name ? parameters.name : this.incrementName( type, 'object' );
  35. var color = parameters.color ? parameters.color : null;
  36. var groundColor = parameters.groundColor ? parameters.groundColor : null;
  37. var intensity = parameters.intensity ? parameters.intensity : null;
  38. var distance = parameters.distance ? parameters.distance : null;
  39. var angle = parameters.angle ? parameters.angle : null;
  40. var exponent = parameters.exponent ? parameters.exponent : null;
  41. if ( type == 'Group' ) {
  42. object = new THREE.Object3D();
  43. } else if ( type == 'Plane' ) {
  44. geometry = this.createGeometry( type, parameters );
  45. object = new THREE.Mesh( geometry, this.defaultMaterial );
  46. object.rotation.x = - Math.PI/2;
  47. } else if ( type == 'Cube' ) {
  48. geometry = this.createGeometry( type, parameters );
  49. object = new THREE.Mesh( geometry, this.defaultMaterial );
  50. } else if ( type == 'Cylinder' ) {
  51. geometry = this.createGeometry( type, parameters );
  52. object = new THREE.Mesh( geometry, this.defaultMaterial );
  53. } else if ( type == 'Sphere' ) {
  54. geometry = this.createGeometry( type, parameters );
  55. object = new THREE.Mesh( geometry, this.defaultMaterial );
  56. } else if ( type == 'Icosahedron' ) {
  57. geometry = this.createGeometry( type, parameters );
  58. object = new THREE.Mesh( geometry, this.defaultMaterial );
  59. } else if ( type == 'Torus' ) {
  60. geometry = this.createGeometry( type, parameters );
  61. object = new THREE.Mesh( geometry, this.defaultMaterial );
  62. } else if ( type == 'TorusKnot' ) {
  63. geometry = this.createGeometry( type, parameters );
  64. object = new THREE.Mesh( geometry, this.defaultMaterial );
  65. } else if ( type == 'PointLight' ) {
  66. color = color ? color : 0xffffff;
  67. intensity = intensity ? intensity : 1;
  68. distance = distance ? distance : 0;
  69. object = new THREE.PointLight( color, intensity, distance );
  70. } else if ( type == 'SpotLight' ) {
  71. color = color ? color : 0xffffff;
  72. intensity = intensity ? intensity : 1;
  73. distance = distance ? distance : 0;
  74. angle = angle ? angle : Math.PI * 0.1;
  75. exponent = exponent ? exponent : 10;
  76. object = new THREE.SpotLight( color, intensity, distance, angle, exponent );
  77. object.target.name = object.name + ' Target';
  78. object.position.set( 0, 1, 0 ).multiplyScalar( 200 );
  79. } else if ( type == 'DirectionalLight' ) {
  80. color = color ? color : 0xffffff;
  81. intensity = intensity ? intensity : 1;
  82. object = new THREE.DirectionalLight( color, intensity );
  83. object.target.name = object.name + ' Target';
  84. object.position.set( 1, 1, 1 ).multiplyScalar( 200 );
  85. } else if ( type == 'HemisphereLight' ) {
  86. color = color ? color : 0x00aaff;
  87. groundColor = groundColor ? groundColor : 0xffaa00;
  88. intensity = intensity ? intensity : 1;
  89. object = new THREE.HemisphereLight( color, groundColor, intensity );
  90. object.position.set( 1, 1, 1 ).multiplyScalar( 200 );
  91. } else if ( type == 'AmbientLight' ) {
  92. color = color ? color : 0x222222;
  93. object = new THREE.AmbientLight( color );
  94. }
  95. object.name = name;
  96. this.addObject( object );
  97. return object;
  98. },
  99. createGeometry: function( type, parameters ) {
  100. type = type ? type : 'Geometry';
  101. parameters = parameters ? parameters : {};
  102. var name = parameters.name ? parameters.name : this.incrementName( type + 'Geometry', 'geometry' );
  103. var width = parameters.width ? parameters.width : null;
  104. var height = parameters.height ? parameters.height : null;
  105. var depth = parameters.depth ? parameters.depth : null;
  106. var widthSegments = parameters.widthSegments ? parameters.widthSegments : null;
  107. var heightSegments = parameters.heightSegments ? parameters.heightSegments : null;
  108. var depthSegments = parameters.depthSegments ? parameters.depthSegments : null;
  109. var radialSegments = parameters.radialSegments ? parameters.radialSegments : null;
  110. var tubularSegments = parameters.tubularSegments ? parameters.tubularSegments : null;
  111. var radius = parameters.radius ? parameters.radius : null;
  112. var radiusTop = parameters.radiusTop ? parameters.radiusTop : null;
  113. var radiusBottom = parameters.radiusBottom ? parameters.radiusBottom : null;
  114. var phiStart = parameters.phiStart ? parameters.phiStart : null;
  115. var phiLength = parameters.phiLength ? parameters.phiLength : null;
  116. var thetaStart = parameters.thetaStart ? parameters.thetaStart : null;
  117. var thetaLength = parameters.thetaLength ? parameters.thetaLength : null;
  118. var tube = parameters.tube ? parameters.tube : null;
  119. var arc = parameters.arc ? parameters.arc : null;
  120. var detail = parameters.detail ? parameters.detail : null;
  121. var p = parameters.p ? parameters.p : null;
  122. var q = parameters.q ? parameters.q : null;
  123. var heightScale = parameters.heightScale ? parameters.heightScale : null;
  124. var openEnded = parameters.openEnded ? parameters.openEnded : false;
  125. var geometry;
  126. if ( type == 'Geometry' ) {
  127. geometry = new THREE.Geometry();
  128. } else if ( type == 'Plane' ) {
  129. width = width ? width : 200;
  130. height = height ? height : 200;
  131. widthSegments = widthSegments ? widthSegments : 1;
  132. heightSegments = heightSegments ? heightSegments : 1;
  133. geometry = new THREE.PlaneGeometry( width, height, widthSegments, heightSegments );
  134. } else if ( type == 'Cube' ) {
  135. width = width ? width : 100;
  136. height = height ? height : 100;
  137. depth = depth ? depth : 100;
  138. widthSegments = widthSegments ? widthSegments : 1;
  139. heightSegments = heightSegments ? heightSegments : 1;
  140. depthSegments = depthSegments ? depthSegments : 1;
  141. geometry = new THREE.CubeGeometry( width, height, depth, widthSegments, heightSegments, depthSegments );
  142. } else if ( type == 'Cylinder' ) {
  143. radiusTop = radiusTop ? radiusTop : 20;
  144. radiusBottom = radiusBottom ? radiusBottom : 20;
  145. height = height ? height : 100;
  146. radialSegments = radialSegments ? radialSegments : 8;
  147. heightSegments = heightSegments ? heightSegments : 1;
  148. openEnded = openEnded ? openEnded : false;
  149. geometry = new THREE.CylinderGeometry( radiusTop, radiusBottom, height, radialSegments, heightSegments, openEnded );
  150. } else if ( type == 'Sphere' ) {
  151. radius = radius ? radius : 75;
  152. widthSegments = widthSegments ? widthSegments : 32;
  153. heightSegments = heightSegments ? heightSegments : 16;
  154. widthSegments = widthSegments ? widthSegments : 32;
  155. heightSegments = heightSegments ? heightSegments : 16;
  156. phiStart = phiStart ? phiStart : 0;
  157. phiLength = phiLength ? phiLength : Math.PI * 2;
  158. thetaStart = thetaStart ? thetaStart : 0;
  159. thetaLength = thetaLength ? thetaLength : Math.PI;
  160. geometry = new THREE.SphereGeometry( radius, widthSegments, heightSegments, phiStart, phiLength, thetaStart, thetaLength );
  161. } else if ( type == 'Icosahedron' ) {
  162. radius = radius ? radius : 75;
  163. detail = detail ? detail : 2;
  164. geometry = new THREE.IcosahedronGeometry ( radius, detail );
  165. } else if ( type == 'Torus' ) {
  166. radius = radius ? radius : 100;
  167. tube = tube ? tube : 40;
  168. radialSegments = radialSegments ? radialSegments : 8;
  169. tubularSegments = tubularSegments ? tubularSegments : 6;
  170. arc = arc ? arc : Math.PI * 2;
  171. geometry = new THREE.TorusGeometry( radius, tube, radialSegments, tubularSegments, arc );
  172. } else if ( type == 'TorusKnot' ) {
  173. radius = radius ? radius : 100;
  174. tube = tube ? tube : 40;
  175. radialSegments = radialSegments ? radialSegments : 64;
  176. tubularSegments = tubularSegments ? tubularSegments : 8;
  177. p = p ? p : 2;
  178. q = q ? q : 3;
  179. heightScale = heightScale ? heightScale : 1;
  180. geometry = new THREE.TorusKnotGeometry( radius, tube, radialSegments, tubularSegments, p, q, heightScale );
  181. }
  182. geometry.name = name;
  183. geometry.computeBoundingSphere();
  184. return geometry;
  185. },
  186. createMaterial: function( type, parameters ) {
  187. type = type ? type : 'MeshPhongMaterial';
  188. parameters = parameters ? parameters : {};
  189. var material;
  190. var name = parameters.name ? parameters.name : this.incrementName( type, 'material' );
  191. if ( type == 'MeshPhongMaterial' ) {
  192. material = new THREE.MeshPhongMaterial( parameters );
  193. } else if ( type == 'MeshLambertMaterial' ) {
  194. material = new THREE.MeshLambertMaterial( parameters );
  195. } else if ( type == 'MeshNormalMaterial' ) {
  196. material = new THREE.MeshNormalMaterial( parameters );
  197. } else if ( type == 'MeshBasicMaterial' ) {
  198. material = new THREE.MeshBasicMaterial( parameters );
  199. }
  200. material.name = name;
  201. this.addMaterial( material );
  202. return material;
  203. },
  204. createTexture: function( image, parameters ) {
  205. image = image ? image : '../examples/textures/ash_uvgrid01.jpg';
  206. parameters = parameters ? parameters : {};
  207. // TODO: implement parameters
  208. var texture = THREE.ImageUtils.loadTexture( image );
  209. texture.name = parameters.name ? parameters.name : this.incrementName( 'Texture', 'texture' );
  210. this.addTexture( texture );
  211. return texture;
  212. },
  213. addObject: function( list, parent ) {
  214. list = ( list instanceof Array ) ? [].concat( list ) : [ list ];
  215. parent = parent ? parent : this.scene;
  216. for ( var i in list ) {
  217. this.objects[ list[ i ].id ] = list[ i ];
  218. this.addHelper( list[ i ] );
  219. if ( list[ i ].target ) {
  220. this.objects[ list[ i ].target.id ] = list[ i ].target;
  221. }
  222. if ( list[ i ].material ) this.addMaterial( list[ i ].material );
  223. if ( list[ i ].geometry ) this.addGeometry( list[ i ].geometry );
  224. if ( parent != list[ i ] ) {
  225. // Add object to the scene
  226. parent.add( list[ i ] );
  227. if ( list[ i ] instanceof THREE.Light ) this.updateMaterials();
  228. signals.objectAdded.dispatch( list[ i ] );
  229. // Continue adding children
  230. if ( list[ i ].children && list[ i ].children.length ) {
  231. this.addObject( list[ i ].children, list[ i ] );
  232. }
  233. }
  234. }
  235. signals.sceneChanged.dispatch( this.scene );
  236. },
  237. addHelper: function( object ) {
  238. if ( object instanceof THREE.PointLight ) {
  239. this.helpers[ object.id ] = new THREE.PointLightHelper( object, 10 );
  240. this.sceneHelpers.add( this.helpers[ object.id ] );
  241. this.helpers[ object.id ].lightSphere.id = object.id;
  242. } else if ( object instanceof THREE.DirectionalLight ) {
  243. this.helpers[ object.id ] = new THREE.DirectionalLightHelper( object, 10 );
  244. this.sceneHelpers.add( this.helpers[ object.id ] );
  245. this.helpers[ object.id ].lightSphere.id = object.id;
  246. } else if ( object instanceof THREE.SpotLight ) {
  247. this.helpers[ object.id ] = new THREE.SpotLightHelper( object, 10 );
  248. this.sceneHelpers.add( this.helpers[ object.id ] );
  249. // this.helpers[ object.id ].lightSphere.id = object.id;
  250. } else if ( object instanceof THREE.HemisphereLight ) {
  251. this.helpers[ object.id ] = new THREE.HemisphereLightHelper( object, 10 );
  252. this.sceneHelpers.add( this.helpers[ object.id ] );
  253. this.helpers[ object.id ].lightSphere.id = object.id;
  254. }
  255. signals.sceneChanged.dispatch( this.scene );
  256. },
  257. deleteHelper: function( object ) {
  258. if ( this.helpers[ object.id ] ) {
  259. this.helpers[ object.id ].parent.remove( this.helpers[ object.id ] );
  260. delete this.helpers[ object.id ];
  261. }
  262. },
  263. addGeometry: function( geometry ) {
  264. this.geometries[ geometry.id ] = geometry;
  265. signals.geometryAdded.dispatch( geometry );
  266. },
  267. addMaterial: function( material ) {
  268. if ( material.name == 'Default Material' ) {
  269. this.delete( this.defaultMaterial );
  270. this.defaultMaterial = material;
  271. }
  272. this.materials[ material.id ] = material;
  273. signals.materialAdded.dispatch( material );
  274. signals.sceneChanged.dispatch( this.scene );
  275. },
  276. addTexture: function( texture ) {
  277. this.textures[ texture.id ] = texture;
  278. signals.textureAdded.dispatch( texture );
  279. signals.sceneChanged.dispatch( this.scene );
  280. },
  281. // Selection
  282. select: function( list, additive ) {
  283. //TODO toggle
  284. list = ( list instanceof Array ) ? list : [ list ];
  285. if ( !additive ) this.selected = {};
  286. for ( var i in list ) {
  287. this.selected[ list[ i ].id ] = list[ i ];
  288. }
  289. signals.selected.dispatch( this.selected );
  290. },
  291. selectById: function( id, additive ) {
  292. var list = this.list();
  293. if ( !additive ) this.selected = {};
  294. for ( var i in list ) {
  295. if ( list[ i ].id == id ) this.select( list[ i ], true );
  296. }
  297. },
  298. selectByName: function( name, type, additive ) {
  299. type = type ? type : "all";
  300. this.select( this.listByName( name, type ), additive );
  301. },
  302. selectAll: function( type, additive ) {
  303. type = type ? type : "all";
  304. this.select( this.listByName( '*', type ), additive );
  305. },
  306. deselect: function( list ) {
  307. list = ( list instanceof Array ) ? list : [ list ];
  308. for ( var i in list ) {
  309. if ( this.selected[ list[ i ].id ] ) delete this.selected[ list[ i ].id ];
  310. }
  311. signals.selected.dispatch( this.selected );
  312. },
  313. deselectById: function( id ) {
  314. if ( this.selected[ id ] ) delete this.selected[ id ];
  315. },
  316. deselectByName: function( name, type ) {
  317. type = type ? type : "all";
  318. this.deselect( this.listByName( name, type ) );
  319. },
  320. deselectAll: function( type ) {
  321. type = type ? type : "all";
  322. this.deselect( this.list( "all" ) );
  323. },
  324. pickWalk: function( direction ) {
  325. direction = direction.toLowerCase();
  326. var selection = this.listSelected();
  327. var newSelection = [];
  328. if ( direction === 'up' ) {
  329. for ( var i in selection ) {
  330. if ( selection[ i ].parent )
  331. newSelection.push( selection[ i ].parent );
  332. else newSelection.push( selection[ i ] );
  333. }
  334. } else if ( direction === 'down' ) {
  335. for ( var i in selection ) {
  336. if ( selection[ i ].children && selection[ i ].children.length )
  337. newSelection.push( selection[ i ].children[0] );
  338. else newSelection.push( selection[ i ] );
  339. }
  340. } else if ( direction === 'left' || direction === 'right' ) {
  341. for ( var i in selection ) {
  342. var siblings = null;
  343. var index = null;
  344. var newIndex = null;
  345. if ( selection[ i ].parent ) {
  346. siblings = selection[ i ].parent.children;
  347. index = selection[ i ].parent.children.indexOf( selection[ i ] );
  348. newIndex = index;
  349. if ( siblings.length > 1 && direction === 'left' )
  350. newIndex = ( index + siblings.length + 1 ) % siblings.length;
  351. else if ( siblings.length > 1 && direction === 'right' )
  352. newIndex = ( index + siblings.length - 1 ) % siblings.length;
  353. newSelection.push( siblings[ newIndex ] );
  354. } else {
  355. newSelection.push( selection[ i ] );
  356. }
  357. }
  358. }
  359. if ( newSelection.length ) this.select( newSelection );
  360. },
  361. // List
  362. list: function( type ) {
  363. type = type ? type : "all";
  364. var list = this.listByName( '*', type );
  365. return list;
  366. },
  367. listSelected: function( type ) {
  368. var list = this.listByName( '*', 'selected' );
  369. if ( type ) {
  370. var typeList = this.listByName( '*', type );
  371. var list = list.filter(function(n) {
  372. if(typeList.indexOf(n) == -1) return false;
  373. return true;
  374. });
  375. }
  376. return list;
  377. },
  378. listByName: function( name, type ) {
  379. type = type ? type.toLowerCase() : "all";
  380. var scope = this;
  381. var list = [];
  382. function listFromMap( map, name ) {
  383. for ( var id in map ) {
  384. if ( scope.regexMatch( map[ id ].name, name ) ) {
  385. list.push( map[ id ] );
  386. }
  387. }
  388. }
  389. if ( type == 'all' || type == 'object' ) {
  390. listFromMap( this.objects, name );
  391. }
  392. if ( type == 'all' || type == 'geometry' ) {
  393. listFromMap( this.geometries, name );
  394. }
  395. if ( type == 'all' || type == 'material' ) {
  396. listFromMap( this.materials, name );
  397. }
  398. if ( type == 'all' || type == 'texture' ) {
  399. listFromMap( this.textures, name );
  400. }
  401. if ( type == 'all' || type == 'selected' ) {
  402. listFromMap( this.selected, name );
  403. }
  404. return list;
  405. },
  406. // Delete
  407. delete: function( list ) {
  408. list = list ? list : this.list( 'selected' );
  409. list = ( list instanceof Array ) ? list : [ list ];
  410. this.deselect( list );
  411. var deletedObjects = {};
  412. for ( var i in list ) {
  413. if ( this.objects[ list[ i ].id ] && list[ i ] != this.scene ) {
  414. delete this.objects[ list[ i ].id ];
  415. this.deleteHelper( list[ i ] );
  416. deletedObjects[ list[ i ].id ] = list[ i ];
  417. if ( list[ i ] instanceof THREE.Light ) this.updateMaterials();
  418. signals.objectDeleted.dispatch();
  419. if ( list[ i ].children.length ) this.delete( list[ i ].children );
  420. }
  421. if ( this.geometries[ list[ i ].id ] ) {
  422. delete this.geometries[ list[ i ].id ];
  423. signals.objectDeleted.dispatch();
  424. }
  425. if ( this.materials[ list[ i ].id ] ) {
  426. delete this.materials[ list[ i ].id ];
  427. signals.materialDeleted.dispatch();
  428. }
  429. if ( this.textures[ list[ i ].id ] ) {
  430. delete this.textures[ list[ i ].id ];
  431. signals.textureDeleted.dispatch();
  432. }
  433. }
  434. for ( var i in deletedObjects ) {
  435. if ( deletedObjects[ i ].parent ) {
  436. deletedObjects[ i ].parent.remove( deletedObjects[ i ] );
  437. }
  438. }
  439. delete deletedObjects;
  440. signals.sceneChanged.dispatch( this.scene );
  441. },
  442. deleteByName: function( name, type ) {
  443. type = type ? type : "all";
  444. this.delete( this.listByName( name, type ) );
  445. },
  446. deleteAll: function( type ) {
  447. type = type ? type : 'all';
  448. this.delete( this.listByName( '*', type ) );
  449. },
  450. deleteUnused: function( type ) {
  451. // TODO: test with textures
  452. type = type ? type.toLowerCase() : 'all';
  453. var used = {};
  454. this.scene.traverse( function( object ) {
  455. used[ object.id ] = object;
  456. if ( object.geometry ) used[ object.geometry.id ] = object.geometry;
  457. if ( object.material ) {
  458. used[ object.material.id ] = object.material;
  459. for ( var i in object.material ){
  460. if ( object.material[ i ] instanceof THREE.Texture ) {
  461. used[ object.material[ i ].id ] = object.material[ i ];
  462. }
  463. }
  464. }
  465. } );
  466. if ( !type || type == 'object' ) {
  467. for ( var id in this.objects ) {
  468. if ( !used[ id ] ) this.delete( this.objects[ id ] );
  469. }
  470. }
  471. if ( !type || type == 'geometry' ) {
  472. for ( var id in this.geometries ) {
  473. if ( !used[ id ] ) this.delete( this.geometries[ id ] );
  474. }
  475. }
  476. if ( !type || type == 'material' ) {
  477. for ( var id in this.materials ) {
  478. if ( !used[ id ] ) this.delete( this.materials[ id ] );
  479. }
  480. }
  481. if ( !type || type == 'texture' ) {
  482. for ( var id in this.textures ) {
  483. if ( !used[ id ] ) this.delete( this.textures[ id ] );
  484. }
  485. }
  486. delete used;
  487. },
  488. // Hierarchy
  489. clone: function( assets, recursive, deep ) {
  490. // TODO: consider using list
  491. // TODO: Implement non-recursive and deep
  492. var assets = assets ? assets : this.selected;
  493. // recursive = recursive ? recursive : true;
  494. // deep = deep ? deep : false;
  495. var clones = {};
  496. for ( var i in assets ){
  497. if ( this.objects[ i ] ) {
  498. var clonedObject = this.objects[assets[ i ].id ].clone();
  499. clonedObject.traverse( function( child ) {
  500. child.name += ' Clone';
  501. } );
  502. this.addObject( clonedObject, assets[ i ].parent );
  503. clones[ clonedObject.id ] = clonedObject;
  504. }
  505. }
  506. return clones;
  507. },
  508. parent: function( list, parent, locked ) {
  509. //TODO: implement locked
  510. list = list ? list : this.list( 'selected' );
  511. list = ( list instanceof Array ) ? list : [ list ];
  512. parent = parent ? parent : this.scene;
  513. for ( var i in list ) {
  514. if ( list[ i ] !== parent && list[ i ] !== this.scene ) {
  515. parent.add( list[ i ] );
  516. signals.objectChanged.dispatch( list[ i ] );
  517. }
  518. }
  519. signals.sceneChanged.dispatch( this.scene );
  520. },
  521. unparent: function( list ) {
  522. this.parent( list, this.scene );
  523. },
  524. group: function( list ) {
  525. list = list ? list : this.listSelected( 'objects' );
  526. list = ( list instanceof Array ) ? list : [ list ];
  527. var parent = ( list.length && list[0].parent ) ? list[0].parent : this.scene;
  528. var group = this.createObject();
  529. this.parent( group, parent );
  530. this.parent( list, group );
  531. },
  532. // Utils
  533. updateObject: function( object, parameters ) {
  534. parameters = parameters ? parameters : {};
  535. if ( parameters.parent && object.parent && object.parent != parameters.parent)
  536. editor.parent( object, parameters.parent );
  537. if ( parameters.geometry && object.geometry && object.geometry != parameters.geometry) {
  538. object.geometry = parameters.geometry;
  539. this.updateGeometry( object.geometry );
  540. }
  541. if ( parameters.material && object.material && object.material != parameters.material)
  542. object.material = parameters.material;
  543. if ( parameters.name !== undefined ) object.name = parameters.name;
  544. if ( parameters.position !== undefined ) object.position = parameters.position;
  545. if ( parameters.rotation !== undefined ) object.rotation = parameters.rotation;
  546. if ( parameters.scale !== undefined ) object.scale = parameters.scale;
  547. if ( object.fov !== undefined && parameters.fov !== undefined ) object.fov = parameters.fov;
  548. if ( object.near !== undefined && parameters.near !== undefined ) object.near = parameters.near;
  549. if ( object.far !== undefined && parameters.far !== undefined ) object.far = parameters.far;
  550. if ( object.intensity !== undefined && parameters.intensity !== undefined ) object.intensity = parameters.intensity;
  551. if ( object.color && parameters.color !== undefined ) object.color.setHex( parameters.color );
  552. if ( object.groundColor && parameters.groundColor !== undefined ) object.groundColor.setHex( parameters.groundColor );
  553. if ( object.distance !== undefined && parameters.distance !== undefined ) object.distance = parameters.distance;
  554. if ( object.angle !== undefined && parameters.angle !== undefined ) object.angle = parameters.angle;
  555. if ( object.exponent !== undefined && parameters.exponent !== undefined ) object.exponent = parameters.exponent;
  556. if ( object.visible !== undefined && parameters.visible !== undefined ) object.visible = parameters.visible;
  557. if ( parameters.userData !== undefined ) {
  558. try {
  559. object.userData = JSON.parse( parameters.userData );
  560. } catch ( error ) {
  561. console.log( error );
  562. }
  563. };
  564. if ( object.updateProjectionMatrix ) object.updateProjectionMatrix();
  565. signals.objectChanged.dispatch( object );
  566. },
  567. updateMaterials: function( list ) {
  568. list = list ? list : this.list( 'material' );
  569. list = ( list instanceof Array ) ? list : [ list ];
  570. for ( var i in list ) {
  571. list[ i ].needsUpdate = true;
  572. if ( list[ i ] instanceof THREE.MeshFaceMaterial ) {
  573. for ( var j in list[ i ].materials ) {
  574. list[ i ].materials[ j ].needsUpdate = true;
  575. }
  576. }
  577. }
  578. },
  579. updateGeometry: function( geometry, parameters ) {
  580. parameters = parameters ? parameters : {};
  581. var id = geometry.id;
  582. var name = geometry.name;
  583. if ( geometry instanceof THREE.PlaneGeometry )
  584. geometry = this.createGeometry( 'Plane', parameters );
  585. if ( geometry instanceof THREE.CubeGeometry )
  586. geometry = this.createGeometry( 'Cube', parameters );
  587. if ( geometry instanceof THREE.CylinderGeometry )
  588. geometry = this.createGeometry( 'Cylinder', parameters );
  589. if ( geometry instanceof THREE.SphereGeometry )
  590. geometry = this.createGeometry( 'Sphere', parameters );
  591. if ( geometry instanceof THREE.IcosahedronGeometry )
  592. geometry = this.createGeometry( 'Icosahedron', parameters );
  593. if ( geometry instanceof THREE.TorusGeometry )
  594. geometry = this.createGeometry( 'Torus', parameters );
  595. if ( geometry instanceof THREE.TorusKnotGeometry )
  596. geometry = this.createGeometry( 'Torusknot', parameters );
  597. geometry.computeBoundingSphere();
  598. geometry.id = id;
  599. geometry.name = name;
  600. for ( var i in editor.objects ) {
  601. var object = editor.objects[i];
  602. if ( object.geometry && object.geometry.id == id ) {
  603. delete object.__webglInit; // TODO: Remove hack (WebGLRenderer refactoring)
  604. object.geometry.dispose();
  605. object.geometry = geometry;
  606. signals.objectChanged.dispatch( object );
  607. }
  608. }
  609. },
  610. setFog: function( parameters ) {
  611. var fogType = parameters.fogType ? parameters.fogType : null;
  612. var near = parameters.near ? parameters.near : null;
  613. var far = parameters.far ? parameters.far : null;
  614. var density = parameters.density ? parameters.density : null;
  615. var color = parameters.color ? parameters.color : null;
  616. if ( fogType ) {
  617. if ( fogType === "None" ) this.scene.fog = null;
  618. else if ( fogType === "Fog" ) this.scene.fog = new THREE.Fog();
  619. else if ( fogType === "FogExp2" ) this.scene.fog = new THREE.FogExp2();
  620. }
  621. if ( this.scene.fog ) {
  622. if ( fogType ) this.scene.fog.fogType = fogType;
  623. if ( near ) this.scene.fog.near = near;
  624. if ( far ) this.scene.fog.far = far;
  625. if ( density ) this.scene.fog.density = density;
  626. if ( color ) this.scene.fog.color.setHex( color );
  627. }
  628. this.updateMaterials();
  629. signals.fogChanged.dispatch( this.scene.fog );
  630. },
  631. regexMatch: function( name, filter ) {
  632. name = name.toLowerCase();
  633. filter = '^' + filter.toLowerCase().replace(/\*/g, '.*').replace(/\?/g, '.') + '$';
  634. var regex = new RegExp(filter);
  635. return regex.test( name );
  636. },
  637. incrementName: function( name, type ) {
  638. var list = this.listByName( name+'\\d+', type );
  639. var lastIncrement = 0;
  640. for ( var i in list ) {
  641. var Increment = parseFloat( list[i].name.replace( name, '' ) );
  642. if ( Increment > lastIncrement ) lastIncrement = Increment;
  643. }
  644. return name + ( lastIncrement + 1 );
  645. }
  646. }