Editor.js 11 KB

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