From f6fbb9651fc6be354f5aa36976f97a2f20b92f6e Mon Sep 17 00:00:00 2001 From: Vitaly Date: Wed, 4 Oct 2023 10:47:49 +0300 Subject: [PATCH] fix: load textures earlier, speeds worlds loading. Remove initial timeout. --- .eslintrc.json | 2 ++ prismarine-viewer/viewer/lib/viewer.js | 12 +++++----- src/index.ts | 31 +++++++++++++------------- 3 files changed, 24 insertions(+), 21 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index 05983c70..74bc0a0c 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -4,6 +4,7 @@ "!*.js" ], "rules": { + "space-infix-ops": "error", "no-multi-spaces": "error", "space-in-parens": [ "error", @@ -86,6 +87,7 @@ "unicorn/no-empty-file": "off", "unicorn/prefer-event-target": "off", // needs to be fixed actually + "complexity": "off", "@typescript-eslint/no-floating-promises": "warn", "no-async-promise-executor": "off", "no-bitwise": "off", diff --git a/prismarine-viewer/viewer/lib/viewer.js b/prismarine-viewer/viewer/lib/viewer.js index 58d07144..0fa16757 100644 --- a/prismarine-viewer/viewer/lib/viewer.js +++ b/prismarine-viewer/viewer/lib/viewer.js @@ -1,21 +1,23 @@ const THREE = require('three') const TWEEN = require('@tweenjs/tween.js') +const { Vec3 } = require('vec3') const { WorldRenderer } = require('./worldrenderer') const { Entities } = require('./entities') const { Primitives } = require('./primitives') const { getVersion } = require('./version') -const { Vec3 } = require('vec3') + +// new THREE.Points(new THREE.BufferGeometry(), new THREE.PointsMaterial()) class Viewer { constructor (renderer, numWorkers = undefined) { this.scene = new THREE.Scene() this.scene.background = new THREE.Color('lightblue') - this.ambientLight = new THREE.AmbientLight(0xcccccc) + this.ambientLight = new THREE.AmbientLight(0xcc_cc_cc) this.scene.add(this.ambientLight) - this.directionalLight = new THREE.DirectionalLight(0xffffff, 0.5) + this.directionalLight = new THREE.DirectionalLight(0xff_ff_ff, 0.5) this.directionalLight.position.set(1, 1, 0.5).normalize() this.directionalLight.castShadow = true this.scene.add(this.directionalLight) @@ -40,7 +42,7 @@ class Viewer { setVersion (userVersion) { const texturesVersion = getVersion(userVersion) - console.log('Using version:', userVersion, 'textures:', texturesVersion) + console.log('[viewer] Using version:', userVersion, 'textures:', texturesVersion) this.version = userVersion this.world.setVersion(userVersion, texturesVersion) this.entities.clear() @@ -103,7 +105,7 @@ class Viewer { mouse.x = (evt.clientX / this.domElement.clientWidth) * 2 - 1 mouse.y = -(evt.clientY / this.domElement.clientHeight) * 2 + 1 raycaster.setFromCamera(mouse, this.camera) - const ray = raycaster.ray + const { ray } = raycaster emitter.emit('mouseClick', { origin: ray.origin, direction: ray.direction, button: evt.button }) }) } diff --git a/src/index.ts b/src/index.ts index 5e1723b9..0567c8dc 100644 --- a/src/index.ts +++ b/src/index.ts @@ -115,6 +115,9 @@ document.body.appendChild(renderer.domElement) // Create viewer const viewer: import('prismarine-viewer/viewer/lib/viewer').Viewer = new Viewer(renderer, options.numWorkers) window.viewer = viewer +viewer.entities.entitiesOptions = { + fontFamily: 'mojangles' +} initPanoramaOptions(viewer) watchTexturepackInViewer(viewer) @@ -316,7 +319,7 @@ async function connect(connectOptions: { }) if (proxy) { - console.log(`using proxy ${proxy.host}:${proxy.port}`) + console.log(`using proxy ${proxy.host}${proxy.port && `:${proxy.port}`}`) net['setProxy']({ hostname: proxy.host, port: proxy.port }) } @@ -331,12 +334,13 @@ async function connect(connectOptions: { await genTexturePackTextures(version) } catch (err) { console.error(err) - const doContinue = prompt('Failed to apply texture pack. See errors in the console. Continue?') + const doContinue = confirm('Failed to apply texture pack. See errors in the console. Continue?') if (!doContinue) { - setLoadingScreenStatus(undefined) + throw err } } await loadScript(`./mc-data/${toMajorVersion(version)}.js`) + viewer.setVersion(version) } const downloadVersion = connectOptions.botVersion || (singeplayer ? serverOptions.version : undefined) @@ -368,7 +372,7 @@ async function connect(connectOptions: { } } - setLoadingScreenStatus('Creating mineflayer bot') + setLoadingScreenStatus('Connecting to server') bot = mineflayer.createBot({ host: server.host, port: +server.port, @@ -517,7 +521,6 @@ async function connect(connectOptions: { }) bot.on('physicsTick', () => updateCursor()) - viewer.setVersion(version) const debugMenu = hud.shadowRoot.querySelector('#debug-overlay') @@ -684,7 +687,6 @@ async function connect(connectOptions: { bot.clearControlStates() }, false) - setLoadingScreenStatus('Done!') console.log('Done!') hud.init(renderer, bot, server.host) @@ -692,16 +694,13 @@ async function connect(connectOptions: { blockInteraction.init() errorAbortController.abort() - setTimeout(() => { - if (loadingScreen.hasError) return - // remove loading screen, wait a second to make sure a frame has properly rendered - setLoadingScreenStatus(undefined) - void viewer.waitForChunksToRender().then(() => { - console.log('All done and ready!') - document.dispatchEvent(new Event('cypress-world-ready')) - }) - miscUiState.gameLoaded = true - }, singeplayer ? 0 : 2500) + if (loadingScreen.hasError) return + setLoadingScreenStatus(undefined) + miscUiState.gameLoaded = true + void viewer.waitForChunksToRender().then(() => { + console.log('All done and ready!') + document.dispatchEvent(new Event('cypress-world-ready')) + }) }) }