Editor.js 12 KB

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