load textures earlier

This commit is contained in:
Vitaly Turovsky 2023-09-19 21:39:39 +03:00
commit 8a094c2d68
3 changed files with 53 additions and 9 deletions

View file

@ -10,21 +10,27 @@ const setLocalStorageSettings = () => {
it('Loads & renders singleplayer', () => {
cy.visit('/')
window.localStorage.clear()
window.localStorage.setItem('renderDistance', '2')
window.localStorage.setItem('options', JSON.stringify({
localServerOptions: {
generation: {
name: 'superflat',
options: { seed: 250869072 }
}
}
},
renderDistance: 2
}))
setLocalStorageSettings()
cy.get('#title-screen').find('[data-test-id="singleplayer-button"]', { includeShadowDom: true, }).click()
// todo implement load event
cy.wait(12000)
cy.get('body').toMatchImageSnapshot({
name: 'superflat-world',
cy.document().then({ timeout: 20_000, }, doc => {
return new Cypress.Promise(resolve => {
doc.addEventListener('cypress-world-ready', resolve)
})
}).then(() => {
// wait for render
cy.wait(6000)
cy.get('body').toMatchImageSnapshot({
name: 'superflat-world',
})
})
})

View file

@ -55,6 +55,7 @@ import {
miscUiState,
gameAdditionalState
} from './globalState'
import viewerSupportedVersions from 'prismarine-viewer/viewer/supportedVersions.json'
import {
pointerLock,
@ -63,7 +64,8 @@ import {
isCypress,
loadScript,
toMajorVersion,
setLoadingScreenStatus
setLoadingScreenStatus,
downloadDefaultTextures
} from './utils'
import {
@ -80,8 +82,10 @@ import { options } from './optionsStorage'
import { subscribeKey } from 'valtio/utils'
import _ from 'lodash'
import { contro } from './controls'
import { genTexturePackTextures, watchTexturepackInViewer } from './texturePack'
import { genTexturePackTextures, resourcePackState, watchTexturepackInViewer } from './texturePack'
import { connectToPeer } from './localServerMultiplayer'
import { loadTexture } from 'prismarine-viewer/viewer/lib/utils.web'
import { getVersion } from 'prismarine-viewer/viewer/lib/version'
//@ts-ignore
window.THREE = THREE
@ -410,9 +414,17 @@ async function connect(connectOptions: {
console.error(err)
const doContinue = prompt('Failed to apply texture pack. See errors in the console. Continue?')
if (!doContinue) {
setLoadingScreenStatus(undefined)
throw new Error('Failed to apply texture pack')
}
}
if (resourcePackState.currentTexturesDataUrl === undefined) {
// resourcepack textures are not applied, we download default manually to ensure testing works
const viewerVersion = getVersion(version) ?? viewerSupportedVersions.at(-1)
const texturesDataUrl = await downloadDefaultTextures(viewerVersion)
viewer.world.texturesDataUrl = texturesDataUrl
viewer.world.blockStatesData = await fetch(`./blocksStates/${viewerVersion}.json`).then(res => res.json())
}
await loadScript(`./mc-data/${toMajorVersion(version)}.js`)
}
@ -721,6 +733,10 @@ async function connect(connectOptions: {
hud.init(renderer, bot, host)
hud.style.display = 'block'
viewer.waitForChunksToRender().then(() => {
console.log('ready!')
document.dispatchEvent(new Event('cypress-world-ready'))
})
setTimeout(function () {
errorAbortController.abort()
if (loadingScreen.hasError) return

View file

@ -244,3 +244,25 @@ export const resolveTimeout = (promise, timeout = 10000) => {
}, timeout)
})
}
const cachedTextures = {}
export const downloadDefaultTextures = async (version) => {
if (cachedTextures[version]) return cachedTextures[version]
const texturesDataUrl = await new Promise<string>(async resolve => {
const result = await fetch(`./textures/${version}.png`)
if (result.status !== 200) {
throw new Error(`Failed to download textures: ${result.status}`)
}
// get base64
const blob = await result.blob()
const reader = new FileReader()
reader.onload = () => {
resolve(reader.result as string)
}
reader.readAsDataURL(blob)
})
cachedTextures[version] = texturesDataUrl
return texturesDataUrl
}