Editor.js 12 KB

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