Editor.js 13 KB

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