Editor.js 8.4 KB

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