Editor.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. import {
  5. AnimationMixer,
  6. CameraHelper,
  7. Color,
  8. DirectionalLightHelper,
  9. HemisphereLightHelper,
  10. Mesh,
  11. MeshBasicMaterial,
  12. ObjectLoader,
  13. PerspectiveCamera,
  14. PointLightHelper,
  15. Scene,
  16. SkeletonHelper,
  17. SphereBufferGeometry,
  18. SpotLightHelper,
  19. Vector3
  20. } from '../../build/three.module.js';
  21. import { Config } from './Config.js';
  22. import { Loader } from './Loader.js';
  23. import { History as _History } from './History.js';
  24. import { Strings } from './Strings.js';
  25. import { Storage as _Storage } from './Storage.js';
  26. var Editor = function () {
  27. this.DEFAULT_CAMERA = new PerspectiveCamera( 50, 1, 0.01, 1000 );
  28. this.DEFAULT_CAMERA.name = 'Camera';
  29. this.DEFAULT_CAMERA.position.set( 0, 5, 10 );
  30. this.DEFAULT_CAMERA.lookAt( new Vector3() );
  31. var Signal = signals.Signal;
  32. this.signals = {
  33. // script
  34. editScript: new Signal(),
  35. // player
  36. startPlayer: new Signal(),
  37. stopPlayer: new Signal(),
  38. // notifications
  39. editorCleared: new Signal(),
  40. savingStarted: new Signal(),
  41. savingFinished: new Signal(),
  42. transformModeChanged: new Signal(),
  43. snapChanged: new Signal(),
  44. spaceChanged: new Signal(),
  45. rendererChanged: new Signal(),
  46. sceneBackgroundChanged: new Signal(),
  47. sceneFogChanged: new Signal(),
  48. sceneGraphChanged: new Signal(),
  49. cameraChanged: new Signal(),
  50. geometryChanged: new Signal(),
  51. objectSelected: new Signal(),
  52. objectFocused: new Signal(),
  53. objectAdded: new Signal(),
  54. objectChanged: new Signal(),
  55. objectRemoved: new Signal(),
  56. cameraAdded: new Signal(),
  57. cameraRemoved: new Signal(),
  58. helperAdded: new Signal(),
  59. helperRemoved: new Signal(),
  60. materialAdded: new Signal(),
  61. materialChanged: new Signal(),
  62. materialRemoved: new Signal(),
  63. scriptAdded: new Signal(),
  64. scriptChanged: new Signal(),
  65. scriptRemoved: new Signal(),
  66. windowResize: new Signal(),
  67. showGridChanged: new Signal(),
  68. refreshSidebarObject3D: new Signal(),
  69. historyChanged: new Signal(),
  70. viewportCameraChanged: new Signal()
  71. };
  72. this.config = new Config();
  73. this.history = new _History( this );
  74. this.storage = new _Storage();
  75. this.strings = new Strings( this.config );
  76. this.loader = new Loader( this );
  77. this.camera = this.DEFAULT_CAMERA.clone();
  78. this.scene = new Scene();
  79. this.scene.name = 'Scene';
  80. this.scene.background = new Color( 0xaaaaaa );
  81. this.sceneHelpers = new Scene();
  82. this.object = {};
  83. this.geometries = {};
  84. this.materials = {};
  85. this.textures = {};
  86. this.scripts = {};
  87. this.animations = {};
  88. this.mixer = new AnimationMixer( this.scene );
  89. this.selected = null;
  90. this.helpers = {};
  91. this.cameras = {};
  92. this.viewportCamera = this.camera;
  93. this.addCamera( this.camera );
  94. };
  95. Editor.prototype = {
  96. setScene: function ( scene ) {
  97. this.scene.uuid = scene.uuid;
  98. this.scene.name = scene.name;
  99. if ( scene.background !== null ) this.scene.background = scene.background.clone();
  100. if ( scene.fog !== null ) this.scene.fog = scene.fog.clone();
  101. this.scene.userData = JSON.parse( JSON.stringify( scene.userData ) );
  102. // avoid render per object
  103. this.signals.sceneGraphChanged.active = false;
  104. while ( scene.children.length > 0 ) {
  105. this.addObject( scene.children[ 0 ] );
  106. }
  107. this.signals.sceneGraphChanged.active = true;
  108. this.signals.sceneGraphChanged.dispatch();
  109. },
  110. //
  111. addObject: function ( object ) {
  112. var scope = this;
  113. object.traverse( function ( child ) {
  114. if ( child.geometry !== undefined ) scope.addGeometry( child.geometry );
  115. if ( child.material !== undefined ) scope.addMaterial( child.material );
  116. scope.addCamera( child );
  117. scope.addHelper( child );
  118. } );
  119. this.scene.add( object );
  120. this.signals.objectAdded.dispatch( object );
  121. this.signals.sceneGraphChanged.dispatch();
  122. },
  123. moveObject: function ( object, parent, before ) {
  124. if ( parent === undefined ) {
  125. parent = this.scene;
  126. }
  127. parent.add( object );
  128. // sort children array
  129. if ( before !== undefined ) {
  130. var index = parent.children.indexOf( before );
  131. parent.children.splice( index, 0, object );
  132. parent.children.pop();
  133. }
  134. this.signals.sceneGraphChanged.dispatch();
  135. },
  136. nameObject: function ( object, name ) {
  137. object.name = name;
  138. this.signals.sceneGraphChanged.dispatch();
  139. },
  140. removeObject: function ( object ) {
  141. if ( object.parent === null ) return; // avoid deleting the camera or scene
  142. var scope = this;
  143. object.traverse( function ( child ) {
  144. scope.removeCamera( child );
  145. scope.removeHelper( child );
  146. } );
  147. object.parent.remove( object );
  148. this.signals.objectRemoved.dispatch( object );
  149. this.signals.sceneGraphChanged.dispatch();
  150. },
  151. addGeometry: function ( geometry ) {
  152. this.geometries[ geometry.uuid ] = geometry;
  153. },
  154. setGeometryName: function ( geometry, name ) {
  155. geometry.name = name;
  156. this.signals.sceneGraphChanged.dispatch();
  157. },
  158. addMaterial: function ( material ) {
  159. this.materials[ material.uuid ] = material;
  160. this.signals.materialAdded.dispatch();
  161. },
  162. removeMaterial: function ( material ) {
  163. delete this.materials[ material.uuid ];
  164. this.signals.materialRemoved.dispatch();
  165. },
  166. getMaterialById: function ( id ) {
  167. var material;
  168. var materials = Object.values( this.materials );
  169. for ( var i = 0; i < materials.length; i ++ ) {
  170. if ( materials[ i ].id === id ) {
  171. material = materials[ i ];
  172. break;
  173. }
  174. }
  175. return material;
  176. },
  177. setMaterialName: function ( material, name ) {
  178. material.name = name;
  179. this.signals.sceneGraphChanged.dispatch();
  180. },
  181. addTexture: function ( texture ) {
  182. this.textures[ texture.uuid ] = texture;
  183. },
  184. addAnimation: function ( object, animations ) {
  185. if ( animations.length > 0 ) {
  186. this.animations[ object.uuid ] = animations;
  187. }
  188. },
  189. //
  190. addCamera: function ( camera ) {
  191. if ( camera.isCamera ) {
  192. this.cameras[ camera.uuid ] = camera;
  193. this.signals.cameraAdded.dispatch( camera );
  194. }
  195. },
  196. removeCamera: function ( camera ) {
  197. if ( this.cameras[ camera.uuid ] !== undefined ) {
  198. delete this.cameras[ camera.uuid ];
  199. this.signals.cameraRemoved.dispatch( camera );
  200. }
  201. },
  202. //
  203. addHelper: function () {
  204. var geometry = new SphereBufferGeometry( 2, 4, 2 );
  205. var material = new MeshBasicMaterial( { color: 0xff0000, visible: false } );
  206. return function ( object ) {
  207. var helper;
  208. if ( object.isCamera ) {
  209. helper = new CameraHelper( object, 1 );
  210. } else if ( object.isPointLight ) {
  211. helper = new PointLightHelper( object, 1 );
  212. } else if ( object.isDirectionalLight ) {
  213. helper = new DirectionalLightHelper( object, 1 );
  214. } else if ( object.isSpotLight ) {
  215. helper = new SpotLightHelper( object, 1 );
  216. } else if ( object.isHemisphereLight ) {
  217. helper = new HemisphereLightHelper( object, 1 );
  218. } else if ( object.isSkinnedMesh ) {
  219. helper = new SkeletonHelper( object.skeleton.bones[ 0 ] );
  220. } else {
  221. // no helper for this object type
  222. return;
  223. }
  224. var picker = new Mesh( geometry, material );
  225. picker.name = 'picker';
  226. picker.userData.object = object;
  227. helper.add( picker );
  228. this.sceneHelpers.add( helper );
  229. this.helpers[ object.id ] = helper;
  230. this.signals.helperAdded.dispatch( helper );
  231. };
  232. }(),
  233. removeHelper: function ( object ) {
  234. if ( this.helpers[ object.id ] !== undefined ) {
  235. var helper = this.helpers[ object.id ];
  236. helper.parent.remove( helper );
  237. delete this.helpers[ object.id ];
  238. this.signals.helperRemoved.dispatch( helper );
  239. }
  240. },
  241. //
  242. addScript: function ( object, script ) {
  243. if ( this.scripts[ object.uuid ] === undefined ) {
  244. this.scripts[ object.uuid ] = [];
  245. }
  246. this.scripts[ object.uuid ].push( script );
  247. this.signals.scriptAdded.dispatch( script );
  248. },
  249. removeScript: function ( object, script ) {
  250. if ( this.scripts[ object.uuid ] === undefined ) return;
  251. var index = this.scripts[ object.uuid ].indexOf( script );
  252. if ( index !== - 1 ) {
  253. this.scripts[ object.uuid ].splice( index, 1 );
  254. }
  255. this.signals.scriptRemoved.dispatch( script );
  256. },
  257. getObjectMaterial: function ( object, slot ) {
  258. var material = object.material;
  259. if ( Array.isArray( material ) ) {
  260. material = material[ slot ];
  261. }
  262. return material;
  263. },
  264. setObjectMaterial: function ( object, slot, newMaterial ) {
  265. if ( Array.isArray( object.material ) ) {
  266. object.material[ slot ] = newMaterial;
  267. } else {
  268. object.material = newMaterial;
  269. }
  270. },
  271. setViewportCamera: function ( uuid ) {
  272. this.viewportCamera = this.cameras[ uuid ];
  273. this.signals.viewportCameraChanged.dispatch( this.viewportCamera );
  274. },
  275. //
  276. select: function ( object ) {
  277. if ( this.selected === object ) return;
  278. var uuid = null;
  279. if ( object !== null ) {
  280. uuid = object.uuid;
  281. }
  282. this.selected = object;
  283. this.config.setKey( 'selected', uuid );
  284. this.signals.objectSelected.dispatch( object );
  285. },
  286. selectById: function ( id ) {
  287. if ( id === this.camera.id ) {
  288. this.select( this.camera );
  289. return;
  290. }
  291. this.select( this.scene.getObjectById( id, true ) );
  292. },
  293. selectByUuid: function ( uuid ) {
  294. var scope = this;
  295. this.scene.traverse( function ( child ) {
  296. if ( child.uuid === uuid ) {
  297. scope.select( child );
  298. }
  299. } );
  300. },
  301. deselect: function () {
  302. this.select( null );
  303. },
  304. focus: function ( object ) {
  305. if ( object !== undefined ) {
  306. this.signals.objectFocused.dispatch( object );
  307. }
  308. },
  309. focusById: function ( id ) {
  310. this.focus( this.scene.getObjectById( id, true ) );
  311. },
  312. clear: function () {
  313. this.history.clear();
  314. this.storage.clear();
  315. this.camera.copy( this.DEFAULT_CAMERA );
  316. this.scene.name = "Scene";
  317. this.scene.userData = {};
  318. this.scene.background.setHex( 0xaaaaaa );
  319. this.scene.fog = null;
  320. var objects = this.scene.children;
  321. while ( objects.length > 0 ) {
  322. this.removeObject( objects[ 0 ] );
  323. }
  324. this.geometries = {};
  325. this.materials = {};
  326. this.textures = {};
  327. this.scripts = {};
  328. this.animations = {};
  329. this.mixer.stopAllAction();
  330. this.deselect();
  331. this.signals.editorCleared.dispatch();
  332. },
  333. //
  334. fromJSON: function ( json ) {
  335. var loader = new ObjectLoader();
  336. // backwards
  337. if ( json.scene === undefined ) {
  338. this.setScene( loader.parse( json ) );
  339. return;
  340. }
  341. var camera = loader.parse( json.camera );
  342. this.camera.copy( camera );
  343. this.camera.aspect = this.DEFAULT_CAMERA.aspect;
  344. this.camera.updateProjectionMatrix();
  345. this.history.fromJSON( json.history );
  346. this.scripts = json.scripts;
  347. this.setScene( loader.parse( json.scene ) );
  348. },
  349. toJSON: function () {
  350. // scripts clean up
  351. var scene = this.scene;
  352. var scripts = this.scripts;
  353. for ( var key in scripts ) {
  354. var script = scripts[ key ];
  355. if ( script.length === 0 || scene.getObjectByProperty( 'uuid', key ) === undefined ) {
  356. delete scripts[ key ];
  357. }
  358. }
  359. //
  360. return {
  361. metadata: {},
  362. project: {
  363. shadows: this.config.getKey( 'project/renderer/shadows' ),
  364. vr: this.config.getKey( 'project/vr' )
  365. },
  366. camera: this.camera.toJSON(),
  367. scene: this.scene.toJSON(),
  368. scripts: this.scripts,
  369. history: this.history.toJSON()
  370. };
  371. },
  372. objectByUuid: function ( uuid ) {
  373. return this.scene.getObjectByProperty( 'uuid', uuid, true );
  374. },
  375. execute: function ( cmd, optionalName ) {
  376. this.history.execute( cmd, optionalName );
  377. },
  378. undo: function () {
  379. this.history.undo();
  380. },
  381. redo: function () {
  382. this.history.redo();
  383. }
  384. };
  385. export { Editor };