Editor.js 10 KB

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