@umicat/three-sdk 1.33.0 → 1.34.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,28 @@
1
+ import * as THREE from 'three';
2
+ /**
3
+ * The pixel ratio to render at, capped by AREA rather than by ratio.
4
+ *
5
+ * Every 3D game here copied `setPixelRatio(Math.min(devicePixelRatio, 2))` out
6
+ * of the template, and a ratio cap bounds nothing that matters: what varies
7
+ * between devices is the viewport's AREA. Measured on an iPhone Duo — closed
8
+ * display 466x678 CSS, open display 951x669, both dpr 3 — the same game goes
9
+ * from 2.84 to 5.72 megapixels of canvas by being unfolded. A Phaser game
10
+ * (Catopia) had its web process killed ~700ms into the load on the open
11
+ * display and was fine on the closed one; a 3D game draws more per pixel than
12
+ * that, not less.
13
+ *
14
+ * Below the cap this returns exactly what the old expression did, so no
15
+ * ordinary phone changes: a 3x 1080p phone is 2.4 megapixels at ratio 2.
16
+ *
17
+ * Never returns less than 1 — a backing store smaller than the CSS box is
18
+ * blurrier than doing nothing.
19
+ */
20
+ export declare function fitPixelRatio(width?: number, height?: number): number;
21
+ /**
22
+ * Apply it, and keep applying it.
23
+ *
24
+ * The ratio is not a constant on a folding phone: unfolding changes the
25
+ * viewport's area under a renderer that was configured once at boot. Games
26
+ * already re-`setSize` on resize; this is the half they were missing.
27
+ */
28
+ export declare function trackPixelRatio(renderer: THREE.WebGLRenderer): () => void;
@@ -0,0 +1,54 @@
1
+ /**
2
+ * Ceiling on the BACKING STORE, in device pixels.
3
+ *
4
+ * Kept the same number as `@umicat/phaser-sdk` and both app player hosts, on
5
+ * purpose: a game should not get a different resolution for being written
6
+ * against a different engine.
7
+ */
8
+ const MAX_PIXELS = 3500000;
9
+ /** Never above this, however small the viewport. Matches the template's. */
10
+ const RATIO_CAP = 2;
11
+ /**
12
+ * The pixel ratio to render at, capped by AREA rather than by ratio.
13
+ *
14
+ * Every 3D game here copied `setPixelRatio(Math.min(devicePixelRatio, 2))` out
15
+ * of the template, and a ratio cap bounds nothing that matters: what varies
16
+ * between devices is the viewport's AREA. Measured on an iPhone Duo — closed
17
+ * display 466x678 CSS, open display 951x669, both dpr 3 — the same game goes
18
+ * from 2.84 to 5.72 megapixels of canvas by being unfolded. A Phaser game
19
+ * (Catopia) had its web process killed ~700ms into the load on the open
20
+ * display and was fine on the closed one; a 3D game draws more per pixel than
21
+ * that, not less.
22
+ *
23
+ * Below the cap this returns exactly what the old expression did, so no
24
+ * ordinary phone changes: a 3x 1080p phone is 2.4 megapixels at ratio 2.
25
+ *
26
+ * Never returns less than 1 — a backing store smaller than the CSS box is
27
+ * blurrier than doing nothing.
28
+ */
29
+ export function fitPixelRatio(width = typeof window === 'undefined' ? 0 : window.innerWidth, height = typeof window === 'undefined' ? 0 : window.innerHeight) {
30
+ const dpr = typeof window === 'undefined' ? 1 : window.devicePixelRatio || 1;
31
+ if (width <= 0 || height <= 0)
32
+ return Math.min(dpr, RATIO_CAP);
33
+ const byArea = Math.sqrt(MAX_PIXELS / (width * height));
34
+ return Math.max(1, Math.min(dpr, RATIO_CAP, byArea));
35
+ }
36
+ /**
37
+ * Apply it, and keep applying it.
38
+ *
39
+ * The ratio is not a constant on a folding phone: unfolding changes the
40
+ * viewport's area under a renderer that was configured once at boot. Games
41
+ * already re-`setSize` on resize; this is the half they were missing.
42
+ */
43
+ export function trackPixelRatio(renderer) {
44
+ const apply = () => { renderer.setPixelRatio(fitPixelRatio()); };
45
+ apply();
46
+ if (typeof window === 'undefined')
47
+ return () => { };
48
+ window.addEventListener('resize', apply);
49
+ window.addEventListener('orientationchange', apply);
50
+ return () => {
51
+ window.removeEventListener('resize', apply);
52
+ window.removeEventListener('orientationchange', apply);
53
+ };
54
+ }
package/dist/index.d.ts CHANGED
@@ -6,6 +6,7 @@ export { CharacterController3D } from './CharacterController3D.js';
6
6
  export type { CharacterOptions, CharacterState, CharacterInput } from './CharacterController3D.js';
7
7
  export { CharacterAnimator } from './CharacterAnimator.js';
8
8
  export type { ClipMap, CharacterAnimatorOptions } from './CharacterAnimator.js';
9
+ export { fitPixelRatio, trackPixelRatio } from './PixelRatio.js';
9
10
  export { Input3D } from './Input3D.js';
10
11
  export type { Input3DOptions, Input3DAction } from './Input3D.js';
11
12
  export { attachToSocket, findBone, boneNames } from './Sockets.js';
package/dist/index.js CHANGED
@@ -8,6 +8,7 @@ export * from './scene3d.js';
8
8
  export { loadScene3D, loadModelAsset, heightfieldGeometry, makeHeightfield, makeWaterSurface, HEIGHTMAP_LIBRARY, HEIGHTMAP_DEFAULT_SIZE } from './SceneLoader3D.js';
9
9
  export { CharacterController3D } from './CharacterController3D.js';
10
10
  export { CharacterAnimator } from './CharacterAnimator.js';
11
+ export { fitPixelRatio, trackPixelRatio } from './PixelRatio.js';
11
12
  export { Input3D } from './Input3D.js';
12
13
  export { attachToSocket, findBone, boneNames } from './Sockets.js';
13
14
  export { flashTint, updateTints, isTinted } from './Tint.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@umicat/three-sdk",
3
- "version": "1.33.0",
3
+ "version": "1.34.0",
4
4
  "description": "Three.js runtime for Umicat games: the scene3d design format, its loader with physics, a kinematic character controller, and the Umicat platform via @umicat/platform-sdk.",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",