Editor.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  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. // 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. rendererChanged: 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. animationStopped: new Signal()
  62. };
  63. this.config = new Config();
  64. this.history = new _History( this );
  65. this.storage = new _Storage();
  66. this.strings = new Strings( this.config );
  67. this.loader = new Loader( this );
  68. this.camera = _DEFAULT_CAMERA.clone();
  69. this.scene = new THREE.Scene();
  70. this.scene.name = 'Scene';
  71. this.sceneHelpers = new THREE.Scene();
  72. this.object = {};
  73. this.geometries = {};
  74. this.materials = {};
  75. this.textures = {};
  76. this.scripts = {};
  77. this.materialsRefCounter = new Map(); // tracks how often is a material used by a 3D object
  78. this.mixer = new THREE.AnimationMixer( this.scene );
  79. this.selected = null;
  80. this.helpers = {};
  81. this.cameras = {};
  82. this.viewportCamera = this.camera;
  83. this.addCamera( this.camera );
  84. }
  85. Editor.prototype = {
  86. setScene: function ( scene ) {
  87. this.scene.uuid = scene.uuid;
  88. this.scene.name = scene.name;
  89. this.scene.background = ( scene.background !== null ) ? scene.background.clone() : null;
  90. if ( scene.fog !== null ) this.scene.fog = scene.fog.clone();
  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, 1 );
  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 {
  246. // no helper for this object type
  247. return;
  248. }
  249. var picker = new THREE.Mesh( geometry, material );
  250. picker.name = 'picker';
  251. picker.userData.object = object;
  252. helper.add( picker );
  253. }
  254. this.sceneHelpers.add( helper );
  255. this.helpers[ object.id ] = helper;
  256. this.signals.helperAdded.dispatch( helper );
  257. };
  258. }(),
  259. removeHelper: function ( object ) {
  260. if ( this.helpers[ object.id ] !== undefined ) {
  261. var helper = this.helpers[ object.id ];
  262. helper.parent.remove( helper );
  263. delete this.helpers[ object.id ];
  264. this.signals.helperRemoved.dispatch( helper );
  265. }
  266. },
  267. //
  268. addScript: function ( object, script ) {
  269. if ( this.scripts[ object.uuid ] === undefined ) {
  270. this.scripts[ object.uuid ] = [];
  271. }
  272. this.scripts[ object.uuid ].push( script );
  273. this.signals.scriptAdded.dispatch( script );
  274. },
  275. removeScript: function ( object, script ) {
  276. if ( this.scripts[ object.uuid ] === undefined ) return;
  277. var index = this.scripts[ object.uuid ].indexOf( script );
  278. if ( index !== - 1 ) {
  279. this.scripts[ object.uuid ].splice( index, 1 );
  280. }
  281. this.signals.scriptRemoved.dispatch( script );
  282. },
  283. getObjectMaterial: function ( object, slot ) {
  284. var material = object.material;
  285. if ( Array.isArray( material ) && slot !== undefined ) {
  286. material = material[ slot ];
  287. }
  288. return material;
  289. },
  290. setObjectMaterial: function ( object, slot, newMaterial ) {
  291. if ( Array.isArray( object.material ) && slot !== undefined ) {
  292. object.material[ slot ] = newMaterial;
  293. } else {
  294. object.material = newMaterial;
  295. }
  296. },
  297. setViewportCamera: function ( uuid ) {
  298. this.viewportCamera = this.cameras[ uuid ];
  299. this.signals.viewportCameraChanged.dispatch();
  300. },
  301. //
  302. select: function ( object ) {
  303. if ( this.selected === object ) return;
  304. var uuid = null;
  305. if ( object !== null ) {
  306. uuid = object.uuid;
  307. }
  308. this.selected = object;
  309. this.config.setKey( 'selected', uuid );
  310. this.signals.objectSelected.dispatch( object );
  311. },
  312. selectById: function ( id ) {
  313. if ( id === this.camera.id ) {
  314. this.select( this.camera );
  315. return;
  316. }
  317. this.select( this.scene.getObjectById( id ) );
  318. },
  319. selectByUuid: function ( uuid ) {
  320. var scope = this;
  321. this.scene.traverse( function ( child ) {
  322. if ( child.uuid === uuid ) {
  323. scope.select( child );
  324. }
  325. } );
  326. },
  327. deselect: function () {
  328. this.select( null );
  329. },
  330. focus: function ( object ) {
  331. if ( object !== undefined ) {
  332. this.signals.objectFocused.dispatch( object );
  333. }
  334. },
  335. focusById: function ( id ) {
  336. this.focus( this.scene.getObjectById( id ) );
  337. },
  338. clear: function () {
  339. this.history.clear();
  340. this.storage.clear();
  341. this.camera.copy( _DEFAULT_CAMERA );
  342. this.signals.cameraResetted.dispatch();
  343. this.scene.name = 'Scene';
  344. this.scene.userData = {};
  345. this.scene.background = null;
  346. this.scene.environment = null;
  347. this.scene.fog = null;
  348. var objects = this.scene.children;
  349. while ( objects.length > 0 ) {
  350. this.removeObject( objects[ 0 ] );
  351. }
  352. this.geometries = {};
  353. this.materials = {};
  354. this.textures = {};
  355. this.scripts = {};
  356. this.materialsRefCounter.clear();
  357. this.animations = {};
  358. this.mixer.stopAllAction();
  359. this.deselect();
  360. this.signals.editorCleared.dispatch();
  361. },
  362. //
  363. fromJSON: function ( json ) {
  364. var scope = this;
  365. var loader = new THREE.ObjectLoader();
  366. var camera = loader.parse( json.camera );
  367. this.camera.copy( camera );
  368. this.signals.cameraResetted.dispatch();
  369. this.history.fromJSON( json.history );
  370. this.scripts = json.scripts;
  371. loader.parse( json.scene, function ( scene ) {
  372. scope.setScene( scene );
  373. } );
  374. },
  375. toJSON: function () {
  376. // scripts clean up
  377. var scene = this.scene;
  378. var scripts = this.scripts;
  379. for ( var key in scripts ) {
  380. var script = scripts[ key ];
  381. if ( script.length === 0 || scene.getObjectByProperty( 'uuid', key ) === undefined ) {
  382. delete scripts[ key ];
  383. }
  384. }
  385. //
  386. return {
  387. metadata: {},
  388. project: {
  389. shadows: this.config.getKey( 'project/renderer/shadows' ),
  390. shadowType: this.config.getKey( 'project/renderer/shadowType' ),
  391. vr: this.config.getKey( 'project/vr' ),
  392. physicallyCorrectLights: this.config.getKey( 'project/renderer/physicallyCorrectLights' ),
  393. toneMapping: this.config.getKey( 'project/renderer/toneMapping' ),
  394. toneMappingExposure: this.config.getKey( 'project/renderer/toneMappingExposure' )
  395. },
  396. camera: this.camera.toJSON(),
  397. scene: this.scene.toJSON(),
  398. scripts: this.scripts,
  399. history: this.history.toJSON()
  400. };
  401. },
  402. objectByUuid: function ( uuid ) {
  403. return this.scene.getObjectByProperty( 'uuid', uuid, true );
  404. },
  405. execute: function ( cmd, optionalName ) {
  406. this.history.execute( cmd, optionalName );
  407. },
  408. undo: function () {
  409. this.history.undo();
  410. },
  411. redo: function () {
  412. this.history.redo();
  413. }
  414. };
  415. export { Editor };