Editor.js 13 KB

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