proper light decreasing

This commit is contained in:
Bixilon 2022-08-15 09:55:00 +02:00
parent d29811c48e
commit 17a54ca760
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
4 changed files with 44 additions and 38 deletions

View File

@ -300,6 +300,6 @@ interface ProfileManager<T : Profile> {
companion object { companion object {
const val DEFAULT_PROFILE_NAME = "Default" const val DEFAULT_PROFILE_NAME = "Default"
val PROFILE_REGEX = "[\\w\\d_ ]{1,32}".toRegex() val PROFILE_REGEX = "[\\w_ ]{1,32}".toRegex()
} }
} }

View File

@ -81,11 +81,10 @@ data class BlockState(
override fun toString(): String { override fun toString(): String {
val out = StringBuilder() val out = StringBuilder()
out.append(block.resourceLocation.toString()) out.append(block.resourceLocation.toString())
out.append(" (")
if (properties.isNotEmpty()) { if (properties.isNotEmpty()) {
if (out.isNotEmpty()) { if (out.isNotEmpty()) {
out.append(", ") out.append(", ")
} else {
out.append(" (")
} }
out.append("properties=") out.append("properties=")
out.append(properties) out.append(properties)
@ -93,7 +92,7 @@ data class BlockState(
if (out.isNotEmpty()) { if (out.isNotEmpty()) {
out.append(")") out.append(")")
} }
return out.toString() return out.toString().removeSuffix("()")
} }
companion object { companion object {

View File

@ -36,7 +36,7 @@ class SectionLight(
} }
if (luminance > previousLuminance) { if (luminance > previousLuminance) {
onLightIncrease(x, y, z, luminance) traceIncrease(x, y, z, luminance, false)
} else { } else {
startDecreaseTrace(x, y, z, luminance) startDecreaseTrace(x, y, z, luminance)
} }
@ -73,71 +73,75 @@ class SectionLight(
val expectedNeighbourLevel = if (expectedLuminance <= 1) 0 else (expectedLuminance - 1).toByte() val expectedNeighbourLevel = if (expectedLuminance <= 1) 0 else (expectedLuminance - 1).toByte()
var highestLevel = expectedLuminance var highestLevel = expectedNeighbourLevel
if (direction.x <= 0) { if (direction.x <= 0) {
highestLevel = maxOf( highestLevel = maxOf(
highestLevel, (if (x > 0) { highestLevel, (if (x > 0) {
traceDecrease(x - 1, y, z, expectedNeighbourLevel, light, Vec3i(-1, direction.y, direction.z)) traceDecrease(x - 1, y, z, highestLevel, light, Vec3i(-1, direction.y, direction.z))
} else { } else {
neighbours[Directions.O_WEST]?.light?.traceDecrease(ProtocolDefinition.SECTION_MAX_X, y, z, expectedLuminance, previous, Vec3i(-1, direction.y, direction.z)) ?: 0 neighbours[Directions.O_WEST]?.light?.traceDecrease(ProtocolDefinition.SECTION_MAX_X, y, z, highestLevel, previous, Vec3i(-1, direction.y, direction.z)) ?: 0
} - 1).toByte() } - 1).toByte()
) )
} }
if (direction.x >= 0) { if (direction.x >= 0) {
highestLevel = maxOf( highestLevel = maxOf(
highestLevel, (if (x < ProtocolDefinition.SECTION_MAX_X) { highestLevel, (if (x < ProtocolDefinition.SECTION_MAX_X) {
traceDecrease(x + 1, y, z, expectedNeighbourLevel, light, Vec3i(1, direction.y, direction.z)) traceDecrease(x + 1, y, z, highestLevel, light, Vec3i(1, direction.y, direction.z))
} else { } else {
neighbours[Directions.O_EAST]?.light?.traceDecrease(0, y, z, expectedNeighbourLevel, light, Vec3i(1, direction.y, direction.z)) ?: 0 neighbours[Directions.O_EAST]?.light?.traceDecrease(0, y, z, highestLevel, light, Vec3i(1, direction.y, direction.z)) ?: 0
} - 1).toByte() } - 1).toByte()
) )
} }
if (direction.y <= 0) { if (direction.y <= 0) {
highestLevel = maxOf( highestLevel = maxOf(
highestLevel, (if (y > 0) { highestLevel, (if (y > 0) {
traceDecrease(x, y - 1, z, expectedNeighbourLevel, light, Vec3i(direction.x, -1, direction.z)) traceDecrease(x, y - 1, z, highestLevel, light, Vec3i(direction.x, -1, direction.z))
} else { } else {
neighbours[Directions.O_DOWN]?.light?.traceDecrease(x, ProtocolDefinition.SECTION_MAX_Y, z, expectedNeighbourLevel, light, Vec3i(direction.x, -1, direction.z)) ?: 0 neighbours[Directions.O_DOWN]?.light?.traceDecrease(x, ProtocolDefinition.SECTION_MAX_Y, z, highestLevel, light, Vec3i(direction.x, -1, direction.z)) ?: 0
} - 1).toByte() } - 1).toByte()
) )
} }
if (direction.y >= 0) { if (direction.y >= 0) {
highestLevel = maxOf( highestLevel = maxOf(
highestLevel, (if (y < ProtocolDefinition.SECTION_MAX_Y) { highestLevel, (if (y < ProtocolDefinition.SECTION_MAX_Y) {
traceDecrease(x, y + 1, z, expectedNeighbourLevel, light, Vec3i(direction.x, 1, direction.z)) traceDecrease(x, y + 1, z, highestLevel, light, Vec3i(direction.x, 1, direction.z))
} else { } else {
neighbours[Directions.O_UP]?.light?.traceDecrease(x, 0, z, expectedNeighbourLevel, light, Vec3i(direction.x, 1, direction.z)) ?: 0 neighbours[Directions.O_UP]?.light?.traceDecrease(x, 0, z, highestLevel, light, Vec3i(direction.x, 1, direction.z)) ?: 0
} - 1).toByte() } - 1).toByte()
) )
} }
if (direction.z <= 0) { if (direction.z <= 0) {
highestLevel = maxOf( highestLevel = maxOf(
highestLevel, (if (z > 0) { highestLevel, (if (z > 0) {
traceDecrease(x, y, z - 1, expectedNeighbourLevel, light, Vec3i(direction.x, direction.y, -1)) traceDecrease(x, y, z - 1, highestLevel, light, Vec3i(direction.x, direction.y, -1))
} else { } else {
neighbours[Directions.O_NORTH]?.light?.traceDecrease(x, y, ProtocolDefinition.SECTION_MAX_Z, expectedNeighbourLevel, light, Vec3i(direction.x, direction.y, -1)) ?: 0 neighbours[Directions.O_NORTH]?.light?.traceDecrease(x, y, ProtocolDefinition.SECTION_MAX_Z, highestLevel, light, Vec3i(direction.x, direction.y, -1)) ?: 0
} - 1).toByte() } - 1).toByte()
) )
} }
if (direction.z >= 0) { if (direction.z >= 0) {
highestLevel = maxOf( highestLevel = maxOf(
highestLevel, (if (z < ProtocolDefinition.SECTION_MAX_Z) { highestLevel, (if (z < ProtocolDefinition.SECTION_MAX_Z) {
traceDecrease(x, y, z + 1, expectedNeighbourLevel, light, Vec3i(direction.x, direction.y, 1)) traceDecrease(x, y, z + 1, highestLevel, light, Vec3i(direction.x, direction.y, 1))
} else { } else {
neighbours[Directions.O_SOUTH]?.light?.traceDecrease(x, y, 0, expectedNeighbourLevel, light, Vec3i(direction.x, direction.y, 1)) ?: 0 neighbours[Directions.O_SOUTH]?.light?.traceDecrease(x, y, 0, highestLevel, light, Vec3i(direction.x, direction.y, 1)) ?: 0
} - 1).toByte() } - 1).toByte()
) )
} }
this.light[index] = highestLevel this.light[index] = highestLevel
if (highestLevel > expectedNeighbourLevel) {
// level increased, we need to trace an increase now
traceIncrease(x, y, z, highestLevel, true)
}
return highestLevel return highestLevel
} }
private fun onLightIncrease(x: Int, y: Int, z: Int, nextLuminance: Byte) { private fun traceIncrease(x: Int, y: Int, z: Int, nextLuminance: Byte, force: Boolean) {
val index = getIndex(x, y, z) val index = getIndex(x, y, z)
val block = section.blocks.unsafeGet(index) val block = section.blocks.unsafeGet(index)
val blockLuminance = block?.luminance ?: 0 val blockLuminance = block?.luminance ?: 0
@ -152,7 +156,7 @@ class SectionLight(
luminance = blockLuminance luminance = blockLuminance
} }
val currentLight = light[index].toInt() // and 0x0F // we just care about block light val currentLight = light[index].toInt() // and 0x0F // we just care about block light
if (currentLight >= luminance) { if (currentLight >= luminance && !force) {
// light is already higher, no need to trace // light is already higher, no need to trace
return return
} }
@ -176,45 +180,48 @@ class SectionLight(
val neighbourLuminance = (luminance - 1).toByte() val neighbourLuminance = (luminance - 1).toByte()
if (y > 0) { if (y > 0) {
onLightIncrease(x, y - 1, z, neighbourLuminance) traceIncrease(x, y - 1, z, neighbourLuminance, false)
} else { } else {
neighbours[Directions.O_DOWN]?.light?.onLightIncrease(x, ProtocolDefinition.SECTION_MAX_Y, z, neighbourLuminance) neighbours[Directions.O_DOWN]?.light?.traceIncrease(x, ProtocolDefinition.SECTION_MAX_Y, z, neighbourLuminance, false)
} }
if (y < ProtocolDefinition.SECTION_MAX_Y) { if (y < ProtocolDefinition.SECTION_MAX_Y) {
onLightIncrease(x, y + 1, z, neighbourLuminance) traceIncrease(x, y + 1, z, neighbourLuminance, false)
} else { } else {
neighbours[Directions.O_UP]?.light?.onLightIncrease(x, 0, z, neighbourLuminance) neighbours[Directions.O_UP]?.light?.traceIncrease(x, 0, z, neighbourLuminance, false)
} }
if (z > 0) { if (z > 0) {
onLightIncrease(x, y, z - 1, neighbourLuminance) traceIncrease(x, y, z - 1, neighbourLuminance, false)
} else { } else {
neighbours[Directions.O_NORTH]?.light?.onLightIncrease(x, y, ProtocolDefinition.SECTION_MAX_Z, neighbourLuminance) neighbours[Directions.O_NORTH]?.light?.traceIncrease(x, y, ProtocolDefinition.SECTION_MAX_Z, neighbourLuminance, false)
} }
if (z < ProtocolDefinition.SECTION_MAX_Y) { if (z < ProtocolDefinition.SECTION_MAX_Y) {
onLightIncrease(x, y, z + 1, neighbourLuminance) traceIncrease(x, y, z + 1, neighbourLuminance, false)
} else { } else {
neighbours[Directions.O_SOUTH]?.light?.onLightIncrease(x, y, 0, neighbourLuminance) neighbours[Directions.O_SOUTH]?.light?.traceIncrease(x, y, 0, neighbourLuminance, false)
} }
if (x > 0) { if (x > 0) {
onLightIncrease(x - 1, y, z, neighbourLuminance) traceIncrease(x - 1, y, z, neighbourLuminance, false)
} else { } else {
neighbours[Directions.O_WEST]?.light?.onLightIncrease(ProtocolDefinition.SECTION_MAX_X, y, z, neighbourLuminance) neighbours[Directions.O_WEST]?.light?.traceIncrease(ProtocolDefinition.SECTION_MAX_X, y, z, neighbourLuminance, false)
} }
if (x < ProtocolDefinition.SECTION_MAX_X) { if (x < ProtocolDefinition.SECTION_MAX_X) {
onLightIncrease(x + 1, y, z, neighbourLuminance) traceIncrease(x + 1, y, z, neighbourLuminance, false)
} else { } else {
neighbours[Directions.O_EAST]?.light?.onLightIncrease(0, y, z, neighbourLuminance) neighbours[Directions.O_EAST]?.light?.traceIncrease(0, y, z, neighbourLuminance, false)
}
}
fun resetLight() {
for (index in light.indices) {
light[index] = 0x00.toByte()
} }
} }
fun recalculate() { fun recalculate() {
// clear light resetLight()
for (index in light.indices) {
light[index] = 0x00.toByte()
}
val blocks = section.blocks val blocks = section.blocks
blocks.acquire() blocks.acquire()
@ -227,7 +234,7 @@ class SectionLight(
// block is not emitting light, ignore it // block is not emitting light, ignore it
continue continue
} }
onLightIncrease(x, y, z, luminance) traceIncrease(x, y, z, luminance, false)
} }
} }
} }

View File

@ -157,6 +157,6 @@ class LightMap(private val renderWindow: RenderWindow) {
} }
private companion object { private companion object {
private const val UNIFORM_BUFFER_SIZE = 16 * 16 * 4 // skyLight * blockLight * RGBA private const val UNIFORM_BUFFER_SIZE = ProtocolDefinition.LIGHT_LEVELS * ProtocolDefinition.LIGHT_LEVELS * 4 // skyLight * blockLight * RGBA
} }
} }