Skip to content

Commit 5604ec8

Browse files
authored
remove DPR cap (#4896)
1 parent 291c9d1 commit 5604ec8

File tree

5 files changed

+10
-104
lines changed

5 files changed

+10
-104
lines changed

packages/model-viewer/src/test/three-components/Renderer-spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import {$intersectionObserver, $isElementInViewport, $onResize, $renderer, $scen
2121
import {ModelViewerElement} from '../../model-viewer.js';
2222
import {ModelScene} from '../../three-components/ModelScene.js';
2323
import {Renderer} from '../../three-components/Renderer.js';
24-
import {resolveDpr, waitForEvent} from '../../utilities.js';
24+
import {waitForEvent} from '../../utilities.js';
2525
import {assetPath} from '../helpers.js';
2626

2727
let externalCamera: Camera;
@@ -148,7 +148,7 @@ suite('Renderer with two scenes', () => {
148148
const height = 400;
149149
externalElement[$onResize]({width, height});
150150

151-
const dpr = resolveDpr();
151+
const dpr = window.devicePixelRatio;
152152
expect(externalWidth).to.be.eq(width * dpr);
153153
expect(externalHeight).to.be.eq(height * dpr);
154154
});

packages/model-viewer/src/test/utilities-spec.ts

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
import {expect} from '@esm-bundle/chai';
1717

18-
import {clamp, deserializeUrl, resolveDpr, step} from '../utilities.js';
18+
import {clamp, deserializeUrl, step} from '../utilities.js';
1919

2020
suite('utils', () => {
2121
suite('deserializeUrl', () => {
@@ -34,31 +34,6 @@ suite('utils', () => {
3434
});
3535
});
3636

37-
suite('resolveDpr', () => {
38-
suite('when <meta name="viewport"> is present', () => {
39-
test('resolves the device pixel ratio', () => {
40-
const resolvedDpr = resolveDpr();
41-
// NOTE(cdata): The main test frame is assumed to have a
42-
// <meta name="viewport">. If this changes, it is probably by
43-
// accident.
44-
const actualDpr = self.devicePixelRatio;
45-
46-
expect(resolvedDpr).to.be.equal(actualDpr);
47-
});
48-
});
49-
50-
suite('when <meta name="viewport"> is not present', () => {
51-
test.skip(
52-
'caps the device pixel ratio to 1',
53-
() => {
54-
// There is not a good way to test this given the current
55-
// factoring and bundling constraints
56-
// TODO:
57-
// https://github.com/google/model-viewer/issues/262
58-
});
59-
});
60-
});
61-
6237
suite('step', () => {
6338
test('returns 0 for values below edge', () => {
6439
expect(step(0.5, 0.1)).to.be.equal(0);

packages/model-viewer/src/three-components/ModelScene.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import ModelViewerElementBase, {$renderer, EffectComposerInterface, RendererInte
2323
import {ModelViewerElement} from '../model-viewer.js';
2424
import {normalizeUnit} from '../styles/conversions.js';
2525
import {NumberNode, parseExpressions} from '../styles/parsers.js';
26-
import {resolveDpr} from '../utilities.js';
2726

2827
import {Damper, SETTLING_TIME} from './Damper.js';
2928
import {ModelViewerGLTFInstance} from './gltf-instance/ModelViewerGLTFInstance.js';
@@ -340,7 +339,7 @@ export class ModelScene extends Scene {
340339
this.aspect = this.width / this.height;
341340

342341
if (this.externalRenderer != null) {
343-
const dpr = resolveDpr();
342+
const dpr = window.devicePixelRatio;
344343
this.externalRenderer.resize(width * dpr, height * dpr);
345344
}
346345

packages/model-viewer/src/three-components/Renderer.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {ACESFilmicToneMapping, Event, EventDispatcher, NeutralToneMapping, Vecto
1818
import {$updateEnvironment} from '../features/environment.js';
1919
import {ModelViewerGlobalConfig} from '../features/loading.js';
2020
import ModelViewerElementBase, {$canvas, $tick, $updateSize} from '../model-viewer-base.js';
21-
import {clamp, isDebugMode, resolveDpr} from '../utilities.js';
21+
import {clamp, isDebugMode} from '../utilities.js';
2222

2323
import {ARRenderer} from './ARRenderer.js';
2424
import {CachingGLTFLoader} from './CachingGLTFLoader.js';
@@ -65,9 +65,9 @@ export class Renderer extends
6565
static get singleton() {
6666
if (!this._singleton) {
6767
this._singleton = new Renderer({
68-
powerPreference:
69-
(((self as any).ModelViewerElement || {}) as ModelViewerGlobalConfig)
70-
.powerPreference ||
68+
powerPreference: (((self as any).ModelViewerElement || {}) as
69+
ModelViewerGlobalConfig)
70+
.powerPreference ||
7171
DEFAULT_POWER_PREFERENCE,
7272
debug: isDebugMode()
7373
});
@@ -134,7 +134,7 @@ export class Renderer extends
134134
constructor(options: RendererOptions) {
135135
super();
136136

137-
this.dpr = resolveDpr();
137+
this.dpr = window.devicePixelRatio;
138138

139139
this.canvas3D = document.createElement('canvas');
140140
this.canvas3D.id = 'webgl-canvas';
@@ -248,7 +248,7 @@ export class Renderer extends
248248
* device pixel ratio.
249249
*/
250250
private updateRendererSize() {
251-
const dpr = resolveDpr();
251+
const dpr = window.devicePixelRatio;
252252
if (dpr !== this.dpr) {
253253
// If the device pixel ratio has changed due to page zoom, elements
254254
// specified by % width do not fire a resize event even though their CSS

packages/model-viewer/src/utilities.ts

Lines changed: 0 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -125,74 +125,6 @@ export const clamp =
125125
(value: number, lowerLimit: number, upperLimit: number): number =>
126126
Math.max(lowerLimit, Math.min(upperLimit, value));
127127

128-
129-
// The DPR we use for a "capped" scenario (see resolveDpr below):
130-
export const CAPPED_DEVICE_PIXEL_RATIO = 1;
131-
132-
133-
/**
134-
* This helper analyzes the layout of the current page to decide if we should
135-
* use the natural device pixel ratio, or a capped value.
136-
*
137-
* We cap DPR if there is no meta viewport (suggesting that user is not
138-
* consciously specifying how to scale the viewport relative to the device
139-
* screen size).
140-
*
141-
* The rationale is that this condition typically leads to a pathological
142-
* outcome on mobile devices. When the window dimensions are scaled up on a
143-
* device with a high DPR, we create a canvas that is much larger than
144-
* appropriate to accommodate for the pixel density if we naively use the
145-
* reported DPR.
146-
*
147-
* This value needs to be measured in real time, as device pixel ratio can
148-
* change over time (e.g., when a user zooms the page). Also, in some cases
149-
* (such as Firefox on Android), the window's innerWidth is initially reported
150-
* as the same as the screen's availWidth but changes later.
151-
*
152-
* A user who specifies a meta viewport, thereby consciously creating scaling
153-
* conditions where <model-viewer> is slow, will be encouraged to live their
154-
* best life.
155-
*/
156-
export const resolveDpr: () => number = (() => {
157-
// If true, implies that the user is conscious of the viewport scaling
158-
// relative to the device screen size.
159-
const HAS_META_VIEWPORT_TAG = (() => {
160-
// Search result pages sometimes do not include a meta viewport tag even
161-
// though they are certainly modern and work properly with devicePixelRatio.
162-
if (document.documentElement.getAttribute('itemtype')
163-
?.includes('schema.org/SearchResultsPage')) {
164-
return true;
165-
}
166-
167-
if (window.self !== window.top) {
168-
// iframes can't detect the meta viewport tag, so assume the top-level
169-
// page has one.
170-
return true;
171-
}
172-
173-
const metas = document.head != null ?
174-
Array.from(document.head.querySelectorAll('meta')) :
175-
[];
176-
177-
for (const meta of metas) {
178-
if (meta.name === 'viewport') {
179-
return true;
180-
}
181-
}
182-
183-
return false;
184-
})();
185-
186-
if (!HAS_META_VIEWPORT_TAG) {
187-
console.warn(
188-
'No <meta name="viewport"> detected; <model-viewer> will cap pixel density at 1.');
189-
}
190-
191-
return () => HAS_META_VIEWPORT_TAG ? window.devicePixelRatio :
192-
CAPPED_DEVICE_PIXEL_RATIO;
193-
})();
194-
195-
196128
/**
197129
* Debug mode is enabled when one of the two following conditions is true:
198130
*

0 commit comments

Comments
 (0)