Editor.js 8.4 KB

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