From 337ba2e802d2fdf9d2cf8d47b4b973394e1c6294 Mon Sep 17 00:00:00 2001 From: zhuxudong Date: Tue, 27 Aug 2024 12:02:30 +0800 Subject: [PATCH] Fix error in node environment (#2362) * fix: error in node environment --- packages/core/src/SystemInfo.ts | 5 +++++ .../loader/src/gltf/extensions/EXT_texture_webp.ts | 13 +++++++++---- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/packages/core/src/SystemInfo.ts b/packages/core/src/SystemInfo.ts index 776492fd4..314f48ac8 100644 --- a/packages/core/src/SystemInfo.ts +++ b/packages/core/src/SystemInfo.ts @@ -8,6 +8,10 @@ export class SystemInfo { static platform: Platform = Platform.Unknown; /** The operating system is running on. */ static operatingSystem: string = ""; + + /** @internal */ + static _isBrowser = true; + /** Whether the system support SIMD. */ private static _simdSupported: boolean | null = null; @@ -24,6 +28,7 @@ export class SystemInfo { static _initialize(): void { { if (typeof navigator == "undefined") { + SystemInfo._isBrowser = false; return; } diff --git a/packages/loader/src/gltf/extensions/EXT_texture_webp.ts b/packages/loader/src/gltf/extensions/EXT_texture_webp.ts index 0000c2e12..5fea6f4f7 100644 --- a/packages/loader/src/gltf/extensions/EXT_texture_webp.ts +++ b/packages/loader/src/gltf/extensions/EXT_texture_webp.ts @@ -1,4 +1,4 @@ -import { Texture2D } from "@galacean/engine-core"; +import { SystemInfo, Texture2D } from "@galacean/engine-core"; import type { ITexture } from "../GLTFSchema"; import { registerGLTFExtension } from "../parser/GLTFParser"; import { GLTFParserContext } from "../parser/GLTFParserContext"; @@ -15,10 +15,15 @@ class EXT_texture_webp extends GLTFExtensionParser { constructor() { super(); - const testCanvas = document.createElement("canvas"); - testCanvas.width = testCanvas.height = 1; - this._supportWebP = testCanvas.toDataURL("image/webp").indexOf("data:image/webp") == 0; + // @ts-ignore + if (SystemInfo._isBrowser) { + const testCanvas = document.createElement("canvas"); + testCanvas.width = testCanvas.height = 1; + this._supportWebP = testCanvas.toDataURL("image/webp").indexOf("data:image/webp") == 0; + } else { + this._supportWebP = false; + } } override async createAndParse(