globalState.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import { GLTFFileLoader, IGLTFLoaderExtension } 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 { LightGizmo } from "babylonjs/Gizmos/lightGizmo";
  9. import { PropertyChangedEvent } from "./propertyChangedEvent";
  10. import { ReplayRecorder } from './replayRecorder';
  11. import { DataStorage } from 'babylonjs/Misc/dataStorage';
  12. export class GlobalState {
  13. public onSelectionChangedObservable: Observable<any>;
  14. public onPropertyChangedObservable: Observable<PropertyChangedEvent>;
  15. public onInspectorClosedObservable = new Observable<Scene>();
  16. public onTabChangedObservable = new Observable<number>();
  17. public onPluginActivatedObserver: Nullable<Observer<ISceneLoaderPlugin | ISceneLoaderPluginAsync>>;
  18. public sceneImportDefaults: { [key: string]: any } = {};
  19. public validationResults: Nullable<IGLTFValidationResults> = null;
  20. public onValidationResultsUpdatedObservable = new Observable<Nullable<IGLTFValidationResults>>();
  21. public onExtensionLoadedObservable: Observable<IGLTFLoaderExtension>;
  22. public glTFLoaderExtensionDefaults: { [name: string]: { [key: string]: any } } = {};
  23. public glTFLoaderDefaults: { [key: string]: any } = { "validate": true };
  24. public blockMutationUpdates = false;
  25. public selectedLineContainerTitle = "";
  26. public recorder = new ReplayRecorder();
  27. private _onlyUseEulers: Nullable<boolean> = null;
  28. public get onlyUseEulers(): boolean {
  29. if (this._onlyUseEulers === null) {
  30. this._onlyUseEulers = DataStorage.ReadBoolean("settings_onlyUseEulers", true);
  31. }
  32. return this._onlyUseEulers!;
  33. }
  34. public set onlyUseEulers(value: boolean) {
  35. this._onlyUseEulers = value;
  36. DataStorage.WriteBoolean("settings_onlyUseEulers", value);
  37. }
  38. private _ignoreBackfacesForPicking: Nullable<boolean> = null;
  39. public get ignoreBackfacesForPicking(): boolean {
  40. if (this._ignoreBackfacesForPicking === null) {
  41. this._ignoreBackfacesForPicking = DataStorage.ReadBoolean("settings_ignoreBackfacesForPicking", false);
  42. }
  43. return this._ignoreBackfacesForPicking!;
  44. }
  45. public set ignoreBackfacesForPicking(value: boolean) {
  46. this._ignoreBackfacesForPicking = value;
  47. DataStorage.WriteBoolean("settings_ignoreBackfacesForPicking", value);
  48. }
  49. public init(propertyChangedObservable: Observable<PropertyChangedEvent>) {
  50. this.onPropertyChangedObservable = propertyChangedObservable;
  51. propertyChangedObservable.add(event => {
  52. this.recorder.record(event);
  53. })
  54. }
  55. public prepareGLTFPlugin(loader: GLTFFileLoader) {
  56. var loaderState = this.glTFLoaderDefaults;
  57. if (loaderState !== undefined) {
  58. for (const key in loaderState) {
  59. (loader as any)[key] = loaderState[key];
  60. }
  61. }
  62. loader.onExtensionLoadedObservable.add((extension: IGLTFLoaderExtension) => {
  63. var extensionState = this.glTFLoaderExtensionDefaults[extension.name];
  64. if (extensionState !== undefined) {
  65. for (const key in extensionState) {
  66. (extension as any)[key] = extensionState[key];
  67. }
  68. }
  69. });
  70. if (this.validationResults) {
  71. this.validationResults = null;
  72. this.onValidationResultsUpdatedObservable.notifyObservers(null);
  73. }
  74. loader.onValidatedObservable.add((results: IGLTFValidationResults) => {
  75. this.validationResults = results;
  76. this.onValidationResultsUpdatedObservable.notifyObservers(results);
  77. if (results.issues.numErrors || results.issues.numWarnings) {
  78. this.onTabChangedObservable.notifyObservers(3);
  79. }
  80. });
  81. }
  82. // Light gizmos
  83. public lightGizmos: Array<LightGizmo> = [];
  84. public enableLightGizmo(light: Light, enable = true) {
  85. if (enable) {
  86. if (!light.reservedDataStore) {
  87. light.reservedDataStore = {}
  88. }
  89. if (!light.reservedDataStore.lightGizmo) {
  90. light.reservedDataStore.lightGizmo = new LightGizmo();
  91. this.lightGizmos.push(light.reservedDataStore.lightGizmo)
  92. light.reservedDataStore.lightGizmo.light = light;
  93. light.reservedDataStore.lightGizmo.material.reservedDataStore = {hidden: true};
  94. }
  95. } else if (light.reservedDataStore && light.reservedDataStore.lightGizmo) {
  96. this.lightGizmos.splice(this.lightGizmos.indexOf(light.reservedDataStore.lightGizmo), 1);
  97. light.reservedDataStore.lightGizmo.dispose();
  98. light.reservedDataStore.lightGizmo = null;
  99. }
  100. }
  101. }