Skip to content
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

fix: improve block light calculation #116

Open
wants to merge 3 commits into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions prismarine-viewer/examples/playground.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ async function main () {
viewer.entities.onSkinUpdate = () => {
viewer.render()
}
viewer.world.mesherConfig.enableLighting = false

viewer.listen(worldView)
// Load chunks
Expand Down
11 changes: 10 additions & 1 deletion prismarine-viewer/viewer/lib/mesher/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,15 @@ function renderElement (world: World, cursor: Vec3, element, doAO: boolean, attr
const maxy = element.to[1]
const maxz = element.to[2]

const isConnectedWithTheBlock = (
(face === 'up' && maxy === 16) ||
(face === 'down' && miny === 0) ||
(face === 'north' && minz === 0) ||
(face === 'south' && maxz === 16) ||
(face === 'west' && minx === 0) ||
(face === 'east' && maxx === 16)
)

const u = eFace.texture.u
const v = eFace.texture.v
const su = eFace.texture.su
Expand Down Expand Up @@ -349,7 +358,7 @@ function renderElement (world: World, cursor: Vec3, element, doAO: boolean, attr

const aos: number[] = []
const neighborPos = position.plus(new Vec3(...dir))
const baseLight = world.getLight(neighborPos) / 15
const baseLight = world.getLight(isConnectedWithTheBlock ? neighborPos : position) / 15
for (const pos of corners) {
let vertex = [
(pos[0] ? maxx : minx),
Expand Down
14 changes: 14 additions & 0 deletions prismarine-viewer/viewer/lib/mesher/test/mesherTester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,22 @@ export const setup = (version, initialBlocks: [number[], string][]) => {
}
reload()

const getLights = () => {
return Object.fromEntries(getGeometry().faces.map(({ face, light }) => ([face, light * 15 - 2])))
}

const setLight = (x: number, y: number, z: number, val = 0) => {
// create columns first
chunk1.setBlockLight(pos.offset(x, y, z), 15)
chunk1.setSkyLight(pos.offset(x, y, z), 15)
chunk1.setBlockLight(pos.offset(x, y, z), val)
chunk1.setSkyLight(pos.offset(x, y, z), 0)
}

return {
mesherWorld,
setLight,
getLights,
getGeometry,
pos,
mcData,
Expand Down
107 changes: 78 additions & 29 deletions prismarine-viewer/viewer/lib/mesher/test/tests.test.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,44 @@
import { test, expect } from 'vitest'
import { setup } from './mesherTester'
import { BlockNames } from '../../../../../src/mcDataTypes'

const version = '1.18.1'

const addPositions = [
// [[0, 0, 0], 'diamond_block'],
[[1, 0, 0], 'stone'],
[[-1, 0, 0], 'stone'],
[[0, 1, 0], 'stone'],
[[0, -1, 0], 'stone'],
[[0, 0, 1], 'stone'],
[[0, 0, -1], 'stone'],
// [[0, 0, 0], 'diamond_block'],
[[1, 0, 0], 'stone'],
[[-1, 0, 0], 'stone'],
[[0, 1, 0], 'stone'],
[[0, -1, 0], 'stone'],
[[0, 0, 1], 'stone'],
[[0, 0, -1], 'stone'],
] as const

test('Known blocks are not rendered', () => {
const { mesherWorld, getGeometry, pos, mcData } = setup(version, addPositions as any)
const { mesherWorld, getGeometry, pos, mcData } = setup(version, addPositions as any)

let time = 0
let times = 0
const invalidBlocks = {}/* as {[number, number]} */
for (const block of mcData.blocksArray) {
if (block.maxStateId! - block.minStateId! > 100) continue
for (let i = block.minStateId!; i <= block.maxStateId!; i++) {
if (block.transparent) continue
mesherWorld.setBlockStateId(pos, i)
const start = performance.now()
const { centerFaces, totalTiles, centerTileNeighbors } = getGeometry()
time += performance.now() - start
times++
if (centerFaces === 0 && centerTileNeighbors !== 0) {
if (invalidBlocks[block.name]) continue
invalidBlocks[block.name] = [i - block.minStateId!, centerTileNeighbors]
// console.log('INVALID', block.name, centerTileNeighbors, i - block.minStateId)
}
}
let time = 0
let times = 0
const invalidBlocks = {}/* as {[number, number]} */
for (const block of mcData.blocksArray) {
if (block.maxStateId! - block.minStateId! > 100) continue
for (let i = block.minStateId!; i <= block.maxStateId!; i++) {
if (block.transparent) continue
mesherWorld.setBlockStateId(pos, i)
const start = performance.now()
const { centerFaces, totalTiles, centerTileNeighbors } = getGeometry()
time += performance.now() - start
times++
if (centerFaces === 0 && centerTileNeighbors !== 0) {
if (invalidBlocks[block.name]) continue
invalidBlocks[block.name] = [i - block.minStateId!, centerTileNeighbors]
// console.log('INVALID', block.name, centerTileNeighbors, i - block.minStateId)
}
}
console.log('Average time', time / times)
// Fully expected
expect(invalidBlocks).toMatchInlineSnapshot(`
}
console.log('Average time', time / times)
// Fully expected
expect(invalidBlocks).toMatchInlineSnapshot(`
{
"creeper_head": [
0,
Expand Down Expand Up @@ -94,3 +95,51 @@ test('Known blocks are not rendered', () => {
}
`)
})


test('Light calculated correctly', () => {
const result = {} as Record<string, any>
for (const block of ['oak_fence', 'oak_slab', 'soul_sand'] as BlockNames[]) {
const positions = [
[[0, 0, 0], block]
]
const { getLights, setLight, reload } = setup('1.20.2', positions as any)
setLight(0, 0, 0, 5)
setLight(0, -1, 0, 10)
setLight(0, 1, 0, 10)
setLight(1, 0, 0, 10)
setLight(-1, 0, 0, 10)
setLight(0, 0, 1, 10)
setLight(0, 0, -1, 10)
reload()
result[block] = getLights()
}
expect(result).toMatchInlineSnapshot(`
{
"oak_fence": {
"down": 10,
"east": 5,
"north": 5,
"south": 5,
"up": 10,
"west": 5,
},
"oak_slab": {
"down": 10,
"east": 10,
"north": 10,
"south": 10,
"up": 5,
"west": 10,
},
"soul_sand": {
"down": 10,
"east": 10,
"north": 10,
"south": 10,
"up": 10,
"west": 10,
},
}
`)
})
14 changes: 8 additions & 6 deletions prismarine-viewer/viewer/lib/mesher/world.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,18 @@ export class World {
// if (lightsCache.has(key)) return lightsCache.get(key)
const column = this.getColumnByPos(pos)
if (!column || !hasChunkSection(column, pos)) return 15
const posChunk = posInChunk(pos)
let result = Math.min(
15,
Math.max(
column.getBlockLight(posInChunk(pos)),
Math.min(skyLight, column.getSkyLight(posInChunk(pos)))
column.getBlockLight(posChunk),
Math.min(skyLight, column.getSkyLight(posChunk))
) + 2
)
// lightsCache.set(key, result)
if (result === 2 && this.getBlock(pos)?.name.match(/_stairs|slab/)) { // todo this is obviously wrong
result = this.getLight(pos.offset(0, 1, 0))
}
// if (result === 2 && this.getBlock(pos)?.name.match(/_stairs|slab/)) { // todo this is obviously wrong
// result = this.getLight(pos.offset(0, 1, 0))
// }
if (isNeighbor && result === 2) result = 15 // TODO
return result
}
Expand Down Expand Up @@ -128,7 +129,8 @@ export class World {
}

// todo export in chunk instead
const hasChunkSection = (column, pos) => {
export const hasChunkSection = (column, pos) => {
pos = posInChunk(pos)
if (column._getSection) return column._getSection(pos)
if (column.skyLightSections) {
return column.skyLightSections[getLightSectionIndex(pos, column.minY)] || column.blockLightSections[getLightSectionIndex(pos, column.minY)]
Expand Down
Loading