Skip to content

Commit 953cd4a

Browse files
authored
Fix withCredentials (#4892)
* make withCredentials static * Revert "make withCredentials static" This reverts commit 55d3bdd. * fix withCredentials
1 parent 5604ec8 commit 953cd4a

File tree

5 files changed

+41
-39
lines changed

5 files changed

+41
-39
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,8 @@ export const EnvironmentMixin = <T extends Constructor<ModelViewerElementBase>>(
133133
await textureUtils.generateEnvironmentMapAndSkybox(
134134
deserializeUrl(skyboxImage),
135135
environmentImage,
136-
(progress: number) => updateEnvProgress(clamp(progress, 0, 1)));
136+
(progress: number) => updateEnvProgress(clamp(progress, 0, 1)),
137+
this.withCredentials);
137138

138139
if (this[$currentEnvironmentMap] !== environmentMap) {
139140
this[$currentEnvironmentMap] = environmentMap;

packages/model-viewer/src/features/scene-graph.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ export const SceneGraphMixin = <T extends Constructor<ModelViewerElementBase>>(
131131
async createTexture(uri: string, type: string = 'image/png'):
132132
Promise<ModelViewerTexture> {
133133
const {textureUtils} = this[$renderer];
134-
const texture = await textureUtils!.loadImage(uri);
134+
const texture = await textureUtils!.loadImage(uri, this.withCredentials);
135135

136136
texture.userData.mimeType = type;
137137

@@ -141,14 +141,16 @@ export const SceneGraphMixin = <T extends Constructor<ModelViewerElementBase>>(
141141
async createLottieTexture(uri: string, quality = 1):
142142
Promise<ModelViewerTexture> {
143143
const {textureUtils} = this[$renderer];
144-
const texture = await textureUtils!.loadLottie(uri, quality);
144+
const texture =
145+
await textureUtils!.loadLottie(uri, quality, this.withCredentials);
145146

146147
return this[$buildTexture](texture);
147148
}
148149

149150
createVideoTexture(uri: string): ModelViewerTexture {
150151
const video = document.createElement('video');
151-
video.crossOrigin = this.withCredentials ? 'use-credentials' : 'anonymous';
152+
video.crossOrigin =
153+
this.withCredentials ? 'use-credentials' : 'anonymous';
152154
video.src = uri;
153155
video.muted = true;
154156
video.playsInline = true;
@@ -170,7 +172,8 @@ export const SceneGraphMixin = <T extends Constructor<ModelViewerElementBase>>(
170172
super.updated(changedProperties);
171173

172174
if (changedProperties.has('variantName')) {
173-
const updateVariantProgress = this[$progressTracker].beginActivity('variant-update');
175+
const updateVariantProgress =
176+
this[$progressTracker].beginActivity('variant-update');
174177
updateVariantProgress(0.1);
175178
const model = this[$model];
176179
const {variantName} = this;

packages/model-viewer/src/model-viewer-base.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -404,11 +404,6 @@ export default class ModelViewerElementBase extends ReactiveElement {
404404
this[$userInputElement].setAttribute('aria-label', this[$ariaLabel]);
405405
}
406406

407-
if (changedProperties.has('withCredentials')) {
408-
CachingGLTFLoader.withCredentials = this.withCredentials;
409-
this[$renderer].textureUtils!.withCredentials = this.withCredentials;
410-
}
411-
412407
if (changedProperties.has('generateSchema')) {
413408
if (this.generateSchema) {
414409
this[$scene].updateSchema(this.src);
@@ -613,7 +608,8 @@ export default class ModelViewerElementBase extends ReactiveElement {
613608
// throw exceptions and/or behave in unexpected ways:
614609
scene.stopAnimation();
615610

616-
const updateSourceProgress = this[$progressTracker].beginActivity('model-load');
611+
const updateSourceProgress =
612+
this[$progressTracker].beginActivity('model-load');
617613
const source = this.src;
618614
try {
619615
const srcUpdated = scene.setSource(

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,6 @@ export class CachingGLTFLoader<T extends GLTFInstanceConstructor =
8686
GLTFInstanceConstructor> extends
8787
EventDispatcher<
8888
{'preload': {element: ModelViewerElementBase, src: String}}> {
89-
static withCredentials: boolean;
90-
9189
static setDRACODecoderLocation(url: string) {
9290
dracoDecoderLocation = url;
9391
dracoLoader.setDecoderPath(url);
@@ -188,7 +186,7 @@ export class CachingGLTFLoader<T extends GLTFInstanceConstructor =
188186
async preload(
189187
url: string, element: ModelViewerElementBase,
190188
progressCallback: ProgressCallback = () => {}) {
191-
this[$loader].setWithCredentials(CachingGLTFLoader.withCredentials);
189+
this[$loader].setWithCredentials(element.withCredentials);
192190
this.dispatchEvent({type: 'preload', element: element, src: url});
193191
if (!cache.has(url)) {
194192
if (meshoptDecoder != null) {

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

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ const HDR_FILE_RE = /\.hdr(\.js)?$/;
3535

3636
export default class TextureUtils {
3737
public lottieLoaderUrl = '';
38-
public withCredentials = false;
3938

4039
private _ldrLoader: TextureLoader|null = null;
4140
private _imageLoader: HDRJPGLoader|null = null;
@@ -53,53 +52,54 @@ export default class TextureUtils {
5352
constructor(private threeRenderer: WebGLRenderer) {
5453
}
5554

56-
get ldrLoader(): TextureLoader {
55+
private ldrLoader(withCredentials: boolean): TextureLoader {
5756
if (this._ldrLoader == null) {
5857
this._ldrLoader = new TextureLoader();
5958
}
60-
this._ldrLoader.setWithCredentials(this.withCredentials);
59+
this._ldrLoader.setWithCredentials(withCredentials);
6160
return this._ldrLoader;
6261
}
6362

64-
get imageLoader(): HDRJPGLoader {
63+
private imageLoader(withCredentials: boolean): HDRJPGLoader {
6564
if (this._imageLoader == null) {
6665
this._imageLoader = new HDRJPGLoader(this.threeRenderer);
6766
}
68-
this._imageLoader.setWithCredentials(this.withCredentials);
67+
this._imageLoader.setWithCredentials(withCredentials);
6968
return this._imageLoader;
7069
}
7170

72-
get hdrLoader(): RGBELoader {
71+
private hdrLoader(withCredentials: boolean): RGBELoader {
7372
if (this._hdrLoader == null) {
7473
this._hdrLoader = new RGBELoader();
7574
this._hdrLoader.setDataType(HalfFloatType);
7675
}
77-
this._hdrLoader.setWithCredentials(this.withCredentials);
76+
this._hdrLoader.setWithCredentials(withCredentials);
7877
return this._hdrLoader;
7978
}
8079

81-
async getLottieLoader(): Promise<any> {
80+
async getLottieLoader(withCredentials: boolean): Promise<any> {
8281
if (this._lottieLoader == null) {
8382
const {LottieLoader} =
8483
await import(/* webpackIgnore: true */ this.lottieLoaderUrl);
8584
this._lottieLoader = new LottieLoader() as Loader;
8685
}
87-
this._lottieLoader.setWithCredentials(this.withCredentials);
86+
this._lottieLoader.setWithCredentials(withCredentials);
8887
return this._lottieLoader;
8988
}
9089

91-
async loadImage(url: string): Promise<Texture> {
90+
async loadImage(url: string, withCredentials: boolean): Promise<Texture> {
9291
const texture: Texture = await new Promise<Texture>(
93-
(resolve, reject) =>
94-
this.ldrLoader.load(url, resolve, () => {}, reject));
92+
(resolve, reject) => this.ldrLoader(withCredentials)
93+
.load(url, resolve, () => {}, reject));
9594
texture.name = url;
9695
texture.flipY = false;
9796

9897
return texture;
9998
}
10099

101-
async loadLottie(url: string, quality: number): Promise<Texture> {
102-
const loader = await this.getLottieLoader();
100+
async loadLottie(url: string, quality: number, withCredentials: boolean):
101+
Promise<Texture> {
102+
const loader = await this.getLottieLoader(withCredentials);
103103
loader.setQuality(quality);
104104
const texture: Texture = await new Promise<Texture>(
105105
(resolve, reject) => loader.load(url, resolve, () => {}, reject));
@@ -109,11 +109,13 @@ export default class TextureUtils {
109109
}
110110

111111
async loadEquirect(
112-
url: string, progressCallback: (progress: number) => void = () => {}):
112+
url: string, withCredentials = false,
113+
progressCallback: (progress: number) => void = () => {}):
113114
Promise<Texture> {
114115
try {
115116
const isHDR: boolean = HDR_FILE_RE.test(url);
116-
const loader = isHDR ? this.hdrLoader : this.imageLoader;
117+
const loader = isHDR ? this.hdrLoader(withCredentials) :
118+
this.imageLoader(withCredentials);
117119
const texture: Texture = await new Promise<Texture>(
118120
(resolve, reject) => loader.load(
119121
url,
@@ -158,8 +160,8 @@ export default class TextureUtils {
158160
*/
159161
async generateEnvironmentMapAndSkybox(
160162
skyboxUrl: string|null = null, environmentMapUrl: string|null = null,
161-
progressCallback: (progress: number) => void = () => {}):
162-
Promise<EnvironmentMapAndSkybox> {
163+
progressCallback: (progress: number) => void = () => {},
164+
withCredentials = false): Promise<EnvironmentMapAndSkybox> {
163165
const useAltEnvironment = environmentMapUrl !== 'legacy';
164166
if (environmentMapUrl === 'legacy' || environmentMapUrl === 'neutral') {
165167
environmentMapUrl = null;
@@ -171,17 +173,18 @@ export default class TextureUtils {
171173

172174
// If we have a skybox URL, attempt to load it as a cubemap
173175
if (!!skyboxUrl) {
174-
skyboxLoads = this.loadEquirectFromUrl(skyboxUrl, progressCallback);
176+
skyboxLoads = this.loadEquirectFromUrl(
177+
skyboxUrl, withCredentials, progressCallback);
175178
}
176179

177180
if (!!environmentMapUrl) {
178181
// We have an available environment map URL
179-
environmentMapLoads =
180-
this.loadEquirectFromUrl(environmentMapUrl, progressCallback);
182+
environmentMapLoads = this.loadEquirectFromUrl(
183+
environmentMapUrl, withCredentials, progressCallback);
181184
} else if (!!skyboxUrl) {
182185
// Fallback to deriving the environment map from an available skybox
183-
environmentMapLoads =
184-
this.loadEquirectFromUrl(skyboxUrl, progressCallback);
186+
environmentMapLoads = this.loadEquirectFromUrl(
187+
skyboxUrl, withCredentials, progressCallback);
185188
} else {
186189
// Fallback to generating the environment map
187190
environmentMapLoads = useAltEnvironment ?
@@ -203,10 +206,11 @@ export default class TextureUtils {
203206
* Loads an equirect Texture from a given URL, for use as a skybox.
204207
*/
205208
private async loadEquirectFromUrl(
206-
url: string,
209+
url: string, withCredentials: boolean,
207210
progressCallback: (progress: number) => void): Promise<Texture> {
208211
if (!this.skyboxCache.has(url)) {
209-
const skyboxMapLoads = this.loadEquirect(url, progressCallback);
212+
const skyboxMapLoads =
213+
this.loadEquirect(url, withCredentials, progressCallback);
210214

211215
this.skyboxCache.set(url, skyboxMapLoads);
212216
}

0 commit comments

Comments
 (0)