|
6 | 6 | resolveBlockstateValueType, |
7 | 7 | } from '../../util/minecraftUtil' |
8 | 8 | import { translate } from '../../util/translation' |
9 | | -import { assetsLoaded, getJSONAsset, getPngAssetAsDataUrl } from './assetManager' |
| 9 | +import { assetsLoaded, getJSONAsset, getPngAssetAsDataUrl, hasAsset } from './assetManager' |
10 | 10 | import { BlockStateValue } from './blockstateManager' |
11 | 11 | import { |
12 | 12 | IBlockModel, |
@@ -366,14 +366,36 @@ async function loadTexture(textures: IBlockModel['textures'], key: string): Prom |
366 | 366 | if (resourceLocation?.at(0) === '#') { |
367 | 367 | return await loadTexture(textures, resourceLocation.slice(1)) |
368 | 368 | } |
369 | | - const textureUrl = getPathFromResourceLocation(resourceLocation, 'textures') + '.png' |
370 | | - if (TEXTURE_CACHE.has(textureUrl)) { |
371 | | - return TEXTURE_CACHE.get(textureUrl)! |
| 369 | + const texturePath = getPathFromResourceLocation(resourceLocation, 'textures') + '.png' |
| 370 | + if (TEXTURE_CACHE.has(texturePath)) { |
| 371 | + return TEXTURE_CACHE.get(texturePath)! |
372 | 372 | } |
373 | | - const texture = await LOADER.loadAsync(getPngAssetAsDataUrl(textureUrl)) |
| 373 | + let texture: THREE.Texture |
| 374 | + if (hasAsset(texturePath + '.mcmeta')) { |
| 375 | + console.log(`Found mcmeta for texture '${texturePath}'`) |
| 376 | + |
| 377 | + const img = new Image() |
| 378 | + img.src = getPngAssetAsDataUrl(texturePath) |
| 379 | + const canvas = document.createElement('canvas') |
| 380 | + const ctx = canvas.getContext('2d')! |
| 381 | + await new Promise<void>(resolve => { |
| 382 | + img.onload = () => { |
| 383 | + canvas.width = img.width |
| 384 | + canvas.height = img.width |
| 385 | + ctx.drawImage(img, 0, 0) |
| 386 | + resolve() |
| 387 | + } |
| 388 | + }) |
| 389 | + texture = new THREE.CanvasTexture(canvas) |
| 390 | + } else { |
| 391 | + texture = await LOADER.loadAsync(getPngAssetAsDataUrl(texturePath)) |
| 392 | + } |
| 393 | + |
374 | 394 | texture.magFilter = THREE.NearestFilter |
375 | 395 | texture.minFilter = THREE.NearestFilter |
376 | | - TEXTURE_CACHE.set(textureUrl, texture) |
| 396 | + |
| 397 | + TEXTURE_CACHE.set(texturePath, texture) |
| 398 | + |
377 | 399 | return texture |
378 | 400 | } |
379 | 401 |
|
|
0 commit comments