Editor.js 13 KB

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