From 86f7a70aa2c80f00cdd8cc4120f3616df7df2ba3 Mon Sep 17 00:00:00 2001 From: Gary Hsu Date: Mon, 18 Nov 2024 15:08:17 -0800 Subject: [PATCH 1/4] Fix ktx texture orientation in sandbox --- packages/dev/core/src/Compat/index.ts | 2 +- packages/dev/core/src/index.ts | 1 + .../sandbox/src/components/renderingZone.tsx | 62 ++++++++++++------- 3 files changed, 40 insertions(+), 25 deletions(-) diff --git a/packages/dev/core/src/Compat/index.ts b/packages/dev/core/src/Compat/index.ts index 7c737be7c7a..4fcf8d3d733 100644 --- a/packages/dev/core/src/Compat/index.ts +++ b/packages/dev/core/src/Compat/index.ts @@ -1 +1 @@ -export { CompatibilityOptions } from "./compatibilityOptions"; +export * from "./compatibilityOptions"; diff --git a/packages/dev/core/src/index.ts b/packages/dev/core/src/index.ts index 1f530660372..6875687852c 100644 --- a/packages/dev/core/src/index.ts +++ b/packages/dev/core/src/index.ts @@ -9,6 +9,7 @@ export * from "./Bones/index"; export * from "./Buffers/index"; export * from "./Cameras/index"; export * from "./Collisions/index"; +export * from "./Compat/index"; export * from "./Compute/index"; export * from "./Culling/index"; export * from "./Debug/index"; diff --git a/packages/tools/sandbox/src/components/renderingZone.tsx b/packages/tools/sandbox/src/components/renderingZone.tsx index fb09523c792..c2a5542712f 100644 --- a/packages/tools/sandbox/src/components/renderingZone.tsx +++ b/packages/tools/sandbox/src/components/renderingZone.tsx @@ -19,17 +19,28 @@ import "core/Helpers/sceneHelpers"; import "../scss/renderingZone.scss"; import { PBRBaseMaterial } from "core/Materials/PBR/pbrBaseMaterial"; -import { Texture } from "core/Materials/Textures/texture"; +import { ITextureCreationOptions, Texture } from "core/Materials/Textures/texture"; import { PBRMaterial } from "core/Materials/PBR/pbrMaterial"; import type { AbstractEngine } from "core/Engines/abstractEngine"; +import { setOpenGLOrientationForUV, useOpenGLOrientationForUV } from "core/Compat/compatibilityOptions"; -function isTextureAsset(name: string): boolean { - const queryStringIndex = name.indexOf("?"); - if (queryStringIndex !== -1) { - name = name.substring(0, queryStringIndex); +function getFileExtension(str: string): string { + return str.split(".").pop() || ""; +} + +function isTextureAsset(extension: string): boolean { + switch (extension.toLowerCase()) { + case "ktx": + case "ktx2": + case "png": + case "jpg": + case "jpeg": + case "webp": { + return true; + } } - return name.endsWith(".ktx") || name.endsWith(".ktx2") || name.endsWith(".png") || name.endsWith(".jpg") || name.endsWith(".jpeg") || name.endsWith(".webp"); + return false; } interface IRenderingZoneProps { @@ -127,7 +138,7 @@ export class RenderingZone extends React.Component { return false; } default: { - if (isTextureAsset(name)) { + if (isTextureAsset(extension)) { setSceneFileToLoad(file); } @@ -142,8 +153,9 @@ export class RenderingZone extends React.Component { filesInput.loadAsync = (sceneFile, onProgress) => { const filesToLoad = filesInput.filesToLoad; if (filesToLoad.length === 1) { - const fileName = (filesToLoad[0] as any).correctName; - if (isTextureAsset(fileName)) { + const fileName = (filesToLoad[0] as any).correctName as string; + const fileExtension = getFileExtension(fileName); + if (isTextureAsset(fileExtension)) { return Promise.resolve(this.loadTextureAsset(`file:${fileName}`)); } } @@ -161,7 +173,7 @@ export class RenderingZone extends React.Component { // Press R to reload if (event.keyCode === 82 && event.target && (event.target as HTMLElement).nodeName !== "INPUT" && this._scene) { if (this.props.assetUrl) { - this.loadAssetFromUrl(); + this.loadAssetFromUrl(this.props.assetUrl); } else { filesInput.reload(); } @@ -285,15 +297,16 @@ export class RenderingZone extends React.Component { loadTextureAsset(url: string): Scene { const scene = new Scene(this._engine); + + const prevousUseOpenGLOrientationForUV = useOpenGLOrientationForUV; + setOpenGLOrientationForUV(true); const plane = CreatePlane("plane", { size: 1 }, scene); + setOpenGLOrientationForUV(prevousUseOpenGLOrientationForUV); - const texture = new Texture( - url, - scene, - undefined, - undefined, - Texture.NEAREST_LINEAR, - () => { + const options: ITextureCreationOptions = { + invertY: false, + samplingMode: Texture.NEAREST_LINEAR, + onLoad: () => { const size = texture.getBaseSize(); if (size.width > size.height) { plane.scaling.y = size.height / size.width; @@ -309,11 +322,12 @@ export class RenderingZone extends React.Component { scene.debugLayer.show(); scene.debugLayer.select(texture, "PREVIEW"); }, - (message, exception) => { + onError: (message, exception) => { this.props.globalState.onError.notifyObservers({ scene: scene, message: message || exception.message || "Failed to load texture" }); - } - ); + }, + }; + const texture = new Texture(url, scene, options); const material = new PBRMaterial("unlit", scene); material.unlit = true; material.albedoTexture = texture; @@ -323,14 +337,14 @@ export class RenderingZone extends React.Component { return scene; } - loadAssetFromUrl() { - const assetUrl = this.props.assetUrl!; + loadAssetFromUrl(assetUrl: string) { const rootUrl = Tools.GetFolderPath(assetUrl); const fileName = Tools.GetFilename(assetUrl); + const fileExtension = getFileExtension(fileName); this._engine.clearInternalTexturesCache(); - const promise = isTextureAsset(assetUrl) ? Promise.resolve(this.loadTextureAsset(assetUrl)) : SceneLoader.LoadAsync(rootUrl, fileName, this._engine); + const promise = isTextureAsset(fileExtension) ? Promise.resolve(this.loadTextureAsset(assetUrl)) : SceneLoader.LoadAsync(rootUrl, fileName, this._engine); promise .then((scene) => { @@ -355,7 +369,7 @@ export class RenderingZone extends React.Component { loadAsset() { if (this.props.assetUrl) { - this.loadAssetFromUrl(); + this.loadAssetFromUrl(this.props.assetUrl); return; } } From 448eadbec0e27545e99b7c4b9a3099e67534e2ad Mon Sep 17 00:00:00 2001 From: Gary Hsu Date: Mon, 18 Nov 2024 15:37:48 -0800 Subject: [PATCH 2/4] lint --- packages/tools/sandbox/src/components/renderingZone.tsx | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/tools/sandbox/src/components/renderingZone.tsx b/packages/tools/sandbox/src/components/renderingZone.tsx index c2a5542712f..5a8e6c16df7 100644 --- a/packages/tools/sandbox/src/components/renderingZone.tsx +++ b/packages/tools/sandbox/src/components/renderingZone.tsx @@ -19,7 +19,8 @@ import "core/Helpers/sceneHelpers"; import "../scss/renderingZone.scss"; import { PBRBaseMaterial } from "core/Materials/PBR/pbrBaseMaterial"; -import { ITextureCreationOptions, Texture } from "core/Materials/Textures/texture"; +import { Texture } from "core/Materials/Textures/texture"; +import type { ITextureCreationOptions } from "core/Materials/Textures/texture"; import { PBRMaterial } from "core/Materials/PBR/pbrMaterial"; import type { AbstractEngine } from "core/Engines/abstractEngine"; import { setOpenGLOrientationForUV, useOpenGLOrientationForUV } from "core/Compat/compatibilityOptions"; From 5cf70a98ee35fd2d17933a61a6e8ff7c27526842 Mon Sep 17 00:00:00 2001 From: Gary Hsu Date: Mon, 18 Nov 2024 16:17:28 -0800 Subject: [PATCH 3/4] Sort index.ts and remove dupe --- packages/dev/core/src/index.ts | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/packages/dev/core/src/index.ts b/packages/dev/core/src/index.ts index 6875687852c..c7ed0914b20 100644 --- a/packages/dev/core/src/index.ts +++ b/packages/dev/core/src/index.ts @@ -16,6 +16,8 @@ export * from "./Debug/index"; export * from "./DeviceInput/index"; export * from "./Engines/index"; export * from "./Events/index"; +export * from "./FlowGraph/index"; +export * from "./FrameGraph/index"; export * from "./Gamepads/index"; export * from "./Gizmos/index"; export * from "./Helpers/index"; @@ -30,6 +32,7 @@ export * from "./Meshes/index"; export * from "./Morph/index"; export * from "./Navigation/index"; export * from "./node"; +export * from "./ObjectModel/index"; export * from "./Offline/index"; export * from "./Particles/index"; export * from "./Physics/index"; @@ -40,10 +43,6 @@ export * from "./scene"; export * from "./sceneComponent"; export * from "./Sprites/index"; export * from "./States/index"; +export * from "./types"; export * from "./Misc/index"; export * from "./XR/index"; -export * from "./types"; -export * from "./Compat/index"; -export * from "./FlowGraph/index"; -export * from "./ObjectModel/index"; -export * from "./FrameGraph/index"; From e02561b3446b266d8280f6b9ad406c6c210e7a70 Mon Sep 17 00:00:00 2001 From: Gary Hsu Date: Mon, 18 Nov 2024 17:03:03 -0800 Subject: [PATCH 4/4] sort --- packages/dev/core/src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/dev/core/src/index.ts b/packages/dev/core/src/index.ts index c7ed0914b20..77022e25c49 100644 --- a/packages/dev/core/src/index.ts +++ b/packages/dev/core/src/index.ts @@ -29,6 +29,7 @@ export * from "./Loading/index"; export * from "./Materials/index"; export * from "./Maths/index"; export * from "./Meshes/index"; +export * from "./Misc/index"; export * from "./Morph/index"; export * from "./Navigation/index"; export * from "./node"; @@ -44,5 +45,4 @@ export * from "./sceneComponent"; export * from "./Sprites/index"; export * from "./States/index"; export * from "./types"; -export * from "./Misc/index"; export * from "./XR/index";