From 3490db362d4d1e56a3f471867cacd9c036cb7ee0 Mon Sep 17 00:00:00 2001 From: piexel Date: Wed, 25 Nov 2026 22:49:01 +0100 Subject: [PATCH] Keep panes smooth while they are resized - Stretch the 3D view's last frame while its pane is resized and draw it anew once the size holds - Count the map's shares by label so the census no longer stalls panning and resizing --- studio/src/renderer/src/map/census.ts | 57 +++++++++++-------- .../src/renderer/src/studio/SceneViewport.tsx | 42 +++++++++++--- 2 files changed, 66 insertions(+), 33 deletions(-) diff --git a/studio/src/renderer/src/map/census.ts b/studio/src/renderer/src/map/census.ts index 8d53821c0..809103b9a 100644 --- a/studio/src/renderer/src/map/census.ts +++ b/studio/src/renderer/src/map/census.ts @@ -35,25 +35,24 @@ export function countShares(tiles: PlacedTile[], area: WorldArea): Share[] { const totalPixels = tiles.reduce((sum, placed) => sum + placed.tile.size * placed.tile.size, 0) const stride = Math.max(1, Math.ceil(Math.sqrt(totalPixels / MAXIMUM_COUNTED_PIXELS))) let counted = 0 - for (const { tile, worldX, worldZ } of tiles) { - const scale = tile.blocksPerPixel - const firstX = Math.max(0, Math.ceil((area.minimumX - worldX) / scale)) - const endX = Math.min(tile.size, Math.ceil((area.maximumX - worldX) / scale)) - const firstZ = Math.max(0, Math.ceil((area.minimumZ - worldZ) / scale)) - const endZ = Math.min(tile.size, Math.ceil((area.maximumZ - worldZ) / scale)) - for (let pixelZ = firstZ; pixelZ < endZ; pixelZ += stride) { - for (let pixelX = firstX; pixelX < endX; pixelX += stride) { - const subject = subjectAt(tile, gridIndex(tile, pixelX, pixelZ)) - if (subject === null || isDryGround(subject.subject)) { - continue - } - const id = `${subject.subject.kind}:${subject.subject.key}` - const entry = counts.get(id) ?? { ...subject, count: 0 } - entry.count++ - counts.set(id, entry) - counted++ + for (const placed of tiles) { + const { tile } = placed + const labelCounts = countLabelsInArea(placed, area, stride) + labelCounts.forEach((count, index) => { + if (count === 0) { + return } - } + const label = tile.labels[index] + const subject = { kind: label.kind, key: label.key, name: label.name } + if (isDryGround(subject)) { + return + } + const id = `${subject.kind}:${subject.key}` + const entry = counts.get(id) ?? { subject, color: label.color, count: 0 } + entry.count += count + counts.set(id, entry) + counted += count + }) } return [...counts.values()] .map((entry) => ({ subject: entry.subject, color: entry.color, fraction: entry.count / Math.max(1, counted) })) @@ -103,11 +102,21 @@ function isDryGround(subject: Highlight): boolean { return subject.kind === 'WATER' && subject.key === 'dry' } -function subjectAt(tile: MapTile, cell: number): { subject: Highlight; color: number } | null { - const index = tile.subjects[cell] - if (index === NO_LABEL) { - return null +// Counted by label index first: building a subject per sampled pixel made the count too slow to run while the map moves. +function countLabelsInArea({ tile, worldX, worldZ }: PlacedTile, area: WorldArea, stride: number): Uint32Array { + const counts = new Uint32Array(tile.labels.length) + const scale = tile.blocksPerPixel + const firstX = Math.max(0, Math.ceil((area.minimumX - worldX) / scale)) + const endX = Math.min(tile.size, Math.ceil((area.maximumX - worldX) / scale)) + const firstZ = Math.max(0, Math.ceil((area.minimumZ - worldZ) / scale)) + const endZ = Math.min(tile.size, Math.ceil((area.maximumZ - worldZ) / scale)) + for (let pixelZ = firstZ; pixelZ < endZ; pixelZ += stride) { + for (let pixelX = firstX; pixelX < endX; pixelX += stride) { + const index = tile.subjects[gridIndex(tile, pixelX, pixelZ)] + if (index !== NO_LABEL) { + counts[index]++ + } + } } - const label = tile.labels[index] - return { subject: { kind: label.kind, key: label.key, name: label.name }, color: label.color } + return counts } diff --git a/studio/src/renderer/src/studio/SceneViewport.tsx b/studio/src/renderer/src/studio/SceneViewport.tsx index df94c201d..293b725c7 100644 --- a/studio/src/renderer/src/studio/SceneViewport.tsx +++ b/studio/src/renderer/src/studio/SceneViewport.tsx @@ -15,6 +15,9 @@ const FIELD_OF_VIEW_DEGREES = 45 const VIEW_DIRECTION = new Vector3(1, 0.8, 1.2).normalize() const FRAMING_MARGIN = 1.15 const NEAREST_CLIP = 0.05 +// Sizing the drawing buffer anew costs more than a frame, and so can drawing a whole world again, so while a pane is +// resized the last frame is stretched to fit and the view is only drawn anew once the size has held this long. +const BUFFER_FIT_DELAY_MILLIS = 150 // The near plane follows the orbit distance: a fixed tiny one leaves too little depth precision far away, so surfaces a block apart flicker. const NEAR_CLIP_PER_ORBIT_DISTANCE = 0.01 // The far plane keeps up with zooming out, so ground reaching to the horizon stays in view. @@ -218,6 +221,8 @@ class Stage { private framed = false private framedFar = this.camera.far private frameRequested = false + private bufferFitted = false + private bufferFitTimer: number | undefined private pickListener: ((picked: PickedSurface) => void) | null = null private pressedAt: { x: number; y: number } | null = null private spotListener: ((asked: AskedSpot) => void) | null = null @@ -227,6 +232,7 @@ class Stage { this.renderer.outputColorSpace = LinearSRGBColorSpace this.renderer.setPixelRatio(window.devicePixelRatio) this.renderer.setClearAlpha(0) + this.renderer.domElement.style.cssText = 'display:block;width:100%;height:100%' host.appendChild(this.renderer.domElement) this.controls = new OrbitControls(this.camera, this.renderer.domElement) this.controls.zoomToCursor = true @@ -245,9 +251,9 @@ class Stage { this.notifyMoved() this.requestFrame() }) - this.resizes = new ResizeObserver(() => this.fitToHost()) + this.resizes = new ResizeObserver(() => this.stretchToHost()) this.resizes.observe(host) - this.fitToHost() + this.fitBufferToHost() } show(content: ViewportContent | null): void { @@ -417,6 +423,7 @@ class Stage { dispose(): void { this.removeShown() this.resizes.disconnect() + window.clearTimeout(this.bufferFitTimer) this.controls.dispose() this.renderer.dispose() // Chromium keeps only so many WebGL contexts, so a closed tab's goes now instead of whenever it is collected. @@ -424,15 +431,32 @@ class Stage { this.renderer.domElement.remove() } - private fitToHost(): void { - const width = Math.max(1, this.host.clientWidth) - const height = Math.max(1, this.host.clientHeight) - this.renderer.setSize(width, height) + private stretchToHost(): void { + if (!this.bufferFitted) { + this.fitBufferToHost() + return + } + window.clearTimeout(this.bufferFitTimer) + this.bufferFitTimer = window.setTimeout(() => this.fitBufferToHost(), BUFFER_FIT_DELAY_MILLIS) + } + + private fitBufferToHost(): void { + const width = this.host.clientWidth + const height = this.host.clientHeight + if (width === 0 || height === 0) { + return + } + this.renderer.setSize(width, height, false) + this.bufferFitted = true + this.fitCameraTo(width, height) + this.notifyMoved() + // Sizing the canvas wipes it, so waiting for the next frame would show it blank. + this.renderer.render(this.scene, this.camera) + } + + private fitCameraTo(width: number, height: number): void { this.camera.aspect = width / height this.camera.updateProjectionMatrix() - this.notifyMoved() - // Sizing the canvas wipes it, so waiting for the next frame would show it blank while a pane is resized. - this.renderer.render(this.scene, this.camera) } private requestFrame(): void {