fix: keep particle density stable across zoom
Some checks failed
validate-scripts / validate-scripts (push) Has been cancelled

This commit is contained in:
OpenAI Codex
2026-09-05 12:23:15 +08:00
parent d9a9c8ed57
commit f8e715d080
8 changed files with 178 additions and 6 deletions

View File

@@ -60,6 +60,8 @@ constexpr float MaxSimulationDeltaSeconds = 2400.0f;
constexpr float ParticleLifecycleRealSeconds = 12.0f;
constexpr float TrailFadeReferenceFramesPerSecond = 60.0f;
constexpr auto TrailCameraStableDelay = std::chrono::milliseconds(150);
constexpr float ViewportRespawnMargin = 0.15f;
constexpr float MinimumRespawnBoundsExtent = 0.000001f;
std::atomic_uint64_t nextGpuGeneration{0};
std::atomic_uint64_t nextMaskGeneration{0};
@@ -191,6 +193,65 @@ std::array<float, 4> fieldBounds(const particle::ParticleFieldSnapshot& field) {
static_cast<float>(mercatorY(field.south))};
}
struct RespawnDomain {
std::array<float, 4> bounds;
bool fallback = true;
};
bool validMercatorBounds(const std::array<float, 4>& bounds) {
return std::isfinite(bounds[0]) && std::isfinite(bounds[1]) && std::isfinite(bounds[2]) &&
std::isfinite(bounds[3]) && bounds[2] - bounds[0] >= MinimumRespawnBoundsExtent &&
bounds[3] - bounds[1] >= MinimumRespawnBoundsExtent;
}
RespawnDomain viewportRespawnDomain(const TransformState& transformState,
const Size viewport,
const particle::ParticleFieldSnapshot& field) {
const auto fullFieldBounds = fieldBounds(field);
if (!validMercatorBounds(fullFieldBounds)) {
return {fullFieldBounds, true};
}
const std::array<ScreenCoordinate, 4> corners = {
ScreenCoordinate{0.0, 0.0},
ScreenCoordinate{static_cast<double>(viewport.width), 0.0},
ScreenCoordinate{0.0, static_cast<double>(viewport.height)},
ScreenCoordinate{static_cast<double>(viewport.width), static_cast<double>(viewport.height)},
};
float minX = std::numeric_limits<float>::infinity();
float minY = std::numeric_limits<float>::infinity();
float maxX = -std::numeric_limits<float>::infinity();
float maxY = -std::numeric_limits<float>::infinity();
for (const auto& corner : corners) {
const auto latLng = transformState.screenCoordinateToLatLng(corner, LatLng::Unwrapped);
const auto x = static_cast<float>((latLng.longitude() + 180.0) / 360.0);
const auto y = static_cast<float>(mercatorY(latLng.latitude()));
minX = std::min(minX, x);
minY = std::min(minY, y);
maxX = std::max(maxX, x);
maxY = std::max(maxY, y);
}
const auto width = maxX - minX;
const auto height = maxY - minY;
if (!std::isfinite(width) || !std::isfinite(height) || width < MinimumRespawnBoundsExtent ||
height < MinimumRespawnBoundsExtent) {
return {fullFieldBounds, true};
}
minX -= width * ViewportRespawnMargin;
maxX += width * ViewportRespawnMargin;
minY -= height * ViewportRespawnMargin;
maxY += height * ViewportRespawnMargin;
const std::array<float, 4> intersection = {
std::max(fullFieldBounds[0], minX),
std::max(fullFieldBounds[1], minY),
std::min(fullFieldBounds[2], maxX),
std::min(fullFieldBounds[3], maxY),
};
return validMercatorBounds(intersection) ? RespawnDomain{intersection, false}
: RespawnDomain{fullFieldBounds, true};
}
// VALIDATION ONLY: deterministic global fixture with a diagonal coast. Alpha
// 255 means OCEAN, alpha 0 means LAND. It is intentionally tiny and is not a
// production coastline source.
@@ -254,6 +315,8 @@ public:
bool antimeridianLogged = false;
bool trailCameraKnown = false;
bool trailCameraInvalidated = false;
bool viewportDensityActive = false;
bool viewportDensityLogged = false;
uint64_t lastCameraLogFrame = 0;
std::chrono::steady_clock::time_point lastPlaybackHeartbeat{};
std::chrono::steady_clock::time_point lastSimulationTick{};
@@ -286,6 +349,7 @@ public:
double trailCameraZoom = 0.0;
double trailCameraBearing = 0.0;
double trailCameraPitch = 0.0;
std::array<float, 4> respawnBounds{};
std::array<RenderTargetPtr, 2> stateTargets;
std::array<RenderTargetPtr, 2> trailTargets;
@@ -526,7 +590,11 @@ public:
resources->trailOpacity,
resources->formalDisplay ? resources->trailFade : 0.965f},
.fieldBounds = fieldBounds(resources->field),
.fieldScale = {resources->fieldScale, resources->nextFieldScale, 0.0f, 0.0f},
.respawnBounds = resources->respawnBounds,
.fieldScale = {resources->fieldScale,
resources->nextFieldScale,
resources->viewportDensityActive ? 1.0f : 0.0f,
0.0f},
.matrix = util::cast<float>(parameters.matrixForTile({worldWrap, {0, 0, 0}})),
};
drawable.mutableUniformBuffers().createOrUpdate(shaders::idParticleGpuValidationUBO, &ubo, parameters.context);
@@ -1407,6 +1475,7 @@ void RenderRasterParticleLayer::update(gfx::ShaderRegistry& shaders,
resources->trailRead = 0;
resources->trailSeeded = false;
resources->trailCameraInvalidated = true;
resources->viewportDensityLogged = false;
resources->lastTrailCameraChange = cameraNow;
Log::Info(Event::Render,
"ParticleTrail: event=VIEWPORT_RESIZE old=" + std::to_string(previousSize.width) + "x" +
@@ -1420,6 +1489,7 @@ void RenderRasterParticleLayer::update(gfx::ShaderRegistry& shaders,
: "pan";
resources->trailCameraInvalidated = true;
resources->trailSeeded = false;
resources->viewportDensityLogged = false;
Log::Info(Event::Render,
"ParticleTrail: event=CAMERA_INVALIDATE reason=" + std::string(reason) +
" statePreserved=true fieldResubmit=false timelineReset=false");
@@ -1437,6 +1507,28 @@ void RenderRasterParticleLayer::update(gfx::ShaderRegistry& shaders,
resources->trailCameraZoom = transformState.getZoom();
resources->trailCameraBearing = transformState.getBearing();
resources->trailCameraPitch = transformState.getPitch();
const auto respawnDomain = viewportRespawnDomain(transformState, viewport, resources->field);
resources->viewportDensityActive = resources->formalDisplay && !resources->trailCameraInvalidated &&
!respawnDomain.fallback;
// During a camera gesture, State continues normally but all ordinary
// lifecycle respawns stay in the full Field. The viewport domain is only
// enabled after the existing stable-camera debounce has completed.
resources->respawnBounds = resources->viewportDensityActive ? respawnDomain.bounds : fieldBounds(resources->field);
if (resources->formalDisplay && !resources->trailCameraInvalidated && !resources->viewportDensityLogged) {
const auto& bounds = resources->respawnBounds;
const auto& currentFieldBounds = fieldBounds(resources->field);
Log::Info(Event::Render,
"ParticleDensity: zoom=" + std::to_string(transformState.getZoom()) +
" mode=" + std::string(respawnDomain.fallback ? "FIELD_FALLBACK" : "VIEWPORT_RESPAWN") +
" cameraStable=true margin=" + std::to_string(ViewportRespawnMargin) + " respawnBounds=[" +
std::to_string(bounds[0]) + "," + std::to_string(bounds[1]) + "," +
std::to_string(bounds[2]) + "," + std::to_string(bounds[3]) + "] fieldBounds=[" +
std::to_string(currentFieldBounds[0]) + "," + std::to_string(currentFieldBounds[1]) + "," +
std::to_string(currentFieldBounds[2]) + "," + std::to_string(currentFieldBounds[3]) +
"] fallback=" + (respawnDomain.fallback ? "true" : "false") +
" configuredSlots=" + std::to_string(resources->particleCount));
resources->viewportDensityLogged = true;
}
const auto ensureTrailTargets = [&] {
if (resources->trailTargets[0] && resources->trailTargets[1] && resources->trailSize == viewport) {
return true;