Editor.js 9.1 KB

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