Skip to content

Commit

Permalink
optimize light update in single chunks
Browse files Browse the repository at this point in the history
  • Loading branch information
zardoy committed Apr 24, 2024
1 parent 241e5d3 commit 619aa5b
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 13 deletions.
8 changes: 4 additions & 4 deletions prismarine-viewer/viewer/lib/viewer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ export class Viewer {
// this.primitives.clear()
}

addColumn (x, z, chunk) {
this.world.addColumn(x, z, chunk)
addColumn (x, z, chunk, isLightUpdate = false) {
this.world.addColumn(x, z, chunk, isLightUpdate)
}

removeColumn (x: string, z: string) {
Expand Down Expand Up @@ -148,9 +148,9 @@ export class Viewer {
// this.updatePrimitive(p)
})

emitter.on('loadChunk', ({ x, z, chunk, worldConfig }) => {
emitter.on('loadChunk', ({ x, z, chunk, worldConfig, isLightUpdate }) => {
this.world.worldConfig = worldConfig
this.addColumn(x, z, chunk)
this.addColumn(x, z, chunk, isLightUpdate)
})
// todo remove and use other architecture instead so data flow is clear
emitter.on('blockEntities', (blockEntities) => {
Expand Down
6 changes: 3 additions & 3 deletions prismarine-viewer/viewer/lib/worldDataEmitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export class WorldDataEmitter extends EventEmitter {

bot._client.on('update_light', ({ chunkX, chunkZ }) => {
const chunkPos = new Vec3(chunkX * 16, 0, chunkZ * 16)
this.loadChunk(chunkPos)
this.loadChunk(chunkPos, true)
})

this.emitter.on('listening', () => {
Expand Down Expand Up @@ -128,7 +128,7 @@ export class WorldDataEmitter extends EventEmitter {
}
}

async loadChunk (pos: ChunkPos) {
async loadChunk (pos: ChunkPos, isLightUpdate = false) {
const [botX, botZ] = chunkPos(this.lastPos)
const dx = Math.abs(botX - Math.floor(pos.x / 16))
const dz = Math.abs(botZ - Math.floor(pos.z / 16))
Expand All @@ -143,7 +143,7 @@ export class WorldDataEmitter extends EventEmitter {
worldHeight: column['worldHeight'] ?? 256,
}
//@ts-ignore
this.emitter.emit('loadChunk', { x: pos.x, z: pos.z, chunk, blockEntities: column.blockEntities, worldConfig })
this.emitter.emit('loadChunk', { x: pos.x, z: pos.z, chunk, blockEntities: column.blockEntities, worldConfig, isLightUpdate })
this.loadedChunks[`${pos.x},${pos.z}`] = true
}
} else {
Expand Down
12 changes: 7 additions & 5 deletions prismarine-viewer/viewer/lib/worldrendererCommon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ export abstract class WorldRendererCommon<WorkerSend = any, WorkerReceive = any>

}

addColumn (x, z, chunk) {
addColumn (x: number, z: number, chunk: any, isLightUpdate: boolean) {
if (this.workers.length === 0) throw new Error('workers not initialized yet')
this.initialChunksLoad = false
this.loadedChunks[`${x},${z}`] = true
Expand All @@ -222,10 +222,12 @@ export abstract class WorldRendererCommon<WorkerSend = any, WorkerReceive = any>
for (let y = this.worldConfig.minY; y < this.worldConfig.worldHeight; y += 16) {
const loc = new Vec3(x, y, z)
this.setSectionDirty(loc)
this.setSectionDirty(loc.offset(-16, 0, 0))
this.setSectionDirty(loc.offset(16, 0, 0))
this.setSectionDirty(loc.offset(0, 0, -16))
this.setSectionDirty(loc.offset(0, 0, 16))
if (!isLightUpdate || this.mesherConfig.smoothLighting) {
this.setSectionDirty(loc.offset(-16, 0, 0))
this.setSectionDirty(loc.offset(16, 0, 0))
this.setSectionDirty(loc.offset(0, 0, -16))
this.setSectionDirty(loc.offset(0, 0, 16))
}
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/watchOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ export const watchOptionsAfterViewerInit = () => {
viewer.entities.setVisible(o.renderEntities)
})

viewer.world.mesherConfig.smoothLighting = options.smoothLighting
// viewer.world.mesherConfig.smoothLighting = options.smoothLighting
viewer.world.mesherConfig.smoothLighting = false // todo not supported for now
subscribeKey(options, 'smoothLighting', () => {
viewer.world.mesherConfig.smoothLighting = options.smoothLighting;
(viewer.world as WorldRendererThree).rerenderAllChunks()
Expand Down

0 comments on commit 619aa5b

Please sign in to comment.