mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-16 16:22:20 +00:00
Drive the Navara volumetric clouds from the cockpit weather behind an optional render.setCloudCover capability
The cockpit's weather feed moves out of the Cesium cloud pass and serves both: Cesium keeps its own cloud canvas, an engine with its own clouds takes their cover from the same observations, with no second WebGL canvas.
This commit is contained in:
1 parent
d8c63c69d8
commit
f0d38897f4
15 files changed
+1004
-79
No files matched your search
+302
-70
@@ -184,13 +184,111 @@ export function cockpitWeatherEnabledFromStoredValue(value) {
|
||||
return value === '1';
|
||||
}
|
||||
|
||||
/**
|
||||
* The cloud strength (0..1) a weather observation gives at an altitude: the
|
||||
* weather profile's cloud, faded out above the cloud deck.
|
||||
*/
|
||||
export function cockpitCloudStrength(weather, altitudeM) {
|
||||
const profile = deriveWeatherEffectProfile(weather);
|
||||
return profile.available
|
||||
? profile.cloud * weatherAltitudeFactors(altitudeM).cloud
|
||||
: 0;
|
||||
}
|
||||
|
||||
/** Highest cloud cover a globe engine is asked for (overcast, not a wall). */
|
||||
export const COCKPIT_CLOUD_COVERAGE_MAX = 0.8;
|
||||
|
||||
/** The globe engine's cloud cover (0..1) for a cockpit cloud strength. */
|
||||
export function cockpitCloudCoverage(strength) {
|
||||
const value = Math.min(1, Math.max(0, Number(strength) || 0));
|
||||
return value * COCKPIT_CLOUD_COVERAGE_MAX;
|
||||
}
|
||||
|
||||
/**
|
||||
* The cockpit's weather observations, shared by both cloud presentations:
|
||||
* one request at a time, the last observation and where and when it was
|
||||
* taken (cockpitWeatherRefreshDue decides when it is stale).
|
||||
*/
|
||||
export class CockpitWeatherFeed {
|
||||
constructor(weatherService = applicationServices.weather) {
|
||||
this.weatherService = weatherService;
|
||||
this.weather = null;
|
||||
this.fetchedAt = 0;
|
||||
this.anchor = null;
|
||||
this.pending = null;
|
||||
this.abort = null;
|
||||
this.sourceStatus = 'idle';
|
||||
}
|
||||
|
||||
/** Whether an observation for `point` must be fetched. */
|
||||
due(point, nowMs = Date.now()) {
|
||||
return cockpitWeatherRefreshDue({
|
||||
nowMs,
|
||||
fetchedAt: this.fetchedAt,
|
||||
anchor: this.anchor,
|
||||
point,
|
||||
hasWeather: Boolean(this.weather),
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch the observation at `point` (replacing a request in flight):
|
||||
* `onWeather(weather, status)` once it arrives, `onError()` when the
|
||||
* observation is unavailable (an abort calls neither). Resolves to the
|
||||
* weather, or null.
|
||||
*/
|
||||
fetch(point, { onWeather, onError } = {}) {
|
||||
this.abort?.abort();
|
||||
const request = (this.abort = new AbortController());
|
||||
const pending = this.weatherService.getConditions(
|
||||
point.latitude,
|
||||
point.longitude,
|
||||
{ signal: request.signal },
|
||||
);
|
||||
this.pending = pending
|
||||
.then(async (payload) => {
|
||||
if (this.abort !== request || request.signal.aborted) return null;
|
||||
if (!payload?.weather)
|
||||
throw new Error('Cloud weather observation unavailable');
|
||||
this.weather = payload.weather;
|
||||
this.fetchedAt = Date.now();
|
||||
this.anchor = point;
|
||||
this.sourceStatus = payload.status || 'ready';
|
||||
onWeather?.(payload.weather, this.sourceStatus);
|
||||
return payload.weather;
|
||||
})
|
||||
.catch((error) => {
|
||||
if (request.signal.aborted || this.abort !== request) return null;
|
||||
if (error?.name !== 'AbortError') {
|
||||
this.sourceStatus = 'unavailable';
|
||||
onError?.(error);
|
||||
}
|
||||
return null;
|
||||
})
|
||||
.finally(() => {
|
||||
if (this.abort === request) {
|
||||
this.pending = null;
|
||||
this.abort = null;
|
||||
}
|
||||
});
|
||||
return this.pending;
|
||||
}
|
||||
|
||||
/** Abandon a request in flight; the last observation stays. */
|
||||
cancel() {
|
||||
this.abort?.abort();
|
||||
this.abort = null;
|
||||
this.pending = null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Weather-backed volumetric cloud pass that exists only while cockpit mode is
|
||||
* active. It owns no Cesium fog/post-process stages and cannot affect map mode.
|
||||
*/
|
||||
class CockpitCloudEffectsController {
|
||||
constructor(viewer, { weatherService = applicationServices.weather } = {}) {
|
||||
this.weatherService = weatherService;
|
||||
this.feed = new CockpitWeatherFeed(weatherService);
|
||||
this.viewer = viewer;
|
||||
this.canvas = document.createElement('canvas');
|
||||
this.canvas.id = 'cockpit-cloud-effects';
|
||||
@@ -206,15 +304,10 @@ class CockpitCloudEffectsController {
|
||||
this.startupTimer = null;
|
||||
this.lastFrameMs = 0;
|
||||
this.lastRefreshCheckMs = 0;
|
||||
this.fetchedAt = 0;
|
||||
this.anchor = null;
|
||||
this.weather = null;
|
||||
this.strength = 0;
|
||||
this.targetStrength = 0;
|
||||
this.windDirectionDeg = 0;
|
||||
this.windStrength = 0;
|
||||
this.pending = null;
|
||||
this.abort = null;
|
||||
this.destroyed = false;
|
||||
this.reducedMotion = window.matchMedia(
|
||||
'(prefers-reduced-motion: reduce)',
|
||||
@@ -387,9 +480,7 @@ class CockpitCloudEffectsController {
|
||||
this.refreshTimer = null;
|
||||
if (this.startupTimer !== null) window.clearTimeout(this.startupTimer);
|
||||
this.startupTimer = null;
|
||||
this.abort?.abort();
|
||||
this.abort = null;
|
||||
this.pending = null;
|
||||
this.feed.cancel();
|
||||
this.strength = 0;
|
||||
this.targetStrength = 0;
|
||||
this.canvas.classList.remove('active');
|
||||
@@ -407,66 +498,33 @@ class CockpitCloudEffectsController {
|
||||
if (
|
||||
this.destroyed ||
|
||||
!this.enabled ||
|
||||
this.pending ||
|
||||
this.feed.pending ||
|
||||
this.suspended ||
|
||||
!document.body.classList.contains('cockpit-mode')
|
||||
) {
|
||||
return this.pending;
|
||||
return this.feed.pending;
|
||||
}
|
||||
const point = this.cameraPoint();
|
||||
if (!point) return null;
|
||||
if (
|
||||
this.weather &&
|
||||
Date.now() - this.fetchedAt < WEATHER_REFRESH_MS &&
|
||||
greatCircleM(this.anchor, point) < WEATHER_MOVE_REFRESH_M
|
||||
) {
|
||||
this.applyWeather(this.weather, point.altitudeM);
|
||||
return this.weather;
|
||||
if (!this.feed.due(point)) {
|
||||
this.applyWeather(this.feed.weather, point.altitudeM);
|
||||
return this.feed.weather;
|
||||
}
|
||||
|
||||
this.abort?.abort();
|
||||
this.abort = new AbortController();
|
||||
const pending = this.weatherService.getConditions(
|
||||
point.latitude,
|
||||
point.longitude,
|
||||
{ signal: this.abort.signal },
|
||||
);
|
||||
const request = this.abort;
|
||||
this.pending = pending
|
||||
.then(async (payload) => {
|
||||
if (this.abort !== request || request.signal.aborted) return null;
|
||||
if (!payload?.weather)
|
||||
throw new Error('Cloud weather observation unavailable');
|
||||
this.weather = payload.weather;
|
||||
this.fetchedAt = Date.now();
|
||||
this.anchor = point;
|
||||
this.applyWeather(payload.weather, point.altitudeM);
|
||||
this.canvas.dataset.sourceStatus = payload.status || 'ready';
|
||||
return payload.weather;
|
||||
})
|
||||
.catch((error) => {
|
||||
if (request.signal.aborted || this.abort !== request) return;
|
||||
if (error?.name !== 'AbortError') {
|
||||
this.targetStrength = 0;
|
||||
this.canvas.dataset.sourceStatus = 'unavailable';
|
||||
}
|
||||
return null;
|
||||
})
|
||||
.finally(() => {
|
||||
if (this.abort === request) {
|
||||
this.pending = null;
|
||||
this.abort = null;
|
||||
}
|
||||
});
|
||||
return this.pending;
|
||||
return this.feed.fetch(point, {
|
||||
onWeather: (weather, status) => {
|
||||
this.applyWeather(weather, point.altitudeM);
|
||||
this.canvas.dataset.sourceStatus = status;
|
||||
},
|
||||
onError: () => {
|
||||
this.targetStrength = 0;
|
||||
this.canvas.dataset.sourceStatus = 'unavailable';
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
applyWeather(weather, altitudeM) {
|
||||
const profile = deriveWeatherEffectProfile(weather);
|
||||
const altitude = weatherAltitudeFactors(altitudeM);
|
||||
this.targetStrength = profile.available
|
||||
? profile.cloud * altitude.cloud
|
||||
: 0;
|
||||
this.targetStrength = cockpitCloudStrength(weather, altitudeM);
|
||||
this.windDirectionDeg = profile.windDirectionDeg || 0;
|
||||
this.windStrength = profile.wind || 0;
|
||||
this.canvas.dataset.cloudStrength = this.targetStrength.toFixed(3);
|
||||
@@ -505,17 +563,7 @@ class CockpitCloudEffectsController {
|
||||
if (timeMs - this.lastRefreshCheckMs >= 1000) {
|
||||
this.lastRefreshCheckMs = timeMs;
|
||||
const point = this.cameraPoint();
|
||||
if (
|
||||
point &&
|
||||
cockpitWeatherRefreshDue({
|
||||
nowMs: Date.now(),
|
||||
fetchedAt: this.fetchedAt,
|
||||
anchor: this.anchor,
|
||||
point,
|
||||
hasWeather: Boolean(this.weather),
|
||||
})
|
||||
)
|
||||
void this.refresh();
|
||||
if (point && this.feed.due(point)) void this.refresh();
|
||||
}
|
||||
|
||||
const blend = this.reducedMotion ? 1 : 0.08;
|
||||
@@ -572,7 +620,7 @@ class CockpitCloudEffectsController {
|
||||
enabled: this.enabled,
|
||||
active: this.canvas.classList.contains('active'),
|
||||
strength: this.targetStrength,
|
||||
weather: this.weather ? { ...this.weather } : null,
|
||||
weather: this.feed.weather ? { ...this.feed.weather } : null,
|
||||
renderSize: { width: this.canvas.width, height: this.canvas.height },
|
||||
sourceStatus: this.canvas.dataset.sourceStatus || 'idle',
|
||||
};
|
||||
@@ -634,3 +682,187 @@ class CockpitCloudEffectsController {
|
||||
export function initCockpitCloudEffects(viewer, options) {
|
||||
return new CockpitCloudEffectsController(viewer, options);
|
||||
}
|
||||
|
||||
/**
|
||||
* The cockpit's weather clouds on a globe engine that draws its own clouds
|
||||
* (render.setCloudCover, the `cloudCover` capability: Navara's volumetric
|
||||
* clouds), with no canvas or WebGL context of its own. While cockpit mode
|
||||
* and the cockpit weather toggle are on, the engine's cloud cover follows
|
||||
* the weather under the camera (the same observations, refresh rule and
|
||||
* strength as the Cesium pass); otherwise the engine keeps its own clouds.
|
||||
*/
|
||||
class EngineCockpitCloudsController {
|
||||
constructor(engine, { weatherService = applicationServices.weather } = {}) {
|
||||
this.engine = engine;
|
||||
this.feed = new CockpitWeatherFeed(weatherService);
|
||||
this.timer = null;
|
||||
this.targetStrength = 0;
|
||||
this.coverage = null;
|
||||
this.suspended = false;
|
||||
this.destroyed = false;
|
||||
this.enabled = this.readEnabledPreference();
|
||||
this.onCockpitMode = (event) => {
|
||||
const active =
|
||||
event?.detail?.active ??
|
||||
document.body.classList.contains('cockpit-mode');
|
||||
if (active && this.enabled) this.start();
|
||||
else this.stop();
|
||||
};
|
||||
this.onEnabledChange = (event) => {
|
||||
const requested = event?.detail?.enabled;
|
||||
this.setEnabled(
|
||||
typeof requested === 'boolean' ? requested : !this.enabled,
|
||||
);
|
||||
};
|
||||
window.addEventListener('gev:cockpit-mode-changed', this.onCockpitMode);
|
||||
window.addEventListener('gev:cockpit-weather-toggle', this.onEnabledChange);
|
||||
this.emitEnabledState();
|
||||
if (this.enabled && document.body.classList.contains('cockpit-mode'))
|
||||
this.start();
|
||||
}
|
||||
|
||||
readEnabledPreference() {
|
||||
try {
|
||||
return cockpitWeatherEnabledFromStoredValue(
|
||||
localStorage.getItem(WEATHER_ENABLED_STORAGE_KEY),
|
||||
);
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
emitEnabledState() {
|
||||
window.dispatchEvent(
|
||||
new CustomEvent('gev:cockpit-weather-state', {
|
||||
detail: { enabled: this.enabled },
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
setEnabled(enabled) {
|
||||
this.enabled = !!enabled;
|
||||
try {
|
||||
localStorage.setItem(
|
||||
WEATHER_ENABLED_STORAGE_KEY,
|
||||
this.enabled ? '1' : '0',
|
||||
);
|
||||
} catch {
|
||||
/* best effort */
|
||||
}
|
||||
if (!this.enabled) this.stop();
|
||||
else if (document.body.classList.contains('cockpit-mode')) this.start();
|
||||
this.emitEnabledState();
|
||||
}
|
||||
|
||||
cameraPoint() {
|
||||
if (this.engine.isDestroyed?.()) return null;
|
||||
let place;
|
||||
try {
|
||||
place = this.engine.camera.pose().cartographic;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
const latitude = geo.math.toDegrees(place?.latitude);
|
||||
const longitude = geo.math.toDegrees(place?.longitude);
|
||||
if (![latitude, longitude].every(Number.isFinite)) return null;
|
||||
return {
|
||||
latitude,
|
||||
longitude,
|
||||
altitudeM: Math.max(0, Number(place.height) || 0),
|
||||
};
|
||||
}
|
||||
|
||||
/** Shows the cloud cover for `strength`; null gives the engine its own. */
|
||||
showCoverage(coverage) {
|
||||
if (coverage === this.coverage || this.engine.isDestroyed?.()) return;
|
||||
this.coverage = coverage;
|
||||
this.engine.render.setCloudCover({ coverage });
|
||||
this.engine.render.request('cockpit-clouds');
|
||||
}
|
||||
|
||||
applyWeather(weather, altitudeM) {
|
||||
this.targetStrength = cockpitCloudStrength(weather, altitudeM);
|
||||
this.showCoverage(cockpitCloudCoverage(this.targetStrength));
|
||||
}
|
||||
|
||||
start() {
|
||||
if (this.destroyed || !this.enabled || this.suspended || this.timer) return;
|
||||
// The first observation at once, then the refresh rule every second
|
||||
// (time or movement), as the Cesium pass checks it.
|
||||
this.timer = window.setInterval(() => this.check(), 1000);
|
||||
this.check();
|
||||
}
|
||||
|
||||
check() {
|
||||
if (
|
||||
this.destroyed ||
|
||||
!this.enabled ||
|
||||
this.suspended ||
|
||||
!document.body.classList.contains('cockpit-mode')
|
||||
)
|
||||
return;
|
||||
const point = this.cameraPoint();
|
||||
if (!point || this.feed.pending) return;
|
||||
if (!this.feed.due(point)) {
|
||||
this.applyWeather(this.feed.weather, point.altitudeM);
|
||||
return;
|
||||
}
|
||||
void this.feed.fetch(point, {
|
||||
onWeather: (weather) => {
|
||||
if (this.timer) this.applyWeather(weather, point.altitudeM);
|
||||
},
|
||||
onError: () => {
|
||||
this.targetStrength = 0;
|
||||
if (this.timer) this.showCoverage(0);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
stop() {
|
||||
if (this.timer) window.clearInterval(this.timer);
|
||||
this.timer = null;
|
||||
this.feed.cancel();
|
||||
this.targetStrength = 0;
|
||||
this.showCoverage(null);
|
||||
}
|
||||
|
||||
getSnapshot() {
|
||||
return {
|
||||
enabled: this.enabled,
|
||||
active: this.timer !== null && this.coverage !== null,
|
||||
strength: this.targetStrength,
|
||||
coverage: this.coverage,
|
||||
weather: this.feed.weather ? { ...this.feed.weather } : null,
|
||||
renderSize: null,
|
||||
sourceStatus: this.feed.sourceStatus,
|
||||
};
|
||||
}
|
||||
|
||||
/** Pauses the weather checks while the document is hidden. */
|
||||
setSuspended(suspended) {
|
||||
this.suspended = Boolean(suspended);
|
||||
if (this.suspended) {
|
||||
if (this.timer) window.clearInterval(this.timer);
|
||||
this.timer = null;
|
||||
this.feed.cancel();
|
||||
} else if (document.body.classList.contains('cockpit-mode')) {
|
||||
this.start();
|
||||
}
|
||||
}
|
||||
|
||||
destroy() {
|
||||
if (this.destroyed) return;
|
||||
this.stop();
|
||||
this.destroyed = true;
|
||||
window.removeEventListener('gev:cockpit-mode-changed', this.onCockpitMode);
|
||||
window.removeEventListener(
|
||||
'gev:cockpit-weather-toggle',
|
||||
this.onEnabledChange,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/** The cockpit weather clouds on a globe engine with the cloudCover capability. */
|
||||
export function initEngineCockpitClouds(engine, options) {
|
||||
return new EngineCockpitCloudsController(engine, options);
|
||||
}
|
||||
Reference in new issue
Block a user