Editor.js 13 KB

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