Editor.js 8.0 KB

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