Editor.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  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.userData = JSON.parse( JSON.stringify( scene.userData ) );
  95. // avoid render per object
  96. this.signals.sceneGraphChanged.active = false;
  97. while ( scene.children.length > 0 ) {
  98. this.addObject( scene.children[ 0 ] );
  99. }
  100. this.signals.sceneGraphChanged.active = true;
  101. this.signals.sceneGraphChanged.dispatch();
  102. },
  103. //
  104. addObject: function ( object, parent, index ) {
  105. var scope = this;
  106. object.traverse( function ( child ) {
  107. if ( child.geometry !== undefined ) scope.addGeometry( child.geometry );
  108. if ( child.material !== undefined ) scope.addMaterial( child.material );
  109. scope.addCamera( child );
  110. scope.addHelper( child );
  111. } );
  112. if ( parent === undefined ) {
  113. this.scene.add( object );
  114. } else {
  115. parent.children.splice( index, 0, object );
  116. object.parent = parent;
  117. }
  118. this.signals.objectAdded.dispatch( object );
  119. this.signals.sceneGraphChanged.dispatch();
  120. },
  121. moveObject: function ( object, parent, before ) {
  122. if ( parent === undefined ) {
  123. parent = this.scene;
  124. }
  125. parent.add( object );
  126. // sort children array
  127. if ( before !== undefined ) {
  128. var index = parent.children.indexOf( before );
  129. parent.children.splice( index, 0, object );
  130. parent.children.pop();
  131. }
  132. this.signals.sceneGraphChanged.dispatch();
  133. },
  134. nameObject: function ( object, name ) {
  135. object.name = name;
  136. this.signals.sceneGraphChanged.dispatch();
  137. },
  138. removeObject: function ( object ) {
  139. if ( object.parent === null ) return; // avoid deleting the camera or scene
  140. var scope = this;
  141. object.traverse( function ( child ) {
  142. scope.removeCamera( child );
  143. scope.removeHelper( child );
  144. if ( child.material !== undefined ) scope.removeMaterial( child.material );
  145. } );
  146. object.parent.remove( object );
  147. this.signals.objectRemoved.dispatch( object );
  148. this.signals.sceneGraphChanged.dispatch();
  149. },
  150. addGeometry: function ( geometry ) {
  151. this.geometries[ geometry.uuid ] = geometry;
  152. },
  153. setGeometryName: function ( geometry, name ) {
  154. geometry.name = name;
  155. this.signals.sceneGraphChanged.dispatch();
  156. },
  157. addMaterial: function ( material ) {
  158. if ( Array.isArray( material ) ) {
  159. for ( var i = 0, l = material.length; i < l; i ++ ) {
  160. this.addMaterialToRefCounter( material[ i ] );
  161. }
  162. } else {
  163. this.addMaterialToRefCounter( material );
  164. }
  165. this.signals.materialAdded.dispatch();
  166. },
  167. addMaterialToRefCounter: function ( material ) {
  168. var materialsRefCounter = this.materialsRefCounter;
  169. var count = materialsRefCounter.get( material );
  170. if ( count === undefined ) {
  171. materialsRefCounter.set( material, 1 );
  172. this.materials[ material.uuid ] = material;
  173. } else {
  174. count ++;
  175. materialsRefCounter.set( material, count );
  176. }
  177. },
  178. removeMaterial: function ( material ) {
  179. if ( Array.isArray( material ) ) {
  180. for ( var i = 0, l = material.length; i < l; i ++ ) {
  181. this.removeMaterialFromRefCounter( material[ i ] );
  182. }
  183. } else {
  184. this.removeMaterialFromRefCounter( material );
  185. }
  186. this.signals.materialRemoved.dispatch();
  187. },
  188. removeMaterialFromRefCounter: function ( material ) {
  189. var materialsRefCounter = this.materialsRefCounter;
  190. var count = materialsRefCounter.get( material );
  191. count --;
  192. if ( count === 0 ) {
  193. materialsRefCounter.delete( material );
  194. delete this.materials[ material.uuid ];
  195. } else {
  196. materialsRefCounter.set( material, count );
  197. }
  198. },
  199. getMaterialById: function ( id ) {
  200. var material;
  201. var materials = Object.values( this.materials );
  202. for ( var i = 0; i < materials.length; i ++ ) {
  203. if ( materials[ i ].id === id ) {
  204. material = materials[ i ];
  205. break;
  206. }
  207. }
  208. return material;
  209. },
  210. setMaterialName: function ( material, name ) {
  211. material.name = name;
  212. this.signals.sceneGraphChanged.dispatch();
  213. },
  214. addTexture: function ( texture ) {
  215. this.textures[ texture.uuid ] = texture;
  216. },
  217. //
  218. addCamera: function ( camera ) {
  219. if ( camera.isCamera ) {
  220. this.cameras[ camera.uuid ] = camera;
  221. this.signals.cameraAdded.dispatch( camera );
  222. }
  223. },
  224. removeCamera: function ( camera ) {
  225. if ( this.cameras[ camera.uuid ] !== undefined ) {
  226. delete this.cameras[ camera.uuid ];
  227. this.signals.cameraRemoved.dispatch( camera );
  228. }
  229. },
  230. //
  231. addHelper: function () {
  232. var geometry = new THREE.SphereGeometry( 2, 4, 2 );
  233. var material = new THREE.MeshBasicMaterial( { color: 0xff0000, visible: false } );
  234. return function ( object, helper ) {
  235. if ( helper === undefined ) {
  236. if ( object.isCamera ) {
  237. helper = new THREE.CameraHelper( object );
  238. } else if ( object.isPointLight ) {
  239. helper = new THREE.PointLightHelper( object, 1 );
  240. } else if ( object.isDirectionalLight ) {
  241. helper = new THREE.DirectionalLightHelper( object, 1 );
  242. } else if ( object.isSpotLight ) {
  243. helper = new THREE.SpotLightHelper( object );
  244. } else if ( object.isHemisphereLight ) {
  245. helper = new THREE.HemisphereLightHelper( object, 1 );
  246. } else if ( object.isSkinnedMesh ) {
  247. helper = new THREE.SkeletonHelper( object.skeleton.bones[ 0 ] );
  248. } else if ( object.isBone === true && object.parent?.isBone !== true ) {
  249. helper = new THREE.SkeletonHelper( object );
  250. } else {
  251. // no helper for this object type
  252. return;
  253. }
  254. const picker = new THREE.Mesh( geometry, material );
  255. picker.name = 'picker';
  256. picker.userData.object = object;
  257. helper.add( picker );
  258. }
  259. this.sceneHelpers.add( helper );
  260. this.helpers[ object.id ] = helper;
  261. this.signals.helperAdded.dispatch( helper );
  262. };
  263. }(),
  264. removeHelper: function ( object ) {
  265. if ( this.helpers[ object.id ] !== undefined ) {
  266. var helper = this.helpers[ object.id ];
  267. helper.parent.remove( helper );
  268. delete this.helpers[ object.id ];
  269. this.signals.helperRemoved.dispatch( helper );
  270. }
  271. },
  272. //
  273. addScript: function ( object, script ) {
  274. if ( this.scripts[ object.uuid ] === undefined ) {
  275. this.scripts[ object.uuid ] = [];
  276. }
  277. this.scripts[ object.uuid ].push( script );
  278. this.signals.scriptAdded.dispatch( script );
  279. },
  280. removeScript: function ( object, script ) {
  281. if ( this.scripts[ object.uuid ] === undefined ) return;
  282. var index = this.scripts[ object.uuid ].indexOf( script );
  283. if ( index !== - 1 ) {
  284. this.scripts[ object.uuid ].splice( index, 1 );
  285. }
  286. this.signals.scriptRemoved.dispatch( script );
  287. },
  288. getObjectMaterial: function ( object, slot ) {
  289. var material = object.material;
  290. if ( Array.isArray( material ) && slot !== undefined ) {
  291. material = material[ slot ];
  292. }
  293. return material;
  294. },
  295. setObjectMaterial: function ( object, slot, newMaterial ) {
  296. if ( Array.isArray( object.material ) && slot !== undefined ) {
  297. object.material[ slot ] = newMaterial;
  298. } else {
  299. object.material = newMaterial;
  300. }
  301. },
  302. setViewportCamera: function ( uuid ) {
  303. this.viewportCamera = this.cameras[ uuid ];
  304. this.signals.viewportCameraChanged.dispatch();
  305. },
  306. //
  307. select: function ( object ) {
  308. this.selector.select( object );
  309. },
  310. selectById: function ( id ) {
  311. if ( id === this.camera.id ) {
  312. this.select( this.camera );
  313. return;
  314. }
  315. this.select( this.scene.getObjectById( id ) );
  316. },
  317. selectByUuid: function ( uuid ) {
  318. var scope = this;
  319. this.scene.traverse( function ( child ) {
  320. if ( child.uuid === uuid ) {
  321. scope.select( child );
  322. }
  323. } );
  324. },
  325. deselect: function () {
  326. this.selector.deselect();
  327. },
  328. focus: function ( object ) {
  329. if ( object !== undefined ) {
  330. this.signals.objectFocused.dispatch( object );
  331. }
  332. },
  333. focusById: function ( id ) {
  334. this.focus( this.scene.getObjectById( id ) );
  335. },
  336. clear: function () {
  337. this.history.clear();
  338. this.storage.clear();
  339. this.camera.copy( _DEFAULT_CAMERA );
  340. this.signals.cameraResetted.dispatch();
  341. this.scene.name = 'Scene';
  342. this.scene.userData = {};
  343. this.scene.background = null;
  344. this.scene.environment = null;
  345. this.scene.fog = null;
  346. var objects = this.scene.children;
  347. while ( objects.length > 0 ) {
  348. this.removeObject( objects[ 0 ] );
  349. }
  350. this.geometries = {};
  351. this.materials = {};
  352. this.textures = {};
  353. this.scripts = {};
  354. this.materialsRefCounter.clear();
  355. this.animations = {};
  356. this.mixer.stopAllAction();
  357. this.deselect();
  358. this.signals.editorCleared.dispatch();
  359. },
  360. //
  361. fromJSON: async function ( json ) {
  362. var loader = new THREE.ObjectLoader();
  363. var camera = await loader.parseAsync( json.camera );
  364. this.camera.copy( camera );
  365. this.signals.cameraResetted.dispatch();
  366. this.history.fromJSON( json.history );
  367. this.scripts = json.scripts;
  368. this.setScene( await loader.parseAsync( json.scene ) );
  369. },
  370. toJSON: function () {
  371. // scripts clean up
  372. var scene = this.scene;
  373. var scripts = this.scripts;
  374. for ( var key in scripts ) {
  375. var script = scripts[ key ];
  376. if ( script.length === 0 || scene.getObjectByProperty( 'uuid', key ) === undefined ) {
  377. delete scripts[ key ];
  378. }
  379. }
  380. //
  381. return {
  382. metadata: {},
  383. project: {
  384. shadows: this.config.getKey( 'project/renderer/shadows' ),
  385. shadowType: this.config.getKey( 'project/renderer/shadowType' ),
  386. vr: this.config.getKey( 'project/vr' ),
  387. physicallyCorrectLights: this.config.getKey( 'project/renderer/physicallyCorrectLights' ),
  388. toneMapping: this.config.getKey( 'project/renderer/toneMapping' ),
  389. toneMappingExposure: this.config.getKey( 'project/renderer/toneMappingExposure' )
  390. },
  391. camera: this.camera.toJSON(),
  392. scene: this.scene.toJSON(),
  393. scripts: this.scripts,
  394. history: this.history.toJSON()
  395. };
  396. },
  397. objectByUuid: function ( uuid ) {
  398. return this.scene.getObjectByProperty( 'uuid', uuid, true );
  399. },
  400. execute: function ( cmd, optionalName ) {
  401. this.history.execute( cmd, optionalName );
  402. },
  403. undo: function () {
  404. this.history.undo();
  405. },
  406. redo: function () {
  407. this.history.redo();
  408. }
  409. };
  410. export { Editor };