From abaaeaef4e1191ed44052bc85ce6b70f881aa756 Mon Sep 17 00:00:00 2001 From: Vitaly Turovsky Date: Sat, 14 Jun 2025 15:11:22 +0300 Subject: [PATCH] restore textures / panorama todo restore loadedData and hand --- renderer/viewer/lib/utils.ts | 7 ++++--- renderer/viewer/lib/utils/skins.ts | 17 +++++++++++++---- renderer/viewer/three/panorama.ts | 5 ++--- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/renderer/viewer/lib/utils.ts b/renderer/viewer/lib/utils.ts index 45522fca..5c9c00f1 100644 --- a/renderer/viewer/lib/utils.ts +++ b/renderer/viewer/lib/utils.ts @@ -1,5 +1,5 @@ import * as THREE from 'three' -import { loadThreeJsTextureFromUrl } from './utils/skins' +import { loadThreeJsTextureFromUrl, loadThreeJsTextureFromUrlSync } from './utils/skins' let textureCache: Record = {} let imagesPromises: Record> = {} @@ -8,8 +8,9 @@ export async function loadTexture (texture: string, cb: (texture: THREE.Texture) const cached = textureCache[texture] if (!cached) { const { promise, resolve } = Promise.withResolvers() - textureCache[texture] = new THREE.Texture() - void loadThreeJsTextureFromUrl(texture, textureCache[texture]).then(resolve) + const t = loadThreeJsTextureFromUrlSync(texture) + textureCache[texture] = t.texture + void t.promise.then(resolve) imagesPromises[texture] = promise } diff --git a/renderer/viewer/lib/utils/skins.ts b/renderer/viewer/lib/utils/skins.ts index cc913caa..fba35f3c 100644 --- a/renderer/viewer/lib/utils/skins.ts +++ b/renderer/viewer/lib/utils/skins.ts @@ -1,14 +1,23 @@ import { loadSkinToCanvas } from 'skinview-utils' import * as THREE from 'three' import stevePng from 'mc-assets/dist/other-textures/latest/entity/player/wide/steve.png' +import { getLoadedImage } from 'mc-assets/dist/utils' -export const loadThreeJsTextureFromUrl = async (imageUrl: string, texture?: THREE.Texture) => { - const loaded = new THREE.TextureLoader().loadAsync(imageUrl) - if (texture) { - texture.image = loaded +export const loadThreeJsTextureFromUrlSync = (imageUrl: string) => { + const texture = new THREE.Texture() + const promise = getLoadedImage(imageUrl).then(image => { + texture.image = image texture.needsUpdate = true return texture + }) + return { + texture, + promise } +} + +export const loadThreeJsTextureFromUrl = async (imageUrl: string) => { + const loaded = new THREE.TextureLoader().loadAsync(imageUrl) return loaded } diff --git a/renderer/viewer/three/panorama.ts b/renderer/viewer/three/panorama.ts index d641576e..58a0fffa 100644 --- a/renderer/viewer/three/panorama.ts +++ b/renderer/viewer/three/panorama.ts @@ -7,7 +7,7 @@ import type { GraphicsInitOptions } from '../../../src/appViewer' import { WorldDataEmitter } from '../lib/worldDataEmitter' import { defaultWorldRendererConfig, WorldRendererCommon } from '../lib/worldrendererCommon' import { getDefaultRendererState } from '../baseGraphicsBackend' -import { loadThreeJsTextureFromUrl } from '../lib/utils/skins' +import { loadThreeJsTextureFromUrl, loadThreeJsTextureFromUrlSync } from '../lib/utils/skins' import { ResourcesManager } from '../../../src/resourcesManager' import { getInitialPlayerStateRenderer } from '../lib/basePlayerState' import { WorldRendererThree } from './worldrendererThree' @@ -89,14 +89,13 @@ export class PanoramaRenderer { for (const file of panoramaFiles) { const load = async () => { - const texture = await loadThreeJsTextureFromUrl(join('background', file)) + const { texture } = loadThreeJsTextureFromUrlSync(join('background', file)) // Instead of using repeat/offset to flip, we'll use the texture matrix texture.matrixAutoUpdate = false texture.matrix.set( -1, 0, 1, 0, 1, 0, 0, 0, 1 ) - texture.updateMatrix() texture.wrapS = THREE.ClampToEdgeWrapping texture.wrapT = THREE.ClampToEdgeWrapping