Editor.js 13 KB

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