Skip to content

Commit 91dd41b

Browse files
committed
🛠️ Fix #357
1 parent c683ed0 commit 91dd41b

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed

src/systems/minecraft/assetManager.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,11 @@ export async function assetsLoaded() {
121121
})
122122
}
123123

124+
export function hasAsset(path: string) {
125+
if (!loadedAssets) throw new Error('Assets not loaded')
126+
return !!loadedAssets[path]
127+
}
128+
124129
export function getRawAsset(path: string) {
125130
if (!loadedAssets) throw new Error('Assets not loaded')
126131

src/systems/minecraft/blockModelManager.ts

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
resolveBlockstateValueType,
77
} from '../../util/minecraftUtil'
88
import { translate } from '../../util/translation'
9-
import { assetsLoaded, getJSONAsset, getPngAssetAsDataUrl } from './assetManager'
9+
import { assetsLoaded, getJSONAsset, getPngAssetAsDataUrl, hasAsset } from './assetManager'
1010
import { BlockStateValue } from './blockstateManager'
1111
import {
1212
IBlockModel,
@@ -366,14 +366,36 @@ async function loadTexture(textures: IBlockModel['textures'], key: string): Prom
366366
if (resourceLocation?.at(0) === '#') {
367367
return await loadTexture(textures, resourceLocation.slice(1))
368368
}
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)!
372372
}
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+
374394
texture.magFilter = THREE.NearestFilter
375395
texture.minFilter = THREE.NearestFilter
376-
TEXTURE_CACHE.set(textureUrl, texture)
396+
397+
TEXTURE_CACHE.set(texturePath, texture)
398+
377399
return texture
378400
}
379401

0 commit comments

Comments
 (0)