Editor.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  1. import * as THREE from '../../build/three.module.js';
  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. // notifications
  20. editorCleared: new Signal(),
  21. savingStarted: new Signal(),
  22. savingFinished: new Signal(),
  23. transformModeChanged: new Signal(),
  24. snapChanged: new Signal(),
  25. spaceChanged: new Signal(),
  26. rendererChanged: new Signal(),
  27. rendererUpdated: new Signal(),
  28. sceneBackgroundChanged: new Signal(),
  29. sceneEnvironmentChanged: new Signal(),
  30. sceneFogChanged: new Signal(),
  31. sceneFogSettingsChanged: new Signal(),
  32. sceneGraphChanged: new Signal(),
  33. sceneRendered: new Signal(),
  34. cameraChanged: new Signal(),
  35. cameraResetted: new Signal(),
  36. geometryChanged: new Signal(),
  37. objectSelected: new Signal(),
  38. objectFocused: new Signal(),
  39. objectAdded: new Signal(),
  40. objectChanged: new Signal(),
  41. objectRemoved: new Signal(),
  42. cameraAdded: new Signal(),
  43. cameraRemoved: new Signal(),
  44. helperAdded: new Signal(),
  45. helperRemoved: new Signal(),
  46. materialAdded: new Signal(),
  47. materialChanged: new Signal(),
  48. materialRemoved: new Signal(),
  49. scriptAdded: new Signal(),
  50. scriptChanged: new Signal(),
  51. scriptRemoved: new Signal(),
  52. windowResize: new Signal(),
  53. showGridChanged: new Signal(),
  54. showHelpersChanged: new Signal(),
  55. refreshSidebarObject3D: new Signal(),
  56. historyChanged: new Signal(),
  57. viewportCameraChanged: new Signal()
  58. };
  59. this.config = new Config();
  60. this.history = new _History( this );
  61. this.storage = new _Storage();
  62. this.strings = new Strings( this.config );
  63. this.loader = new Loader( this );
  64. this.camera = _DEFAULT_CAMERA.clone();
  65. this.scene = new THREE.Scene();
  66. this.scene.name = 'Scene';
  67. this.sceneHelpers = new THREE.Scene();
  68. this.object = {};
  69. this.geometries = {};
  70. this.materials = {};
  71. this.textures = {};
  72. this.scripts = {};
  73. this.materialsRefCounter = new Map(); // tracks how often is a material used by a 3D object
  74. this.animations = {};
  75. this.mixer = new THREE.AnimationMixer( this.scene );
  76. this.selected = null;
  77. this.helpers = {};
  78. this.cameras = {};
  79. this.viewportCamera = this.camera;
  80. this.addCamera( this.camera );
  81. }
  82. Editor.prototype = {
  83. setScene: function ( scene ) {
  84. this.scene.uuid = scene.uuid;
  85. this.scene.name = scene.name;
  86. this.scene.background = ( scene.background !== null ) ? scene.background.clone() : null;
  87. if ( scene.fog !== null ) this.scene.fog = scene.fog.clone();
  88. this.scene.userData = JSON.parse( JSON.stringify( scene.userData ) );
  89. // avoid render per object
  90. this.signals.sceneGraphChanged.active = false;
  91. while ( scene.children.length > 0 ) {
  92. this.addObject( scene.children[ 0 ] );
  93. }
  94. this.signals.sceneGraphChanged.active = true;
  95. this.signals.sceneGraphChanged.dispatch();
  96. },
  97. //
  98. addObject: function ( object, parent, index ) {
  99. var scope = this;
  100. object.traverse( function ( child ) {
  101. if ( child.geometry !== undefined ) scope.addGeometry( child.geometry );
  102. if ( child.material !== undefined ) scope.addMaterial( child.material );
  103. scope.addCamera( child );
  104. scope.addHelper( child );
  105. } );
  106. if ( parent === undefined ) {
  107. this.scene.add( object );
  108. } else {
  109. parent.children.splice( index, 0, object );
  110. object.parent = parent;
  111. }
  112. this.signals.objectAdded.dispatch( object );
  113. this.signals.sceneGraphChanged.dispatch();
  114. },
  115. moveObject: function ( object, parent, before ) {
  116. if ( parent === undefined ) {
  117. parent = this.scene;
  118. }
  119. parent.add( object );
  120. // sort children array
  121. if ( before !== undefined ) {
  122. var index = parent.children.indexOf( before );
  123. parent.children.splice( index, 0, object );
  124. parent.children.pop();
  125. }
  126. this.signals.sceneGraphChanged.dispatch();
  127. },
  128. nameObject: function ( object, name ) {
  129. object.name = name;
  130. this.signals.sceneGraphChanged.dispatch();
  131. },
  132. removeObject: function ( object ) {
  133. if ( object.parent === null ) return; // avoid deleting the camera or scene
  134. var scope = this;
  135. object.traverse( function ( child ) {
  136. scope.removeCamera( child );
  137. scope.removeHelper( child );
  138. if ( child.material !== undefined ) scope.removeMaterial( child.material );
  139. } );
  140. object.parent.remove( object );
  141. this.signals.objectRemoved.dispatch( object );
  142. this.signals.sceneGraphChanged.dispatch();
  143. },
  144. addGeometry: function ( geometry ) {
  145. this.geometries[ geometry.uuid ] = geometry;
  146. },
  147. setGeometryName: function ( geometry, name ) {
  148. geometry.name = name;
  149. this.signals.sceneGraphChanged.dispatch();
  150. },
  151. addMaterial: function ( material ) {
  152. if ( Array.isArray( material ) ) {
  153. for ( var i = 0, l = material.length; i < l; i ++ ) {
  154. this.addMaterialToRefCounter( material[ i ] );
  155. }
  156. } else {
  157. this.addMaterialToRefCounter( material );
  158. }
  159. this.signals.materialAdded.dispatch();
  160. },
  161. addMaterialToRefCounter: function ( material ) {
  162. var materialsRefCounter = this.materialsRefCounter;
  163. var count = materialsRefCounter.get( material );
  164. if ( count === undefined ) {
  165. materialsRefCounter.set( material, 1 );
  166. this.materials[ material.uuid ] = material;
  167. } else {
  168. count ++;
  169. materialsRefCounter.set( material, count );
  170. }
  171. },
  172. removeMaterial: function ( material ) {
  173. if ( Array.isArray( material ) ) {
  174. for ( var i = 0, l = material.length; i < l; i ++ ) {
  175. this.removeMaterialFromRefCounter( material[ i ] );
  176. }
  177. } else {
  178. this.removeMaterialFromRefCounter( material );
  179. }
  180. this.signals.materialRemoved.dispatch();
  181. },
  182. removeMaterialFromRefCounter: function ( material ) {
  183. var materialsRefCounter = this.materialsRefCounter;
  184. var count = materialsRefCounter.get( material );
  185. count --;
  186. if ( count === 0 ) {
  187. materialsRefCounter.delete( material );
  188. delete this.materials[ material.uuid ];
  189. } else {
  190. materialsRefCounter.set( material, count );
  191. }
  192. },
  193. getMaterialById: function ( id ) {
  194. var material;
  195. var materials = Object.values( this.materials );
  196. for ( var i = 0; i < materials.length; i ++ ) {
  197. if ( materials[ i ].id === id ) {
  198. material = materials[ i ];
  199. break;
  200. }
  201. }
  202. return material;
  203. },
  204. setMaterialName: function ( material, name ) {
  205. material.name = name;
  206. this.signals.sceneGraphChanged.dispatch();
  207. },
  208. addTexture: function ( texture ) {
  209. this.textures[ texture.uuid ] = texture;
  210. },
  211. addAnimation: function ( object, animations ) {
  212. if ( animations.length > 0 ) {
  213. this.animations[ object.uuid ] = animations;
  214. }
  215. },
  216. //
  217. addCamera: function ( camera ) {
  218. if ( camera.isCamera ) {
  219. this.cameras[ camera.uuid ] = camera;
  220. this.signals.cameraAdded.dispatch( camera );
  221. }
  222. },
  223. removeCamera: function ( camera ) {
  224. if ( this.cameras[ camera.uuid ] !== undefined ) {
  225. delete this.cameras[ camera.uuid ];
  226. this.signals.cameraRemoved.dispatch( camera );
  227. }
  228. },
  229. //
  230. addHelper: function () {
  231. var geometry = new THREE.SphereBufferGeometry( 2, 4, 2 );
  232. var material = new THREE.MeshBasicMaterial( { color: 0xff0000, visible: false } );
  233. return function ( object, helper ) {
  234. if ( helper === undefined ) {
  235. if ( object.isCamera ) {
  236. helper = new THREE.CameraHelper( object );
  237. } else if ( object.isPointLight ) {
  238. helper = new THREE.PointLightHelper( object, 1 );
  239. } else if ( object.isDirectionalLight ) {
  240. helper = new THREE.DirectionalLightHelper( object, 1 );
  241. } else if ( object.isSpotLight ) {
  242. helper = new THREE.SpotLightHelper( object, 1 );
  243. } else if ( object.isHemisphereLight ) {
  244. helper = new THREE.HemisphereLightHelper( object, 1 );
  245. } else if ( object.isSkinnedMesh ) {
  246. helper = new THREE.SkeletonHelper( object.skeleton.bones[ 0 ] );
  247. } else {
  248. // no helper for this object type
  249. return;
  250. }
  251. var 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, true ) );
  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, true ) );
  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: function ( json ) {
  366. var scope = this;
  367. var loader = new THREE.ObjectLoader();
  368. var camera = loader.parse( json.camera );
  369. this.camera.copy( camera );
  370. this.signals.cameraResetted.dispatch();
  371. this.history.fromJSON( json.history );
  372. this.scripts = json.scripts;
  373. loader.parse( json.scene, function ( scene ) {
  374. scope.setScene( scene );
  375. } );
  376. },
  377. toJSON: function () {
  378. // scripts clean up
  379. var scene = this.scene;
  380. var scripts = this.scripts;
  381. for ( var key in scripts ) {
  382. var script = scripts[ key ];
  383. if ( script.length === 0 || scene.getObjectByProperty( 'uuid', key ) === undefined ) {
  384. delete scripts[ key ];
  385. }
  386. }
  387. //
  388. return {
  389. metadata: {},
  390. project: {
  391. shadows: this.config.getKey( 'project/renderer/shadows' ),
  392. shadowType: this.config.getKey( 'project/renderer/shadowType' ),
  393. vr: this.config.getKey( 'project/vr' ),
  394. physicallyCorrectLights: this.config.getKey( 'project/renderer/physicallyCorrectLights' ),
  395. toneMapping: this.config.getKey( 'project/renderer/toneMapping' ),
  396. toneMappingExposure: this.config.getKey( 'project/renderer/toneMappingExposure' )
  397. },
  398. camera: this.camera.toJSON(),
  399. scene: this.scene.toJSON(),
  400. scripts: this.scripts,
  401. history: this.history.toJSON()
  402. };
  403. },
  404. objectByUuid: function ( uuid ) {
  405. return this.scene.getObjectByProperty( 'uuid', uuid, true );
  406. },
  407. execute: function ( cmd, optionalName ) {
  408. this.history.execute( cmd, optionalName );
  409. },
  410. undo: function () {
  411. this.history.undo();
  412. },
  413. redo: function () {
  414. this.history.redo();
  415. }
  416. };
  417. export { Editor };