Editor.js 13 KB

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