Editor.js 28 KB

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