Editor.js 8.0 KB

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