Editor.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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. showModal: 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. refreshSidebarObject3D: 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. setScene: function ( scene ) {
  72. this.scene.uuid = scene.uuid;
  73. this.scene.name = scene.name;
  74. this.scene.userData = JSON.parse( JSON.stringify( scene.userData ) );
  75. // avoid render per object
  76. this.signals.sceneGraphChanged.active = false;
  77. while ( scene.children.length > 0 ) {
  78. this.addObject( scene.children[ 0 ] );
  79. }
  80. this.signals.sceneGraphChanged.active = true;
  81. this.signals.sceneGraphChanged.dispatch();
  82. },
  83. //
  84. addObject: function ( object ) {
  85. var scope = this;
  86. object.traverse( function ( child ) {
  87. if ( child.geometry !== undefined ) scope.addGeometry( child.geometry );
  88. if ( child.material !== undefined ) scope.addMaterial( child.material );
  89. scope.addHelper( child );
  90. } );
  91. this.scene.add( object );
  92. this.signals.objectAdded.dispatch( object );
  93. this.signals.sceneGraphChanged.dispatch();
  94. },
  95. moveObject: function ( object, parent, before ) {
  96. if ( parent === undefined ) {
  97. parent = this.scene;
  98. }
  99. parent.add( object );
  100. // sort children array
  101. if ( before !== undefined ) {
  102. var index = parent.children.indexOf( before );
  103. parent.children.splice( index, 0, object );
  104. parent.children.pop();
  105. }
  106. this.signals.sceneGraphChanged.dispatch();
  107. },
  108. nameObject: function ( object, name ) {
  109. object.name = name;
  110. this.signals.sceneGraphChanged.dispatch();
  111. },
  112. removeObject: function ( object ) {
  113. if ( object.parent === null ) return; // avoid deleting the camera or scene
  114. var scope = this;
  115. object.traverse( function ( child ) {
  116. scope.removeHelper( child );
  117. } );
  118. object.parent.remove( object );
  119. this.signals.objectRemoved.dispatch( object );
  120. this.signals.sceneGraphChanged.dispatch();
  121. },
  122. addGeometry: function ( geometry ) {
  123. this.geometries[ geometry.uuid ] = geometry;
  124. },
  125. setGeometryName: function ( geometry, name ) {
  126. geometry.name = name;
  127. this.signals.sceneGraphChanged.dispatch();
  128. },
  129. addMaterial: function ( material ) {
  130. this.materials[ material.uuid ] = material;
  131. },
  132. setMaterialName: function ( material, name ) {
  133. material.name = name;
  134. this.signals.sceneGraphChanged.dispatch();
  135. },
  136. addTexture: function ( texture ) {
  137. this.textures[ texture.uuid ] = texture;
  138. },
  139. //
  140. addHelper: function () {
  141. var geometry = new THREE.SphereBufferGeometry( 20, 4, 2 );
  142. var material = new THREE.MeshBasicMaterial( { color: 0xff0000, visible: false } );
  143. return function ( object ) {
  144. var helper;
  145. if ( object instanceof THREE.Camera ) {
  146. helper = new THREE.CameraHelper( object, 10 );
  147. } else if ( object instanceof THREE.PointLight ) {
  148. helper = new THREE.PointLightHelper( object, 10 );
  149. } else if ( object instanceof THREE.DirectionalLight ) {
  150. helper = new THREE.DirectionalLightHelper( object, 20 );
  151. } else if ( object instanceof THREE.SpotLight ) {
  152. helper = new THREE.SpotLightHelper( object, 10 );
  153. } else if ( object instanceof THREE.HemisphereLight ) {
  154. helper = new THREE.HemisphereLightHelper( object, 10 );
  155. } else if ( object instanceof THREE.SkinnedMesh ) {
  156. helper = new THREE.SkeletonHelper( object );
  157. } else {
  158. // no helper for this object type
  159. return;
  160. }
  161. var picker = new THREE.Mesh( geometry, material );
  162. picker.name = 'picker';
  163. picker.userData.object = object;
  164. helper.add( picker );
  165. this.sceneHelpers.add( helper );
  166. this.helpers[ object.id ] = helper;
  167. this.signals.helperAdded.dispatch( helper );
  168. };
  169. }(),
  170. removeHelper: function ( object ) {
  171. if ( this.helpers[ object.id ] !== undefined ) {
  172. var helper = this.helpers[ object.id ];
  173. helper.parent.remove( helper );
  174. delete this.helpers[ object.id ];
  175. this.signals.helperRemoved.dispatch( helper );
  176. }
  177. },
  178. //
  179. addScript: function ( object, script ) {
  180. if ( this.scripts[ object.uuid ] === undefined ) {
  181. this.scripts[ object.uuid ] = [];
  182. }
  183. this.scripts[ object.uuid ].push( script );
  184. this.signals.scriptAdded.dispatch( script );
  185. },
  186. removeScript: function ( object, script ) {
  187. if ( this.scripts[ object.uuid ] === undefined ) return;
  188. var index = this.scripts[ object.uuid ].indexOf( script );
  189. if ( index !== - 1 ) {
  190. this.scripts[ object.uuid ].splice( index, 1 );
  191. }
  192. this.signals.scriptRemoved.dispatch( script );
  193. },
  194. //
  195. select: function ( object ) {
  196. if ( this.selected === object ) return;
  197. var uuid = null;
  198. if ( object !== null ) {
  199. uuid = object.uuid;
  200. }
  201. this.selected = object;
  202. this.config.setKey( 'selected', uuid );
  203. this.signals.objectSelected.dispatch( object );
  204. },
  205. selectById: function ( id ) {
  206. if ( id === this.camera.id ) {
  207. this.select( this.camera );
  208. return;
  209. }
  210. this.select( this.scene.getObjectById( id, true ) );
  211. },
  212. selectByUuid: function ( uuid ) {
  213. var scope = this;
  214. this.scene.traverse( function ( child ) {
  215. if ( child.uuid === uuid ) {
  216. scope.select( child );
  217. }
  218. } );
  219. },
  220. deselect: function () {
  221. this.select( null );
  222. },
  223. focus: function ( object ) {
  224. this.signals.objectFocused.dispatch( object );
  225. },
  226. focusById: function ( id ) {
  227. this.focus( this.scene.getObjectById( id, true ) );
  228. },
  229. clear: function () {
  230. this.history.clear();
  231. this.storage.clear();
  232. this.camera.position.set( 500, 250, 500 );
  233. this.camera.lookAt( new THREE.Vector3() );
  234. var objects = this.scene.children;
  235. while ( objects.length > 0 ) {
  236. this.removeObject( objects[ 0 ] );
  237. }
  238. this.geometries = {};
  239. this.materials = {};
  240. this.textures = {};
  241. this.scripts = {};
  242. this.deselect();
  243. this.signals.editorCleared.dispatch();
  244. },
  245. //
  246. fromJSON: function ( json ) {
  247. var loader = new THREE.ObjectLoader();
  248. // backwards
  249. if ( json.scene === undefined ) {
  250. this.setScene( loader.parse( json ) );
  251. return;
  252. }
  253. // TODO: Clean this up somehow
  254. var camera = loader.parse( json.camera );
  255. this.camera.position.copy( camera.position );
  256. this.camera.rotation.copy( camera.rotation );
  257. this.camera.aspect = camera.aspect;
  258. this.camera.near = camera.near;
  259. this.camera.far = camera.far;
  260. this.setScene( loader.parse( json.scene ) );
  261. this.scripts = json.scripts;
  262. this.history.fromJSON( json.history );
  263. },
  264. toJSON: function () {
  265. return {
  266. project: {
  267. shadows: this.config.getKey( 'project/renderer/shadows' ),
  268. vr: this.config.getKey( 'project/vr' )
  269. },
  270. camera: this.camera.toJSON(),
  271. scene: this.scene.toJSON(),
  272. scripts: this.scripts,
  273. history: this.history.toJSON()
  274. };
  275. },
  276. objectByUuid: function ( uuid ) {
  277. return this.scene.getObjectByProperty( 'uuid', uuid, true );
  278. },
  279. execute: function ( cmd, optionalName ) {
  280. this.history.execute( cmd, optionalName );
  281. },
  282. undo: function () {
  283. this.history.undo();
  284. },
  285. redo: function () {
  286. this.history.redo();
  287. }
  288. }