-
Notifications
You must be signed in to change notification settings - Fork 120
feat: Client Side Light Engine! #304
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: next
Are you sure you want to change the base?
Changes from 13 commits
245300f
d5c61d8
48ead54
037e297
ace45a9
ec6b249
9f505f8
f18b3a1
e10f610
0fa66e2
b1ba2cd
3cd1ac3
1918c68
b4c72db
1f5b682
5a57d29
2f6191a
f4eab39
79f0fdd
27c55b1
f4f5edd
c97c7e0
c4b9c33
7d224fb
d6f394f
ddf0810
5720cfa
7dba526
e95f84e
90de0d0
f185df9
6be3c5c
56aee16
b8c8f8a
6a8d15b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| import { LightWorld, createLightEngineForSyncWorld, convertPrismarineBlockToWorldBlock } from 'minecraft-lighting' | ||
| import { world } from 'prismarine-world' | ||
| import { WorldRendererCommon } from './worldrendererCommon' | ||
|
|
||
| let lightEngine: LightWorld | null = null | ||
| export const getLightEngine = () => { | ||
| if (!lightEngine) throw new Error('Light engine not initialized') | ||
| return lightEngine | ||
| } | ||
| export const getLightEngineSafe = () => { | ||
| return lightEngine | ||
| } | ||
|
|
||
| export const createLightEngine = (world: WorldRendererCommon) => { | ||
| lightEngine = createLightEngineForSyncWorld(world.displayOptions.worldView.world as unknown as world.WorldSync, loadedData, { | ||
| minY: world.worldSizeParams.minY, | ||
| height: world.worldSizeParams.worldHeight, | ||
| // enableSkyLight: false, | ||
| }) | ||
| lightEngine.PARALLEL_CHUNK_PROCESSING = false | ||
| globalThis.lightEngine = lightEngine | ||
| } | ||
|
|
||
| export const processLightChunk = async (x: number, z: number) => { | ||
| const chunkX = Math.floor(x / 16) | ||
| const chunkZ = Math.floor(z / 16) | ||
| const engine = getLightEngine() | ||
| // fillColumnWithZeroLight(engine.externalWorld, chunkX, chunkZ) | ||
|
|
||
| const updated = engine.receiveUpdateColumn(chunkX, chunkZ) | ||
| return updated | ||
| } | ||
|
|
||
| export const dumpLightData = (x: number, z: number) => { | ||
| const engine = getLightEngineSafe() | ||
| return engine?.worldLightHolder.dumpChunk(Math.floor(x / 16), Math.floor(z / 16)) | ||
| } | ||
|
|
||
| export const updateBlockLight = (x: number, y: number, z: number, stateId: number) => { | ||
| const engine = getLightEngine() | ||
| engine.setBlock(x, y, z, convertPrismarineBlockToWorldBlock(mcData.blocks[stateId], loadedData)) | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -7,10 +7,9 @@ import { Vec3 } from 'vec3' | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { BotEvents } from 'mineflayer' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { proxy } from 'valtio' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import TypedEmitter from 'typed-emitter' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { getItemFromBlock } from '../../../src/chatUtils' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { delayedIterator } from '../../playground/shared' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { playerState } from '../../../src/mineflayer/playerState' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { chunkPos } from './simpleUtils' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { processLightChunk, updateBlockLight } from './lightEngine' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| export type ChunkPosKey = string // like '16,16' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| type ChunkPos = { x: number, z: number } // like { x: 16, z: 16 } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -81,6 +80,7 @@ export class WorldDataEmitter extends (EventEmitter as new () => TypedEmitter<Wo | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // return | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| updateBlockLight(position.x, position.y, position.z, stateId) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.emit('blockUpdate', { pos: position, stateId }) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -252,13 +252,23 @@ export class WorldDataEmitter extends (EventEmitter as new () => TypedEmitter<Wo | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| async loadChunk (pos: ChunkPos, isLightUpdate = false, reason = 'spiral') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const [botX, botZ] = chunkPos(this.lastPos) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const chunkX = Math.floor(pos.x / 16) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const chunkZ = Math.floor(pos.z / 16) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const dx = Math.abs(botX - Math.floor(pos.x / 16)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const dz = Math.abs(botZ - Math.floor(pos.z / 16)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const dx = Math.abs(botX - chunkX) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const dz = Math.abs(botZ - chunkZ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (dx <= this.viewDistance && dz <= this.viewDistance) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // eslint-disable-next-line @typescript-eslint/await-thenable -- todo allow to use async world provider but not sure if needed | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const column = await this.world.getColumnAt(pos['y'] ? pos as Vec3 : new Vec3(pos.x, 0, pos.z)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (column) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const result = await processLightChunk(pos.x, pos.z) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!result) return | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Potential logic issue with null/empty array check The check If you're trying to check for an empty array, use -if (!result) return
+if (!result || !result.length) return📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (const affectedChunk of result) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (affectedChunk.x === chunkX && affectedChunk.z === chunkZ) continue | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const loadedChunk = this.loadedChunks[`${affectedChunk.x},${affectedChunk.z}`] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!loadedChunk) continue | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| void this.loadChunk(new Vec3(affectedChunk.x * 16, 0, affectedChunk.z * 16), true) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const result = await processLightChunk(pos.x, pos.z) | |
| if (!result) return | |
| for (const affectedChunk of result) { | |
| if (affectedChunk.x === chunkX && affectedChunk.z === chunkZ) continue | |
| const loadedChunk = this.loadedChunks[`${affectedChunk.x},${affectedChunk.z}`] | |
| if (!loadedChunk) continue | |
| void this.loadChunk(new Vec3(affectedChunk.x * 16, 0, affectedChunk.z * 16), true) | |
| } | |
| async loadChunk (pos: ChunkPos, isLightUpdate = false, recursionDepth = 0) { | |
| // existing code... | |
| const column = await this.world.getColumnAt(pos['y'] ? pos as Vec3 : new Vec3(pos.x, 0, pos.z)) | |
| if (column) { | |
| const result = await processLightChunk(pos.x, pos.z) | |
| if (!result) return | |
| // Prevent excessive recursion by capping the recursion depth | |
| if (recursionDepth >= 5) return | |
| for (const affectedChunk of result) { | |
| if (affectedChunk.x === chunkX && affectedChunk.z === chunkZ) continue | |
| const loadedChunk = this.loadedChunks[`${affectedChunk.x},${affectedChunk.z}`] | |
| if (!loadedChunk) continue | |
| void this.loadChunk(new Vec3(affectedChunk.x * 16, 0, affectedChunk.z * 16), true, recursionDepth + 1) | |
| } | |
| // rest of the existing code... | |
| } | |
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,6 +19,7 @@ import { chunkPos } from './simpleUtils' | |
| import { addNewStat, removeAllStats, removeStat, updatePanesVisibility, updateStatText } from './ui/newStats' | ||
| import { WorldDataEmitter } from './worldDataEmitter' | ||
| import { IPlayerState } from './basePlayerState' | ||
| import { createLightEngine, dumpLightData, getLightEngine, getLightEngineSafe } from './lightEngine' | ||
| import { MesherLogReader } from './mesherlogReader' | ||
|
|
||
| function mod (x, n) { | ||
|
|
@@ -39,6 +40,7 @@ export const defaultWorldRendererConfig = { | |
| clipWorldBelowY: undefined as number | undefined, | ||
| smoothLighting: true, | ||
| enableLighting: true, | ||
| clientSideLighting: false, | ||
| starfield: true, | ||
| addChunksBatchWaitTime: 200, | ||
| vrSupport: true, | ||
|
|
@@ -169,6 +171,8 @@ export abstract class WorldRendererCommon<WorkerSend = any, WorkerReceive = any> | |
| } | ||
|
|
||
| constructor (public readonly resourcesManager: ResourcesManager, public displayOptions: DisplayWorldOptions, public initOptions: GraphicsInitOptions) { | ||
| createLightEngine(this) | ||
|
|
||
| // this.initWorkers(1) // preload script on page load | ||
| this.snapshotInitialValues() | ||
| this.worldRendererConfig = displayOptions.inWorldRenderingConfig | ||
|
|
@@ -610,7 +614,8 @@ export abstract class WorldRendererCommon<WorkerSend = any, WorkerReceive = any> | |
| x, | ||
| z, | ||
| chunk, | ||
| customBlockModels: customBlockModels || undefined | ||
| customBlockModels: customBlockModels || undefined, | ||
| lightData: dumpLightData(x, z) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix commented-out light data dumping. The #!/bin/bash
# Verify the implementation of dumpLightData function
echo "Current dumpLightData implementation:"
rg -A 5 "dumpLightData" --glob "*.{ts,tsx,js,jsx}"🤖 Prompt for AI Agents |
||
| }) | ||
| } | ||
| this.logWorkerWork(() => `-> chunk ${JSON.stringify({ x, z, chunkLength: chunk.length, customBlockModelsLength: customBlockModels ? Object.keys(customBlockModels).length : 0 })}`) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.