Editor.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  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. sceneBackgroundChanged: new Signal(),
  32. sceneFogChanged: new Signal(),
  33. sceneGraphChanged: new Signal(),
  34. cameraChanged: new Signal(),
  35. geometryChanged: new Signal(),
  36. objectSelected: new Signal(),
  37. objectFocused: new Signal(),
  38. objectAdded: new Signal(),
  39. objectChanged: new Signal(),
  40. objectRemoved: new Signal(),
  41. helperAdded: new Signal(),
  42. helperRemoved: new Signal(),
  43. materialChanged: new Signal(),
  44. scriptAdded: new Signal(),
  45. scriptChanged: new Signal(),
  46. scriptRemoved: new Signal(),
  47. windowResize: new Signal(),
  48. showGridChanged: new Signal(),
  49. refreshSidebarObject3D: new Signal(),
  50. historyChanged: new Signal(),
  51. refreshScriptEditor: new Signal()
  52. };
  53. this.config = new Config( 'threejs-editor' );
  54. this.history = new History( this );
  55. this.storage = new Storage();
  56. this.loader = new Loader( this );
  57. this.camera = this.DEFAULT_CAMERA.clone();
  58. this.scene = new THREE.Scene();
  59. this.scene.name = 'Scene';
  60. this.scene.background = new THREE.Color( 0xaaaaaa );
  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. if ( scene.background !== null ) this.scene.background = scene.background.clone();
  80. if ( scene.fog !== null ) this.scene.fog = scene.fog.clone();
  81. this.scene.userData = JSON.parse( JSON.stringify( scene.userData ) );
  82. // avoid render per object
  83. this.signals.sceneGraphChanged.active = false;
  84. while ( scene.children.length > 0 ) {
  85. this.addObject( scene.children[ 0 ] );
  86. }
  87. this.signals.sceneGraphChanged.active = true;
  88. this.signals.sceneGraphChanged.dispatch();
  89. },
  90. //
  91. addObject: function ( object ) {
  92. var scope = this;
  93. object.traverse( function ( child ) {
  94. if ( child.geometry !== undefined ) scope.addGeometry( child.geometry );
  95. if ( child.material !== undefined ) scope.addMaterial( child.material );
  96. scope.addHelper( child );
  97. } );
  98. this.scene.add( object );
  99. this.signals.objectAdded.dispatch( object );
  100. this.signals.sceneGraphChanged.dispatch();
  101. },
  102. moveObject: function ( object, parent, before ) {
  103. if ( parent === undefined ) {
  104. parent = this.scene;
  105. }
  106. parent.add( object );
  107. // sort children array
  108. if ( before !== undefined ) {
  109. var index = parent.children.indexOf( before );
  110. parent.children.splice( index, 0, object );
  111. parent.children.pop();
  112. }
  113. this.signals.sceneGraphChanged.dispatch();
  114. },
  115. nameObject: function ( object, name ) {
  116. object.name = name;
  117. this.signals.sceneGraphChanged.dispatch();
  118. },
  119. removeObject: function ( object ) {
  120. if ( object.parent === null ) return; // avoid deleting the camera or scene
  121. var scope = this;
  122. object.traverse( function ( child ) {
  123. scope.removeHelper( child );
  124. } );
  125. object.parent.remove( object );
  126. this.signals.objectRemoved.dispatch( object );
  127. this.signals.sceneGraphChanged.dispatch();
  128. },
  129. addGeometry: function ( geometry ) {
  130. this.geometries[ geometry.uuid ] = geometry;
  131. },
  132. setGeometryName: function ( geometry, name ) {
  133. geometry.name = name;
  134. this.signals.sceneGraphChanged.dispatch();
  135. },
  136. addMaterial: function ( material ) {
  137. this.materials[ material.uuid ] = material;
  138. },
  139. setMaterialName: function ( material, name ) {
  140. material.name = name;
  141. this.signals.sceneGraphChanged.dispatch();
  142. },
  143. addTexture: function ( texture ) {
  144. this.textures[ texture.uuid ] = texture;
  145. },
  146. //
  147. addHelper: function () {
  148. var geometry = new THREE.SphereBufferGeometry( 2, 4, 2 );
  149. var material = new THREE.MeshBasicMaterial( { color: 0xff0000, visible: false } );
  150. return function ( object ) {
  151. var helper;
  152. if ( object instanceof THREE.Camera ) {
  153. helper = new THREE.CameraHelper( object, 1 );
  154. } else if ( object instanceof THREE.PointLight ) {
  155. helper = new THREE.PointLightHelper( object, 1 );
  156. } else if ( object instanceof THREE.DirectionalLight ) {
  157. helper = new THREE.DirectionalLightHelper( object, 1 );
  158. } else if ( object instanceof THREE.SpotLight ) {
  159. helper = new THREE.SpotLightHelper( object, 1 );
  160. } else if ( object instanceof THREE.HemisphereLight ) {
  161. helper = new THREE.HemisphereLightHelper( object, 1 );
  162. } else if ( object instanceof THREE.SkinnedMesh ) {
  163. helper = new THREE.SkeletonHelper( object );
  164. } else {
  165. // no helper for this object type
  166. return;
  167. }
  168. var picker = new THREE.Mesh( geometry, material );
  169. picker.name = 'picker';
  170. picker.userData.object = object;
  171. helper.add( picker );
  172. this.sceneHelpers.add( helper );
  173. this.helpers[ object.id ] = helper;
  174. this.signals.helperAdded.dispatch( helper );
  175. };
  176. }(),
  177. removeHelper: function ( object ) {
  178. if ( this.helpers[ object.id ] !== undefined ) {
  179. var helper = this.helpers[ object.id ];
  180. helper.parent.remove( helper );
  181. delete this.helpers[ object.id ];
  182. this.signals.helperRemoved.dispatch( helper );
  183. }
  184. },
  185. //
  186. addScript: function ( object, script ) {
  187. if ( this.scripts[ object.uuid ] === undefined ) {
  188. this.scripts[ object.uuid ] = [];
  189. }
  190. this.scripts[ object.uuid ].push( script );
  191. this.signals.scriptAdded.dispatch( script );
  192. },
  193. removeScript: function ( object, script ) {
  194. if ( this.scripts[ object.uuid ] === undefined ) return;
  195. var index = this.scripts[ object.uuid ].indexOf( script );
  196. if ( index !== - 1 ) {
  197. this.scripts[ object.uuid ].splice( index, 1 );
  198. }
  199. this.signals.scriptRemoved.dispatch( script );
  200. },
  201. //
  202. select: function ( object ) {
  203. if ( this.selected === object ) return;
  204. var uuid = null;
  205. if ( object !== null ) {
  206. uuid = object.uuid;
  207. }
  208. this.selected = object;
  209. this.config.setKey( 'selected', uuid );
  210. this.signals.objectSelected.dispatch( object );
  211. },
  212. selectById: function ( id ) {
  213. if ( id === this.camera.id ) {
  214. this.select( this.camera );
  215. return;
  216. }
  217. this.select( this.scene.getObjectById( id, true ) );
  218. },
  219. selectByUuid: function ( uuid ) {
  220. var scope = this;
  221. this.scene.traverse( function ( child ) {
  222. if ( child.uuid === uuid ) {
  223. scope.select( child );
  224. }
  225. } );
  226. },
  227. deselect: function () {
  228. this.select( null );
  229. },
  230. focus: function ( object ) {
  231. this.signals.objectFocused.dispatch( object );
  232. },
  233. focusById: function ( id ) {
  234. this.focus( this.scene.getObjectById( id, true ) );
  235. },
  236. clear: function () {
  237. this.history.clear();
  238. this.storage.clear();
  239. this.camera.copy( this.DEFAULT_CAMERA );
  240. this.scene.background.setHex( 0xaaaaaa );
  241. this.scene.fog = null;
  242. var objects = this.scene.children;
  243. while ( objects.length > 0 ) {
  244. this.removeObject( objects[ 0 ] );
  245. }
  246. this.geometries = {};
  247. this.materials = {};
  248. this.textures = {};
  249. this.scripts = {};
  250. this.deselect();
  251. this.signals.editorCleared.dispatch();
  252. },
  253. //
  254. fromJSON: function ( json ) {
  255. var loader = new THREE.ObjectLoader();
  256. // backwards
  257. if ( json.scene === undefined ) {
  258. this.setScene( loader.parse( json ) );
  259. return;
  260. }
  261. var camera = loader.parse( json.camera );
  262. this.camera.copy( camera );
  263. this.camera.aspect = this.DEFAULT_CAMERA.aspect;
  264. this.camera.updateProjectionMatrix();
  265. this.history.fromJSON( json.history );
  266. this.scripts = json.scripts;
  267. this.setScene( loader.parse( json.scene ) );
  268. },
  269. toJSON: function () {
  270. // scripts clean up
  271. var scene = this.scene;
  272. var scripts = this.scripts;
  273. for ( var key in scripts ) {
  274. var script = scripts[ key ];
  275. if ( script.length === 0 || scene.getObjectByProperty( 'uuid', key ) === undefined ) {
  276. delete scripts[ key ];
  277. }
  278. }
  279. //
  280. return {
  281. metadata: {},
  282. project: {
  283. gammaInput: this.config.getKey( 'project/renderer/gammaInput' ),
  284. gammaOutput: this.config.getKey( 'project/renderer/gammaOutput' ),
  285. shadows: this.config.getKey( 'project/renderer/shadows' ),
  286. editable: this.config.getKey( 'project/editable' ),
  287. vr: this.config.getKey( 'project/vr' )
  288. },
  289. camera: this.camera.toJSON(),
  290. scene: this.scene.toJSON(),
  291. scripts: this.scripts,
  292. history: this.history.toJSON()
  293. };
  294. },
  295. objectByUuid: function ( uuid ) {
  296. return this.scene.getObjectByProperty( 'uuid', uuid, true );
  297. },
  298. execute: function ( cmd, optionalName ) {
  299. this.history.execute( cmd, optionalName );
  300. },
  301. undo: function () {
  302. this.history.undo();
  303. },
  304. redo: function () {
  305. this.history.redo();
  306. }
  307. };