Editor.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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 if ( object instanceof THREE.SkinnedMesh ) {
  116. helper = new THREE.SkeletonHelper( object );
  117. } else {
  118. // no helper for this object type
  119. return;
  120. }
  121. var picker = new THREE.Mesh( geometry, material );
  122. picker.name = 'picker';
  123. picker.userData.object = object;
  124. picker.visible = false;
  125. helper.add( picker );
  126. this.sceneHelpers.add( helper );
  127. this.helpers[ object.id ] = helper;
  128. this.signals.helperAdded.dispatch( helper );
  129. };
  130. }(),
  131. removeHelper: function ( object ) {
  132. if ( this.helpers[ object.id ] !== undefined ) {
  133. var helper = this.helpers[ object.id ];
  134. helper.parent.remove( helper );
  135. delete this.helpers[ object.id ];
  136. this.signals.helperRemoved.dispatch( helper );
  137. }
  138. },
  139. //
  140. parent: function ( object, parent ) {
  141. if ( parent === undefined ) {
  142. parent = this.scene;
  143. }
  144. parent.add( object );
  145. this.signals.sceneGraphChanged.dispatch();
  146. },
  147. //
  148. select: function ( object ) {
  149. this.selected = object;
  150. if ( object !== null ) {
  151. this.config.setKey( 'selected', object.uuid );
  152. } else {
  153. this.config.setKey( 'selected', null );
  154. }
  155. this.signals.objectSelected.dispatch( object );
  156. },
  157. selectById: function ( id ) {
  158. var scope = this;
  159. this.scene.traverse( function ( child ) {
  160. if ( child.id === id ) {
  161. scope.select( child );
  162. }
  163. } );
  164. },
  165. selectByUuid: function ( uuid ) {
  166. var scope = this;
  167. this.scene.traverse( function ( child ) {
  168. if ( child.uuid === uuid ) {
  169. scope.select( child );
  170. }
  171. } );
  172. },
  173. deselect: function () {
  174. this.select( null );
  175. },
  176. // utils
  177. getObjectType: function ( object ) {
  178. var types = {
  179. 'Scene': THREE.Scene,
  180. 'PerspectiveCamera': THREE.PerspectiveCamera,
  181. 'AmbientLight': THREE.AmbientLight,
  182. 'DirectionalLight': THREE.DirectionalLight,
  183. 'HemisphereLight': THREE.HemisphereLight,
  184. 'PointLight': THREE.PointLight,
  185. 'SpotLight': THREE.SpotLight,
  186. 'SkinnedMesh': THREE.SkinnedMesh,
  187. 'Mesh': THREE.Mesh,
  188. 'Sprite': THREE.Sprite,
  189. 'Group': THREE.Group,
  190. 'Object3D': THREE.Object3D
  191. };
  192. for ( var type in types ) {
  193. if ( object instanceof types[ type ] ) return type;
  194. }
  195. },
  196. getGeometryType: function ( geometry ) {
  197. var types = {
  198. 'BoxGeometry': THREE.BoxGeometry,
  199. 'CircleGeometry': THREE.CircleGeometry,
  200. 'CylinderGeometry': THREE.CylinderGeometry,
  201. 'ExtrudeGeometry': THREE.ExtrudeGeometry,
  202. 'IcosahedronGeometry': THREE.IcosahedronGeometry,
  203. 'LatheGeometry': THREE.LatheGeometry,
  204. 'OctahedronGeometry': THREE.OctahedronGeometry,
  205. 'ParametricGeometry': THREE.ParametricGeometry,
  206. 'PlaneGeometry': THREE.PlaneGeometry,
  207. 'PolyhedronGeometry': THREE.PolyhedronGeometry,
  208. 'ShapeGeometry': THREE.ShapeGeometry,
  209. 'SphereGeometry': THREE.SphereGeometry,
  210. 'TetrahedronGeometry': THREE.TetrahedronGeometry,
  211. 'TextGeometry': THREE.TextGeometry,
  212. 'TorusGeometry': THREE.TorusGeometry,
  213. 'TorusKnotGeometry': THREE.TorusKnotGeometry,
  214. 'TubeGeometry': THREE.TubeGeometry,
  215. 'Geometry': THREE.Geometry,
  216. 'BufferGeometry': THREE.BufferGeometry
  217. };
  218. for ( var type in types ) {
  219. if ( geometry instanceof types[ type ] ) return type;
  220. }
  221. },
  222. getMaterialType: function ( material ) {
  223. var types = {
  224. 'LineBasicMaterial': THREE.LineBasicMaterial,
  225. 'LineDashedMaterial': THREE.LineDashedMaterial,
  226. 'MeshBasicMaterial': THREE.MeshBasicMaterial,
  227. 'MeshDepthMaterial': THREE.MeshDepthMaterial,
  228. 'MeshFaceMaterial': THREE.MeshFaceMaterial,
  229. 'MeshLambertMaterial': THREE.MeshLambertMaterial,
  230. 'MeshNormalMaterial': THREE.MeshNormalMaterial,
  231. 'MeshPhongMaterial': THREE.MeshPhongMaterial,
  232. 'PointCloudMaterial': THREE.PointCloudMaterial,
  233. 'ShaderMaterial': THREE.ShaderMaterial,
  234. 'SpriteCanvasMaterial': THREE.SpriteCanvasMaterial,
  235. 'SpriteMaterial': THREE.SpriteMaterial,
  236. 'Material': THREE.Material
  237. };
  238. for ( var type in types ) {
  239. if ( material instanceof types[ type ] ) return type;
  240. }
  241. }
  242. }