Editor.js 14 KB

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