Editor.js 13 KB

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