Editor.js 15 KB

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