Editor.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809
  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 './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. // xr
  21. enterXR: new Signal(),
  22. offerXR: new Signal(),
  23. leaveXR: 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. rendererDetectKTX2Support: new Signal(),
  34. sceneBackgroundChanged: new Signal(),
  35. sceneEnvironmentChanged: new Signal(),
  36. sceneFogChanged: new Signal(),
  37. sceneFogSettingsChanged: new Signal(),
  38. sceneGraphChanged: new Signal(),
  39. sceneRendered: new Signal(),
  40. cameraChanged: new Signal(),
  41. cameraResetted: new Signal(),
  42. geometryChanged: new Signal(),
  43. objectSelected: new Signal(),
  44. objectFocused: new Signal(),
  45. objectAdded: new Signal(),
  46. objectChanged: new Signal(),
  47. objectRemoved: new Signal(),
  48. cameraAdded: new Signal(),
  49. cameraRemoved: new Signal(),
  50. helperAdded: new Signal(),
  51. helperRemoved: new Signal(),
  52. materialAdded: new Signal(),
  53. materialChanged: new Signal(),
  54. materialRemoved: new Signal(),
  55. scriptAdded: new Signal(),
  56. scriptChanged: new Signal(),
  57. scriptRemoved: new Signal(),
  58. windowResize: new Signal(),
  59. showHelpersChanged: new Signal(),
  60. refreshSidebarObject3D: new Signal(),
  61. refreshSidebarEnvironment: new Signal(),
  62. historyChanged: new Signal(),
  63. viewportCameraChanged: new Signal(),
  64. viewportShadingChanged: new Signal(),
  65. intersectionsDetected: new Signal(),
  66. pathTracerUpdated: new Signal(),
  67. };
  68. this.config = new Config();
  69. this.history = new _History( this );
  70. this.selector = new Selector( this );
  71. this.storage = new _Storage();
  72. this.strings = new Strings( this.config );
  73. this.loader = new Loader( this );
  74. this.camera = _DEFAULT_CAMERA.clone();
  75. this.scene = new THREE.Scene();
  76. this.scene.name = 'Scene';
  77. this.sceneHelpers = new THREE.Scene();
  78. this.sceneHelpers.add( new THREE.HemisphereLight( 0xffffff, 0x888888, 2 ) );
  79. this.object = {};
  80. this.geometries = {};
  81. this.materials = {};
  82. this.textures = {};
  83. this.scripts = {};
  84. this.materialsRefCounter = new Map(); // tracks how often is a material used by a 3D object
  85. this.mixer = new THREE.AnimationMixer( this.scene );
  86. this.selected = null;
  87. this.helpers = {};
  88. this.cameras = {};
  89. this.viewportCamera = this.camera;
  90. this.viewportShading = 'default';
  91. this.addCamera( this.camera );
  92. }
  93. Editor.prototype = {
  94. setScene: function ( scene ) {
  95. this.scene.uuid = scene.uuid;
  96. this.scene.name = scene.name;
  97. this.scene.background = scene.background;
  98. this.scene.environment = scene.environment;
  99. this.scene.fog = scene.fog;
  100. this.scene.backgroundBlurriness = scene.backgroundBlurriness;
  101. this.scene.backgroundIntensity = scene.backgroundIntensity;
  102. this.scene.userData = JSON.parse( JSON.stringify( scene.userData ) );
  103. // avoid render per object
  104. this.signals.sceneGraphChanged.active = false;
  105. while ( scene.children.length > 0 ) {
  106. this.addObject( scene.children[ 0 ] );
  107. }
  108. this.signals.sceneGraphChanged.active = true;
  109. this.signals.sceneGraphChanged.dispatch();
  110. },
  111. //
  112. addObject: function ( object, parent, index ) {
  113. var scope = this;
  114. object.traverse( function ( child ) {
  115. if ( child.geometry !== undefined ) scope.addGeometry( child.geometry );
  116. if ( child.material !== undefined ) scope.addMaterial( child.material );
  117. scope.addCamera( child );
  118. scope.addHelper( child );
  119. } );
  120. if ( parent === undefined ) {
  121. this.scene.add( object );
  122. } else {
  123. parent.children.splice( index, 0, object );
  124. object.parent = parent;
  125. }
  126. this.signals.objectAdded.dispatch( object );
  127. this.signals.sceneGraphChanged.dispatch();
  128. },
  129. moveObject: function ( object, parent, before ) {
  130. if ( parent === undefined ) {
  131. parent = this.scene;
  132. }
  133. parent.add( object );
  134. // sort children array
  135. if ( before !== undefined ) {
  136. var index = parent.children.indexOf( before );
  137. parent.children.splice( index, 0, object );
  138. parent.children.pop();
  139. }
  140. this.signals.sceneGraphChanged.dispatch();
  141. },
  142. nameObject: function ( object, name ) {
  143. object.name = name;
  144. this.signals.sceneGraphChanged.dispatch();
  145. },
  146. removeObject: function ( object ) {
  147. if ( object.parent === null ) return; // avoid deleting the camera or scene
  148. var scope = this;
  149. object.traverse( function ( child ) {
  150. scope.removeCamera( child );
  151. scope.removeHelper( child );
  152. if ( child.material !== undefined ) scope.removeMaterial( child.material );
  153. } );
  154. object.parent.remove( object );
  155. this.signals.objectRemoved.dispatch( object );
  156. this.signals.sceneGraphChanged.dispatch();
  157. },
  158. addGeometry: function ( geometry ) {
  159. this.geometries[ geometry.uuid ] = geometry;
  160. },
  161. setGeometryName: function ( geometry, name ) {
  162. geometry.name = name;
  163. this.signals.sceneGraphChanged.dispatch();
  164. },
  165. addMaterial: function ( material ) {
  166. if ( Array.isArray( material ) ) {
  167. for ( var i = 0, l = material.length; i < l; i ++ ) {
  168. this.addMaterialToRefCounter( material[ i ] );
  169. }
  170. } else {
  171. this.addMaterialToRefCounter( material );
  172. }
  173. this.signals.materialAdded.dispatch();
  174. },
  175. addMaterialToRefCounter: function ( material ) {
  176. var materialsRefCounter = this.materialsRefCounter;
  177. var count = materialsRefCounter.get( material );
  178. if ( count === undefined ) {
  179. materialsRefCounter.set( material, 1 );
  180. this.materials[ material.uuid ] = material;
  181. } else {
  182. count ++;
  183. materialsRefCounter.set( material, count );
  184. }
  185. },
  186. removeMaterial: function ( material ) {
  187. if ( Array.isArray( material ) ) {
  188. for ( var i = 0, l = material.length; i < l; i ++ ) {
  189. this.removeMaterialFromRefCounter( material[ i ] );
  190. }
  191. } else {
  192. this.removeMaterialFromRefCounter( material );
  193. }
  194. this.signals.materialRemoved.dispatch();
  195. },
  196. removeMaterialFromRefCounter: function ( material ) {
  197. var materialsRefCounter = this.materialsRefCounter;
  198. var count = materialsRefCounter.get( material );
  199. count --;
  200. if ( count === 0 ) {
  201. materialsRefCounter.delete( material );
  202. delete this.materials[ material.uuid ];
  203. } else {
  204. materialsRefCounter.set( material, count );
  205. }
  206. },
  207. getMaterialById: function ( id ) {
  208. var material;
  209. var materials = Object.values( this.materials );
  210. for ( var i = 0; i < materials.length; i ++ ) {
  211. if ( materials[ i ].id === id ) {
  212. material = materials[ i ];
  213. break;
  214. }
  215. }
  216. return material;
  217. },
  218. setMaterialName: function ( material, name ) {
  219. material.name = name;
  220. this.signals.sceneGraphChanged.dispatch();
  221. },
  222. addTexture: function ( texture ) {
  223. this.textures[ texture.uuid ] = texture;
  224. },
  225. //
  226. addCamera: function ( camera ) {
  227. if ( camera.isCamera ) {
  228. this.cameras[ camera.uuid ] = camera;
  229. this.signals.cameraAdded.dispatch( camera );
  230. }
  231. },
  232. removeCamera: function ( camera ) {
  233. if ( this.cameras[ camera.uuid ] !== undefined ) {
  234. delete this.cameras[ camera.uuid ];
  235. this.signals.cameraRemoved.dispatch( camera );
  236. }
  237. },
  238. //
  239. addHelper: function () {
  240. var geometry = new THREE.SphereGeometry( 2, 4, 2 );
  241. var material = new THREE.MeshBasicMaterial( { color: 0xff0000, visible: false } );
  242. return function ( object, helper ) {
  243. if ( helper === undefined ) {
  244. if ( object.isCamera ) {
  245. helper = new THREE.CameraHelper( object );
  246. } else if ( object.isPointLight ) {
  247. helper = new THREE.PointLightHelper( object, 1 );
  248. } else if ( object.isDirectionalLight ) {
  249. helper = new THREE.DirectionalLightHelper( object, 1 );
  250. } else if ( object.isSpotLight ) {
  251. helper = new THREE.SpotLightHelper( object );
  252. } else if ( object.isHemisphereLight ) {
  253. helper = new THREE.HemisphereLightHelper( object, 1 );
  254. } else if ( object.isSkinnedMesh ) {
  255. helper = new THREE.SkeletonHelper( object.skeleton.bones[ 0 ] );
  256. } else if ( object.isBone === true && object.parent && object.parent.isBone !== true ) {
  257. helper = new THREE.SkeletonHelper( object );
  258. } else {
  259. // no helper for this object type
  260. return;
  261. }
  262. const picker = new THREE.Mesh( geometry, material );
  263. picker.name = 'picker';
  264. picker.userData.object = object;
  265. helper.add( picker );
  266. }
  267. this.sceneHelpers.add( helper );
  268. this.helpers[ object.id ] = helper;
  269. this.signals.helperAdded.dispatch( helper );
  270. };
  271. }(),
  272. removeHelper: function ( object ) {
  273. if ( this.helpers[ object.id ] !== undefined ) {
  274. var helper = this.helpers[ object.id ];
  275. helper.parent.remove( helper );
  276. helper.dispose();
  277. delete this.helpers[ object.id ];
  278. this.signals.helperRemoved.dispatch( helper );
  279. }
  280. },
  281. //
  282. addScript: function ( object, script ) {
  283. if ( this.scripts[ object.uuid ] === undefined ) {
  284. this.scripts[ object.uuid ] = [];
  285. }
  286. this.scripts[ object.uuid ].push( script );
  287. this.signals.scriptAdded.dispatch( script );
  288. },
  289. removeScript: function ( object, script ) {
  290. if ( this.scripts[ object.uuid ] === undefined ) return;
  291. var index = this.scripts[ object.uuid ].indexOf( script );
  292. if ( index !== - 1 ) {
  293. this.scripts[ object.uuid ].splice( index, 1 );
  294. }
  295. this.signals.scriptRemoved.dispatch( script );
  296. },
  297. getObjectMaterial: function ( object, slot ) {
  298. var material = object.material;
  299. if ( Array.isArray( material ) && slot !== undefined ) {
  300. material = material[ slot ];
  301. }
  302. return material;
  303. },
  304. setObjectMaterial: function ( object, slot, newMaterial ) {
  305. if ( Array.isArray( object.material ) && slot !== undefined ) {
  306. object.material[ slot ] = newMaterial;
  307. } else {
  308. object.material = newMaterial;
  309. }
  310. },
  311. setViewportCamera: function ( uuid ) {
  312. this.viewportCamera = this.cameras[ uuid ];
  313. this.signals.viewportCameraChanged.dispatch();
  314. },
  315. setViewportShading: function ( value ) {
  316. this.viewportShading = value;
  317. this.signals.viewportShadingChanged.dispatch();
  318. },
  319. //
  320. select: function ( object ) {
  321. this.selector.select( object );
  322. },
  323. selectById: function ( id ) {
  324. if ( id === this.camera.id ) {
  325. this.select( this.camera );
  326. return;
  327. }
  328. this.select( this.scene.getObjectById( id ) );
  329. },
  330. selectByUuid: function ( uuid ) {
  331. var scope = this;
  332. this.scene.traverse( function ( child ) {
  333. if ( child.uuid === uuid ) {
  334. scope.select( child );
  335. }
  336. } );
  337. },
  338. deselect: function () {
  339. this.selector.deselect();
  340. },
  341. focus: function ( object ) {
  342. if ( object !== undefined ) {
  343. this.signals.objectFocused.dispatch( object );
  344. }
  345. },
  346. focusById: function ( id ) {
  347. this.focus( this.scene.getObjectById( id ) );
  348. },
  349. clear: function () {
  350. this.history.clear();
  351. this.storage.clear();
  352. this.camera.copy( _DEFAULT_CAMERA );
  353. this.signals.cameraResetted.dispatch();
  354. this.scene.name = 'Scene';
  355. this.scene.userData = {};
  356. this.scene.background = null;
  357. this.scene.environment = null;
  358. this.scene.fog = null;
  359. var objects = this.scene.children;
  360. this.signals.sceneGraphChanged.active = false;
  361. while ( objects.length > 0 ) {
  362. this.removeObject( objects[ 0 ] );
  363. }
  364. this.signals.sceneGraphChanged.active = true;
  365. this.geometries = {};
  366. this.materials = {};
  367. this.textures = {};
  368. this.scripts = {};
  369. this.materialsRefCounter.clear();
  370. this.animations = {};
  371. this.mixer.stopAllAction();
  372. this.deselect();
  373. this.signals.editorCleared.dispatch();
  374. },
  375. //
  376. fromJSON: async function ( json ) {
  377. var loader = new THREE.ObjectLoader();
  378. var camera = await loader.parseAsync( json.camera );
  379. const existingUuid = this.camera.uuid;
  380. const incomingUuid = camera.uuid;
  381. // copy all properties, including uuid
  382. this.camera.copy( camera );
  383. this.camera.uuid = incomingUuid;
  384. delete this.cameras[ existingUuid ]; // remove old entry [existingUuid, this.camera]
  385. this.cameras[ incomingUuid ] = this.camera; // add new entry [incomingUuid, this.camera]
  386. this.signals.cameraResetted.dispatch();
  387. this.history.fromJSON( json.history );
  388. this.scripts = json.scripts;
  389. this.setScene( await loader.parseAsync( json.scene ) );
  390. if ( json.environment === 'ModelViewer' ) {
  391. this.signals.sceneEnvironmentChanged.dispatch( json.environment );
  392. this.signals.refreshSidebarEnvironment.dispatch();
  393. }
  394. },
  395. toJSON: function () {
  396. // scripts clean up
  397. var scene = this.scene;
  398. var scripts = this.scripts;
  399. for ( var key in scripts ) {
  400. var script = scripts[ key ];
  401. if ( script.length === 0 || scene.getObjectByProperty( 'uuid', key ) === undefined ) {
  402. delete scripts[ key ];
  403. }
  404. }
  405. // honor modelviewer environment
  406. let environment = null;
  407. if ( this.scene.environment !== null && this.scene.environment.isRenderTargetTexture === true ) {
  408. environment = 'ModelViewer';
  409. }
  410. //
  411. return {
  412. metadata: {},
  413. project: {
  414. shadows: this.config.getKey( 'project/renderer/shadows' ),
  415. shadowType: this.config.getKey( 'project/renderer/shadowType' ),
  416. toneMapping: this.config.getKey( 'project/renderer/toneMapping' ),
  417. toneMappingExposure: this.config.getKey( 'project/renderer/toneMappingExposure' )
  418. },
  419. camera: this.viewportCamera.toJSON(),
  420. scene: this.scene.toJSON(),
  421. scripts: this.scripts,
  422. history: this.history.toJSON(),
  423. environment: environment
  424. };
  425. },
  426. objectByUuid: function ( uuid ) {
  427. return this.scene.getObjectByProperty( 'uuid', uuid, true );
  428. },
  429. execute: function ( cmd, optionalName ) {
  430. this.history.execute( cmd, optionalName );
  431. },
  432. undo: function () {
  433. this.history.undo();
  434. },
  435. redo: function () {
  436. this.history.redo();
  437. },
  438. utils: {
  439. save: save,
  440. saveArrayBuffer: saveArrayBuffer,
  441. saveString: saveString,
  442. formatNumber: formatNumber
  443. }
  444. };
  445. const link = document.createElement( 'a' );
  446. function save( blob, filename ) {
  447. if ( link.href ) {
  448. URL.revokeObjectURL( link.href );
  449. }
  450. link.href = URL.createObjectURL( blob );
  451. link.download = filename || 'data.json';
  452. link.dispatchEvent( new MouseEvent( 'click' ) );
  453. }
  454. function saveArrayBuffer( buffer, filename ) {
  455. save( new Blob( [ buffer ], { type: 'application/octet-stream' } ), filename );
  456. }
  457. function saveString( text, filename ) {
  458. save( new Blob( [ text ], { type: 'text/plain' } ), filename );
  459. }
  460. function formatNumber( number ) {
  461. return new Intl.NumberFormat( 'en-us', { useGrouping: true } ).format( number );
  462. }
  463. export { Editor };