Editor.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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.history = new History( this );
  47. this.storage = new Storage();
  48. this.loader = new Loader( this );
  49. this.camera = new THREE.PerspectiveCamera( 50, 1, 1, 100000 );
  50. this.camera.position.set( 500, 250, 500 );
  51. this.camera.lookAt( new THREE.Vector3() );
  52. this.camera.name = 'Camera';
  53. this.scene = new THREE.Scene();
  54. this.scene.name = 'Scene';
  55. this.sceneHelpers = new THREE.Scene();
  56. this.object = {};
  57. this.geometries = {};
  58. this.materials = {};
  59. this.textures = {};
  60. this.scripts = {};
  61. this.selected = null;
  62. this.helpers = {};
  63. };
  64. Editor.prototype = {
  65. setTheme: function ( value ) {
  66. document.getElementById( 'theme' ).href = value;
  67. this.signals.themeChanged.dispatch( value );
  68. },
  69. /*
  70. showDialog: function ( value ) {
  71. this.signals.showDialog.dispatch( value );
  72. },
  73. */
  74. //
  75. setScene: function ( scene ) {
  76. this.scene.uuid = scene.uuid;
  77. this.scene.name = scene.name;
  78. this.scene.userData = JSON.parse( JSON.stringify( scene.userData ) );
  79. // avoid render per object
  80. this.signals.sceneGraphChanged.active = false;
  81. while ( scene.children.length > 0 ) {
  82. this.addObject( scene.children[ 0 ] );
  83. }
  84. this.signals.sceneGraphChanged.active = true;
  85. this.signals.sceneGraphChanged.dispatch();
  86. },
  87. //
  88. addObject: function ( object ) {
  89. var scope = this;
  90. object.traverse( function ( child ) {
  91. if ( child.geometry !== undefined ) scope.addGeometry( child.geometry );
  92. if ( child.material !== undefined ) scope.addMaterial( child.material );
  93. scope.addHelper( child );
  94. } );
  95. this.scene.add( object );
  96. this.signals.objectAdded.dispatch( object );
  97. this.signals.sceneGraphChanged.dispatch();
  98. },
  99. moveObject: function ( object, parent, before ) {
  100. if ( parent === undefined ) {
  101. parent = this.scene;
  102. }
  103. parent.add( object );
  104. // sort children array
  105. if ( before !== undefined ) {
  106. var index = parent.children.indexOf( before );
  107. parent.children.splice( index, 0, object );
  108. parent.children.pop();
  109. }
  110. this.signals.sceneGraphChanged.dispatch();
  111. },
  112. nameObject: function ( object, name ) {
  113. object.name = name;
  114. this.signals.sceneGraphChanged.dispatch();
  115. },
  116. removeObject: function ( object ) {
  117. if ( object.parent === null ) return; // avoid deleting the camera or scene
  118. var scope = this;
  119. object.traverse( function ( child ) {
  120. scope.removeHelper( child );
  121. } );
  122. object.parent.remove( object );
  123. this.signals.objectRemoved.dispatch( object );
  124. this.signals.sceneGraphChanged.dispatch();
  125. },
  126. addGeometry: function ( geometry ) {
  127. this.geometries[ geometry.uuid ] = geometry;
  128. },
  129. setGeometryName: function ( geometry, name ) {
  130. geometry.name = name;
  131. this.signals.sceneGraphChanged.dispatch();
  132. },
  133. addMaterial: function ( material ) {
  134. this.materials[ material.uuid ] = material;
  135. },
  136. setMaterialName: function ( material, name ) {
  137. material.name = name;
  138. this.signals.sceneGraphChanged.dispatch();
  139. },
  140. addTexture: function ( texture ) {
  141. this.textures[ texture.uuid ] = texture;
  142. },
  143. //
  144. addHelper: function () {
  145. var geometry = new THREE.SphereGeometry( 20, 4, 2 );
  146. var material = new THREE.MeshBasicMaterial( { color: 0xff0000 } );
  147. return function ( object ) {
  148. var helper;
  149. if ( object instanceof THREE.Camera ) {
  150. helper = new THREE.CameraHelper( object, 10 );
  151. } else if ( object instanceof THREE.PointLight ) {
  152. helper = new THREE.PointLightHelper( object, 10 );
  153. } else if ( object instanceof THREE.DirectionalLight ) {
  154. helper = new THREE.DirectionalLightHelper( object, 20 );
  155. } else if ( object instanceof THREE.SpotLight ) {
  156. helper = new THREE.SpotLightHelper( object, 10 );
  157. } else if ( object instanceof THREE.HemisphereLight ) {
  158. helper = new THREE.HemisphereLightHelper( object, 10 );
  159. } else if ( object instanceof THREE.SkinnedMesh ) {
  160. helper = new THREE.SkeletonHelper( object );
  161. } else {
  162. // no helper for this object type
  163. return;
  164. }
  165. var picker = new THREE.Mesh( geometry, material );
  166. picker.name = 'picker';
  167. picker.userData.object = object;
  168. picker.visible = false;
  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.signals.editorCleared.dispatch();
  248. },
  249. //
  250. fromJSON: function ( json ) {
  251. var loader = new THREE.ObjectLoader();
  252. // backwards
  253. if ( json.scene === undefined ) {
  254. this.setScene( loader.parse( json ) );
  255. return;
  256. }
  257. // TODO: Clean this up somehow
  258. var camera = loader.parse( json.camera );
  259. this.camera.position.copy( camera.position );
  260. this.camera.rotation.copy( camera.rotation );
  261. this.camera.aspect = camera.aspect;
  262. this.camera.near = camera.near;
  263. this.camera.far = camera.far;
  264. this.setScene( loader.parse( json.scene ) );
  265. this.scripts = json.scripts;
  266. },
  267. toJSON: function () {
  268. return {
  269. project: {
  270. vr: this.config.getKey( 'project/vr' )
  271. },
  272. camera: this.camera.toJSON(),
  273. scene: this.scene.toJSON(),
  274. scripts: this.scripts
  275. };
  276. }
  277. }