Editor.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  1. import * as THREE from 'three';
  2. import { Config } from './Config.js';
  3. import { Loader } from './Loader.js';
  4. import { History as _History } from './History.js';
  5. import { Strings } from './Strings.js';
  6. import { Storage as _Storage } from './Storage.js';
  7. import { Selector } from './Selector.js';
  8. var _DEFAULT_CAMERA = new THREE.PerspectiveCamera( 50, 1, 0.001, 1e10 );
  9. _DEFAULT_CAMERA.name = 'Camera';
  10. _DEFAULT_CAMERA.position.set( 0, 5, 10 );
  11. _DEFAULT_CAMERA.lookAt( new THREE.Vector3() );
  12. function Editor() {
  13. const Signal = signals.Signal; // eslint-disable-line no-undef
  14. this.signals = {
  15. // script
  16. editScript: new Signal(),
  17. // player
  18. startPlayer: new Signal(),
  19. stopPlayer: new Signal(),
  20. // xr
  21. enterXR: new Signal(),
  22. offerXR: new Signal(),
  23. leaveXR: new Signal(),
  24. // notifications
  25. editorCleared: new Signal(),
  26. savingStarted: new Signal(),
  27. savingFinished: new Signal(),
  28. transformModeChanged: new Signal(),
  29. snapChanged: new Signal(),
  30. spaceChanged: new Signal(),
  31. rendererCreated: new Signal(),
  32. rendererUpdated: new Signal(),
  33. rendererDetectKTX2Support: new Signal(),
  34. sceneBackgroundChanged: new Signal(),
  35. sceneEnvironmentChanged: new Signal(),
  36. sceneFogChanged: new Signal(),
  37. sceneFogSettingsChanged: new Signal(),
  38. sceneGraphChanged: new Signal(),
  39. sceneRendered: new Signal(),
  40. cameraChanged: new Signal(),
  41. cameraResetted: new Signal(),
  42. geometryChanged: new Signal(),
  43. objectSelected: new Signal(),
  44. objectFocused: new Signal(),
  45. objectAdded: new Signal(),
  46. objectChanged: new Signal(),
  47. objectRemoved: new Signal(),
  48. cameraAdded: new Signal(),
  49. cameraRemoved: new Signal(),
  50. helperAdded: new Signal(),
  51. helperRemoved: new Signal(),
  52. materialAdded: new Signal(),
  53. materialChanged: new Signal(),
  54. materialRemoved: new Signal(),
  55. scriptAdded: new Signal(),
  56. scriptChanged: new Signal(),
  57. scriptRemoved: new Signal(),
  58. windowResize: new Signal(),
  59. showHelpersChanged: new Signal(),
  60. refreshSidebarObject3D: new Signal(),
  61. historyChanged: new Signal(),
  62. viewportCameraChanged: new Signal(),
  63. viewportShadingChanged: new Signal(),
  64. intersectionsDetected: new Signal(),
  65. pathTracerUpdated: new Signal(),
  66. morphTargetsUpdated: new Signal()
  67. };
  68. this.config = new Config();
  69. this.history = new _History( this );
  70. this.selector = new Selector( this );
  71. this.storage = new _Storage();
  72. this.strings = new Strings( this.config );
  73. this.loader = new Loader( this );
  74. this.camera = _DEFAULT_CAMERA.clone();
  75. this.scene = new THREE.Scene();
  76. this.scene.name = 'Scene';
  77. this.sceneHelpers = new THREE.Scene();
  78. this.sceneHelpers.add( new THREE.HemisphereLight( 0xffffff, 0x888888, 2 ) );
  79. this.backgroundType = 'Default';
  80. this.environmentType = 'Default';
  81. this.object = {};
  82. this.geometries = {};
  83. this.materials = {};
  84. this.textures = {};
  85. this.scripts = {};
  86. this.materialsRefCounter = new Map(); // tracks how often is a material used by a 3D object
  87. this.mixer = new THREE.AnimationMixer( this.scene );
  88. this.selected = null;
  89. this.helpers = {};
  90. this.cameras = {};
  91. this.viewportCamera = this.camera;
  92. this.viewportShading = 'default';
  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. this.scene.background = scene.background;
  100. this.scene.environment = scene.environment;
  101. this.scene.fog = scene.fog;
  102. this.scene.backgroundBlurriness = scene.backgroundBlurriness;
  103. this.scene.backgroundIntensity = scene.backgroundIntensity;
  104. this.scene.userData = JSON.parse( JSON.stringify( scene.userData ) );
  105. // avoid render per object
  106. this.signals.sceneGraphChanged.active = false;
  107. while ( scene.children.length > 0 ) {
  108. this.addObject( scene.children[ 0 ] );
  109. }
  110. this.signals.sceneGraphChanged.active = true;
  111. this.signals.sceneGraphChanged.dispatch();
  112. this.signals.sceneEnvironmentChanged.dispatch( this.environmentType, scene.environment );
  113. },
  114. //
  115. addObject: function ( object, parent, index ) {
  116. var scope = this;
  117. object.traverse( function ( child ) {
  118. if ( child.geometry !== undefined ) scope.addGeometry( child.geometry );
  119. if ( child.material !== undefined ) scope.addMaterial( child.material );
  120. scope.addCamera( child );
  121. scope.addHelper( child );
  122. } );
  123. if ( parent === undefined ) {
  124. this.scene.add( object );
  125. } else {
  126. parent.children.splice( index, 0, object );
  127. object.parent = parent;
  128. }
  129. this.signals.objectAdded.dispatch( object );
  130. this.signals.sceneGraphChanged.dispatch();
  131. },
  132. nameObject: function ( object, name ) {
  133. object.name = name;
  134. this.signals.sceneGraphChanged.dispatch();
  135. },
  136. removeObject: function ( object ) {
  137. if ( object.parent === null ) return; // avoid deleting the camera or scene
  138. var scope = this;
  139. object.traverse( function ( child ) {
  140. scope.removeCamera( child );
  141. scope.removeHelper( child );
  142. if ( child.material !== undefined ) scope.removeMaterial( child.material );
  143. } );
  144. object.parent.remove( object );
  145. this.signals.objectRemoved.dispatch( object );
  146. this.signals.sceneGraphChanged.dispatch();
  147. },
  148. addGeometry: function ( geometry ) {
  149. this.geometries[ geometry.uuid ] = geometry;
  150. },
  151. setGeometryName: function ( geometry, name ) {
  152. geometry.name = name;
  153. this.signals.sceneGraphChanged.dispatch();
  154. },
  155. addMaterial: function ( material ) {
  156. if ( Array.isArray( material ) ) {
  157. for ( var i = 0, l = material.length; i < l; i ++ ) {
  158. this.addMaterialToRefCounter( material[ i ] );
  159. }
  160. } else {
  161. this.addMaterialToRefCounter( material );
  162. }
  163. this.signals.materialAdded.dispatch();
  164. },
  165. addMaterialToRefCounter: function ( material ) {
  166. var materialsRefCounter = this.materialsRefCounter;
  167. var count = materialsRefCounter.get( material );
  168. if ( count === undefined ) {
  169. materialsRefCounter.set( material, 1 );
  170. this.materials[ material.uuid ] = material;
  171. } else {
  172. count ++;
  173. materialsRefCounter.set( material, count );
  174. }
  175. },
  176. removeMaterial: function ( material ) {
  177. if ( Array.isArray( material ) ) {
  178. for ( var i = 0, l = material.length; i < l; i ++ ) {
  179. this.removeMaterialFromRefCounter( material[ i ] );
  180. }
  181. } else {
  182. this.removeMaterialFromRefCounter( material );
  183. }
  184. this.signals.materialRemoved.dispatch();
  185. },
  186. removeMaterialFromRefCounter: function ( material ) {
  187. var materialsRefCounter = this.materialsRefCounter;
  188. var count = materialsRefCounter.get( material );
  189. count --;
  190. if ( count === 0 ) {
  191. materialsRefCounter.delete( material );
  192. delete this.materials[ material.uuid ];
  193. } else {
  194. materialsRefCounter.set( material, count );
  195. }
  196. },
  197. getMaterialById: function ( id ) {
  198. var material;
  199. var materials = Object.values( this.materials );
  200. for ( var i = 0; i < materials.length; i ++ ) {
  201. if ( materials[ i ].id === id ) {
  202. material = materials[ i ];
  203. break;
  204. }
  205. }
  206. return material;
  207. },
  208. setMaterialName: function ( material, name ) {
  209. material.name = name;
  210. this.signals.sceneGraphChanged.dispatch();
  211. },
  212. addTexture: function ( texture ) {
  213. this.textures[ texture.uuid ] = texture;
  214. },
  215. //
  216. addCamera: function ( camera ) {
  217. if ( camera.isCamera ) {
  218. this.cameras[ camera.uuid ] = camera;
  219. this.signals.cameraAdded.dispatch( camera );
  220. }
  221. },
  222. removeCamera: function ( camera ) {
  223. if ( this.cameras[ camera.uuid ] !== undefined ) {
  224. delete this.cameras[ camera.uuid ];
  225. this.signals.cameraRemoved.dispatch( camera );
  226. }
  227. },
  228. //
  229. addHelper: function () {
  230. var geometry = new THREE.SphereGeometry( 2, 4, 2 );
  231. var material = new THREE.MeshBasicMaterial( { color: 0xff0000, visible: false } );
  232. return function ( object, helper ) {
  233. if ( helper === undefined ) {
  234. if ( object.isCamera ) {
  235. helper = new THREE.CameraHelper( object );
  236. } else if ( object.isPointLight ) {
  237. helper = new THREE.PointLightHelper( object, 1 );
  238. helper.matrix = new THREE.Matrix4();
  239. helper.matrixAutoUpdate = true;
  240. const light = object;
  241. const editor = this;
  242. helper.updateMatrixWorld = function () {
  243. light.getWorldPosition( this.position );
  244. const distance = editor.viewportCamera.position.distanceTo( this.position );
  245. this.scale.setScalar( distance / 30 );
  246. this.updateMatrix();
  247. this.matrixWorld.copy( this.matrix );
  248. const children = this.children;
  249. for ( let i = 0, l = children.length; i < l; i ++ ) {
  250. children[ i ].updateMatrixWorld();
  251. }
  252. };
  253. } else if ( object.isDirectionalLight ) {
  254. helper = new THREE.DirectionalLightHelper( object, 1 );
  255. } else if ( object.isSpotLight ) {
  256. helper = new THREE.SpotLightHelper( object );
  257. } else if ( object.isHemisphereLight ) {
  258. helper = new THREE.HemisphereLightHelper( object, 1 );
  259. } else if ( object.isSkinnedMesh ) {
  260. helper = new THREE.SkeletonHelper( object.skeleton.bones[ 0 ] );
  261. } else if ( object.isBone === true && object.parent && object.parent.isBone !== true ) {
  262. helper = new THREE.SkeletonHelper( object );
  263. } else {
  264. // no helper for this object type
  265. return;
  266. }
  267. const picker = new THREE.Mesh( geometry, material );
  268. picker.name = 'picker';
  269. picker.userData.object = object;
  270. helper.add( picker );
  271. }
  272. this.sceneHelpers.add( helper );
  273. this.helpers[ object.id ] = helper;
  274. this.signals.helperAdded.dispatch( helper );
  275. };
  276. }(),
  277. removeHelper: function ( object ) {
  278. if ( this.helpers[ object.id ] !== undefined ) {
  279. var helper = this.helpers[ object.id ];
  280. helper.parent.remove( helper );
  281. helper.dispose();
  282. delete this.helpers[ object.id ];
  283. this.signals.helperRemoved.dispatch( helper );
  284. }
  285. },
  286. //
  287. addScript: function ( object, script ) {
  288. if ( this.scripts[ object.uuid ] === undefined ) {
  289. this.scripts[ object.uuid ] = [];
  290. }
  291. this.scripts[ object.uuid ].push( script );
  292. this.signals.scriptAdded.dispatch( script );
  293. },
  294. removeScript: function ( object, script ) {
  295. if ( this.scripts[ object.uuid ] === undefined ) return;
  296. var index = this.scripts[ object.uuid ].indexOf( script );
  297. if ( index !== - 1 ) {
  298. this.scripts[ object.uuid ].splice( index, 1 );
  299. }
  300. this.signals.scriptRemoved.dispatch( script );
  301. },
  302. getObjectMaterial: function ( object, slot ) {
  303. var material = object.material;
  304. if ( Array.isArray( material ) && slot !== undefined ) {
  305. material = material[ slot ];
  306. }
  307. return material;
  308. },
  309. setObjectMaterial: function ( object, slot, newMaterial ) {
  310. if ( Array.isArray( object.material ) && slot !== undefined ) {
  311. object.material[ slot ] = newMaterial;
  312. } else {
  313. object.material = newMaterial;
  314. }
  315. },
  316. setViewportCamera: function ( uuid ) {
  317. this.viewportCamera = this.cameras[ uuid ];
  318. this.signals.viewportCameraChanged.dispatch();
  319. },
  320. setViewportShading: function ( value ) {
  321. this.viewportShading = value;
  322. this.signals.viewportShadingChanged.dispatch();
  323. },
  324. //
  325. select: function ( object ) {
  326. this.selector.select( object );
  327. },
  328. selectById: function ( id ) {
  329. if ( id === this.camera.id ) {
  330. this.select( this.camera );
  331. return;
  332. }
  333. this.select( this.scene.getObjectById( id ) );
  334. },
  335. selectByUuid: function ( uuid ) {
  336. var scope = this;
  337. this.scene.traverse( function ( child ) {
  338. if ( child.uuid === uuid ) {
  339. scope.select( child );
  340. }
  341. } );
  342. },
  343. deselect: function () {
  344. this.selector.deselect();
  345. },
  346. focus: function ( object ) {
  347. if ( object !== undefined ) {
  348. this.signals.objectFocused.dispatch( object );
  349. }
  350. },
  351. focusById: function ( id ) {
  352. this.focus( this.scene.getObjectById( id ) );
  353. },
  354. clear: function () {
  355. this.history.clear();
  356. this.storage.clear();
  357. this.camera.copy( _DEFAULT_CAMERA );
  358. this.signals.cameraResetted.dispatch();
  359. this.scene.name = 'Scene';
  360. this.scene.userData = {};
  361. this.scene.background = null;
  362. this.scene.environment = null;
  363. this.scene.fog = null;
  364. var objects = this.scene.children;
  365. this.signals.sceneGraphChanged.active = false;
  366. while ( objects.length > 0 ) {
  367. this.removeObject( objects[ 0 ] );
  368. }
  369. this.signals.sceneGraphChanged.active = true;
  370. this.geometries = {};
  371. this.materials = {};
  372. this.textures = {};
  373. this.scripts = {};
  374. this.materialsRefCounter.clear();
  375. this.animations = {};
  376. this.mixer.stopAllAction();
  377. this.deselect();
  378. this.backgroundType = 'Default';
  379. this.environmentType = 'Default';
  380. this.signals.editorCleared.dispatch();
  381. },
  382. //
  383. fromJSON: async function ( json ) {
  384. var loader = new THREE.ObjectLoader();
  385. var camera = await loader.parseAsync( json.camera );
  386. const existingUuid = this.camera.uuid;
  387. const incomingUuid = camera.uuid;
  388. // copy all properties, including uuid
  389. this.camera.copy( camera );
  390. this.camera.uuid = incomingUuid;
  391. delete this.cameras[ existingUuid ]; // remove old entry [existingUuid, this.camera]
  392. this.cameras[ incomingUuid ] = this.camera; // add new entry [incomingUuid, this.camera]
  393. if ( json.controls !== undefined ) {
  394. this.controls.fromJSON( json.controls );
  395. }
  396. this.signals.cameraResetted.dispatch();
  397. this.history.fromJSON( json.history );
  398. this.scripts = json.scripts;
  399. const scene = await loader.parseAsync( json.scene );
  400. this.backgroundType = json.backgroundType || 'Default';
  401. this.environmentType = json.environmentType || 'Default';
  402. this.setScene( scene );
  403. },
  404. toJSON: function () {
  405. // scripts clean up
  406. var scene = this.scene;
  407. var scripts = this.scripts;
  408. for ( var key in scripts ) {
  409. var script = scripts[ key ];
  410. if ( script.length === 0 || scene.getObjectByProperty( 'uuid', key ) === undefined ) {
  411. delete scripts[ key ];
  412. }
  413. }
  414. return {
  415. metadata: {},
  416. project: {
  417. renderer: this.config.getKey( 'project/renderer/type' ),
  418. shadows: this.config.getKey( 'project/renderer/shadows' ),
  419. shadowType: this.config.getKey( 'project/renderer/shadowType' ),
  420. toneMapping: this.config.getKey( 'project/renderer/toneMapping' ),
  421. toneMappingExposure: this.config.getKey( 'project/renderer/toneMappingExposure' )
  422. },
  423. camera: this.viewportCamera.toJSON(),
  424. controls: this.controls.toJSON(),
  425. scene: this.scene.toJSON(),
  426. scripts: this.scripts,
  427. history: this.history.toJSON(),
  428. backgroundType: this.backgroundType,
  429. environmentType: this.environmentType
  430. };
  431. },
  432. objectByUuid: function ( uuid ) {
  433. return this.scene.getObjectByProperty( 'uuid', uuid, true );
  434. },
  435. execute: function ( cmd, optionalName ) {
  436. this.history.execute( cmd, optionalName );
  437. },
  438. undo: function () {
  439. this.history.undo();
  440. },
  441. redo: function () {
  442. this.history.redo();
  443. },
  444. utils: {
  445. save: save,
  446. saveArrayBuffer: saveArrayBuffer,
  447. saveString: saveString,
  448. formatNumber: formatNumber
  449. }
  450. };
  451. const link = document.createElement( 'a' );
  452. function save( blob, filename ) {
  453. if ( link.href ) {
  454. URL.revokeObjectURL( link.href );
  455. }
  456. link.href = URL.createObjectURL( blob );
  457. link.download = filename || 'data.json';
  458. link.dispatchEvent( new MouseEvent( 'click' ) );
  459. }
  460. function saveArrayBuffer( buffer, filename ) {
  461. save( new Blob( [ buffer ], { type: 'application/octet-stream' } ), filename );
  462. }
  463. function saveString( text, filename ) {
  464. save( new Blob( [ text ], { type: 'text/plain' } ), filename );
  465. }
  466. function formatNumber( number ) {
  467. return new Intl.NumberFormat( 'en-us', { useGrouping: true } ).format( number );
  468. }
  469. export { Editor };
粤ICP备19079148号