Editor.js 8.9 KB

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