Editor.js 8.4 KB

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