← Commit history
v0.12.2
4 files changed
+93−20
package.json+42−6@@ -3,7 +3,47 @@ "version": "0.12.2", "description": "The Babylon 9.5 engine behind every component page's 3D tab on wiki.adom.inc. Versioned ESM bundle with GLB loading, view cube, layers toolbar, ground shadows, and Z-up CAD framing.", "type": "app",+ "main": "./dist/index.js",+ "module": "./dist/index.js",+ "types": "./dist/index.d.ts",+ "exports": {+ ".": {+ "import": "./dist/index.js",+ "types": "./dist/index.d.ts"+ }+ },+ "files": [+ "dist",+ "dist-standalone",+ "src",+ "standalone.ts"+ ],+ "scripts": {+ "build": "vite build",+ "build:standalone": "vite build --config vite.standalone.config.mts",+ "dev": "vite build --watch",+ "prepare": "npm run build",+ "test:sandbox": "node test/wiki-sandbox.test.mjs",+ "test:orbit": "node test/orbit.test.mjs",+ "test": "node test/wiki-sandbox.test.mjs && node test/orbit.test.mjs && node test/viewcube.test.mjs && node test/help.test.mjs && node test/lights.test.mjs && node test/replace.test.mjs && node test/layers.test.mjs && node test/bundle.test.mjs",+ "test:viewcube": "node test/viewcube.test.mjs",+ "test:help": "node test/help.test.mjs",+ "test:lights": "node test/lights.test.mjs",+ "test:replace": "node test/replace.test.mjs",+ "test:layers": "node test/layers.test.mjs",+ "test:bundle": "node test/bundle.test.mjs"+ }, "dependencies": {},+ "devDependencies": {+ "@sveltejs/vite-plugin-svelte": "^3.1.2",+ "svelte-check": "^3.8.6",+ "terser": "^5.46.2",+ "typescript": "^5.5.4",+ "vite": "^5.4.1"+ },+ "peerDependencies": {+ "svelte": "^4.0.0 || ^5.0.0"+ }, "slug": "adom-3d-viewer", "license": "MIT", "tags": [@@ -37,9 +77,5 @@ "prompt": "Download the 0.4.3 viewer bundle so my tool vendors a fixed build" } ],- "title": "Adom 3D Viewer",- "scripts": {- "install": "./install.sh",- "uninstall": "./uninstall.sh"- }-}+ "title": "Adom 3D Viewer"+}
src/ThreeDViewer.svelte+23−9@@ -837,8 +837,10 @@ const engine = AdomStatics.engine!; const scene = AdomStatics.scene!; - canvas.width = container.clientWidth;- canvas.height = container.clientHeight;+ // The engine sizes the backing store from the canvas's CSS box at the+ // display's pixel density; setting canvas.width by hand would pin it+ // to CSS pixels.+ engine.resize(); const loadingScreen = new ResizableLoadingScreen(canvas, 'Loading...'); engine.loadingScreen = loadingScreen;@@ -929,16 +931,27 @@ const eng = AdomStatics.engine; const sc = AdomStatics.scene; if (canvas && eng) {- const cont = canvas.parentElement;- if (cont) {- canvas.width = cont.clientWidth;- canvas.height = cont.clientHeight;- eng.resize();- sc?.render();- }+ eng.resize();+ sc?.render(); } }); resizeObserver.observe(container);+ watchDeviceRatio();+ }++ // A ResizeObserver does not fire when only the pixel density changes+ // (the window dragged to a Retina screen, browser zoom), so watch it.+ let deviceRatioQuery: MediaQueryList | null = null;+ function onDeviceRatioChange() {+ AdomStatics.syncDeviceRatio();+ AdomStatics.scene?.render();+ watchDeviceRatio();+ }+ function watchDeviceRatio() {+ deviceRatioQuery?.removeEventListener('change', onDeviceRatioChange);+ if (destroyed || typeof window.matchMedia !== 'function') return;+ deviceRatioQuery = window.matchMedia(`(resolution: ${window.devicePixelRatio || 1}dppx)`);+ deviceRatioQuery.addEventListener('change', onDeviceRatioChange); } /**@@ -1757,6 +1770,7 @@ canvas?.removeEventListener('pointerdown', onCanvasPointerDown); if (pivotHintTimer) clearTimeout(pivotHintTimer); resizeObserver?.disconnect();+ deviceRatioQuery?.removeEventListener('change', onDeviceRatioChange); AdomStatics.engine?.stopRenderLoop(); AdomStatics.dispose(); });
src/lib/adom-babylon/3DViewer/ViewCubeBuilder.ts+6−4@@ -258,8 +258,10 @@ export class ViewCubeBuilder { // cube stays readable at small canvas sizes. The center is kept // at (0.925, 0.925) — same as the default — and only shifts when // the viewport would exceed the canvas edge.- const canvasW = engine.getRenderWidth()- const canvasH = engine.getRenderHeight()+ // CSS pixels, the unit of scene.pointerX and of minVpPx: the+ // render size is that times the display's pixel density.+ const canvasW = engine.getRenderWidth() * engine.getHardwareScalingLevel()+ const canvasH = engine.getRenderHeight() * engine.getHardwareScalingLevel() const minVpPx = 120 const defaultFrac = 0.15 const vpNormW = Math.min(Math.max(canvasW * defaultFrac, minVpPx) / canvasW, 0.3)@@ -720,8 +722,8 @@ export class ViewCubeBuilder { if ( // camera2.viewport.x < evt.offsetX / engine.getRenderWidth() && // 1 - endMousePosY < evt.offsetY / engine.getRenderHeight() &&- xOffset / engine.getRenderWidth() < endMousePos &&- yOffset / engine.getRenderHeight() < 1 - camera2.viewport.y+ xOffset / (engine.getRenderWidth() * engine.getHardwareScalingLevel()) < endMousePos &&+ yOffset / (engine.getRenderHeight() * engine.getHardwareScalingLevel()) < 1 - camera2.viewport.y ) { const id = cubeRayCollision(pickResult) if (id && id !== ViewCubeBuilder.prevPick) {
src/lib/adom-babylon/AdomStatics.ts+22−1@@ -7,6 +7,9 @@ import type { SpotLight } from '@babylonjs/core/Lights/spotLight'; import type { HemisphericLight } from '@babylonjs/core/Lights/hemisphericLight'; import { RenderTargetTexture } from '@babylonjs/core/Materials/Textures/renderTargetTexture'; +// The highest display density the scene renders at (see init).+const MAX_DEVICE_RATIO = 2;+ export class AdomStatics { public static engine: Nullable<Engine> = null; public static scene: Nullable<Scene> = null;@@ -37,11 +40,16 @@ export class AdomStatics { // 9). Babylon's own CreateScreenshotUsingRenderTarget re-renders into // an RTT and does not need this, but consumers reaching for the canvas // directly do, and the cost is one retained buffer.+ // adaptToDeviceRatio: render at the display's pixel density, not at+ // CSS pixels, or a 2x (Retina) screen shows the scene upscaled and+ // blurry next to the page's text. Capped at 2x: a 3x phone would pay+ // 9x the fill of 1x for detail nobody can see at that size. this.engine = new Engine(canvas, true, { useHighPrecisionFloats: true, preserveDrawingBuffer: true, stencil: true,- });+ limitDeviceRatio: MAX_DEVICE_RATIO,+ }, true); // Decode images (environment mip levels, GLB-embedded textures) as // ImageBitmaps instead of <img src="blob:...">. Babylon's default on // WebGL builds a blob URL per image, and a sandboxed frame whose CSP@@ -61,6 +69,19 @@ export class AdomStatics { this.scene.activeCamera = this.camera; } + /**+ * Re-read the display's pixel density (the window moved to a screen with+ * a different one, or the page zoom changed) and resize to it.+ */+ public static syncDeviceRatio(): void {+ if (!this.engine) return;+ const ratio = Math.min(window.devicePixelRatio || 1, MAX_DEVICE_RATIO);+ if (Math.abs(this.engine.getHardwareScalingLevel() - 1 / ratio) > 1e-6) {+ this.engine.setHardwareScalingLevel(1 / ratio);+ }+ this.engine.resize();+ }+ public static dispose(): void { if (this.scene) { this.scene.dispose();