Editor.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817
  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.viewportColor = new THREE.Color();
  94. this.addCamera( this.camera );
  95. }
  96. Editor.prototype = {
  97. setScene: function ( scene ) {
  98. this.scene.uuid = scene.uuid;
  99. this.scene.name = scene.name;
  100. this.scene.background = scene.background;
  101. this.scene.environment = scene.environment;
  102. this.scene.fog = scene.fog;
  103. this.scene.backgroundBlurriness = scene.backgroundBlurriness;
  104. this.scene.backgroundIntensity = scene.backgroundIntensity;
  105. this.scene.userData = JSON.parse( JSON.stringify( scene.userData ) );
  106. // avoid render per object
  107. this.signals.sceneGraphChanged.active = false;
  108. while ( scene.children.length > 0 ) {
  109. this.addObject( scene.children[ 0 ] );
  110. }
  111. this.signals.sceneGraphChanged.active = true;
  112. this.signals.sceneGraphChanged.dispatch();
  113. this.signals.sceneEnvironmentChanged.dispatch( this.environmentType, scene.environment );
  114. },
  115. //
  116. addObject: function ( object, parent, index ) {
  117. var scope = this;
  118. object.traverse( function ( child ) {
  119. if ( child.geometry !== undefined ) scope.addGeometry( child.geometry );
  120. if ( child.material !== undefined ) scope.addMaterial( child.material );
  121. scope.addCamera( child );
  122. scope.addHelper( child );
  123. } );
  124. if ( parent === undefined ) {
  125. this.scene.add( object );
  126. } else {
  127. parent.children.splice( index, 0, object );
  128. object.parent = parent;
  129. }
  130. this.signals.objectAdded.dispatch( object );
  131. this.signals.sceneGraphChanged.dispatch();
  132. },
  133. nameObject: function ( object, name ) {
  134. object.name = name;
  135. this.signals.sceneGraphChanged.dispatch();
  136. },
  137. removeObject: function ( object ) {
  138. if ( object.parent === null ) return; // avoid deleting the camera or scene
  139. var scope = this;
  140. object.traverse( function ( child ) {
  141. scope.removeCamera( child );
  142. scope.removeHelper( child );
  143. if ( child.material !== undefined ) scope.removeMaterial( child.material );
  144. } );
  145. object.parent.remove( object );
  146. this.signals.objectRemoved.dispatch( object );
  147. this.signals.sceneGraphChanged.dispatch();
  148. },
  149. addGeometry: function ( geometry ) {
  150. this.geometries[ geometry.uuid ] = geometry;
  151. },
  152. setGeometryName: function ( geometry, name ) {
  153. geometry.name = name;
  154. this.signals.sceneGraphChanged.dispatch();
  155. },
  156. addMaterial: function ( material ) {
  157. if ( Array.isArray( material ) ) {
  158. for ( var i = 0, l = material.length; i < l; i ++ ) {
  159. this.addMaterialToRefCounter( material[ i ] );
  160. }
  161. } else {
  162. this.addMaterialToRefCounter( material );
  163. }
  164. this.signals.materialAdded.dispatch();
  165. },
  166. addMaterialToRefCounter: function ( material ) {
  167. var materialsRefCounter = this.materialsRefCounter;
  168. var count = materialsRefCounter.get( material );
  169. if ( count === undefined ) {
  170. materialsRefCounter.set( material, 1 );
  171. this.materials[ material.uuid ] = material;
  172. } else {
  173. count ++;
  174. materialsRefCounter.set( material, count );
  175. }
  176. },
  177. removeMaterial: function ( material ) {
  178. if ( Array.isArray( material ) ) {
  179. for ( var i = 0, l = material.length; i < l; i ++ ) {
  180. this.removeMaterialFromRefCounter( material[ i ] );
  181. }
  182. } else {
  183. this.removeMaterialFromRefCounter( material );
  184. }
  185. this.signals.materialRemoved.dispatch();
  186. },
  187. removeMaterialFromRefCounter: function ( material ) {
  188. var materialsRefCounter = this.materialsRefCounter;
  189. var count = materialsRefCounter.get( material );
  190. count --;
  191. if ( count === 0 ) {
  192. materialsRefCounter.delete( material );
  193. delete this.materials[ material.uuid ];
  194. } else {
  195. materialsRefCounter.set( material, count );
  196. }
  197. },
  198. getMaterialById: function ( id ) {
  199. var material;
  200. var materials = Object.values( this.materials );
  201. for ( var i = 0; i < materials.length; i ++ ) {
  202. if ( materials[ i ].id === id ) {
  203. material = materials[ i ];
  204. break;
  205. }
  206. }
  207. return material;
  208. },
  209. setMaterialName: function ( material, name ) {
  210. material.name = name;
  211. this.signals.sceneGraphChanged.dispatch();
  212. },
  213. addTexture: function ( texture ) {
  214. this.textures[ texture.uuid ] = texture;
  215. },
  216. //
  217. addCamera: function ( camera ) {
  218. if ( camera.isCamera ) {
  219. this.cameras[ camera.uuid ] = camera;
  220. this.signals.cameraAdded.dispatch( camera );
  221. }
  222. },
  223. removeCamera: function ( camera ) {
  224. if ( this.cameras[ camera.uuid ] !== undefined ) {
  225. delete this.cameras[ camera.uuid ];
  226. this.signals.cameraRemoved.dispatch( camera );
  227. }
  228. },
  229. //
  230. addHelper: function () {
  231. var geometry = new THREE.SphereGeometry( 2, 4, 2 );
  232. var material = new THREE.MeshBasicMaterial( { color: 0xff0000, visible: false } );
  233. return function ( object, helper ) {
  234. if ( helper === undefined ) {
  235. if ( object.isCamera ) {
  236. helper = new THREE.CameraHelper( object );
  237. } else if ( object.isPointLight ) {
  238. helper = new THREE.PointLightHelper( object, 1 );
  239. helper.matrix = new THREE.Matrix4();
  240. helper.matrixAutoUpdate = true;
  241. const light = object;
  242. const editor = this;
  243. helper.updateMatrixWorld = function () {
  244. light.getWorldPosition( this.position );
  245. const distance = editor.viewportCamera.position.distanceTo( this.position );
  246. this.scale.setScalar( distance / 30 );
  247. this.updateMatrix();
  248. this.matrixWorld.copy( this.matrix );
  249. const children = this.children;
  250. for ( let i = 0, l = children.length; i < l; i ++ ) {
  251. children[ i ].updateMatrixWorld();
  252. }
  253. };
  254. } else if ( object.isDirectionalLight ) {
  255. helper = new THREE.DirectionalLightHelper( object, 1 );
  256. } else if ( object.isSpotLight ) {
  257. helper = new THREE.SpotLightHelper( object );
  258. } else if ( object.isHemisphereLight ) {
  259. helper = new THREE.HemisphereLightHelper( object, 1 );
  260. } else if ( object.isSkinnedMesh ) {
  261. helper = new THREE.SkeletonHelper( object.skeleton.bones[ 0 ] );
  262. } else if ( object.isBone === true && object.parent && object.parent.isBone !== true ) {
  263. helper = new THREE.SkeletonHelper( object );
  264. } else {
  265. // no helper for this object type
  266. return;
  267. }
  268. const picker = new THREE.Mesh( geometry, material );
  269. picker.name = 'picker';
  270. picker.userData.object = object;
  271. helper.add( picker );
  272. }
  273. this.sceneHelpers.add( helper );
  274. this.helpers[ object.id ] = helper;
  275. this.signals.helperAdded.dispatch( helper );
  276. };
  277. }(),
  278. removeHelper: function ( object ) {
  279. if ( this.helpers[ object.id ] !== undefined ) {
  280. var helper = this.helpers[ object.id ];
  281. helper.parent.remove( helper );
  282. helper.dispose();
  283. delete this.helpers[ object.id ];
  284. this.signals.helperRemoved.dispatch( helper );
  285. }
  286. },
  287. //
  288. addScript: function ( object, script ) {
  289. if ( this.scripts[ object.uuid ] === undefined ) {
  290. this.scripts[ object.uuid ] = [];
  291. }
  292. this.scripts[ object.uuid ].push( script );
  293. this.signals.scriptAdded.dispatch( script );
  294. },
  295. removeScript: function ( object, script ) {
  296. if ( this.scripts[ object.uuid ] === undefined ) return;
  297. var index = this.scripts[ object.uuid ].indexOf( script );
  298. if ( index !== - 1 ) {
  299. this.scripts[ object.uuid ].splice( index, 1 );
  300. }
  301. this.signals.scriptRemoved.dispatch( script );
  302. },
  303. getObjectMaterial: function ( object, slot ) {
  304. var material = object.material;
  305. if ( Array.isArray( material ) && slot !== undefined ) {
  306. material = material[ slot ];
  307. }
  308. return material;
  309. },
  310. setObjectMaterial: function ( object, slot, newMaterial ) {
  311. if ( Array.isArray( object.material ) && slot !== undefined ) {
  312. object.material[ slot ] = newMaterial;
  313. } else {
  314. object.material = newMaterial;
  315. }
  316. },
  317. setViewportCamera: function ( uuid ) {
  318. this.viewportCamera = this.cameras[ uuid ];
  319. this.signals.viewportCameraChanged.dispatch();
  320. },
  321. setViewportShading: function ( value ) {
  322. this.viewportShading = value;
  323. this.signals.viewportShadingChanged.dispatch();
  324. },
  325. //
  326. select: function ( object ) {
  327. this.selector.select( object );
  328. },
  329. selectById: function ( id ) {
  330. if ( id === this.camera.id ) {
  331. this.select( this.camera );
  332. return;
  333. }
  334. this.select( this.scene.getObjectById( id ) );
  335. },
  336. selectByUuid: function ( uuid ) {
  337. var scope = this;
  338. this.scene.traverse( function ( child ) {
  339. if ( child.uuid === uuid ) {
  340. scope.select( child );
  341. }
  342. } );
  343. },
  344. deselect: function () {
  345. this.selector.deselect();
  346. },
  347. focus: function ( object ) {
  348. if ( object !== undefined ) {
  349. this.signals.objectFocused.dispatch( object );
  350. }
  351. },
  352. focusById: function ( id ) {
  353. this.focus( this.scene.getObjectById( id ) );
  354. },
  355. clear: function () {
  356. this.history.clear();
  357. this.storage.clear();
  358. this.camera.copy( _DEFAULT_CAMERA );
  359. this.signals.cameraResetted.dispatch();
  360. this.scene.name = 'Scene';
  361. this.scene.userData = {};
  362. this.scene.background = null;
  363. this.scene.environment = null;
  364. this.scene.fog = null;
  365. var objects = this.scene.children;
  366. this.signals.sceneGraphChanged.active = false;
  367. while ( objects.length > 0 ) {
  368. this.removeObject( objects[ 0 ] );
  369. }
  370. this.signals.sceneGraphChanged.active = true;
  371. this.geometries = {};
  372. this.materials = {};
  373. this.textures = {};
  374. this.scripts = {};
  375. this.materialsRefCounter.clear();
  376. this.animations = {};
  377. this.mixer.stopAllAction();
  378. this.deselect();
  379. this.backgroundType = 'Default';
  380. this.environmentType = 'Default';
  381. this.signals.editorCleared.dispatch();
  382. },
  383. //
  384. fromJSON: async function ( json ) {
  385. var loader = new THREE.ObjectLoader();
  386. var camera = await loader.parseAsync( json.camera );
  387. const existingUuid = this.camera.uuid;
  388. const incomingUuid = camera.uuid;
  389. // copy all properties, including uuid
  390. this.camera.copy( camera );
  391. this.camera.uuid = incomingUuid;
  392. delete this.cameras[ existingUuid ]; // remove old entry [existingUuid, this.camera]
  393. this.cameras[ incomingUuid ] = this.camera; // add new entry [incomingUuid, this.camera]
  394. if ( json.controls !== undefined ) {
  395. this.controls.fromJSON( json.controls );
  396. }
  397. this.signals.cameraResetted.dispatch();
  398. this.history.fromJSON( json.history );
  399. this.scripts = json.scripts;
  400. const scene = await loader.parseAsync( json.scene );
  401. this.backgroundType = json.backgroundType || 'Default';
  402. this.environmentType = json.environmentType || 'Default';
  403. this.setScene( scene );
  404. },
  405. toJSON: function () {
  406. // scripts clean up
  407. var scene = this.scene;
  408. var scripts = this.scripts;
  409. for ( var key in scripts ) {
  410. var script = scripts[ key ];
  411. if ( script.length === 0 || scene.getObjectByProperty( 'uuid', key ) === undefined ) {
  412. delete scripts[ key ];
  413. }
  414. }
  415. return {
  416. metadata: {},
  417. project: {
  418. renderer: this.config.getKey( 'project/renderer/type' ),
  419. shadows: this.config.getKey( 'project/renderer/shadows' ),
  420. shadowType: this.config.getKey( 'project/renderer/shadowType' ),
  421. toneMapping: this.config.getKey( 'project/renderer/toneMapping' ),
  422. toneMappingExposure: this.config.getKey( 'project/renderer/toneMappingExposure' )
  423. },
  424. camera: this.viewportCamera.toJSON(),
  425. controls: this.controls.toJSON(),
  426. scene: this.scene.toJSON(),
  427. scripts: this.scripts,
  428. history: this.history.toJSON(),
  429. backgroundType: this.backgroundType,
  430. environmentType: this.environmentType
  431. };
  432. },
  433. objectByUuid: function ( uuid ) {
  434. return this.scene.getObjectByProperty( 'uuid', uuid, true );
  435. },
  436. execute: function ( cmd, optionalName ) {
  437. this.history.execute( cmd, optionalName );
  438. },
  439. undo: function () {
  440. this.history.undo();
  441. },
  442. redo: function () {
  443. this.history.redo();
  444. },
  445. utils: {
  446. save: save,
  447. saveArrayBuffer: saveArrayBuffer,
  448. saveString: saveString,
  449. formatNumber: formatNumber
  450. }
  451. };
  452. const link = document.createElement( 'a' );
  453. function save( blob, filename ) {
  454. if ( link.href ) {
  455. URL.revokeObjectURL( link.href );
  456. }
  457. link.href = URL.createObjectURL( blob );
  458. link.download = filename || 'data.json';
  459. link.dispatchEvent( new MouseEvent( 'click' ) );
  460. }
  461. function saveArrayBuffer( buffer, filename ) {
  462. save( new Blob( [ buffer ], { type: 'application/octet-stream' } ), filename );
  463. }
  464. function saveString( text, filename ) {
  465. save( new Blob( [ text ], { type: 'text/plain' } ), filename );
  466. }
  467. function formatNumber( number ) {
  468. return new Intl.NumberFormat( 'en-us', { useGrouping: true } ).format( number );
  469. }
  470. export { Editor };
粤ICP备19079148号