Editor.js 8.0 KB

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