globalState.ts 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. import { GLTFLoaderAnimationStartMode, GLTFLoaderCoordinateSystemMode } from "babylonjs-loaders/glTF/index";
  2. import { IGLTFValidationResults } from "babylonjs-gltf2interface";
  3. import { Nullable } from "babylonjs/types";
  4. import { Observable, Observer } from "babylonjs/Misc/observable";
  5. import { ISceneLoaderPlugin, ISceneLoaderPluginAsync } from "babylonjs/Loading/sceneLoader";
  6. import { Scene } from "babylonjs/scene";
  7. import { Light } from "babylonjs/Lights/light";
  8. import { Camera } from "babylonjs/Cameras/camera";
  9. import { LightGizmo } from "babylonjs/Gizmos/lightGizmo";
  10. import { CameraGizmo } from "babylonjs/Gizmos/cameraGizmo";
  11. import { PropertyChangedEvent } from "./propertyChangedEvent";
  12. import { ReplayRecorder } from "./replayRecorder";
  13. import { DataStorage } from "babylonjs/Misc/dataStorage";
  14. export class GlobalState {
  15. public onSelectionChangedObservable: Observable<any>;
  16. public onPropertyChangedObservable: Observable<PropertyChangedEvent>;
  17. public onInspectorClosedObservable = new Observable<Scene>();
  18. public onTabChangedObservable = new Observable<number>();
  19. public onSelectionRenamedObservable = new Observable<void>();
  20. public onPluginActivatedObserver: Nullable<Observer<ISceneLoaderPlugin | ISceneLoaderPluginAsync>>;
  21. public onNewSceneObservable = new Observable<Scene>();
  22. public sceneImportDefaults: { [key: string]: any } = {};
  23. public validationResults: Nullable<IGLTFValidationResults> = null;
  24. public onValidationResultsUpdatedObservable = new Observable<Nullable<IGLTFValidationResults>>();
  25. public onExtensionLoadedObservable: Observable<import("babylonjs-loaders/glTF/index").IGLTFLoaderExtension>;
  26. public glTFLoaderExtensionDefaults: { [name: string]: { [key: string]: any } } = {
  27. MSFT_lod: { enabled: true, maxLODsToLoad: 10 },
  28. MSFT_minecraftMesh: { enabled: true },
  29. MSFT_sRGBFactors: { enabled: true },
  30. MSFT_audio_emitter: { enabled: true },
  31. KHR_xmp: { enabled: true },
  32. KHR_draco_mesh_compression: { enabled: true },
  33. KHR_mesh_quantization: { enabled: true },
  34. KHR_materials_pbrSpecularGlossiness: { enabled: true },
  35. KHR_materials_clearcoat: { enabled: true },
  36. KHR_materials_ior: { enabled: true },
  37. KHR_materials_sheen: { enabled: true },
  38. KHR_materials_specular: { enabled: true },
  39. KHR_materials_unlit: { enabled: true },
  40. KHR_materials_variants: { enabled: true },
  41. KHR_materials_transmission: { enabled: true },
  42. KHR_lights_punctual: { enabled: true },
  43. KHR_texture_basisu: { enabled: true },
  44. KHR_texture_transform: { enabled: true },
  45. EXT_lights_image_based: { enabled: true },
  46. EXT_mesh_gpu_instancing: { enabled: true },
  47. EXT_texture_webp: { enabled: true },
  48. };
  49. public glTFLoaderDefaults: { [key: string]: any } = {
  50. animationStartMode: typeof GLTFLoaderAnimationStartMode !== 'undefined' ? GLTFLoaderAnimationStartMode.FIRST : 1 ,
  51. capturePerformanceCounters: false,
  52. compileMaterials: false,
  53. compileShadowGenerators: false,
  54. coordinateSystemMode: typeof GLTFLoaderCoordinateSystemMode !== 'undefined' ? GLTFLoaderCoordinateSystemMode.AUTO : 0,
  55. loggingEnabled: false,
  56. transparencyAsCoverage: false,
  57. useClipPlane: false,
  58. validate: true,
  59. };
  60. public glTFLoaderExtensions: { [key: string]: import("babylonjs-loaders/glTF/index").IGLTFLoaderExtension } = {};
  61. public blockMutationUpdates = false;
  62. public selectedLineContainerTitles: Array<string> = [];
  63. public selectedLineContainerTitlesNoFocus: Array<string> = [];
  64. public recorder = new ReplayRecorder();
  65. private _onlyUseEulers: Nullable<boolean> = null;
  66. public get onlyUseEulers(): boolean {
  67. if (this._onlyUseEulers === null) {
  68. this._onlyUseEulers = DataStorage.ReadBoolean("settings_onlyUseEulers", true);
  69. }
  70. return this._onlyUseEulers!;
  71. }
  72. public set onlyUseEulers(value: boolean) {
  73. this._onlyUseEulers = value;
  74. DataStorage.WriteBoolean("settings_onlyUseEulers", value);
  75. }
  76. private _ignoreBackfacesForPicking: Nullable<boolean> = null;
  77. public get ignoreBackfacesForPicking(): boolean {
  78. if (this._ignoreBackfacesForPicking === null) {
  79. this._ignoreBackfacesForPicking = DataStorage.ReadBoolean("settings_ignoreBackfacesForPicking", false);
  80. }
  81. return this._ignoreBackfacesForPicking!;
  82. }
  83. public set ignoreBackfacesForPicking(value: boolean) {
  84. this._ignoreBackfacesForPicking = value;
  85. DataStorage.WriteBoolean("settings_ignoreBackfacesForPicking", value);
  86. }
  87. public init(propertyChangedObservable: Observable<PropertyChangedEvent>) {
  88. this.onPropertyChangedObservable = propertyChangedObservable;
  89. this.onNewSceneObservable.add((scene) => {
  90. this.recorder.cancel();
  91. });
  92. }
  93. public prepareGLTFPlugin(loader: import("babylonjs-loaders/glTF/index").GLTFFileLoader) {
  94. this.glTFLoaderExtensions = {};
  95. var loaderState = this.glTFLoaderDefaults;
  96. if (loaderState !== undefined) {
  97. for (const key in loaderState) {
  98. (loader as any)[key] = loaderState[key];
  99. }
  100. }
  101. loader.onExtensionLoadedObservable.add((extension: import("babylonjs-loaders/glTF/index").IGLTFLoaderExtension) => {
  102. var extensionState = this.glTFLoaderExtensionDefaults[extension.name];
  103. if (extensionState !== undefined) {
  104. for (const key in extensionState) {
  105. (extension as any)[key] = extensionState[key];
  106. }
  107. }
  108. this.glTFLoaderExtensions[extension.name] = extension;
  109. });
  110. if (this.validationResults) {
  111. this.validationResults = null;
  112. this.onValidationResultsUpdatedObservable.notifyObservers(null);
  113. }
  114. loader.onValidatedObservable.add((results: IGLTFValidationResults) => {
  115. this.validationResults = results;
  116. this.onValidationResultsUpdatedObservable.notifyObservers(results);
  117. if (results.issues.numErrors || results.issues.numWarnings) {
  118. this.selectedLineContainerTitlesNoFocus.push("GLTF VALIDATION");
  119. this.onTabChangedObservable.notifyObservers(3);
  120. }
  121. });
  122. }
  123. // Light gizmos
  124. public lightGizmos: Array<LightGizmo> = [];
  125. public enableLightGizmo(light: Light, enable = true) {
  126. if (enable) {
  127. if (!light.reservedDataStore) {
  128. light.reservedDataStore = {};
  129. }
  130. if (!light.reservedDataStore.lightGizmo) {
  131. light.reservedDataStore.lightGizmo = new LightGizmo();
  132. this.lightGizmos.push(light.reservedDataStore.lightGizmo);
  133. light.reservedDataStore.lightGizmo.light = light;
  134. light.reservedDataStore.lightGizmo.material.reservedDataStore = { hidden: true };
  135. }
  136. } else if (light.reservedDataStore && light.reservedDataStore.lightGizmo) {
  137. this.lightGizmos.splice(this.lightGizmos.indexOf(light.reservedDataStore.lightGizmo), 1);
  138. light.reservedDataStore.lightGizmo.dispose();
  139. light.reservedDataStore.lightGizmo = null;
  140. }
  141. }
  142. // Camera gizmos
  143. public cameraGizmos: Array<CameraGizmo> = [];
  144. public enableCameraGizmo(camera: Camera, enable = true) {
  145. if (enable) {
  146. if (!camera.reservedDataStore) {
  147. camera.reservedDataStore = {};
  148. }
  149. if (!camera.reservedDataStore.cameraGizmo) {
  150. camera.reservedDataStore.cameraGizmo = new CameraGizmo();
  151. this.cameraGizmos.push(camera.reservedDataStore.cameraGizmo);
  152. camera.reservedDataStore.cameraGizmo.camera = camera;
  153. camera.reservedDataStore.cameraGizmo.material.reservedDataStore = { hidden: true };
  154. }
  155. } else if (camera.reservedDataStore && camera.reservedDataStore.cameraGizmo) {
  156. this.cameraGizmos.splice(this.cameraGizmos.indexOf(camera.reservedDataStore.cameraGizmo), 1);
  157. camera.reservedDataStore.cameraGizmo.dispose();
  158. camera.reservedDataStore.cameraGizmo = null;
  159. }
  160. }
  161. }