Editor.js 9.1 KB

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