Editor.js 9.3 KB

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