Editor.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  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. animationStopped: new Signal()
  59. };
  60. this.config = new Config();
  61. this.history = new _History( this );
  62. this.storage = new _Storage();
  63. this.strings = new Strings( this.config );
  64. this.loader = new Loader( this );
  65. this.camera = _DEFAULT_CAMERA.clone();
  66. this.scene = new THREE.Scene();
  67. this.scene.name = 'Scene';
  68. this.sceneHelpers = new THREE.Scene();
  69. this.object = {};
  70. this.geometries = {};
  71. this.materials = {};
  72. this.textures = {};
  73. this.scripts = {};
  74. this.materialsRefCounter = new Map(); // tracks how often is a material used by a 3D object
  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. //
  212. addCamera: function ( camera ) {
  213. if ( camera.isCamera ) {
  214. this.cameras[ camera.uuid ] = camera;
  215. this.signals.cameraAdded.dispatch( camera );
  216. }
  217. },
  218. removeCamera: function ( camera ) {
  219. if ( this.cameras[ camera.uuid ] !== undefined ) {
  220. delete this.cameras[ camera.uuid ];
  221. this.signals.cameraRemoved.dispatch( camera );
  222. }
  223. },
  224. //
  225. addHelper: function () {
  226. var geometry = new THREE.SphereGeometry( 2, 4, 2 );
  227. var material = new THREE.MeshBasicMaterial( { color: 0xff0000, visible: false } );
  228. return function ( object, helper ) {
  229. if ( helper === undefined ) {
  230. if ( object.isCamera ) {
  231. helper = new THREE.CameraHelper( object );
  232. } else if ( object.isPointLight ) {
  233. helper = new THREE.PointLightHelper( object, 1 );
  234. } else if ( object.isDirectionalLight ) {
  235. helper = new THREE.DirectionalLightHelper( object, 1 );
  236. } else if ( object.isSpotLight ) {
  237. helper = new THREE.SpotLightHelper( object, 1 );
  238. } else if ( object.isHemisphereLight ) {
  239. helper = new THREE.HemisphereLightHelper( object, 1 );
  240. } else if ( object.isSkinnedMesh ) {
  241. helper = new THREE.SkeletonHelper( object.skeleton.bones[ 0 ] );
  242. } else {
  243. // no helper for this object type
  244. return;
  245. }
  246. var picker = new THREE.Mesh( geometry, material );
  247. picker.name = 'picker';
  248. picker.userData.object = object;
  249. helper.add( picker );
  250. }
  251. this.sceneHelpers.add( helper );
  252. this.helpers[ object.id ] = helper;
  253. this.signals.helperAdded.dispatch( helper );
  254. };
  255. }(),
  256. removeHelper: function ( object ) {
  257. if ( this.helpers[ object.id ] !== undefined ) {
  258. var helper = this.helpers[ object.id ];
  259. helper.parent.remove( helper );
  260. delete this.helpers[ object.id ];
  261. this.signals.helperRemoved.dispatch( helper );
  262. }
  263. },
  264. //
  265. addScript: function ( object, script ) {
  266. if ( this.scripts[ object.uuid ] === undefined ) {
  267. this.scripts[ object.uuid ] = [];
  268. }
  269. this.scripts[ object.uuid ].push( script );
  270. this.signals.scriptAdded.dispatch( script );
  271. },
  272. removeScript: function ( object, script ) {
  273. if ( this.scripts[ object.uuid ] === undefined ) return;
  274. var index = this.scripts[ object.uuid ].indexOf( script );
  275. if ( index !== - 1 ) {
  276. this.scripts[ object.uuid ].splice( index, 1 );
  277. }
  278. this.signals.scriptRemoved.dispatch( script );
  279. },
  280. getObjectMaterial: function ( object, slot ) {
  281. var material = object.material;
  282. if ( Array.isArray( material ) && slot !== undefined ) {
  283. material = material[ slot ];
  284. }
  285. return material;
  286. },
  287. setObjectMaterial: function ( object, slot, newMaterial ) {
  288. if ( Array.isArray( object.material ) && slot !== undefined ) {
  289. object.material[ slot ] = newMaterial;
  290. } else {
  291. object.material = newMaterial;
  292. }
  293. },
  294. setViewportCamera: function ( uuid ) {
  295. this.viewportCamera = this.cameras[ uuid ];
  296. this.signals.viewportCameraChanged.dispatch();
  297. },
  298. //
  299. select: function ( object ) {
  300. if ( this.selected === object ) return;
  301. var uuid = null;
  302. if ( object !== null ) {
  303. uuid = object.uuid;
  304. }
  305. this.selected = object;
  306. this.config.setKey( 'selected', uuid );
  307. this.signals.objectSelected.dispatch( object );
  308. },
  309. selectById: function ( id ) {
  310. if ( id === this.camera.id ) {
  311. this.select( this.camera );
  312. return;
  313. }
  314. this.select( this.scene.getObjectById( id ) );
  315. },
  316. selectByUuid: function ( uuid ) {
  317. var scope = this;
  318. this.scene.traverse( function ( child ) {
  319. if ( child.uuid === uuid ) {
  320. scope.select( child );
  321. }
  322. } );
  323. },
  324. deselect: function () {
  325. this.select( null );
  326. },
  327. focus: function ( object ) {
  328. if ( object !== undefined ) {
  329. this.signals.objectFocused.dispatch( object );
  330. }
  331. },
  332. focusById: function ( id ) {
  333. this.focus( this.scene.getObjectById( id ) );
  334. },
  335. clear: function () {
  336. this.history.clear();
  337. this.storage.clear();
  338. this.camera.copy( _DEFAULT_CAMERA );
  339. this.signals.cameraResetted.dispatch();
  340. this.scene.name = 'Scene';
  341. this.scene.userData = {};
  342. this.scene.background = null;
  343. this.scene.environment = null;
  344. this.scene.fog = null;
  345. var objects = this.scene.children;
  346. while ( objects.length > 0 ) {
  347. this.removeObject( objects[ 0 ] );
  348. }
  349. this.geometries = {};
  350. this.materials = {};
  351. this.textures = {};
  352. this.scripts = {};
  353. this.materialsRefCounter.clear();
  354. this.animations = {};
  355. this.mixer.stopAllAction();
  356. this.deselect();
  357. this.signals.editorCleared.dispatch();
  358. },
  359. //
  360. fromJSON: function ( json ) {
  361. var scope = this;
  362. var loader = new THREE.ObjectLoader();
  363. var camera = loader.parse( json.camera );
  364. this.camera.copy( camera );
  365. this.signals.cameraResetted.dispatch();
  366. this.history.fromJSON( json.history );
  367. this.scripts = json.scripts;
  368. loader.parse( json.scene, function ( scene ) {
  369. scope.setScene( scene );
  370. } );
  371. },
  372. toJSON: function () {
  373. // scripts clean up
  374. var scene = this.scene;
  375. var scripts = this.scripts;
  376. for ( var key in scripts ) {
  377. var script = scripts[ key ];
  378. if ( script.length === 0 || scene.getObjectByProperty( 'uuid', key ) === undefined ) {
  379. delete scripts[ key ];
  380. }
  381. }
  382. //
  383. return {
  384. metadata: {},
  385. project: {
  386. shadows: this.config.getKey( 'project/renderer/shadows' ),
  387. shadowType: this.config.getKey( 'project/renderer/shadowType' ),
  388. vr: this.config.getKey( 'project/vr' ),
  389. physicallyCorrectLights: this.config.getKey( 'project/renderer/physicallyCorrectLights' ),
  390. toneMapping: this.config.getKey( 'project/renderer/toneMapping' ),
  391. toneMappingExposure: this.config.getKey( 'project/renderer/toneMappingExposure' )
  392. },
  393. camera: this.camera.toJSON(),
  394. scene: this.scene.toJSON(),
  395. scripts: this.scripts,
  396. history: this.history.toJSON()
  397. };
  398. },
  399. objectByUuid: function ( uuid ) {
  400. return this.scene.getObjectByProperty( 'uuid', uuid, true );
  401. },
  402. execute: function ( cmd, optionalName ) {
  403. this.history.execute( cmd, optionalName );
  404. },
  405. undo: function () {
  406. this.history.undo();
  407. },
  408. redo: function () {
  409. this.history.redo();
  410. }
  411. };
  412. export { Editor };