Editor.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  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. fogChanged: new Signal(),
  46. windowResize: new Signal(),
  47. showGridChanged: new Signal(),
  48. refreshSidebarObject3D: new Signal(),
  49. historyChanged: new Signal(),
  50. refreshScriptEditor: new Signal()
  51. };
  52. this.config = new Config( 'threejs-editor' );
  53. this.history = new History( this );
  54. this.storage = new Storage();
  55. this.loader = new Loader( this );
  56. this.camera = this.DEFAULT_CAMERA.clone();
  57. this.scene = new THREE.Scene();
  58. this.scene.name = 'Scene';
  59. this.sceneHelpers = new THREE.Scene();
  60. this.object = {};
  61. this.geometries = {};
  62. this.materials = {};
  63. this.textures = {};
  64. this.scripts = {};
  65. this.selected = null;
  66. this.helpers = {};
  67. };
  68. Editor.prototype = {
  69. setTheme: function ( value ) {
  70. document.getElementById( 'theme' ).href = value;
  71. this.signals.themeChanged.dispatch( value );
  72. },
  73. //
  74. setScene: function ( scene ) {
  75. this.scene.uuid = scene.uuid;
  76. this.scene.name = scene.name;
  77. if ( scene.fog !== null ) this.scene.fog = scene.fog.clone();
  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.SphereBufferGeometry( 2, 4, 2 );
  146. var material = new THREE.MeshBasicMaterial( { color: 0xff0000, visible: false } );
  147. return function ( object ) {
  148. var helper;
  149. if ( object instanceof THREE.Camera ) {
  150. helper = new THREE.CameraHelper( object, 1 );
  151. } else if ( object instanceof THREE.PointLight ) {
  152. helper = new THREE.PointLightHelper( object, 1 );
  153. } else if ( object instanceof THREE.DirectionalLight ) {
  154. helper = new THREE.DirectionalLightHelper( object, 1 );
  155. } else if ( object instanceof THREE.SpotLight ) {
  156. helper = new THREE.SpotLightHelper( object, 1 );
  157. } else if ( object instanceof THREE.HemisphereLight ) {
  158. helper = new THREE.HemisphereLightHelper( object, 1 );
  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. helper.add( picker );
  169. this.sceneHelpers.add( helper );
  170. this.helpers[ object.id ] = helper;
  171. this.signals.helperAdded.dispatch( helper );
  172. };
  173. }(),
  174. removeHelper: function ( object ) {
  175. if ( this.helpers[ object.id ] !== undefined ) {
  176. var helper = this.helpers[ object.id ];
  177. helper.parent.remove( helper );
  178. delete this.helpers[ object.id ];
  179. this.signals.helperRemoved.dispatch( helper );
  180. }
  181. },
  182. //
  183. addScript: function ( object, script ) {
  184. if ( this.scripts[ object.uuid ] === undefined ) {
  185. this.scripts[ object.uuid ] = [];
  186. }
  187. this.scripts[ object.uuid ].push( script );
  188. this.signals.scriptAdded.dispatch( script );
  189. },
  190. removeScript: function ( object, script ) {
  191. if ( this.scripts[ object.uuid ] === undefined ) return;
  192. var index = this.scripts[ object.uuid ].indexOf( script );
  193. if ( index !== - 1 ) {
  194. this.scripts[ object.uuid ].splice( index, 1 );
  195. }
  196. this.signals.scriptRemoved.dispatch( script );
  197. },
  198. //
  199. select: function ( object ) {
  200. if ( this.selected === object ) return;
  201. var uuid = null;
  202. if ( object !== null ) {
  203. uuid = object.uuid;
  204. }
  205. this.selected = object;
  206. this.config.setKey( 'selected', uuid );
  207. this.signals.objectSelected.dispatch( object );
  208. },
  209. selectById: function ( id ) {
  210. if ( id === this.camera.id ) {
  211. this.select( this.camera );
  212. return;
  213. }
  214. this.select( this.scene.getObjectById( id, true ) );
  215. },
  216. selectByUuid: function ( uuid ) {
  217. var scope = this;
  218. this.scene.traverse( function ( child ) {
  219. if ( child.uuid === uuid ) {
  220. scope.select( child );
  221. }
  222. } );
  223. },
  224. deselect: function () {
  225. this.select( null );
  226. },
  227. focus: function ( object ) {
  228. this.signals.objectFocused.dispatch( object );
  229. },
  230. focusById: function ( id ) {
  231. this.focus( this.scene.getObjectById( id, true ) );
  232. },
  233. clear: function () {
  234. this.history.clear();
  235. this.storage.clear();
  236. this.camera.copy( this.DEFAULT_CAMERA );
  237. var objects = this.scene.children;
  238. while ( objects.length > 0 ) {
  239. this.removeObject( objects[ 0 ] );
  240. }
  241. this.geometries = {};
  242. this.materials = {};
  243. this.textures = {};
  244. this.scripts = {};
  245. this.deselect();
  246. this.signals.editorCleared.dispatch();
  247. },
  248. //
  249. fromJSON: function ( json ) {
  250. var loader = new THREE.ObjectLoader();
  251. // backwards
  252. if ( json.scene === undefined ) {
  253. this.setScene( loader.parse( json ) );
  254. return;
  255. }
  256. var camera = loader.parse( json.camera );
  257. this.camera.copy( camera );
  258. this.camera.aspect = this.DEFAULT_CAMERA.aspect;
  259. this.camera.updateProjectionMatrix();
  260. this.history.fromJSON( json.history );
  261. this.scripts = json.scripts;
  262. this.setScene( loader.parse( json.scene ) );
  263. },
  264. toJSON: function () {
  265. // scripts clean up
  266. var scene = this.scene;
  267. var scripts = this.scripts;
  268. for ( var key in scripts ) {
  269. var script = scripts[ key ];
  270. if ( script.length === 0 || scene.getObjectByProperty( 'uuid', key ) === undefined ) {
  271. delete scripts[ key ];
  272. }
  273. }
  274. //
  275. return {
  276. metadata: {},
  277. project: {
  278. gammaInput: this.config.getKey( 'project/renderer/gammaInput' ),
  279. gammaOutput: this.config.getKey( 'project/renderer/gammaOutput' ),
  280. shadows: this.config.getKey( 'project/renderer/shadows' ),
  281. editable: this.config.getKey( 'project/editable' ),
  282. vr: this.config.getKey( 'project/vr' )
  283. },
  284. camera: this.camera.toJSON(),
  285. scene: this.scene.toJSON(),
  286. scripts: this.scripts,
  287. history: this.history.toJSON()
  288. };
  289. },
  290. objectByUuid: function ( uuid ) {
  291. return this.scene.getObjectByProperty( 'uuid', uuid, true );
  292. },
  293. execute: function ( cmd, optionalName ) {
  294. this.history.execute( cmd, optionalName );
  295. },
  296. undo: function () {
  297. this.history.undo();
  298. },
  299. redo: function () {
  300. this.history.redo();
  301. }
  302. };