Skip to content

Commit

Permalink
feat: implement experimental clipWorldBelowY setting for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
zardoy committed Nov 9, 2024
1 parent 574cdfc commit 32931ef
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 5 deletions.
3 changes: 2 additions & 1 deletion prismarine-viewer/viewer/lib/mesher/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ export const defaultMesherConfig = {
smoothLighting: true,
outputFormat: 'threeJs' as 'threeJs' | 'webgpu',
textureSize: 1024, // for testing
debugModelVariant: undefined as undefined | number[]
debugModelVariant: undefined as undefined | number[],
clipWorldBelowY: undefined as undefined | number
}

export type MesherConfig = typeof defaultMesherConfig
Expand Down
6 changes: 5 additions & 1 deletion prismarine-viewer/viewer/lib/worldrendererCommon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,10 @@ export abstract class WorldRendererCommon<WorkerSend = any, WorkerReceive = any>
console.log('texture loaded')
}

get worldMinYRender () {
return Math.floor(Math.max(this.worldConfig.minY, this.mesherConfig.clipWorldBelowY ?? -Infinity) / 16) * 16
}

addColumn (x: number, z: number, chunk: any, isLightUpdate: boolean) {
if (!this.active) return
if (this.workers.length === 0) throw new Error('workers not initialized yet')
Expand All @@ -330,7 +334,7 @@ export abstract class WorldRendererCommon<WorkerSend = any, WorkerReceive = any>
// todo optimize
worker.postMessage({ type: 'chunk', x, z, chunk })
}
for (let y = this.worldConfig.minY; y < this.worldConfig.worldHeight; y += 16) {
for (let y = this.worldMinYRender; y < this.worldConfig.worldHeight; y += 16) {
const loc = new Vec3(x, y, z)
this.setSectionDirty(loc)
if (this.neighborChunkUpdates && (!isLightUpdate || this.mesherConfig.smoothLighting)) {
Expand Down
7 changes: 4 additions & 3 deletions src/optionsStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ const defaultOptions = {

// antiAliasing: false,

clipWorldBelowY: undefined as undefined | number, // will be removed
showChunkBorders: false, // todo rename option
frameLimit: false as number | false,
alwaysBackupWorldBeforeLoading: undefined as boolean | undefined | null,
Expand Down Expand Up @@ -157,7 +158,7 @@ subscribe(options, () => {
localStorage.options = JSON.stringify(saveOptions)
})

type WatchValue = <T extends Record<string, any>>(proxy: T, callback: (p: T) => void) => void
type WatchValue = <T extends Record<string, any>>(proxy: T, callback: (p: T, isChanged: boolean) => void) => void

export const watchValue: WatchValue = (proxy, callback) => {
const watchedProps = new Set<string>()
Expand All @@ -166,10 +167,10 @@ export const watchValue: WatchValue = (proxy, callback) => {
watchedProps.add(p.toString())
return Reflect.get(target, p, receiver)
},
}))
}), false)
for (const prop of watchedProps) {
subscribeKey(proxy, prop, () => {
callback(proxy)
callback(proxy, true)
})
}
}
Expand Down
6 changes: 6 additions & 0 deletions src/watchOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ export const watchOptionsAfterViewerInit = () => {
watchValue(options, o => {
viewer.world.displayStats = o.renderDebug === 'advanced'
})
watchValue(options, (o, isChanged) => {
viewer.world.mesherConfig.clipWorldBelowY = o.clipWorldBelowY
if (isChanged) {
(viewer.world as WorldRendererThree).rerenderAllChunks()
}
})

viewer.world.mesherConfig.smoothLighting = options.smoothLighting
subscribeKey(options, 'smoothLighting', () => {
Expand Down

0 comments on commit 32931ef

Please sign in to comment.