Editor.js 15 KB

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