Editor.js 14 KB

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