Editor.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. var Editor = function () {
  2. var SIGNALS = signals;
  3. this.signals = {
  4. // actions
  5. playAnimations: new SIGNALS.Signal(),
  6. // notifications
  7. themeChanged: new SIGNALS.Signal(),
  8. transformModeChanged: new SIGNALS.Signal(),
  9. snapChanged: new SIGNALS.Signal(),
  10. spaceChanged: new SIGNALS.Signal(),
  11. rendererChanged: new SIGNALS.Signal(),
  12. sceneGraphChanged: new SIGNALS.Signal(),
  13. cameraChanged: new SIGNALS.Signal(),
  14. objectSelected: new SIGNALS.Signal(),
  15. objectAdded: new SIGNALS.Signal(),
  16. objectChanged: new SIGNALS.Signal(),
  17. objectRemoved: new SIGNALS.Signal(),
  18. helperAdded: new SIGNALS.Signal(),
  19. helperRemoved: new SIGNALS.Signal(),
  20. materialChanged: new SIGNALS.Signal(),
  21. fogTypeChanged: new SIGNALS.Signal(),
  22. fogColorChanged: new SIGNALS.Signal(),
  23. fogParametersChanged: new SIGNALS.Signal(),
  24. windowResize: new SIGNALS.Signal()
  25. };
  26. this.config = new Config();
  27. this.storage = new Storage();
  28. this.loader = new Loader( this );
  29. this.scene = new THREE.Scene();
  30. this.sceneHelpers = new THREE.Scene();
  31. this.object = {};
  32. this.geometries = {};
  33. this.materials = {};
  34. this.textures = {};
  35. this.selected = null;
  36. this.helpers = {};
  37. };
  38. Editor.prototype = {
  39. setTheme: function ( value ) {
  40. document.getElementById( 'theme' ).href = value;
  41. this.signals.themeChanged.dispatch( value );
  42. },
  43. setScene: function ( scene ) {
  44. this.scene.name = scene.name;
  45. this.scene.userData = JSON.parse( JSON.stringify( scene.userData ) );
  46. // avoid render per object
  47. this.signals.sceneGraphChanged.active = false;
  48. while ( scene.children.length > 0 ) {
  49. this.addObject( scene.children[ 0 ] );
  50. }
  51. this.signals.sceneGraphChanged.active = true;
  52. this.signals.sceneGraphChanged.dispatch();
  53. },
  54. //
  55. addObject: function ( object ) {
  56. var scope = this;
  57. object.traverse( function ( child ) {
  58. if ( child.geometry !== undefined ) scope.addGeometry( child.geometry );
  59. if ( child.material !== undefined ) scope.addMaterial( child.material );
  60. scope.addHelper( child );
  61. } );
  62. this.scene.add( object );
  63. this.signals.objectAdded.dispatch( object );
  64. this.signals.sceneGraphChanged.dispatch();
  65. },
  66. setObjectName: function ( object, name ) {
  67. object.name = name;
  68. this.signals.sceneGraphChanged.dispatch();
  69. },
  70. removeObject: function ( object ) {
  71. if ( object.parent === undefined ) return; // avoid deleting the camera or scene
  72. if ( confirm( 'Delete ' + object.name + '?' ) === false ) return;
  73. var scope = this;
  74. object.traverse( function ( child ) {
  75. scope.removeHelper( child );
  76. } );
  77. object.parent.remove( object );
  78. this.signals.objectRemoved.dispatch( object );
  79. this.signals.sceneGraphChanged.dispatch();
  80. },
  81. addGeometry: function ( geometry ) {
  82. this.geometries[ geometry.uuid ] = geometry;
  83. },
  84. setGeometryName: function ( geometry, name ) {
  85. geometry.name = name;
  86. this.signals.sceneGraphChanged.dispatch();
  87. },
  88. addMaterial: function ( material ) {
  89. this.materials[ material.uuid ] = material;
  90. },
  91. setMaterialName: function ( material, name ) {
  92. material.name = name;
  93. this.signals.sceneGraphChanged.dispatch();
  94. },
  95. addTexture: function ( texture ) {
  96. this.textures[ texture.uuid ] = texture;
  97. },
  98. //
  99. addHelper: function () {
  100. var geometry = new THREE.SphereGeometry( 20, 4, 2 );
  101. var material = new THREE.MeshBasicMaterial( { color: 0xff0000 } );
  102. return function ( object ) {
  103. var helper;
  104. if ( object instanceof THREE.Camera ) {
  105. helper = new THREE.CameraHelper( object, 10 );
  106. } else if ( object instanceof THREE.PointLight ) {
  107. helper = new THREE.PointLightHelper( object, 10 );
  108. } else if ( object instanceof THREE.DirectionalLight ) {
  109. helper = new THREE.DirectionalLightHelper( object, 20 );
  110. } else if ( object instanceof THREE.SpotLight ) {
  111. helper = new THREE.SpotLightHelper( object, 10 );
  112. } else if ( object instanceof THREE.HemisphereLight ) {
  113. helper = new THREE.HemisphereLightHelper( object, 10 );
  114. } else {
  115. // no helper for this object type
  116. return;
  117. }
  118. var picker = new THREE.Mesh( geometry, material );
  119. picker.name = 'picker';
  120. picker.userData.object = object;
  121. picker.visible = false;
  122. helper.add( picker );
  123. this.sceneHelpers.add( helper );
  124. this.helpers[ object.id ] = helper;
  125. this.signals.helperAdded.dispatch( helper );
  126. };
  127. }(),
  128. removeHelper: function ( object ) {
  129. if ( this.helpers[ object.id ] !== undefined ) {
  130. var helper = this.helpers[ object.id ];
  131. helper.parent.remove( helper );
  132. delete this.helpers[ object.id ];
  133. this.signals.helperRemoved.dispatch( helper );
  134. }
  135. },
  136. //
  137. parent: function ( object, parent ) {
  138. if ( parent === undefined ) {
  139. parent = this.scene;
  140. }
  141. parent.add( object );
  142. this.signals.sceneGraphChanged.dispatch();
  143. },
  144. //
  145. select: function ( object ) {
  146. this.selected = object;
  147. if ( object !== null ) {
  148. this.config.setKey( 'selected', object.uuid );
  149. } else {
  150. this.config.setKey( 'selected', null );
  151. }
  152. this.signals.objectSelected.dispatch( object );
  153. },
  154. selectById: function ( id ) {
  155. var scope = this;
  156. this.scene.traverse( function ( child ) {
  157. if ( child.id === id ) {
  158. scope.select( child );
  159. }
  160. } );
  161. },
  162. selectByUuid: function ( uuid ) {
  163. var scope = this;
  164. this.scene.traverse( function ( child ) {
  165. if ( child.uuid === uuid ) {
  166. scope.select( child );
  167. }
  168. } );
  169. },
  170. deselect: function () {
  171. this.select( null );
  172. },
  173. // utils
  174. getObjectType: function ( object ) {
  175. var types = {
  176. 'Scene': THREE.Scene,
  177. 'PerspectiveCamera': THREE.PerspectiveCamera,
  178. 'AmbientLight': THREE.AmbientLight,
  179. 'DirectionalLight': THREE.DirectionalLight,
  180. 'HemisphereLight': THREE.HemisphereLight,
  181. 'PointLight': THREE.PointLight,
  182. 'SpotLight': THREE.SpotLight,
  183. 'Mesh': THREE.Mesh,
  184. 'Sprite': THREE.Sprite,
  185. 'Object3D': THREE.Object3D
  186. };
  187. for ( var type in types ) {
  188. if ( object instanceof types[ type ] ) return type;
  189. }
  190. },
  191. getGeometryType: function ( geometry ) {
  192. var types = {
  193. 'BoxGeometry': THREE.BoxGeometry,
  194. 'CircleGeometry': THREE.CircleGeometry,
  195. 'CylinderGeometry': THREE.CylinderGeometry,
  196. 'ExtrudeGeometry': THREE.ExtrudeGeometry,
  197. 'IcosahedronGeometry': THREE.IcosahedronGeometry,
  198. 'LatheGeometry': THREE.LatheGeometry,
  199. 'OctahedronGeometry': THREE.OctahedronGeometry,
  200. 'ParametricGeometry': THREE.ParametricGeometry,
  201. 'PlaneGeometry': THREE.PlaneGeometry,
  202. 'PolyhedronGeometry': THREE.PolyhedronGeometry,
  203. 'ShapeGeometry': THREE.ShapeGeometry,
  204. 'SphereGeometry': THREE.SphereGeometry,
  205. 'TetrahedronGeometry': THREE.TetrahedronGeometry,
  206. 'TextGeometry': THREE.TextGeometry,
  207. 'TorusGeometry': THREE.TorusGeometry,
  208. 'TorusKnotGeometry': THREE.TorusKnotGeometry,
  209. 'TubeGeometry': THREE.TubeGeometry,
  210. 'Geometry': THREE.Geometry,
  211. 'Geometry2': THREE.Geometry2,
  212. 'BufferGeometry': THREE.BufferGeometry
  213. };
  214. for ( var type in types ) {
  215. if ( geometry instanceof types[ type ] ) return type;
  216. }
  217. },
  218. getMaterialType: function ( material ) {
  219. var types = {
  220. 'LineBasicMaterial': THREE.LineBasicMaterial,
  221. 'LineDashedMaterial': THREE.LineDashedMaterial,
  222. 'MeshBasicMaterial': THREE.MeshBasicMaterial,
  223. 'MeshDepthMaterial': THREE.MeshDepthMaterial,
  224. 'MeshFaceMaterial': THREE.MeshFaceMaterial,
  225. 'MeshLambertMaterial': THREE.MeshLambertMaterial,
  226. 'MeshNormalMaterial': THREE.MeshNormalMaterial,
  227. 'MeshPhongMaterial': THREE.MeshPhongMaterial,
  228. 'ParticleSystemMaterial': THREE.ParticleSystemMaterial,
  229. 'ShaderMaterial': THREE.ShaderMaterial,
  230. 'SpriteCanvasMaterial': THREE.SpriteCanvasMaterial,
  231. 'SpriteMaterial': THREE.SpriteMaterial,
  232. 'Material': THREE.Material
  233. };
  234. for ( var type in types ) {
  235. if ( material instanceof types[ type ] ) return type;
  236. }
  237. }
  238. }