Compare commits

...
Sign in to create a new pull request.

1 commit

Author SHA1 Message Date
Vitaly Turovsky
8a094c2d68 load textures earlier 2023-09-19 21:39:39 +03:00
3 changed files with 53 additions and 9 deletions

View file

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

View file

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

View file

@ -244,3 +244,25 @@ export const resolveTimeout = (promise, timeout = 10000) => {
}, timeout) }, 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
}