Editor.js 13 KB

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