|
| 1 | +import * as z from "zod"; |
| 2 | +import Cesium from "./Cesium"; |
| 3 | + |
| 4 | +const CESIUM_OPTIONS_KEY = "cesiumOptions"; |
| 5 | + |
| 6 | +export interface CesiumOptionsSchema { |
| 7 | + msaaSamples: number; |
| 8 | + resolutionScale: number; |
| 9 | + highDynamicRange: boolean; |
| 10 | + fxaa: boolean; |
| 11 | + enableCollisionDetection: boolean; |
| 12 | + targetFrameRate?: number | "any"; |
| 13 | + bloom: boolean; |
| 14 | + bloomBrightness: number; |
| 15 | + bloomContrast: number; |
| 16 | +} |
| 17 | + |
| 18 | +export const CesiumOptionsSchema = z.object({ |
| 19 | + msaaSamples: z.number(), |
| 20 | + resolutionScale: z.number(), |
| 21 | + highDynamicRange: z.boolean(), |
| 22 | + fxaa: z.boolean(), |
| 23 | + enableCollisionDetection: z.boolean(), |
| 24 | + targetFrameRate: z.union([z.number(), z.string("any")]).optional(), |
| 25 | + bloom: z.boolean(), |
| 26 | + bloomBrightness: z.number(), |
| 27 | + bloomContrast: z.number() |
| 28 | +}); |
| 29 | + |
| 30 | +/** |
| 31 | + * Manage Cesium options |
| 32 | + */ |
| 33 | +export default class CesiumOptions implements CesiumOptionsSchema { |
| 34 | + private readonly cesium: Cesium; |
| 35 | + readonly defaultOptions: CesiumOptionsSchema; |
| 36 | + |
| 37 | + constructor(cesium: Cesium, initialOptions: Partial<CesiumOptionsSchema>) { |
| 38 | + this.cesium = cesium; |
| 39 | + this.updateFromJson(initialOptions); |
| 40 | + |
| 41 | + // Save snapshot of initial state so that we can diff and reset changes |
| 42 | + this.defaultOptions = this.toJson(); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Reset all options to terria/cesium defaults |
| 47 | + */ |
| 48 | + resetAll() { |
| 49 | + this.updateFromJson(this.defaultOptions); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Reset specified option to terria/cesium default |
| 54 | + */ |
| 55 | + resetOption<K extends keyof CesiumOptionsSchema>(name: K) { |
| 56 | + (this as CesiumOptionsSchema)[name] = this.defaultOptions[name]; |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Return snapshot of all options as JSON object |
| 61 | + */ |
| 62 | + toJson(): CesiumOptionsSchema { |
| 63 | + return Object.fromEntries( |
| 64 | + Object.keys(CesiumOptionsSchema.shape).map((name) => [ |
| 65 | + name, |
| 66 | + (this as any)[name] |
| 67 | + ]) |
| 68 | + ) as any; |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Update options from JSON |
| 73 | + */ |
| 74 | + updateFromJson(json: any) { |
| 75 | + const parsed = CesiumOptionsSchema.partial().parse(json); |
| 76 | + (Object.keys(parsed) as Array<keyof CesiumOptionsSchema>).forEach( |
| 77 | + (name) => { |
| 78 | + (this as any)[name] = parsed[name]; |
| 79 | + } |
| 80 | + ); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Return locally stored options |
| 85 | + */ |
| 86 | + getUserPreferences() { |
| 87 | + const str = this.cesium.terria.getLocalProperty(CESIUM_OPTIONS_KEY); |
| 88 | + try { |
| 89 | + const localChanges = typeof str === "string" ? JSON.parse(str) : {}; |
| 90 | + return CesiumOptionsSchema.partial().parse(localChanges); |
| 91 | + } catch (error) { |
| 92 | + console.error("Ignoring invalid cesium options from local storage"); |
| 93 | + console.error(error); |
| 94 | + return {}; |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Load user preferences |
| 100 | + */ |
| 101 | + loadUserPreferences(): any { |
| 102 | + this.updateFromJson(this.getUserPreferences()); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Save user preferences |
| 107 | + */ |
| 108 | + saveUserPreferences() { |
| 109 | + const currentOptions = this.toJson(); |
| 110 | + // Save only those options that differ from default options |
| 111 | + const onlyChangedOptions = Object.fromEntries( |
| 112 | + Object.entries(currentOptions).filter( |
| 113 | + ([name, value]) => (this.defaultOptions as any)[name] !== value |
| 114 | + ) |
| 115 | + ); |
| 116 | + // TODO: remove item if empty instead of setting to empty object {} |
| 117 | + this.cesium.terria.setLocalProperty( |
| 118 | + CESIUM_OPTIONS_KEY, |
| 119 | + JSON.stringify(onlyChangedOptions) |
| 120 | + ); |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Returns true if user has any stored preferences |
| 125 | + */ |
| 126 | + get hasSavedPreferences() { |
| 127 | + return Object.keys(this.getUserPreferences()).length > 0; |
| 128 | + } |
| 129 | + |
| 130 | + get msaaSamples() { |
| 131 | + return this.cesium.scene.msaaSamples; |
| 132 | + } |
| 133 | + |
| 134 | + set msaaSamples(value: number) { |
| 135 | + this.cesium.scene.msaaSamples = value; |
| 136 | + } |
| 137 | + |
| 138 | + get resolutionScale(): number { |
| 139 | + return this.cesium.cesiumWidget.resolutionScale; |
| 140 | + } |
| 141 | + |
| 142 | + set resolutionScale(value: number) { |
| 143 | + this.cesium.cesiumWidget.resolutionScale = value; |
| 144 | + } |
| 145 | + |
| 146 | + get highDynamicRange(): boolean { |
| 147 | + return this.cesium.scene.highDynamicRange; |
| 148 | + } |
| 149 | + |
| 150 | + set highDynamicRange(value: boolean) { |
| 151 | + this.cesium.scene.highDynamicRange = value; |
| 152 | + } |
| 153 | + |
| 154 | + get fxaa(): boolean { |
| 155 | + return this.cesium.scene.postProcessStages.fxaa.enabled; |
| 156 | + } |
| 157 | + |
| 158 | + set fxaa(value: boolean) { |
| 159 | + this.cesium.scene.postProcessStages.fxaa.enabled = value; |
| 160 | + } |
| 161 | + |
| 162 | + get enableCollisionDetection(): boolean { |
| 163 | + return this.cesium.scene.screenSpaceCameraController |
| 164 | + .enableCollisionDetection; |
| 165 | + } |
| 166 | + |
| 167 | + set enableCollisionDetection(value: boolean) { |
| 168 | + this.cesium.scene.screenSpaceCameraController.enableCollisionDetection = |
| 169 | + value; |
| 170 | + } |
| 171 | + |
| 172 | + get targetFrameRate(): number | "any" { |
| 173 | + return this.cesium.cesiumWidget.targetFrameRate ?? "any"; |
| 174 | + } |
| 175 | + |
| 176 | + set targetFrameRate(value: number | "any") { |
| 177 | + //@ts-expect-error 2322 - Cesium typings does not allow setting |
| 178 | + //targetFrameRate=undefined but it is still valid as per doc |
| 179 | + this.cesium.cesiumWidget.targetFrameRate = |
| 180 | + value === "any" ? undefined : value; |
| 181 | + } |
| 182 | + |
| 183 | + get bloom(): boolean { |
| 184 | + return this.cesium.scene.postProcessStages.bloom.enabled; |
| 185 | + } |
| 186 | + |
| 187 | + set bloom(value: boolean) { |
| 188 | + this.cesium.scene.postProcessStages.bloom.enabled = value; |
| 189 | + } |
| 190 | + |
| 191 | + get bloomBrightness(): number { |
| 192 | + return this.cesium.scene.postProcessStages.bloom.uniforms.brightness; |
| 193 | + } |
| 194 | + |
| 195 | + set bloomBrightness(value: number) { |
| 196 | + this.cesium.scene.postProcessStages.bloom.uniforms.brightness = value; |
| 197 | + } |
| 198 | + |
| 199 | + get bloomContrast(): number { |
| 200 | + return this.cesium.scene.postProcessStages.bloom.uniforms.contrast; |
| 201 | + } |
| 202 | + |
| 203 | + set bloomContrast(value: number) { |
| 204 | + this.cesium.scene.postProcessStages.bloom.uniforms.contrast = value; |
| 205 | + } |
| 206 | +} |
0 commit comments