mirror of
https://github.com/curioustorvald/Terrarum.git
synced 2026-06-18 22:44:04 +09:00
memoised dynamic luminosity
This commit is contained in:
@@ -7,6 +7,7 @@ import net.torvald.terrarum.gameworld.FluidType
|
||||
import net.torvald.terrarum.gameworld.GameWorld
|
||||
import net.torvald.terrarum.utils.CSVFetcher
|
||||
import net.torvald.terrarum.worlddrawer.LightmapRenderer
|
||||
import net.torvald.util.SortedArrayList
|
||||
import org.apache.commons.csv.CSVRecord
|
||||
import java.io.IOException
|
||||
|
||||
@@ -17,6 +18,8 @@ object BlockCodex {
|
||||
|
||||
private var blockProps = HashMap<Int, BlockProp>()
|
||||
|
||||
val dynamicLights = SortedArrayList<Int>()
|
||||
|
||||
/** 4096 */
|
||||
const val MAX_TERRAIN_TILES = GameWorld.TILES_SUPPORTED
|
||||
|
||||
@@ -45,6 +48,10 @@ object BlockCodex {
|
||||
val id = intVal(it, "id")
|
||||
setProp(id, it)
|
||||
|
||||
if ((blockProps[id]?.dynamicLuminosityFunction ?: 0) != 0) {
|
||||
dynamicLights.add(id)
|
||||
}
|
||||
|
||||
if (id > highestNumber)
|
||||
highestNumber = id
|
||||
}
|
||||
@@ -113,11 +120,11 @@ object BlockCodex {
|
||||
prop.strength = intVal(record, "str")
|
||||
prop.density = intVal(record, "dsty")
|
||||
|
||||
prop.lumColR = floatVal(record, "lumr") / LightmapRenderer.MUL_FLOAT
|
||||
prop.lumColG = floatVal(record, "lumg") / LightmapRenderer.MUL_FLOAT
|
||||
prop.lumColB = floatVal(record, "lumb") / LightmapRenderer.MUL_FLOAT
|
||||
prop.lumColA = floatVal(record, "lumuv") / LightmapRenderer.MUL_FLOAT
|
||||
prop.internalLumCol = Cvec(prop.lumColR, prop.lumColG, prop.lumColB, prop.lumColA)
|
||||
prop.baseLumColR = floatVal(record, "lumr") / LightmapRenderer.MUL_FLOAT
|
||||
prop.baseLumColG = floatVal(record, "lumg") / LightmapRenderer.MUL_FLOAT
|
||||
prop.baseLumColB = floatVal(record, "lumb") / LightmapRenderer.MUL_FLOAT
|
||||
prop.baseLumColA = floatVal(record, "lumuv") / LightmapRenderer.MUL_FLOAT
|
||||
prop.baseLumCol.set(prop.baseLumColR, prop.baseLumColG, prop.baseLumColB, prop.baseLumColA)
|
||||
|
||||
prop.friction = intVal(record, "fr")
|
||||
prop.viscosity = intVal(record, "vscs")
|
||||
|
||||
@@ -44,27 +44,24 @@ class BlockProp {
|
||||
|
||||
|
||||
/** 1.0f for 1023, 0.25f for 255 */
|
||||
var lumColR = 0f
|
||||
var lumColG = 0f
|
||||
var lumColB = 0f
|
||||
var lumColA = 0f
|
||||
lateinit var internalLumCol: Cvec
|
||||
internal var baseLumColR = 0f // base value used to calculate dynamic luminosity
|
||||
internal var baseLumColG = 0f // base value used to calculate dynamic luminosity
|
||||
internal var baseLumColB = 0f // base value used to calculate dynamic luminosity
|
||||
internal var baseLumColA = 0f // base value used to calculate dynamic luminosity
|
||||
internal val baseLumCol = Cvec(0)
|
||||
var lumColR = 0f // memoised value of dynamic luminosity
|
||||
var lumColG = 0f // memoised value of dynamic luminosity
|
||||
var lumColB = 0f // memoised value of dynamic luminosity
|
||||
var lumColA = 0f // memoised value of dynamic luminosity
|
||||
var lumCol = Cvec(0)
|
||||
|
||||
/**
|
||||
* @param luminosity
|
||||
*/
|
||||
inline val luminosity: Cvec
|
||||
get() = BlockPropUtil.getDynamicLumFunc(internalLumCol, dynamicLuminosityFunction)
|
||||
//inline val luminosity: Cvec
|
||||
// get() = BlockPropUtil.getDynamicLumFunc(internalLumCol, dynamicLuminosityFunction)
|
||||
|
||||
fun getLum(channel: Int) = BlockPropUtil.getDynamicLumFuncByChan(
|
||||
when (channel) {
|
||||
0 -> lumColR
|
||||
1 -> lumColG
|
||||
2 -> lumColB
|
||||
3 -> lumColA
|
||||
else -> throw IllegalArgumentException("Invalid channel $channel")
|
||||
}, dynamicLuminosityFunction, channel
|
||||
)
|
||||
fun getLum(channel: Int) = lumCol.getElem(channel)
|
||||
|
||||
var drop: Int = 0
|
||||
|
||||
|
||||
@@ -86,13 +86,28 @@ object BlockPropUtil {
|
||||
|
||||
// pulsate-related vars
|
||||
if (pulsateFuncX > pulsateCycleDuration) pulsateFuncX -= pulsateCycleDuration
|
||||
|
||||
// update the memoised values in props
|
||||
for (key in BlockCodex.dynamicLights) {
|
||||
try {
|
||||
val prop = BlockCodex[key]
|
||||
if (prop.dynamicLuminosityFunction != 0) {
|
||||
prop.lumCol.set(getDynamicLumFunc(prop.baseLumCol, prop.dynamicLuminosityFunction))
|
||||
prop.lumColR = prop.lumCol.r
|
||||
prop.lumColG = prop.lumCol.g
|
||||
prop.lumColB = prop.lumCol.b
|
||||
prop.lumColA = prop.lumCol.a
|
||||
}
|
||||
}
|
||||
catch (skip: NullPointerException) {}
|
||||
}
|
||||
}
|
||||
|
||||
private fun getNewRandom() = random.nextFloat().times(2).minus(1f) * flickerFuncRange
|
||||
|
||||
private fun linearInterpolation1D(a: Float, b: Float, x: Float) = a * (1 - x) + b * x
|
||||
|
||||
fun getDynamicLumFunc(baseLum: Cvec, type: Int): Cvec {
|
||||
private fun getDynamicLumFunc(baseLum: Cvec, type: Int): Cvec {
|
||||
return when (type) {
|
||||
1 -> getTorchFlicker(baseLum)
|
||||
2 -> (Terrarum.ingame!!.world).globalLight.cpy().mul(LightmapRenderer.DIV_FLOAT) // current global light
|
||||
@@ -106,7 +121,7 @@ object BlockPropUtil {
|
||||
/**
|
||||
* @param chan 0 for R, 1 for G, 2 for B, 3 for A
|
||||
*/
|
||||
fun getDynamicLumFuncByChan(baseLum: Float, type: Int, chan: Int): Float {
|
||||
private fun getDynamicLumFuncByChan(baseLum: Float, type: Int, chan: Int): Float {
|
||||
return when (type) {
|
||||
1 -> getTorchFlicker(baseLum)
|
||||
2 -> (Terrarum.ingame!!.world).globalLight.cpy().mul(LightmapRenderer.DIV_FLOAT).getElem(chan) // current global light
|
||||
|
||||
Reference in New Issue
Block a user