Editor.js 9.6 KB

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