Editor.js 13 KB

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