Editor.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  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. THREE.ColorManagement.legacyMode = false;
  9. var _DEFAULT_CAMERA = new THREE.PerspectiveCamera( 50, 1, 0.01, 1000 );
  10. _DEFAULT_CAMERA.name = 'Camera';
  11. _DEFAULT_CAMERA.position.set( 0, 5, 10 );
  12. _DEFAULT_CAMERA.lookAt( new THREE.Vector3() );
  13. function Editor() {
  14. const Signal = signals.Signal; // eslint-disable-line no-undef
  15. this.signals = {
  16. // script
  17. editScript: new Signal(),
  18. // player
  19. startPlayer: new Signal(),
  20. stopPlayer: new Signal(),
  21. // vr
  22. toggleVR: new Signal(),
  23. exitedVR: new Signal(),
  24. // notifications
  25. editorCleared: new Signal(),
  26. savingStarted: new Signal(),
  27. savingFinished: new Signal(),
  28. transformModeChanged: new Signal(),
  29. snapChanged: new Signal(),
  30. spaceChanged: new Signal(),
  31. rendererCreated: new Signal(),
  32. rendererUpdated: new Signal(),
  33. sceneBackgroundChanged: new Signal(),
  34. sceneEnvironmentChanged: new Signal(),
  35. sceneFogChanged: new Signal(),
  36. sceneFogSettingsChanged: new Signal(),
  37. sceneGraphChanged: new Signal(),
  38. sceneRendered: new Signal(),
  39. cameraChanged: new Signal(),
  40. cameraResetted: new Signal(),
  41. geometryChanged: new Signal(),
  42. objectSelected: new Signal(),
  43. objectFocused: new Signal(),
  44. objectAdded: new Signal(),
  45. objectChanged: new Signal(),
  46. objectRemoved: new Signal(),
  47. cameraAdded: new Signal(),
  48. cameraRemoved: new Signal(),
  49. helperAdded: new Signal(),
  50. helperRemoved: new Signal(),
  51. materialAdded: new Signal(),
  52. materialChanged: new Signal(),
  53. materialRemoved: new Signal(),
  54. scriptAdded: new Signal(),
  55. scriptChanged: new Signal(),
  56. scriptRemoved: new Signal(),
  57. windowResize: new Signal(),
  58. showGridChanged: new Signal(),
  59. showHelpersChanged: new Signal(),
  60. refreshSidebarObject3D: new Signal(),
  61. historyChanged: new Signal(),
  62. viewportCameraChanged: new Signal(),
  63. intersectionsDetected: new Signal(),
  64. };
  65. this.config = new Config();
  66. this.history = new _History( this );
  67. this.storage = new _Storage();
  68. this.strings = new Strings( this.config );
  69. this.selector = new Selector( this );
  70. this.loader = new Loader( this );
  71. this.camera = _DEFAULT_CAMERA.clone();
  72. this.scene = new THREE.Scene();
  73. this.scene.name = 'Scene';
  74. this.sceneHelpers = new THREE.Scene();
  75. this.object = {};
  76. this.geometries = {};
  77. this.materials = {};
  78. this.textures = {};
  79. this.scripts = {};
  80. this.materialsRefCounter = new Map(); // tracks how often is a material used by a 3D object
  81. this.mixer = new THREE.AnimationMixer( this.scene );
  82. this.selected = null;
  83. this.helpers = {};
  84. this.cameras = {};
  85. this.viewportCamera = this.camera;
  86. this.addCamera( this.camera );
  87. }
  88. Editor.prototype = {
  89. setScene: function ( scene ) {
  90. this.scene.uuid = scene.uuid;
  91. this.scene.name = scene.name;
  92. this.scene.background = scene.background;
  93. this.scene.environment = scene.environment;
  94. this.scene.fog = scene.fog;
  95. this.scene.backgroundBlurriness = scene.backgroundBlurriness;
  96. this.scene.backgroundIntensity = scene.backgroundIntensity;
  97. this.scene.userData = JSON.parse( JSON.stringify( scene.userData ) );
  98. // avoid render per object
  99. this.signals.sceneGraphChanged.active = false;
  100. while ( scene.children.length > 0 ) {
  101. this.addObject( scene.children[ 0 ] );
  102. }
  103. this.signals.sceneGraphChanged.active = true;
  104. this.signals.sceneGraphChanged.dispatch();
  105. },
  106. //
  107. addObject: function ( object, parent, index ) {
  108. var scope = this;
  109. object.traverse( function ( child ) {
  110. if ( child.geometry !== undefined ) scope.addGeometry( child.geometry );
  111. if ( child.material !== undefined ) scope.addMaterial( child.material );
  112. scope.addCamera( child );
  113. scope.addHelper( child );
  114. } );
  115. if ( parent === undefined ) {
  116. this.scene.add( object );
  117. } else {
  118. parent.children.splice( index, 0, object );
  119. object.parent = parent;
  120. }
  121. this.signals.objectAdded.dispatch( object );
  122. this.signals.sceneGraphChanged.dispatch();
  123. },
  124. moveObject: function ( object, parent, before ) {
  125. if ( parent === undefined ) {
  126. parent = this.scene;
  127. }
  128. parent.add( object );
  129. // sort children array
  130. if ( before !== undefined ) {
  131. var index = parent.children.indexOf( before );
  132. parent.children.splice( index, 0, object );
  133. parent.children.pop();
  134. }
  135. this.signals.sceneGraphChanged.dispatch();
  136. },
  137. nameObject: function ( object, name ) {
  138. object.name = name;
  139. this.signals.sceneGraphChanged.dispatch();
  140. },
  141. removeObject: function ( object ) {
  142. if ( object.parent === null ) return; // avoid deleting the camera or scene
  143. var scope = this;
  144. object.traverse( function ( child ) {
  145. scope.removeCamera( child );
  146. scope.removeHelper( child );
  147. if ( child.material !== undefined ) scope.removeMaterial( child.material );
  148. } );
  149. object.parent.remove( object );
  150. this.signals.objectRemoved.dispatch( object );
  151. this.signals.sceneGraphChanged.dispatch();
  152. },
  153. addGeometry: function ( geometry ) {
  154. this.geometries[ geometry.uuid ] = geometry;
  155. },
  156. setGeometryName: function ( geometry, name ) {
  157. geometry.name = name;
  158. this.signals.sceneGraphChanged.dispatch();
  159. },
  160. addMaterial: function ( material ) {
  161. if ( Array.isArray( material ) ) {
  162. for ( var i = 0, l = material.length; i < l; i ++ ) {
  163. this.addMaterialToRefCounter( material[ i ] );
  164. }
  165. } else {
  166. this.addMaterialToRefCounter( material );
  167. }
  168. this.signals.materialAdded.dispatch();
  169. },
  170. addMaterialToRefCounter: function ( material ) {
  171. var materialsRefCounter = this.materialsRefCounter;
  172. var count = materialsRefCounter.get( material );
  173. if ( count === undefined ) {
  174. materialsRefCounter.set( material, 1 );
  175. this.materials[ material.uuid ] = material;
  176. } else {
  177. count ++;
  178. materialsRefCounter.set( material, count );
  179. }
  180. },
  181. removeMaterial: function ( material ) {
  182. if ( Array.isArray( material ) ) {
  183. for ( var i = 0, l = material.length; i < l; i ++ ) {
  184. this.removeMaterialFromRefCounter( material[ i ] );
  185. }
  186. } else {
  187. this.removeMaterialFromRefCounter( material );
  188. }
  189. this.signals.materialRemoved.dispatch();
  190. },
  191. removeMaterialFromRefCounter: function ( material ) {
  192. var materialsRefCounter = this.materialsRefCounter;
  193. var count = materialsRefCounter.get( material );
  194. count --;
  195. if ( count === 0 ) {
  196. materialsRefCounter.delete( material );
  197. delete this.materials[ material.uuid ];
  198. } else {
  199. materialsRefCounter.set( material, count );
  200. }
  201. },
  202. getMaterialById: function ( id ) {
  203. var material;
  204. var materials = Object.values( this.materials );
  205. for ( var i = 0; i < materials.length; i ++ ) {
  206. if ( materials[ i ].id === id ) {
  207. material = materials[ i ];
  208. break;
  209. }
  210. }
  211. return material;
  212. },
  213. setMaterialName: function ( material, name ) {
  214. material.name = name;
  215. this.signals.sceneGraphChanged.dispatch();
  216. },
  217. addTexture: function ( texture ) {
  218. this.textures[ texture.uuid ] = texture;
  219. },
  220. //
  221. addCamera: function ( camera ) {
  222. if ( camera.isCamera ) {
  223. this.cameras[ camera.uuid ] = camera;
  224. this.signals.cameraAdded.dispatch( camera );
  225. }
  226. },
  227. removeCamera: function ( camera ) {
  228. if ( this.cameras[ camera.uuid ] !== undefined ) {
  229. delete this.cameras[ camera.uuid ];
  230. this.signals.cameraRemoved.dispatch( camera );
  231. }
  232. },
  233. //
  234. addHelper: function () {
  235. var geometry = new THREE.SphereGeometry( 2, 4, 2 );
  236. var material = new THREE.MeshBasicMaterial( { color: 0xff0000, visible: false } );
  237. return function ( object, helper ) {
  238. if ( helper === undefined ) {
  239. if ( object.isCamera ) {
  240. helper = new THREE.CameraHelper( object );
  241. } else if ( object.isPointLight ) {
  242. helper = new THREE.PointLightHelper( object, 1 );
  243. } else if ( object.isDirectionalLight ) {
  244. helper = new THREE.DirectionalLightHelper( object, 1 );
  245. } else if ( object.isSpotLight ) {
  246. helper = new THREE.SpotLightHelper( object );
  247. } else if ( object.isHemisphereLight ) {
  248. helper = new THREE.HemisphereLightHelper( object, 1 );
  249. } else if ( object.isSkinnedMesh ) {
  250. helper = new THREE.SkeletonHelper( object.skeleton.bones[ 0 ] );
  251. } else if ( object.isBone === true && object.parent?.isBone !== true ) {
  252. helper = new THREE.SkeletonHelper( object );
  253. } else {
  254. // no helper for this object type
  255. return;
  256. }
  257. const picker = new THREE.Mesh( geometry, material );
  258. picker.name = 'picker';
  259. picker.userData.object = object;
  260. helper.add( picker );
  261. }
  262. this.sceneHelpers.add( helper );
  263. this.helpers[ object.id ] = helper;
  264. this.signals.helperAdded.dispatch( helper );
  265. };
  266. }(),
  267. removeHelper: function ( object ) {
  268. if ( this.helpers[ object.id ] !== undefined ) {
  269. var helper = this.helpers[ object.id ];
  270. helper.parent.remove( helper );
  271. delete this.helpers[ object.id ];
  272. this.signals.helperRemoved.dispatch( helper );
  273. }
  274. },
  275. //
  276. addScript: function ( object, script ) {
  277. if ( this.scripts[ object.uuid ] === undefined ) {
  278. this.scripts[ object.uuid ] = [];
  279. }
  280. this.scripts[ object.uuid ].push( script );
  281. this.signals.scriptAdded.dispatch( script );
  282. },
  283. removeScript: function ( object, script ) {
  284. if ( this.scripts[ object.uuid ] === undefined ) return;
  285. var index = this.scripts[ object.uuid ].indexOf( script );
  286. if ( index !== - 1 ) {
  287. this.scripts[ object.uuid ].splice( index, 1 );
  288. }
  289. this.signals.scriptRemoved.dispatch( script );
  290. },
  291. getObjectMaterial: function ( object, slot ) {
  292. var material = object.material;
  293. if ( Array.isArray( material ) && slot !== undefined ) {
  294. material = material[ slot ];
  295. }
  296. return material;
  297. },
  298. setObjectMaterial: function ( object, slot, newMaterial ) {
  299. if ( Array.isArray( object.material ) && slot !== undefined ) {
  300. object.material[ slot ] = newMaterial;
  301. } else {
  302. object.material = newMaterial;
  303. }
  304. },
  305. setViewportCamera: function ( uuid ) {
  306. this.viewportCamera = this.cameras[ uuid ];
  307. this.signals.viewportCameraChanged.dispatch();
  308. },
  309. //
  310. select: function ( object ) {
  311. this.selector.select( 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.selector.deselect();
  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 };