Editor.js 8.1 KB

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