Editor.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. var Editor = function () {
  5. var SIGNALS = signals;
  6. this.signals = {
  7. // script
  8. editScript: new SIGNALS.Signal(),
  9. // player
  10. startPlayer: new SIGNALS.Signal(),
  11. stopPlayer: new SIGNALS.Signal(),
  12. // actions
  13. playAnimation: new SIGNALS.Signal(),
  14. stopAnimation: new SIGNALS.Signal(),
  15. // showDialog: new SIGNALS.Signal(),
  16. // notifications
  17. editorCleared: new SIGNALS.Signal(),
  18. savingStarted: new SIGNALS.Signal(),
  19. savingFinished: new SIGNALS.Signal(),
  20. themeChanged: new SIGNALS.Signal(),
  21. transformModeChanged: new SIGNALS.Signal(),
  22. snapChanged: new SIGNALS.Signal(),
  23. spaceChanged: new SIGNALS.Signal(),
  24. rendererChanged: new SIGNALS.Signal(),
  25. sceneGraphChanged: new SIGNALS.Signal(),
  26. cameraChanged: new SIGNALS.Signal(),
  27. geometryChanged: new SIGNALS.Signal(),
  28. objectSelected: new SIGNALS.Signal(),
  29. objectFocused: new SIGNALS.Signal(),
  30. objectAdded: new SIGNALS.Signal(),
  31. objectChanged: new SIGNALS.Signal(),
  32. objectRemoved: new SIGNALS.Signal(),
  33. helperAdded: new SIGNALS.Signal(),
  34. helperRemoved: new SIGNALS.Signal(),
  35. materialChanged: new SIGNALS.Signal(),
  36. scriptAdded: new SIGNALS.Signal(),
  37. scriptChanged: new SIGNALS.Signal(),
  38. scriptRemoved: new SIGNALS.Signal(),
  39. fogTypeChanged: new SIGNALS.Signal(),
  40. fogColorChanged: new SIGNALS.Signal(),
  41. fogParametersChanged: new SIGNALS.Signal(),
  42. windowResize: new SIGNALS.Signal(),
  43. showGridChanged: new SIGNALS.Signal()
  44. };
  45. this.config = new Config();
  46. this.storage = new Storage();
  47. this.loader = new Loader( this );
  48. this.camera = new THREE.PerspectiveCamera( 50, 1, 1, 100000 );
  49. this.camera.name = 'Camera';
  50. this.scene = new THREE.Scene();
  51. this.scene.name = 'Scene';
  52. this.sceneHelpers = new THREE.Scene();
  53. this.object = {};
  54. this.geometries = {};
  55. this.materials = {};
  56. this.textures = {};
  57. this.scripts = {};
  58. this.selected = null;
  59. this.helpers = {};
  60. };
  61. Editor.prototype = {
  62. setTheme: function ( value ) {
  63. document.getElementById( 'theme' ).href = value;
  64. this.signals.themeChanged.dispatch( value );
  65. },
  66. /*
  67. showDialog: function ( value ) {
  68. this.signals.showDialog.dispatch( value );
  69. },
  70. */
  71. //
  72. setScene: function ( scene ) {
  73. this.scene.uuid = scene.uuid;
  74. this.scene.name = scene.name;
  75. this.scene.userData = JSON.parse( JSON.stringify( scene.userData ) );
  76. // avoid render per object
  77. this.signals.sceneGraphChanged.active = false;
  78. while ( scene.children.length > 0 ) {
  79. this.addObject( scene.children[ 0 ] );
  80. }
  81. this.signals.sceneGraphChanged.active = true;
  82. this.signals.sceneGraphChanged.dispatch();
  83. },
  84. //
  85. addObject: function ( object ) {
  86. var scope = this;
  87. object.traverse( function ( child ) {
  88. if ( child.geometry !== undefined ) scope.addGeometry( child.geometry );
  89. if ( child.material !== undefined ) scope.addMaterial( child.material );
  90. scope.addHelper( child );
  91. } );
  92. this.scene.add( object );
  93. this.signals.objectAdded.dispatch( object );
  94. this.signals.sceneGraphChanged.dispatch();
  95. },
  96. setObjectName: function ( object, name ) {
  97. object.name = name;
  98. this.signals.sceneGraphChanged.dispatch();
  99. },
  100. removeObject: function ( object ) {
  101. if ( object.parent === undefined ) return; // avoid deleting the camera or scene
  102. var scope = this;
  103. object.traverse( function ( child ) {
  104. scope.removeHelper( child );
  105. } );
  106. object.parent.remove( object );
  107. this.signals.objectRemoved.dispatch( object );
  108. this.signals.sceneGraphChanged.dispatch();
  109. },
  110. addGeometry: function ( geometry ) {
  111. this.geometries[ geometry.uuid ] = geometry;
  112. },
  113. setGeometryName: function ( geometry, name ) {
  114. geometry.name = name;
  115. this.signals.sceneGraphChanged.dispatch();
  116. },
  117. addMaterial: function ( material ) {
  118. this.materials[ material.uuid ] = material;
  119. },
  120. setMaterialName: function ( material, name ) {
  121. material.name = name;
  122. this.signals.sceneGraphChanged.dispatch();
  123. },
  124. addTexture: function ( texture ) {
  125. this.textures[ texture.uuid ] = texture;
  126. },
  127. //
  128. addHelper: function () {
  129. var geometry = new THREE.SphereGeometry( 20, 4, 2 );
  130. var material = new THREE.MeshBasicMaterial( { color: 0xff0000 } );
  131. return function ( object ) {
  132. var helper;
  133. if ( object instanceof THREE.Camera ) {
  134. helper = new THREE.CameraHelper( object, 10 );
  135. } else if ( object instanceof THREE.PointLight ) {
  136. helper = new THREE.PointLightHelper( object, 10 );
  137. } else if ( object instanceof THREE.DirectionalLight ) {
  138. helper = new THREE.DirectionalLightHelper( object, 20 );
  139. } else if ( object instanceof THREE.SpotLight ) {
  140. helper = new THREE.SpotLightHelper( object, 10 );
  141. } else if ( object instanceof THREE.HemisphereLight ) {
  142. helper = new THREE.HemisphereLightHelper( object, 10 );
  143. } else if ( object instanceof THREE.SkinnedMesh ) {
  144. helper = new THREE.SkeletonHelper( object );
  145. } else {
  146. // no helper for this object type
  147. return;
  148. }
  149. var picker = new THREE.Mesh( geometry, material );
  150. picker.name = 'picker';
  151. picker.userData.object = object;
  152. picker.visible = false;
  153. helper.add( picker );
  154. this.sceneHelpers.add( helper );
  155. this.helpers[ object.id ] = helper;
  156. this.signals.helperAdded.dispatch( helper );
  157. };
  158. }(),
  159. removeHelper: function ( object ) {
  160. if ( this.helpers[ object.id ] !== undefined ) {
  161. var helper = this.helpers[ object.id ];
  162. helper.parent.remove( helper );
  163. delete this.helpers[ object.id ];
  164. this.signals.helperRemoved.dispatch( helper );
  165. }
  166. },
  167. //
  168. addScript: function ( object, script ) {
  169. if ( this.scripts[ object.uuid ] === undefined ) {
  170. this.scripts[ object.uuid ] = [];
  171. }
  172. this.scripts[ object.uuid ].push( script );
  173. this.signals.scriptAdded.dispatch( script );
  174. },
  175. removeScript: function ( object, script ) {
  176. if ( this.scripts[ object.uuid ] === undefined ) return;
  177. var index = this.scripts[ object.uuid ].indexOf( script );
  178. if ( index !== - 1 ) {
  179. this.scripts[ object.uuid ].splice( index, 1 );
  180. }
  181. this.signals.scriptRemoved.dispatch( script );
  182. },
  183. //
  184. parent: function ( object, parent, before ) {
  185. if ( parent === undefined ) {
  186. parent = this.scene;
  187. }
  188. parent.add( object );
  189. // sort children array
  190. if ( before !== undefined ) {
  191. var index = parent.children.indexOf( before );
  192. parent.children.splice( index, 0, object );
  193. parent.children.pop();
  194. }
  195. this.signals.sceneGraphChanged.dispatch();
  196. },
  197. //
  198. select: function ( object ) {
  199. if ( this.selected === object ) return;
  200. var uuid = null;
  201. if ( object !== null ) {
  202. uuid = object.uuid;
  203. }
  204. this.selected = object;
  205. this.config.setKey( 'selected', uuid );
  206. this.signals.objectSelected.dispatch( object );
  207. },
  208. selectById: function ( id ) {
  209. if ( id === this.camera.id ) {
  210. this.select( this.camera );
  211. return;
  212. }
  213. this.select( this.scene.getObjectById( id, true ) );
  214. },
  215. selectByUuid: function ( uuid ) {
  216. var scope = this;
  217. this.scene.traverse( function ( child ) {
  218. if ( child.uuid === uuid ) {
  219. scope.select( child );
  220. }
  221. } );
  222. },
  223. deselect: function () {
  224. this.select( null );
  225. },
  226. focus: function ( object ) {
  227. this.signals.objectFocused.dispatch( object );
  228. },
  229. focusById: function ( id ) {
  230. this.focus( this.scene.getObjectById( id, true ) );
  231. },
  232. clear: function () {
  233. this.camera.position.set( 500, 250, 500 );
  234. this.camera.lookAt( new THREE.Vector3() );
  235. var objects = this.scene.children;
  236. while ( objects.length > 0 ) {
  237. this.removeObject( objects[ 0 ] );
  238. }
  239. this.geometries = {};
  240. this.materials = {};
  241. this.textures = {};
  242. this.scripts = {};
  243. this.deselect();
  244. this.signals.editorCleared.dispatch();
  245. },
  246. //
  247. fromJSON: function ( json ) {
  248. var loader = new THREE.ObjectLoader();
  249. // backwards
  250. if ( json.scene === undefined ) {
  251. var scene = loader.parse( json );
  252. this.setScene( scene );
  253. return;
  254. }
  255. // TODO: Clean this up somehow
  256. var camera = loader.parse( json.camera );
  257. this.camera.position.copy( camera.position );
  258. this.camera.rotation.copy( camera.rotation );
  259. this.camera.aspect = camera.aspect;
  260. this.camera.near = camera.near;
  261. this.camera.far = camera.far;
  262. this.setScene( loader.parse( json.scene ) );
  263. this.scripts = json.scripts;
  264. },
  265. toJSON: function () {
  266. return {
  267. camera: this.camera.toJSON(),
  268. scene: this.scene.toJSON(),
  269. scripts: this.scripts
  270. };
  271. }
  272. }