Editor.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  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. showGridChanged: new Signal(),
  60. showHelpersChanged: new Signal(),
  61. refreshSidebarObject3D: new Signal(),
  62. refreshSidebarEnvironment: new Signal(),
  63. historyChanged: new Signal(),
  64. viewportCameraChanged: new Signal(),
  65. viewportShadingChanged: new Signal(),
  66. intersectionsDetected: 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. delete this.helpers[ object.id ];
  277. this.signals.helperRemoved.dispatch( helper );
  278. }
  279. },
  280. //
  281. addScript: function ( object, script ) {
  282. if ( this.scripts[ object.uuid ] === undefined ) {
  283. this.scripts[ object.uuid ] = [];
  284. }
  285. this.scripts[ object.uuid ].push( script );
  286. this.signals.scriptAdded.dispatch( script );
  287. },
  288. removeScript: function ( object, script ) {
  289. if ( this.scripts[ object.uuid ] === undefined ) return;
  290. var index = this.scripts[ object.uuid ].indexOf( script );
  291. if ( index !== - 1 ) {
  292. this.scripts[ object.uuid ].splice( index, 1 );
  293. }
  294. this.signals.scriptRemoved.dispatch( script );
  295. },
  296. getObjectMaterial: function ( object, slot ) {
  297. var material = object.material;
  298. if ( Array.isArray( material ) && slot !== undefined ) {
  299. material = material[ slot ];
  300. }
  301. return material;
  302. },
  303. setObjectMaterial: function ( object, slot, newMaterial ) {
  304. if ( Array.isArray( object.material ) && slot !== undefined ) {
  305. object.material[ slot ] = newMaterial;
  306. } else {
  307. object.material = newMaterial;
  308. }
  309. },
  310. setViewportCamera: function ( uuid ) {
  311. this.viewportCamera = this.cameras[ uuid ];
  312. this.signals.viewportCameraChanged.dispatch();
  313. },
  314. setViewportShading: function ( value ) {
  315. this.viewportShading = value;
  316. this.signals.viewportShadingChanged.dispatch();
  317. },
  318. //
  319. select: function ( object ) {
  320. this.selector.select( object );
  321. },
  322. selectById: function ( id ) {
  323. if ( id === this.camera.id ) {
  324. this.select( this.camera );
  325. return;
  326. }
  327. this.select( this.scene.getObjectById( id ) );
  328. },
  329. selectByUuid: function ( uuid ) {
  330. var scope = this;
  331. this.scene.traverse( function ( child ) {
  332. if ( child.uuid === uuid ) {
  333. scope.select( child );
  334. }
  335. } );
  336. },
  337. deselect: function () {
  338. this.selector.deselect();
  339. },
  340. focus: function ( object ) {
  341. if ( object !== undefined ) {
  342. this.signals.objectFocused.dispatch( object );
  343. }
  344. },
  345. focusById: function ( id ) {
  346. this.focus( this.scene.getObjectById( id ) );
  347. },
  348. clear: function () {
  349. this.history.clear();
  350. this.storage.clear();
  351. this.camera.copy( _DEFAULT_CAMERA );
  352. this.signals.cameraResetted.dispatch();
  353. this.scene.name = 'Scene';
  354. this.scene.userData = {};
  355. this.scene.background = null;
  356. this.scene.environment = null;
  357. this.scene.fog = null;
  358. var objects = this.scene.children;
  359. this.signals.sceneGraphChanged.active = false;
  360. while ( objects.length > 0 ) {
  361. this.removeObject( objects[ 0 ] );
  362. }
  363. this.signals.sceneGraphChanged.active = true;
  364. this.geometries = {};
  365. this.materials = {};
  366. this.textures = {};
  367. this.scripts = {};
  368. this.materialsRefCounter.clear();
  369. this.animations = {};
  370. this.mixer.stopAllAction();
  371. this.deselect();
  372. this.signals.editorCleared.dispatch();
  373. },
  374. //
  375. fromJSON: async function ( json ) {
  376. var loader = new THREE.ObjectLoader();
  377. var camera = await loader.parseAsync( json.camera );
  378. this.camera.copy( camera );
  379. this.signals.cameraResetted.dispatch();
  380. this.history.fromJSON( json.history );
  381. this.scripts = json.scripts;
  382. this.setScene( await loader.parseAsync( json.scene ) );
  383. if ( json.environment === 'ModelViewer' ) {
  384. this.signals.sceneEnvironmentChanged.dispatch( json.environment );
  385. this.signals.refreshSidebarEnvironment.dispatch();
  386. }
  387. },
  388. toJSON: function () {
  389. // scripts clean up
  390. var scene = this.scene;
  391. var scripts = this.scripts;
  392. for ( var key in scripts ) {
  393. var script = scripts[ key ];
  394. if ( script.length === 0 || scene.getObjectByProperty( 'uuid', key ) === undefined ) {
  395. delete scripts[ key ];
  396. }
  397. }
  398. // honor modelviewer environment
  399. let environment = null;
  400. if ( this.scene.environment !== null && this.scene.environment.isRenderTargetTexture === true ) {
  401. environment = 'ModelViewer';
  402. }
  403. //
  404. return {
  405. metadata: {},
  406. project: {
  407. shadows: this.config.getKey( 'project/renderer/shadows' ),
  408. shadowType: this.config.getKey( 'project/renderer/shadowType' ),
  409. toneMapping: this.config.getKey( 'project/renderer/toneMapping' ),
  410. toneMappingExposure: this.config.getKey( 'project/renderer/toneMappingExposure' )
  411. },
  412. camera: this.viewportCamera.toJSON(),
  413. scene: this.scene.toJSON(),
  414. scripts: this.scripts,
  415. history: this.history.toJSON(),
  416. environment: environment
  417. };
  418. },
  419. objectByUuid: function ( uuid ) {
  420. return this.scene.getObjectByProperty( 'uuid', uuid, true );
  421. },
  422. execute: function ( cmd, optionalName ) {
  423. this.history.execute( cmd, optionalName );
  424. },
  425. undo: function () {
  426. this.history.undo();
  427. },
  428. redo: function () {
  429. this.history.redo();
  430. },
  431. utils: {
  432. save: save,
  433. saveArrayBuffer: saveArrayBuffer,
  434. saveString: saveString
  435. }
  436. };
  437. const link = document.createElement( 'a' );
  438. function save( blob, filename ) {
  439. if ( link.href ) {
  440. URL.revokeObjectURL( link.href );
  441. }
  442. link.href = URL.createObjectURL( blob );
  443. link.download = filename || 'data.json';
  444. link.dispatchEvent( new MouseEvent( 'click' ) );
  445. }
  446. function saveArrayBuffer( buffer, filename ) {
  447. save( new Blob( [ buffer ], { type: 'application/octet-stream' } ), filename );
  448. }
  449. function saveString( text, filename ) {
  450. save( new Blob( [ text ], { type: 'text/plain' } ), filename );
  451. }
  452. export { Editor };