Skip to content

Commit 09435c2

Browse files
committed
restore playground integration!
1 parent 31bed0c commit 09435c2

11 files changed

Lines changed: 349 additions & 203 deletions

File tree

renderer/playground/baseScene.ts

Lines changed: 248 additions & 135 deletions
Large diffs are not rendered by default.

renderer/playground/playground.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
if (!new URL(location.href).searchParams.get('playground')) location.href = '/?playground=true'
2-
// import { BasePlaygroundScene } from './baseScene'
3-
// import { playgroundGlobalUiState } from './playgroundUi'
4-
// import * as scenes from './scenes'
1+
import { BasePlaygroundScene } from './baseScene'
2+
import { playgroundGlobalUiState } from './playgroundUi'
3+
import * as scenes from './scenes'
54

6-
// const qsScene = new URLSearchParams(window.location.search).get('scene')
7-
// const Scene: typeof BasePlaygroundScene = qsScene ? scenes[qsScene] : scenes.main
8-
// playgroundGlobalUiState.scenes = ['main', 'railsCobweb', 'floorRandom', 'lightingStarfield', 'transparencyIssue', 'entities', 'frequentUpdates', 'slabsOptimization', 'allEntities']
9-
// playgroundGlobalUiState.selected = qsScene ?? 'main'
5+
const qsScene = new URLSearchParams(window.location.search).get('scene')
6+
const Scene: typeof BasePlaygroundScene = qsScene ? scenes[qsScene] : scenes.main
7+
playgroundGlobalUiState.scenes = ['main', 'railsCobweb', 'floorRandom', 'lightingStarfield', 'transparencyIssue', 'entities', 'frequentUpdates', 'slabsOptimization', 'allEntities']
8+
playgroundGlobalUiState.selected = qsScene ?? 'main'
109

11-
// const scene = new Scene()
12-
// globalThis.scene = scene
10+
const scene = new Scene()
11+
globalThis.scene = scene

renderer/playground/playgroundUi.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ export const playgroundGlobalUiState = proxy({
1414
actions: {} as Record<string, () => void>,
1515
})
1616

17-
renderToDom(<Playground />)
17+
renderToDom(<Playground />, {
18+
strictMode: false,
19+
selector: '#react-root',
20+
})
1821

1922
function Playground () {
2023
useEffect(() => {

renderer/playground/scenes/main.ts

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
1-
//@ts-nocheck
21
// eslint-disable-next-line import/no-named-as-default
32
import GUI, { Controller } from 'lil-gui'
43
import * as THREE from 'three'
54
import JSZip from 'jszip'
65
import { BasePlaygroundScene } from '../baseScene'
76
import { TWEEN_DURATION } from '../../viewer/three/entities'
87
import { EntityMesh } from '../../viewer/three/entity/EntityMesh'
8+
import supportedVersions from '../../../src/supportedVersions.mjs'
9+
10+
const includedVersions = globalThis.includedVersions ?? supportedVersions
911

1012
class MainScene extends BasePlaygroundScene {
1113
// eslint-disable-next-line @typescript-eslint/no-useless-constructor
1214
constructor (...args) {
13-
//@ts-expect-error
1415
super(...args)
1516
}
1617

1718
override initGui (): void {
1819
// initial values
1920
this.params = {
20-
version: globalThis.includedVersions.at(-1),
21+
version: includedVersions.at(-1),
2122
skipQs: '',
2223
block: '',
2324
metadata: 0,
@@ -35,7 +36,7 @@ class MainScene extends BasePlaygroundScene {
3536
this.metadataGui = this.gui.add(this.params, 'metadata')
3637
this.paramOptions = {
3738
version: {
38-
options: globalThis.includedVersions,
39+
options: includedVersions,
3940
hide: false
4041
},
4142
block: {
@@ -113,8 +114,8 @@ class MainScene extends BasePlaygroundScene {
113114
this.entityUpdateShared()
114115
if (!this.params.entity) return
115116
if (this.params.entity === 'player') {
116-
viewer.entities.updatePlayerSkin('id', viewer.entities.entities.id.username, undefined, true, true)
117-
viewer.entities.playAnimation('id', 'running')
117+
this.worldRenderer!.entities.updatePlayerSkin('id', this.worldRenderer!.entities.entities.id.username, undefined, true, true)
118+
this.worldRenderer!.entities.playAnimation('id', 'running')
118119
}
119120
// let prev = false
120121
// setInterval(() => {
@@ -129,17 +130,17 @@ class MainScene extends BasePlaygroundScene {
129130
// entityRotationFolder.open()
130131
},
131132
supportBlock: () => {
132-
viewer.setBlockStateId(this.targetPos.offset(0, -1, 0), this.params.supportBlock ? 1 : 0)
133+
this.worldView!.setBlockStateId(this.targetPos.offset(0, -1, 0), this.params.supportBlock ? 1 : 0)
133134
},
134135
modelVariant: () => {
135-
viewer.world.mesherConfig.debugModelVariant = this.params.modelVariant === 0 ? undefined : [this.params.modelVariant]
136+
this.worldRenderer!.worldRendererConfig.debugModelVariant = this.params.modelVariant === 0 ? undefined : [this.params.modelVariant]
136137
}
137138
}
138139

139140
entityUpdateShared () {
140-
viewer.entities.clear()
141+
this.worldRenderer!.entities.clear()
141142
if (!this.params.entity) return
142-
worldView!.emit('entity', {
143+
this.worldView!.emit('entity', {
143144
id: 'id', name: this.params.entity, pos: this.targetPos.offset(0.5, 1, 0.5), width: 1, height: 1, username: localStorage.testUsername, yaw: Math.PI, pitch: 0
144145
})
145146
const enableSkeletonDebug = (obj) => {
@@ -153,14 +154,14 @@ class MainScene extends BasePlaygroundScene {
153154
if (typeof child === 'object') enableSkeletonDebug(child)
154155
}
155156
}
156-
enableSkeletonDebug(viewer.entities.entities['id'])
157+
enableSkeletonDebug(this.worldRenderer!.entities.entities['id'])
157158
setTimeout(() => {
158-
viewer.render()
159+
this.render()
159160
}, TWEEN_DURATION)
160161
}
161162

162163
blockIsomorphicRenderBundle () {
163-
const { renderer } = viewer
164+
const { renderer } = this.worldRenderer!
164165

165166
const canvas = renderer.domElement
166167
const onlyCurrent = !confirm('Ok - render all blocks, Cancel - render only current one')
@@ -169,28 +170,28 @@ class MainScene extends BasePlaygroundScene {
169170
const size = parseInt(sizeRaw, 10)
170171
// const size = 512
171172

172-
this.ignoreResize = true
173173
canvas.width = size
174174
canvas.height = size
175175
renderer.setSize(size, size)
176176

177-
viewer.camera = new THREE.OrthographicCamera(-1, 1, 1, -1, 0, 10)
178-
viewer.scene.background = null
177+
// Temporarily replace PerspectiveCamera with OrthographicCamera for block rendering
178+
this.worldRenderer!.camera = new THREE.OrthographicCamera(-1, 1, 1, -1, 0, 10) as any
179+
this.worldRenderer!.scene.background = null
179180

180181
const rad = THREE.MathUtils.degToRad(-120)
181-
viewer.directionalLight.position.set(
182+
this.worldRenderer!.directionalLight.position.set(
182183
Math.cos(rad),
183184
Math.sin(rad),
184185
0.2
185186
).normalize()
186-
viewer.directionalLight.intensity = 1
187+
this.worldRenderer!.directionalLight.intensity = 1
187188

188189
const cameraPos = this.targetPos.offset(2, 2, 2)
189190
const pitch = THREE.MathUtils.degToRad(-30)
190191
const yaw = THREE.MathUtils.degToRad(45)
191-
viewer.camera.rotation.set(pitch, yaw, 0, 'ZYX')
192-
// viewer.camera.lookAt(center.x + 0.5, center.y + 0.5, center.z + 0.5)
193-
viewer.camera.position.set(cameraPos.x + 1, cameraPos.y + 0.5, cameraPos.z + 1)
192+
this.worldRenderer!.camera.rotation.set(pitch, yaw, 0, 'ZYX')
193+
// this.worldRenderer!.camera.lookAt(center.x + 0.5, center.y + 0.5, center.z + 0.5)
194+
this.worldRenderer!.camera.position.set(cameraPos.x + 1, cameraPos.y + 0.5, cameraPos.z + 1)
194195

195196
const allBlocks = mcData.blocksArray.map(b => b.name)
196197
// const allBlocks = ['stone', 'warped_slab']
@@ -206,17 +207,17 @@ class MainScene extends BasePlaygroundScene {
206207
// onUpdate.block()
207208
// applyChanges(false, true)
208209
}
209-
void viewer.waitForChunksToRender().then(async () => {
210+
void this.worldRenderer!.waitForChunksToRender().then(async () => {
210211
// wait for next macro task
211212
await new Promise(resolve => {
212213
setTimeout(resolve, 0)
213214
})
214215
if (onlyCurrent) {
215-
viewer.render()
216+
this.render()
216217
onWorldUpdate()
217218
} else {
218219
// will be called on every render update
219-
viewer.world.renderUpdateEmitter.addListener('update', onWorldUpdate)
220+
this.worldRenderer!.renderUpdateEmitter.addListener('update', onWorldUpdate)
220221
updateBlock()
221222
}
222223
})
@@ -236,7 +237,7 @@ class MainScene extends BasePlaygroundScene {
236237
URL.revokeObjectURL(dataUrlZip)
237238
console.log('end')
238239

239-
viewer.world.renderUpdateEmitter.removeListener('update', onWorldUpdate)
240+
this.worldRenderer!.renderUpdateEmitter.removeListener('update', onWorldUpdate)
240241
}
241242

242243
async function onWorldUpdate () {
@@ -295,7 +296,7 @@ class MainScene extends BasePlaygroundScene {
295296
}
296297
}
297298

298-
worldView!.setBlockStateId(this.targetPos, block.stateId ?? 0)
299+
this.worldView!.setBlockStateId(this.targetPos, block.stateId ?? 0)
299300
console.log('up stateId', block.stateId)
300301
this.params.metadata = block.metadata
301302
this.metadataGui.updateDisplay()

renderer/viewer/lib/worldrendererCommon.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export const defaultWorldRendererConfig = {
3535
// Debug settings
3636
showChunkBorders: false,
3737
enableDebugOverlay: false,
38+
debugModelVariant: undefined as undefined | number[],
3839

3940
// Performance settings
4041
mesherWorkers: 4,
@@ -582,7 +583,7 @@ export abstract class WorldRendererCommon<WorkerSend = any, WorkerReceive = any>
582583
smoothLighting: this.worldRendererConfig.smoothLighting,
583584
outputFormat: this.outputFormat,
584585
// textureSize: this.resourcesManager.currentResources!.blocksAtlasParser.atlas.latest.width,
585-
debugModelVariant: undefined,
586+
debugModelVariant: this.worldRendererConfig.debugModelVariant,
586587
clipWorldBelowY: this.worldRendererConfig.clipWorldBelowY,
587588
disableSignsMapsSupport: !this.worldRendererConfig.extraBlockRenderers,
588589
worldMinY: this.worldMinYRender,

src/app/progressControllers.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { options } from '../optionsStorage'
33
import { progressNotificationsProxy, setNotificationProgress } from '../react/NotificationProvider'
44

55
const loadingChunksProgress = () => {
6+
if (!appViewer) return
67
let gameStopped = false
78

89
const checkStartProgress = () => {

src/appParams.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export type AppQsParams = {
3535
// UI params
3636
modal?: string
3737
viewerConnect?: string
38+
playground?: string
3839
// Map version param
3940
mapVersion?: string
4041
// Command params

src/appViewerLoad.ts

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,34 +18,36 @@ const loadBackend = async () => {
1818
await appViewer.loadBackend(backend)
1919
}
2020
window.loadBackend = loadBackend
21-
if (process.env.SINGLE_FILE_BUILD_MODE) {
22-
const unsub = subscribeKey(miscUiState, 'fsReady', () => {
23-
if (miscUiState.fsReady) {
24-
// don't do it earlier to load fs and display menu faster
21+
22+
export const appLoadBackend = async () => {
23+
if (process.env.SINGLE_FILE_BUILD_MODE) {
24+
const unsub = subscribeKey(miscUiState, 'fsReady', () => {
25+
if (miscUiState.fsReady) {
26+
// don't do it earlier to load fs and display menu faster
27+
void loadBackend()
28+
unsub()
29+
}
30+
})
31+
} else {
32+
setTimeout(() => {
2533
void loadBackend()
26-
unsub()
34+
})
35+
}
36+
37+
watchOptionsAfterViewerInit()
38+
39+
// reset backend when renderer changes
40+
subscribeKey(options, 'activeRenderer', async () => {
41+
if (appViewer.currentDisplay === 'world' && bot) {
42+
appViewer.resetBackend(true)
43+
await loadBackend()
44+
void appViewer.startWithBot()
2745
}
2846
})
29-
} else {
30-
setTimeout(() => {
31-
void loadBackend()
32-
})
3347
}
3448

3549
const animLoop = () => {
3650
for (const fn of beforeRenderFrame) fn()
3751
requestAnimationFrame(animLoop)
3852
}
3953
requestAnimationFrame(animLoop)
40-
41-
watchOptionsAfterViewerInit()
42-
43-
// reset backend when renderer changes
44-
45-
subscribeKey(options, 'activeRenderer', async () => {
46-
if (appViewer.currentDisplay === 'world' && bot) {
47-
appViewer.resetBackend(true)
48-
await loadBackend()
49-
void appViewer.startWithBot()
50-
}
51-
})

src/index.ts

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,19 +93,26 @@ import { initMotionTracking } from './react/uiMotion'
9393
import { UserError } from './mineflayer/userError'
9494
import { startLocalReplayServer } from './packetsReplay/replayPackets'
9595
import { createFullScreenProgressReporter, createWrappedProgressReporter, ProgressReporter } from './core/progressReporter'
96-
import { appViewer } from './appViewer'
97-
import './appViewerLoad'
9896
import { registerOpenBenchmarkListener } from './benchmark'
9997
import { tryHandleBuiltinCommand } from './builtinCommands'
10098
import { loadingTimerState } from './react/LoadingTimer'
10199
import { loadPluginsIntoWorld } from './react/CreateWorldProvider'
102100
import { getCurrentProxy, getCurrentUsername } from './react/ServersList'
101+
import { isPlayground } from './playgroundIntegration'
102+
import { appLoadBackend } from './appViewerLoad'
103103

104104
window.debug = debug
105105
window.beforeRenderFrame = []
106106

107107
// ACTUAL CODE
108108

109+
if (!isPlayground) {
110+
void appLoadBackend()
111+
}
112+
if (isPlayground) {
113+
void import('renderer/playground/playground')
114+
}
115+
109116
void registerServiceWorker().then(() => {
110117
mainMenuState.serviceWorkerLoaded = true
111118
})
@@ -1080,11 +1087,14 @@ const maybeEnterGame = () => {
10801087
void possiblyHandleStateVariable()
10811088
}
10821089

1083-
try {
1084-
maybeEnterGame()
1085-
} catch (err) {
1086-
console.error(err)
1087-
alert(`Something went wrong: ${err}`)
1090+
// Skip game connection logic in playground mode
1091+
if (!isPlayground) {
1092+
try {
1093+
maybeEnterGame()
1094+
} catch (err) {
1095+
console.error(err)
1096+
alert(`Something went wrong: ${err}`)
1097+
}
10881098
}
10891099

10901100
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
@@ -1095,5 +1105,7 @@ if (initialLoader) {
10951105
}
10961106
window.pageLoaded = true
10971107

1098-
appViewer.waitBackendLoadPromises.push(appStartup())
1108+
if (!isPlayground) {
1109+
appViewer.waitBackendLoadPromises.push(appStartup())
1110+
}
10991111
registerOpenBenchmarkListener()

src/playgroundIntegration.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export const urlParams = new URLSearchParams(window.location.search)
2+
export const isPlayground = urlParams.get('playground') === 'true' || urlParams.get('playground') === '1'
3+
4+
if (isPlayground) {
5+
// hide #ui-root
6+
const uiRoot = document.getElementById('ui-root')
7+
if (uiRoot) {
8+
uiRoot.style.display = 'none'
9+
}
10+
}

0 commit comments

Comments
 (0)