Editor.js 14 KB

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