Skip to content

Commit 8614b3e

Browse files
adding powerPreference to Moelviewer static options (#2656)
* adding powerPreference to Moelviewer static options * fixed formatting * fixed resetSingleton * addressed the comments about setting powerPreference * fix the runtime error about can't access lexical declration before initialization this is causing trouble bc of the circular dependency we have unfortunately * revert config.json which was added by mistake * fix the bad merge2 * Addressing comments: reverting changes to modelviewerbase and test Co-authored-by: Emmett Lalish <[email protected]>
1 parent 70fe8a8 commit 8614b3e

File tree

3 files changed

+49
-11
lines changed

3 files changed

+49
-11
lines changed

packages/model-viewer/src/features/loading.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,10 @@ export declare interface LoadingStaticInterface {
9393
mapURLs(callback: (url: string) => string): void;
9494
}
9595

96-
interface ModelViewerGlobalConfig {
96+
export interface ModelViewerGlobalConfig {
9797
dracoDecoderLocation?: string;
9898
ktx2TranscoderLocation?: string;
99+
powerPreference?: string;
99100
}
100101

101102
/**

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

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ import {ACESFilmicToneMapping, Event, EventDispatcher, GammaEncoding, PCFSoftSha
1717
import {RoughnessMipmapper} from 'three/examples/jsm/utils/RoughnessMipmapper';
1818

1919
import {$updateEnvironment} from '../features/environment.js';
20-
import {$canvas, $tick, $updateSize} from '../model-viewer-base.js';
20+
import {ModelViewerGlobalConfig} from '../features/loading.js';
21+
import ModelViewerElementBase, {$canvas, $tick, $updateSize} from '../model-viewer-base.js';
2122
import {clamp, isDebugMode, resolveDpr} from '../utilities.js';
2223

2324
import {ARRenderer} from './ARRenderer.js';
@@ -28,6 +29,7 @@ import {ModelScene} from './ModelScene.js';
2829
import TextureUtils from './TextureUtils.js';
2930

3031
export interface RendererOptions {
32+
powerPreference: string;
3133
debug?: boolean;
3234
}
3335

@@ -44,6 +46,8 @@ const MAX_AVG_CHANGE_MS = 2;
4446
const SCALE_STEPS = [1, 0.79, 0.62, 0.5, 0.4, 0.31, 0.25];
4547
const DEFAULT_LAST_STEP = 3;
4648

49+
export const DEFAULT_POWER_PREFERENCE: string = 'high-performance';
50+
4751
/**
4852
* Registers canvases with Canvas2DRenderingContexts and renders them
4953
* all in the same WebGLRenderingContext, spitting out textures to apply
@@ -56,15 +60,35 @@ const DEFAULT_LAST_STEP = 3;
5660
* the texture.
5761
*/
5862
export class Renderer extends EventDispatcher {
59-
private static _singleton = new Renderer({debug: isDebugMode()});
63+
private static _singleton = new Renderer({
64+
powerPreference:
65+
(((self as any).ModelViewerElement || {}) as ModelViewerGlobalConfig)
66+
.powerPreference ||
67+
DEFAULT_POWER_PREFERENCE,
68+
debug: isDebugMode()
69+
});
6070

6171
static get singleton() {
6272
return this._singleton;
6373
}
6474

6575
static resetSingleton() {
66-
this._singleton.dispose();
67-
this._singleton = new Renderer({debug: isDebugMode()});
76+
const elements = this._singleton.dispose();
77+
for (const element of elements) {
78+
element.disconnectedCallback();
79+
}
80+
81+
this._singleton = new Renderer({
82+
powerPreference:
83+
(((self as any).ModelViewerElement || {}) as ModelViewerGlobalConfig)
84+
.powerPreference ||
85+
DEFAULT_POWER_PREFERENCE,
86+
debug: isDebugMode()
87+
});
88+
89+
for (const element of elements) {
90+
element.connectedCallback();
91+
}
6892
}
6993

7094
public threeRenderer!: WebGLRenderer;
@@ -105,7 +129,7 @@ export class Renderer extends EventDispatcher {
105129
this.lastStep = i - 1;
106130
}
107131

108-
constructor(options?: RendererOptions) {
132+
constructor(options: RendererOptions) {
109133
super();
110134

111135
this.dpr = resolveDpr();
@@ -118,7 +142,7 @@ export class Renderer extends EventDispatcher {
118142
canvas: this.canvas3D,
119143
alpha: true,
120144
antialias: true,
121-
powerPreference: 'high-performance' as WebGLPowerPreference,
145+
powerPreference: options.powerPreference as WebGLPowerPreference,
122146
preserveDrawingBuffer: true
123147
});
124148
this.threeRenderer.autoClear = true;
@@ -129,8 +153,7 @@ export class Renderer extends EventDispatcher {
129153
this.threeRenderer.shadowMap.type = PCFSoftShadowMap;
130154
this.threeRenderer.shadowMap.autoUpdate = false;
131155

132-
this.debugger =
133-
options != null && !!options.debug ? new Debugger(this) : null;
156+
this.debugger = !!options.debug ? new Debugger(this) : null;
134157
this.threeRenderer.debug = {checkShaderErrors: !!this.debugger};
135158

136159
// ACESFilmicToneMapping appears to be the most "saturated",
@@ -466,7 +489,7 @@ export class Renderer extends EventDispatcher {
466489
}
467490
}
468491

469-
dispose() {
492+
dispose(): Array<ModelViewerElementBase> {
470493
if (this.textureUtils != null) {
471494
this.textureUtils.dispose();
472495
}
@@ -482,12 +505,17 @@ export class Renderer extends EventDispatcher {
482505
this.textureUtils = null;
483506
(this as any).threeRenderer = null;
484507

485-
this.scenes.clear();
508+
const elements = [];
509+
for (const scene of this.scenes) {
510+
elements.push(scene.element);
511+
}
486512

487513
this.canvas3D.removeEventListener(
488514
'webglcontextlost', this.onWebGLContextLost);
489515
this.canvas3D.removeEventListener(
490516
'webglcontextrestored', this.onWebGLContextRestored);
517+
518+
return elements;
491519
}
492520

493521
onWebGLContextLost = (event: Event) => {

packages/modelviewer.dev/data/docs.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,15 @@
138138
"links": [
139139
"<a href=\"../examples/loading/#dracoSupport\">Static property usage example</a>"
140140
]
141+
},
142+
{
143+
"name": "powerPreference",
144+
"htmlName": "powerPreference",
145+
"description": "This static, writable property sets <span class='attribute'>&lt;model-viewer&gt;</span>'s power preference value. Model-viewer sets this property to high-performance if no value is provided.",
146+
"default": {
147+
"default": "high-performance",
148+
"options": "high-performance, low-power, default"
149+
}
141150
}
142151
],
143152
"Methods": [

0 commit comments

Comments
 (0)