Editor.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726
  1. import * as THREE from '../../build/three.module.js';
  2. import { Config } from './Config.js';
  3. import { Loader } from './Loader.js';
  4. import { History as _History } from './History.js';
  5. import { Strings } from './Strings.js';
  6. import { Storage as _Storage } from './Storage.js';
  7. var _DEFAULT_CAMERA = new THREE.PerspectiveCamera( 50, 1, 0.01, 1000 );
  8. _DEFAULT_CAMERA.name = 'Camera';
  9. _DEFAULT_CAMERA.position.set( 0, 5, 10 );
  10. _DEFAULT_CAMERA.lookAt( new THREE.Vector3() );
  11. function Editor() {
  12. var Signal = signals.Signal;
  13. this.signals = {
  14. // script
  15. editScript: new Signal(),
  16. // player
  17. startPlayer: new Signal(),
  18. stopPlayer: new Signal(),
  19. // vr
  20. toggleVR: new Signal(),
  21. exitedVR: new Signal(),
  22. // notifications
  23. editorCleared: new Signal(),
  24. savingStarted: new Signal(),
  25. savingFinished: new Signal(),
  26. transformModeChanged: new Signal(),
  27. snapChanged: new Signal(),
  28. spaceChanged: new Signal(),
  29. rendererCreated: new Signal(),
  30. rendererUpdated: new Signal(),
  31. sceneBackgroundChanged: new Signal(),
  32. sceneEnvironmentChanged: new Signal(),
  33. sceneFogChanged: new Signal(),
  34. sceneFogSettingsChanged: new Signal(),
  35. sceneGraphChanged: new Signal(),
  36. sceneRendered: new Signal(),
  37. cameraChanged: new Signal(),
  38. cameraResetted: new Signal(),
  39. geometryChanged: new Signal(),
  40. objectSelected: new Signal(),
  41. objectFocused: new Signal(),
  42. objectAdded: new Signal(),
  43. objectChanged: new Signal(),
  44. objectRemoved: new Signal(),
  45. cameraAdded: new Signal(),
  46. cameraRemoved: new Signal(),
  47. helperAdded: new Signal(),
  48. helperRemoved: new Signal(),
  49. materialAdded: new Signal(),
  50. materialChanged: new Signal(),
  51. materialRemoved: new Signal(),
  52. scriptAdded: new Signal(),
  53. scriptChanged: new Signal(),
  54. scriptRemoved: new Signal(),
  55. windowResize: new Signal(),
  56. showGridChanged: new Signal(),
  57. showHelpersChanged: new Signal(),
  58. refreshSidebarObject3D: new Signal(),
  59. historyChanged: new Signal(),
  60. viewportCameraChanged: new Signal(),
  61. animationStopped: new Signal()
  62. };
  63. this.config = new Config();
  64. this.history = new _History( this );
  65. this.storage = new _Storage();
  66. this.strings = new Strings( this.config );
  67. this.loader = new Loader( this );
  68. this.camera = _DEFAULT_CAMERA.clone();
  69. this.scene = new THREE.Scene();
  70. this.scene.name = 'Scene';
  71. this.sceneHelpers = new THREE.Scene();
  72. this.object = {};
  73. this.geometries = {};
  74. this.materials = {};
  75. this.textures = {};
  76. this.scripts = {};
  77. this.materialsRefCounter = new Map(); // tracks how often is a material used by a 3D object
  78. this.mixer = new THREE.AnimationMixer( this.scene );
  79. this.selected = null;
  80. this.helpers = {};
  81. this.cameras = {};
  82. this.viewportCamera = this.camera;
  83. this.addCamera( this.camera );
  84. }
  85. Editor.prototype = {
  86. setScene: function ( scene ) {
  87. this.scene.uuid = scene.uuid;
  88. this.scene.name = scene.name;
  89. this.scene.background = scene.background;
  90. this.scene.environment = scene.environment;
  91. this.scene.fog = scene.fog;
  92. this.scene.userData = JSON.parse( JSON.stringify( scene.userData ) );
  93. // avoid render per object
  94. this.signals.sceneGraphChanged.active = false;
  95. while ( scene.children.length > 0 ) {
  96. this.addObject( scene.children[ 0 ] );
  97. }
  98. this.signals.sceneGraphChanged.active = true;
  99. this.signals.sceneGraphChanged.dispatch();
  100. },
  101. //
  102. addObject: function ( object, parent, index ) {
  103. var scope = this;
  104. object.traverse( function ( child ) {
  105. if ( child.geometry !== undefined ) scope.addGeometry( child.geometry );
  106. if ( child.material !== undefined ) scope.addMaterial( child.material );
  107. scope.addCamera( child );
  108. scope.addHelper( child );
  109. } );
  110. if ( parent === undefined ) {
  111. this.scene.add( object );
  112. } else {
  113. parent.children.splice( index, 0, object );
  114. object.parent = parent;
  115. }
  116. this.signals.objectAdded.dispatch( object );
  117. this.signals.sceneGraphChanged.dispatch();
  118. },
  119. moveObject: function ( object, parent, before ) {
  120. if ( parent === undefined ) {
  121. parent = this.scene;
  122. }
  123. parent.add( object );
  124. // sort children array
  125. if ( before !== undefined ) {
  126. var index = parent.children.indexOf( before );
  127. parent.children.splice( index, 0, object );
  128. parent.children.pop();
  129. }
  130. this.signals.sceneGraphChanged.dispatch();
  131. },
  132. nameObject: function ( object, name ) {
  133. object.name = name;
  134. this.signals.sceneGraphChanged.dispatch();
  135. },
  136. removeObject: function ( object ) {
  137. if ( object.parent === null ) return; // avoid deleting the camera or scene
  138. var scope = this;
  139. object.traverse( function ( child ) {
  140. scope.removeCamera( child );
  141. scope.removeHelper( child );
  142. if ( child.material !== undefined ) scope.removeMaterial( child.material );
  143. } );
  144. object.parent.remove( object );
  145. this.signals.objectRemoved.dispatch( object );
  146. this.signals.sceneGraphChanged.dispatch();
  147. },
  148. addGeometry: function ( geometry ) {
  149. this.geometries[ geometry.uuid ] = geometry;
  150. },
  151. setGeometryName: function ( geometry, name ) {
  152. geometry.name = name;
  153. this.signals.sceneGraphChanged.dispatch();
  154. },
  155. addMaterial: function ( material ) {
  156. if ( Array.isArray( material ) ) {
  157. for ( var i = 0, l = material.length; i < l; i ++ ) {
  158. this.addMaterialToRefCounter( material[ i ] );
  159. }
  160. } else {
  161. this.addMaterialToRefCounter( material );
  162. }
  163. this.signals.materialAdded.dispatch();
  164. },
  165. addMaterialToRefCounter: function ( material ) {
  166. var materialsRefCounter = this.materialsRefCounter;
  167. var count = materialsRefCounter.get( material );
  168. if ( count === undefined ) {
  169. materialsRefCounter.set( material, 1 );
  170. this.materials[ material.uuid ] = material;
  171. } else {
  172. count ++;
  173. materialsRefCounter.set( material, count );
  174. }
  175. },
  176. removeMaterial: function ( material ) {
  177. if ( Array.isArray( material ) ) {
  178. for ( var i = 0, l = material.length; i < l; i ++ ) {
  179. this.removeMaterialFromRefCounter( material[ i ] );
  180. }
  181. } else {
  182. this.removeMaterialFromRefCounter( material );
  183. }
  184. this.signals.materialRemoved.dispatch();
  185. },
  186. removeMaterialFromRefCounter: function ( material ) {
  187. var materialsRefCounter = this.materialsRefCounter;
  188. var count = materialsRefCounter.get( material );
  189. count --;
  190. if ( count === 0 ) {
  191. materialsRefCounter.delete( material );
  192. delete this.materials[ material.uuid ];
  193. } else {
  194. materialsRefCounter.set( material, count );
  195. }
  196. },
  197. getMaterialById: function ( id ) {
  198. var material;
  199. var materials = Object.values( this.materials );
  200. for ( var i = 0; i < materials.length; i ++ ) {
  201. if ( materials[ i ].id === id ) {
  202. material = materials[ i ];
  203. break;
  204. }
  205. }
  206. return material;
  207. },
  208. setMaterialName: function ( material, name ) {
  209. material.name = name;
  210. this.signals.sceneGraphChanged.dispatch();
  211. },
  212. addTexture: function ( texture ) {
  213. this.textures[ texture.uuid ] = texture;
  214. },
  215. //
  216. addCamera: function ( camera ) {
  217. if ( camera.isCamera ) {
  218. this.cameras[ camera.uuid ] = camera;
  219. this.signals.cameraAdded.dispatch( camera );
  220. }
  221. },
  222. removeCamera: function ( camera ) {
  223. if ( this.cameras[ camera.uuid ] !== undefined ) {
  224. delete this.cameras[ camera.uuid ];
  225. this.signals.cameraRemoved.dispatch( camera );
  226. }
  227. },
  228. //
  229. addHelper: function () {
  230. var geometry = new THREE.SphereGeometry( 2, 4, 2 );
  231. var material = new THREE.MeshBasicMaterial( { color: 0xff0000, visible: false } );
  232. return function ( object, helper ) {
  233. if ( helper === undefined ) {
  234. if ( object.isCamera ) {
  235. helper = new THREE.CameraHelper( object );
  236. } else if ( object.isPointLight ) {
  237. helper = new THREE.PointLightHelper( object, 1 );
  238. } else if ( object.isDirectionalLight ) {
  239. helper = new THREE.DirectionalLightHelper( object, 1 );
  240. } else if ( object.isSpotLight ) {
  241. helper = new THREE.SpotLightHelper( object );
  242. } else if ( object.isHemisphereLight ) {
  243. helper = new THREE.HemisphereLightHelper( object, 1 );
  244. } else if ( object.isSkinnedMesh ) {
  245. helper = new THREE.SkeletonHelper( object.skeleton.bones[ 0 ] );
  246. } else {
  247. // no helper for this object type
  248. return;
  249. }
  250. var picker = new THREE.Mesh( geometry, material );
  251. picker.name = 'picker';
  252. picker.userData.object = object;
  253. helper.add( picker );
  254. }
  255. this.sceneHelpers.add( helper );
  256. this.helpers[ object.id ] = helper;
  257. this.signals.helperAdded.dispatch( helper );
  258. };
  259. }(),
  260. removeHelper: function ( object ) {
  261. if ( this.helpers[ object.id ] !== undefined ) {
  262. var helper = this.helpers[ object.id ];
  263. helper.parent.remove( helper );
  264. delete this.helpers[ object.id ];
  265. this.signals.helperRemoved.dispatch( helper );
  266. }
  267. },
  268. //
  269. addScript: function ( object, script ) {
  270. if ( this.scripts[ object.uuid ] === undefined ) {
  271. this.scripts[ object.uuid ] = [];
  272. }
  273. this.scripts[ object.uuid ].push( script );
  274. this.signals.scriptAdded.dispatch( script );
  275. },
  276. removeScript: function ( object, script ) {
  277. if ( this.scripts[ object.uuid ] === undefined ) return;
  278. var index = this.scripts[ object.uuid ].indexOf( script );
  279. if ( index !== - 1 ) {
  280. this.scripts[ object.uuid ].splice( index, 1 );
  281. }
  282. this.signals.scriptRemoved.dispatch( script );
  283. },
  284. getObjectMaterial: function ( object, slot ) {
  285. var material = object.material;
  286. if ( Array.isArray( material ) && slot !== undefined ) {
  287. material = material[ slot ];
  288. }
  289. return material;
  290. },
  291. setObjectMaterial: function ( object, slot, newMaterial ) {
  292. if ( Array.isArray( object.material ) && slot !== undefined ) {
  293. object.material[ slot ] = newMaterial;
  294. } else {
  295. object.material = newMaterial;
  296. }
  297. },
  298. setViewportCamera: function ( uuid ) {
  299. this.viewportCamera = this.cameras[ uuid ];
  300. this.signals.viewportCameraChanged.dispatch();
  301. },
  302. //
  303. select: function ( object ) {
  304. if ( this.selected === object ) return;
  305. var uuid = null;
  306. if ( object !== null ) {
  307. uuid = object.uuid;
  308. }
  309. this.selected = object;
  310. this.config.setKey( 'selected', uuid );
  311. this.signals.objectSelected.dispatch( object );
  312. },
  313. selectById: function ( id ) {
  314. if ( id === this.camera.id ) {
  315. this.select( this.camera );
  316. return;
  317. }
  318. this.select( this.scene.getObjectById( id ) );
  319. },
  320. selectByUuid: function ( uuid ) {
  321. var scope = this;
  322. this.scene.traverse( function ( child ) {
  323. if ( child.uuid === uuid ) {
  324. scope.select( child );
  325. }
  326. } );
  327. },
  328. deselect: function () {
  329. this.select( null );
  330. },
  331. focus: function ( object ) {
  332. if ( object !== undefined ) {
  333. this.signals.objectFocused.dispatch( object );
  334. }
  335. },
  336. focusById: function ( id ) {
  337. this.focus( this.scene.getObjectById( id ) );
  338. },
  339. clear: function () {
  340. this.history.clear();
  341. this.storage.clear();
  342. this.camera.copy( _DEFAULT_CAMERA );
  343. this.signals.cameraResetted.dispatch();
  344. this.scene.name = 'Scene';
  345. this.scene.userData = {};
  346. this.scene.background = null;
  347. this.scene.environment = null;
  348. this.scene.fog = null;
  349. var objects = this.scene.children;
  350. while ( objects.length > 0 ) {
  351. this.removeObject( objects[ 0 ] );
  352. }
  353. this.geometries = {};
  354. this.materials = {};
  355. this.textures = {};
  356. this.scripts = {};
  357. this.materialsRefCounter.clear();
  358. this.animations = {};
  359. this.mixer.stopAllAction();
  360. this.deselect();
  361. this.signals.editorCleared.dispatch();
  362. },
  363. //
  364. fromJSON: async function ( json ) {
  365. var loader = new THREE.ObjectLoader();
  366. var camera = await loader.parseAsync( json.camera );
  367. this.camera.copy( camera );
  368. this.signals.cameraResetted.dispatch();
  369. this.history.fromJSON( json.history );
  370. this.scripts = json.scripts;
  371. this.setScene( await loader.parseAsync( json.scene ) );
  372. },
  373. toJSON: function () {
  374. // scripts clean up
  375. var scene = this.scene;
  376. var scripts = this.scripts;
  377. for ( var key in scripts ) {
  378. var script = scripts[ key ];
  379. if ( script.length === 0 || scene.getObjectByProperty( 'uuid', key ) === undefined ) {
  380. delete scripts[ key ];
  381. }
  382. }
  383. //
  384. return {
  385. metadata: {},
  386. project: {
  387. shadows: this.config.getKey( 'project/renderer/shadows' ),
  388. shadowType: this.config.getKey( 'project/renderer/shadowType' ),
  389. vr: this.config.getKey( 'project/vr' ),
  390. physicallyCorrectLights: this.config.getKey( 'project/renderer/physicallyCorrectLights' ),
  391. toneMapping: this.config.getKey( 'project/renderer/toneMapping' ),
  392. toneMappingExposure: this.config.getKey( 'project/renderer/toneMappingExposure' )
  393. },
  394. camera: this.camera.toJSON(),
  395. scene: this.scene.toJSON(),
  396. scripts: this.scripts,
  397. history: this.history.toJSON()
  398. };
  399. },
  400. objectByUuid: function ( uuid ) {
  401. return this.scene.getObjectByProperty( 'uuid', uuid, true );
  402. },
  403. execute: function ( cmd, optionalName ) {
  404. this.history.execute( cmd, optionalName );
  405. },
  406. undo: function () {
  407. this.history.undo();
  408. },
  409. redo: function () {
  410. this.history.redo();
  411. }
  412. };
  413. export { Editor };