Editor.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  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 Editor = function () {
  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. sceneBackgroundChanged: new Signal(),
  31. sceneFogChanged: new Signal(),
  32. sceneGraphChanged: new Signal(),
  33. sceneRendered: new Signal(),
  34. cameraChanged: new Signal(),
  35. geometryChanged: new Signal(),
  36. objectSelected: new Signal(),
  37. objectFocused: new Signal(),
  38. objectAdded: new Signal(),
  39. objectChanged: new Signal(),
  40. objectRemoved: new Signal(),
  41. cameraAdded: new Signal(),
  42. cameraRemoved: new Signal(),
  43. helperAdded: new Signal(),
  44. helperRemoved: new Signal(),
  45. materialAdded: new Signal(),
  46. materialChanged: new Signal(),
  47. materialRemoved: new Signal(),
  48. scriptAdded: new Signal(),
  49. scriptChanged: new Signal(),
  50. scriptRemoved: new Signal(),
  51. windowResize: new Signal(),
  52. showGridChanged: new Signal(),
  53. refreshSidebarObject3D: new Signal(),
  54. historyChanged: new Signal(),
  55. viewportCameraChanged: new Signal()
  56. };
  57. this.config = new Config();
  58. this.history = new _History( this );
  59. this.storage = new _Storage();
  60. this.strings = new Strings( this.config );
  61. this.loader = new Loader( this );
  62. this.camera = this.DEFAULT_CAMERA.clone();
  63. this.scene = new THREE.Scene();
  64. this.scene.name = 'Scene';
  65. this.scene.background = new THREE.Color( 0xaaaaaa );
  66. this.sceneHelpers = new THREE.Scene();
  67. this.object = {};
  68. this.geometries = {};
  69. this.materials = {};
  70. this.textures = {};
  71. this.scripts = {};
  72. this.materialsRefCounter = new Map(); // tracks how often is a material used by a 3D object
  73. this.animations = {};
  74. this.mixer = new THREE.AnimationMixer( this.scene );
  75. this.selected = null;
  76. this.helpers = {};
  77. this.cameras = {};
  78. this.viewportCamera = this.camera;
  79. this.addCamera( this.camera );
  80. };
  81. Editor.prototype = {
  82. setScene: function ( scene ) {
  83. this.scene.uuid = scene.uuid;
  84. this.scene.name = scene.name;
  85. this.scene.background = ( scene.background !== null ) ? scene.background.clone() : null;
  86. if ( scene.fog !== null ) this.scene.fog = scene.fog.clone();
  87. this.scene.userData = JSON.parse( JSON.stringify( scene.userData ) );
  88. // avoid render per object
  89. this.signals.sceneGraphChanged.active = false;
  90. while ( scene.children.length > 0 ) {
  91. this.addObject( scene.children[ 0 ] );
  92. }
  93. this.signals.sceneGraphChanged.active = true;
  94. this.signals.sceneGraphChanged.dispatch();
  95. },
  96. //
  97. addObject: function ( object, parent, index ) {
  98. var scope = this;
  99. object.traverse( function ( child ) {
  100. if ( child.geometry !== undefined ) scope.addGeometry( child.geometry );
  101. if ( child.material !== undefined ) scope.addMaterial( child.material );
  102. scope.addCamera( child );
  103. scope.addHelper( child );
  104. } );
  105. if ( parent === undefined ) {
  106. this.scene.add( object );
  107. } else {
  108. parent.children.splice( index, 0, object );
  109. object.parent = parent;
  110. }
  111. this.signals.objectAdded.dispatch( object );
  112. this.signals.sceneGraphChanged.dispatch();
  113. },
  114. moveObject: function ( object, parent, before ) {
  115. if ( parent === undefined ) {
  116. parent = this.scene;
  117. }
  118. parent.add( object );
  119. // sort children array
  120. if ( before !== undefined ) {
  121. var index = parent.children.indexOf( before );
  122. parent.children.splice( index, 0, object );
  123. parent.children.pop();
  124. }
  125. this.signals.sceneGraphChanged.dispatch();
  126. },
  127. nameObject: function ( object, name ) {
  128. object.name = name;
  129. this.signals.sceneGraphChanged.dispatch();
  130. },
  131. removeObject: function ( object ) {
  132. if ( object.parent === null ) return; // avoid deleting the camera or scene
  133. var scope = this;
  134. object.traverse( function ( child ) {
  135. scope.removeCamera( child );
  136. scope.removeHelper( child );
  137. if ( child.material !== undefined ) scope.removeMaterial( child.material );
  138. } );
  139. object.parent.remove( object );
  140. this.signals.objectRemoved.dispatch( object );
  141. this.signals.sceneGraphChanged.dispatch();
  142. },
  143. addGeometry: function ( geometry ) {
  144. this.geometries[ geometry.uuid ] = geometry;
  145. },
  146. setGeometryName: function ( geometry, name ) {
  147. geometry.name = name;
  148. this.signals.sceneGraphChanged.dispatch();
  149. },
  150. addMaterial: function ( material ) {
  151. if ( Array.isArray( material ) ) {
  152. for ( var i = 0, l = material.length; i < l; i ++ ) {
  153. this.addMaterialToRefCounter( material[ i ] );
  154. }
  155. } else {
  156. this.addMaterialToRefCounter( material );
  157. }
  158. this.signals.materialAdded.dispatch();
  159. },
  160. addMaterialToRefCounter: function ( material ) {
  161. var materialsRefCounter = this.materialsRefCounter;
  162. var count = materialsRefCounter.get( material );
  163. if ( count === undefined ) {
  164. materialsRefCounter.set( material, 1 );
  165. this.materials[ material.uuid ] = material;
  166. } else {
  167. count ++;
  168. materialsRefCounter.set( material, count );
  169. }
  170. },
  171. removeMaterial: function ( material ) {
  172. if ( Array.isArray( material ) ) {
  173. for ( var i = 0, l = material.length; i < l; i ++ ) {
  174. this.removeMaterialFromRefCounter( material[ i ] );
  175. }
  176. } else {
  177. this.removeMaterialFromRefCounter( material );
  178. }
  179. this.signals.materialRemoved.dispatch();
  180. },
  181. removeMaterialFromRefCounter: function ( material ) {
  182. var materialsRefCounter = this.materialsRefCounter;
  183. var count = materialsRefCounter.get( material );
  184. count --;
  185. if ( count === 0 ) {
  186. materialsRefCounter.delete( material );
  187. delete this.materials[ material.uuid ];
  188. } else {
  189. materialsRefCounter.set( material, count );
  190. }
  191. },
  192. getMaterialById: function ( id ) {
  193. var material;
  194. var materials = Object.values( this.materials );
  195. for ( var i = 0; i < materials.length; i ++ ) {
  196. if ( materials[ i ].id === id ) {
  197. material = materials[ i ];
  198. break;
  199. }
  200. }
  201. return material;
  202. },
  203. setMaterialName: function ( material, name ) {
  204. material.name = name;
  205. this.signals.sceneGraphChanged.dispatch();
  206. },
  207. addTexture: function ( texture ) {
  208. this.textures[ texture.uuid ] = texture;
  209. },
  210. addAnimation: function ( object, animations ) {
  211. if ( animations.length > 0 ) {
  212. this.animations[ object.uuid ] = animations;
  213. }
  214. },
  215. //
  216. addCamera: function ( camera ) {
  217. if ( camera.isCamera ) {
  218. this.cameras[ camera.uuid ] = camera;
  219. this.signals.cameraAdded.dispatch( camera );
  220. }
  221. },
  222. removeCamera: function ( camera ) {
  223. if ( this.cameras[ camera.uuid ] !== undefined ) {
  224. delete this.cameras[ camera.uuid ];
  225. this.signals.cameraRemoved.dispatch( camera );
  226. }
  227. },
  228. //
  229. addHelper: function () {
  230. var geometry = new THREE.SphereBufferGeometry( 2, 4, 2 );
  231. var material = new THREE.MeshBasicMaterial( { color: 0xff0000, visible: false } );
  232. return function ( object ) {
  233. var helper;
  234. if ( object.isCamera ) {
  235. helper = new THREE.CameraHelper( object );
  236. } else if ( object.isPointLight ) {
  237. helper = new THREE.PointLightHelper( object, 1 );
  238. } else if ( object.isDirectionalLight ) {
  239. helper = new THREE.DirectionalLightHelper( object, 1 );
  240. } else if ( object.isSpotLight ) {
  241. helper = new THREE.SpotLightHelper( object, 1 );
  242. } else if ( object.isHemisphereLight ) {
  243. helper = new THREE.HemisphereLightHelper( object, 1 );
  244. } else if ( object.isSkinnedMesh ) {
  245. helper = new THREE.SkeletonHelper( object.skeleton.bones[ 0 ] );
  246. } else {
  247. // no helper for this object type
  248. return;
  249. }
  250. var picker = new THREE.Mesh( geometry, material );
  251. picker.name = 'picker';
  252. picker.userData.object = object;
  253. helper.add( picker );
  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( this.viewportCamera );
  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, true ) );
  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, true ) );
  337. },
  338. clear: function () {
  339. this.history.clear();
  340. this.storage.clear();
  341. this.camera.copy( this.DEFAULT_CAMERA );
  342. this.scene.name = "Scene";
  343. this.scene.userData = {};
  344. this.scene.background = new THREE.Color( 0xaaaaaa );
  345. this.scene.fog = null;
  346. var objects = this.scene.children;
  347. while ( objects.length > 0 ) {
  348. this.removeObject( objects[ 0 ] );
  349. }
  350. this.geometries = {};
  351. this.materials = {};
  352. this.textures = {};
  353. this.scripts = {};
  354. this.materialsRefCounter.clear();
  355. this.animations = {};
  356. this.mixer.stopAllAction();
  357. this.deselect();
  358. this.signals.editorCleared.dispatch();
  359. },
  360. //
  361. fromJSON: function ( json ) {
  362. var loader = new THREE.ObjectLoader();
  363. // backwards
  364. if ( json.scene === undefined ) {
  365. this.setScene( loader.parse( json ) );
  366. return;
  367. }
  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. this.setScene( loader.parse( json.scene ) );
  375. },
  376. toJSON: function () {
  377. // scripts clean up
  378. var scene = this.scene;
  379. var scripts = this.scripts;
  380. for ( var key in scripts ) {
  381. var script = scripts[ key ];
  382. if ( script.length === 0 || scene.getObjectByProperty( 'uuid', key ) === undefined ) {
  383. delete scripts[ key ];
  384. }
  385. }
  386. //
  387. return {
  388. metadata: {},
  389. project: {
  390. shadows: this.config.getKey( 'project/renderer/shadows' ),
  391. vr: this.config.getKey( 'project/vr' )
  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 };