Editor.js 7.7 KB

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