2
0

Editor.js 11 KB

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