Compare commits

...

5 Commits

Author SHA1 Message Date
minjaesong
ae52de85a2 it darkens because you forgot the fucking assignment, same mistake from 2013 2019-06-07 10:43:41 +09:00
minjaesong
585a20542f minor improvement 2019-05-25 01:11:42 +09:00
minjaesong
2ab1443885 a little experimets 2019-05-22 00:01:38 +09:00
minjaesong
046f2d4b6c made it work but slower?! 2019-05-21 17:52:20 +09:00
minjaesong
e3fbb57530 something's screwing up but commiting anyway 2019-05-20 23:31:43 +09:00
19 changed files with 214 additions and 165 deletions

2
.idea/misc.xml generated
View File

@@ -38,7 +38,7 @@
<property name="caretWidth" class="java.lang.Integer" />
</properties>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_10" default="false" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_X" default="false" project-jdk-name="13-incubate" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

View File

@@ -19,9 +19,10 @@ class GdxColorMap {
height = pixmap.height
is2D = pixmap.height > 1
data = kotlin.IntArray(pixmap.width * pixmap.height) {
dataRaw = kotlin.IntArray(pixmap.width * pixmap.height) {
pixmap.getPixel(it % pixmap.width, it / pixmap.width)
}
dataGdxColor = dataRaw.map { Color(it) }.toTypedArray()
pixmap.dispose()
}
@@ -31,39 +32,46 @@ class GdxColorMap {
height = pixmap.height
is2D = pixmap.height > 1
data = kotlin.IntArray(pixmap.width * pixmap.height) {
dataRaw = kotlin.IntArray(pixmap.width * pixmap.height) {
pixmap.getPixel(it % pixmap.width, it / pixmap.width)
}
dataGdxColor = dataRaw.map { Color(it) }.toTypedArray()
if (disposePixmap) pixmap.dispose()
}
constructor(color: Color) {
data = intArrayOf(color.toIntBits())
dataRaw = intArrayOf(color.toIntBits())
dataGdxColor = dataRaw.map { Color(it) }.toTypedArray()
width = 1
height = 1
is2D = false
}
constructor(gradStart: Color, gradEnd: Color) {
data = intArrayOf(gradStart.toIntBits(), gradEnd.toIntBits())
dataRaw = intArrayOf(gradStart.toIntBits(), gradEnd.toIntBits())
dataGdxColor = dataRaw.map { Color(it) }.toTypedArray()
width = 1
height = 2
is2D = true
}
private val data: IntArray
private val dataRaw: IntArray
private val dataGdxColor: Array<Color>
//private val dataCvec: Array<Cvec>
val width: Int
val height: Int
val is2D: Boolean
fun get(x: Int, y: Int): Color = Color(data[y * width + x])
operator fun get(x: Int): Color = if (is2D) throw OperationNotSupportedException("This is 2D color map") else Color(data[x])
fun get(x: Int, y: Int): Color = dataGdxColor[y * width + x]
operator fun get(x: Int): Color = if (is2D) throw OperationNotSupportedException("This is 2D color map") else dataGdxColor[x]
fun getRaw(x: Int, y: Int): RGBA8888 = data[y * width + x]
fun getRaw(x: Int): RGBA8888 = if (is2D) throw OperationNotSupportedException("This is 2D color map") else data[x]
fun getRaw(x: Int, y: Int): RGBA8888 = dataRaw[y * width + x]
fun getRaw(x: Int): RGBA8888 = if (is2D) throw OperationNotSupportedException("This is 2D color map") else dataRaw[x]
//fun getAsCvec(x: Int, y: Int): Cvec = dataCvec[y * width + x]
override fun toString(): String {
val sb = StringBuilder()

View File

@@ -1,6 +1,5 @@
package net.torvald.terrarum.blockproperties
import com.badlogic.gdx.graphics.Color
import net.torvald.terrarum.AppLoader
import net.torvald.terrarum.AppLoader.printmsg
import net.torvald.terrarum.gameworld.FluidType
@@ -8,6 +7,7 @@ import net.torvald.terrarum.gameworld.GameWorld
import net.torvald.terrarum.gameworld.MapLayer
import net.torvald.terrarum.gameworld.PairedMapLayer
import net.torvald.terrarum.utils.CSVFetcher
import net.torvald.terrarum.worlddrawer.Cvec
import net.torvald.terrarum.worlddrawer.LightmapRenderer
import org.apache.commons.csv.CSVRecord
import java.io.IOException
@@ -110,7 +110,7 @@ object BlockCodex {
prop.shadeColG = floatVal(record, "shdg") / LightmapRenderer.MUL_FLOAT
prop.shadeColB = floatVal(record, "shdb") / LightmapRenderer.MUL_FLOAT
prop.shadeColA = floatVal(record, "shduv") / LightmapRenderer.MUL_FLOAT
prop.opacity = Color(prop.shadeColR, prop.shadeColG, prop.shadeColB, prop.shadeColA)
prop.opacity = Cvec(prop.shadeColR, prop.shadeColG, prop.shadeColB, prop.shadeColA)
prop.strength = intVal(record, "str")
prop.density = intVal(record, "dsty")
@@ -119,7 +119,7 @@ object BlockCodex {
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 = Color(prop.lumColR, prop.lumColG, prop.lumColB, prop.lumColA)
prop.internalLumCol = Cvec(prop.lumColR, prop.lumColG, prop.lumColB, prop.lumColA)
prop.friction = intVal(record, "fr")
prop.viscosity = intVal(record, "vscs")

View File

@@ -1,6 +1,6 @@
package net.torvald.terrarum.blockproperties
import com.badlogic.gdx.graphics.Color
import net.torvald.terrarum.worlddrawer.Cvec
/**
* Created by minjaesong on 2016-02-16.
@@ -17,7 +17,7 @@ class BlockProp {
var shadeColB = 0f
var shadeColA = 0f
lateinit var opacity: Color
var opacity: Cvec = Cvec()
var strength: Int = 0
var density: Int = 0
@@ -36,12 +36,12 @@ class BlockProp {
var lumColG = 0f
var lumColB = 0f
var lumColA = 0f
lateinit var internalLumCol: Color
var internalLumCol: Cvec = Cvec()
/**
* @param luminosity
*/
inline val luminosity: Color
inline val luminosity: Cvec
get() = BlockPropUtil.getDynamicLumFunc(internalLumCol, dynamicLuminosityFunction)
var drop: Int = 0

View File

@@ -1,13 +1,13 @@
package net.torvald.terrarum.blockproperties
import com.badlogic.gdx.Gdx
import com.badlogic.gdx.graphics.Color
import com.jme3.math.FastMath
import net.torvald.random.HQRNG
import net.torvald.terrarum.Second
import net.torvald.terrarum.Terrarum
import net.torvald.terrarum.modulebasegame.gameworld.WorldTime
import net.torvald.terrarum.modulebasegame.weather.WeatherMixer
import net.torvald.terrarum.worlddrawer.Cvec
import net.torvald.terrarum.worlddrawer.LightmapRenderer
/**
@@ -37,7 +37,7 @@ object BlockPropUtil {
}
private fun getTorchFlicker(baseLum: Color): Color {
private fun getTorchFlicker(baseLum: Cvec): Cvec {
val funcY = FastMath.interpolateCatmullRom(0.0f, flickerFuncX / flickerFuncDomain,
flickerP0, flickerP1, flickerP2, flickerP3
)
@@ -45,13 +45,13 @@ object BlockPropUtil {
return LightmapRenderer.alterBrightnessUniform(baseLum, funcY)
}
private fun getSlowBreath(baseLum: Color): Color {
private fun getSlowBreath(baseLum: Cvec): Cvec {
val funcY = FastMath.sin(FastMath.PI * breathFuncX / breathCycleDuration) * breathRange
return LightmapRenderer.alterBrightnessUniform(baseLum, funcY)
}
private fun getPulsate(baseLum: Color): Color {
private fun getPulsate(baseLum: Cvec): Cvec {
val funcY = FastMath.sin(FastMath.PI * pulsateFuncX / pulsateCycleDuration) * pulsateRange
return LightmapRenderer.alterBrightnessUniform(baseLum, funcY)
@@ -91,11 +91,11 @@ object BlockPropUtil {
private fun linearInterpolation1D(a: Float, b: Float, x: Float) = a * (1 - x) + b * x
fun getDynamicLumFunc(baseLum: Color, type: Int): Color {
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
3 -> WeatherMixer.getGlobalLightOfTime(WorldTime.DAY_LENGTH / 2).cpy().mul(LightmapRenderer.DIV_FLOAT) // daylight at noon
2 -> (Terrarum.ingame!!.world).globalLight * LightmapRenderer.DIV_FLOAT // current global light
3 -> WeatherMixer.getGlobalLightOfTime(WorldTime.DAY_LENGTH / 2) * LightmapRenderer.DIV_FLOAT // daylight at noon
4 -> getSlowBreath(baseLum)
5 -> getPulsate(baseLum)
else -> baseLum

View File

@@ -1,10 +1,9 @@
package net.torvald.terrarum.console
import com.badlogic.gdx.graphics.Color
import net.torvald.terrarum.worlddrawer.LightmapRenderer
import net.torvald.terrarum.Terrarum
import net.torvald.terrarum.modulebasegame.Ingame
import net.torvald.terrarum.modulebasegame.weather.WeatherMixer
import net.torvald.terrarum.worlddrawer.Cvec
import net.torvald.terrarum.worlddrawer.LightmapRenderer
/**
* Created by minjaesong on 2016-02-17.
@@ -14,11 +13,12 @@ internal object SetGlobalLightOverride : ConsoleCommand {
override fun execute(args: Array<String>) {
if (args.size == 5) {
try {
val r = args[1].toFloat()
val g = args[2].toFloat()
val b = args[3].toFloat()
val a = args[4].toFloat()
val GL = Color(r, g, b, a)
val GL = Cvec(
args[1].toFloat(),
args[2].toFloat(),
args[3].toFloat(),
args[4].toFloat()
)
WeatherMixer.globalLightOverridden = true
(Terrarum.ingame!!.world).globalLight = GL

View File

@@ -1,6 +1,6 @@
package net.torvald.terrarum.gameactors
import com.badlogic.gdx.graphics.Color
import net.torvald.terrarum.worlddrawer.Cvec
/**
* Created by minjaesong on 2016-02-19.
@@ -26,7 +26,7 @@ interface Luminous {
actorValue[AVKey.LUMA] = value.a * LightmapRenderer.MUL_FLOAT
}
*/
var color: Color
var color: Cvec
/**
* Arguments:

View File

@@ -1,7 +1,6 @@
package net.torvald.terrarum.gameworld
import com.badlogic.gdx.graphics.Color
import net.torvald.terrarum.AppLoader.printdbg
import net.torvald.terrarum.Terrarum
import net.torvald.terrarum.blockproperties.Block
@@ -10,6 +9,7 @@ import net.torvald.terrarum.blockproperties.Fluid
import net.torvald.terrarum.modulebasegame.gameworld.WorldSimulator
import net.torvald.terrarum.realestate.LandUtil
import net.torvald.terrarum.serialise.ReadLayerDataZip
import net.torvald.terrarum.worlddrawer.Cvec
import net.torvald.util.SortedArrayList
import org.dyn4j.geometry.Vector2
import kotlin.math.absoluteValue
@@ -90,7 +90,7 @@ open class GameWorld {
/** Meter per second squared. Currently only the downward gravity is supported. No reverse gravity :p */
var gravitation: Vector2 = Vector2(0.0, 9.80665)
/** 0.0..1.0+ */
var globalLight = Color(0f,0f,0f,0f)
var globalLight = Cvec()
var averageTemperature = 288f // 15 deg celsius; simulates global warming

View File

@@ -27,6 +27,7 @@ import net.torvald.terrarum.serialise.toULittle48
import net.torvald.terrarum.ui.UICanvas
import net.torvald.terrarum.ui.UINSMenu
import net.torvald.terrarum.worlddrawer.CreateTileAtlas.TILE_SIZE
import net.torvald.terrarum.worlddrawer.Cvec
import net.torvald.terrarum.worlddrawer.LightmapRenderer
import net.torvald.terrarum.worlddrawer.WorldCamera
import net.torvald.terrarumsansbitmap.gdx.TextureRegionPack
@@ -279,7 +280,7 @@ class BuildingMaker(batch: SpriteBatch) : IngameInstance(batch) {
init {
gameWorld.time.setTimeOfToday(WorldTime.HOUR_SEC * 10)
gameWorld.globalLight = Color(.8f,.8f,.8f,.8f)
gameWorld.globalLight = Cvec(.8f)
essentialOverlays.add(blockPointingCursor)

View File

@@ -1,7 +1,6 @@
package net.torvald.terrarum.modulebasegame.gameactors
import com.badlogic.gdx.Gdx
import com.badlogic.gdx.graphics.Color
import com.jme3.math.FastMath
import net.torvald.spriteanimation.HasAssembledSprite
import net.torvald.terrarum.*
@@ -15,6 +14,7 @@ import net.torvald.terrarum.modulebasegame.Ingame
import net.torvald.terrarum.modulebasegame.gameworld.time_t
import net.torvald.terrarum.modulebasegame.ui.UIInventoryFull
import net.torvald.terrarum.realestate.LandUtil
import net.torvald.terrarum.worlddrawer.Cvec
import net.torvald.terrarum.worlddrawer.LightmapRenderer
import org.dyn4j.geometry.Vector2
import java.util.*
@@ -68,18 +68,18 @@ open class ActorHumanoid(
if (houseDesignation != null) houseDesignation!!.clear()
}
override var color: Color
get() = Color(
override var color: Cvec
get() = Cvec(
(actorValue.getAsFloat(AVKey.LUMR) ?: 0f) / LightmapRenderer.MUL_FLOAT,
(actorValue.getAsFloat(AVKey.LUMG) ?: 0f) / LightmapRenderer.MUL_FLOAT,
(actorValue.getAsFloat(AVKey.LUMB) ?: 0f) / LightmapRenderer.MUL_FLOAT,
(actorValue.getAsFloat(AVKey.LUMA) ?: 0f) / LightmapRenderer.MUL_FLOAT
)
set(value) {
actorValue[AVKey.LUMR] = value.r * LightmapRenderer.MUL_FLOAT
actorValue[AVKey.LUMG] = value.g * LightmapRenderer.MUL_FLOAT
actorValue[AVKey.LUMB] = value.b * LightmapRenderer.MUL_FLOAT
actorValue[AVKey.LUMA] = value.a * LightmapRenderer.MUL_FLOAT
actorValue[AVKey.LUMR] = value.vec.lane(0) * LightmapRenderer.MUL_FLOAT
actorValue[AVKey.LUMG] = value.vec.lane(1) * LightmapRenderer.MUL_FLOAT
actorValue[AVKey.LUMB] = value.vec.lane(2) * LightmapRenderer.MUL_FLOAT
actorValue[AVKey.LUMA] = value.vec.lane(3) * LightmapRenderer.MUL_FLOAT
}
/**

View File

@@ -1,12 +1,12 @@
package net.torvald.terrarum.modulebasegame.gameactors
import com.badlogic.gdx.graphics.Color
import net.torvald.terrarum.ModMgr
import net.torvald.terrarum.blockproperties.Block
import net.torvald.terrarum.blockproperties.BlockCodex
import net.torvald.terrarum.gameactors.AVKey
import net.torvald.terrarum.gameactors.Hitbox
import net.torvald.terrarum.gameactors.Luminous
import net.torvald.terrarum.worlddrawer.Cvec
import net.torvald.terrarumsansbitmap.gdx.TextureRegionPack
import java.util.*
@@ -17,7 +17,7 @@ internal class FixtureTikiTorch : FixtureBase(
BlockBox(BlockBox.NO_COLLISION, 1, 2)
), Luminous {
override var color: Color
override var color: Cvec
get() = BlockCodex[Block.TORCH].luminosity
set(value) {
throw UnsupportedOperationException()

View File

@@ -9,6 +9,7 @@ import net.torvald.terrarum.blockproperties.BlockCodex
import net.torvald.terrarum.gameactors.ActorWBMovable
import net.torvald.terrarum.gameactors.Hitbox
import net.torvald.terrarum.gameactors.Luminous
import net.torvald.terrarum.worlddrawer.Cvec
import org.dyn4j.geometry.Vector2
import java.util.*
@@ -31,8 +32,8 @@ open class ProjectileSimple(
val speed: Int
override var color: Color
get() = (bulletDatabase[type][OFFSET_LUMINOSITY] as Color).cpy()
override var color: Cvec
get() = (bulletDatabase[type][OFFSET_LUMINOSITY] as Cvec)
set(value) {
}
/**
@@ -118,8 +119,8 @@ open class ProjectileSimple(
val OFFSET_LUMINOSITY = 4
val bulletDatabase = arrayOf(
// damage, display colour, no gravity, speed
arrayOf(7, Color(0xFF5429_FF.toInt()), true, 40, 32),
arrayOf(8, Color(0xFF5429_FF.toInt()), true, 20, 0)
arrayOf(7, Cvec(1f, .329f, .161f, 1f), true, 40, 32),
arrayOf(8, Cvec(1f, .329f, .161f, 1f), true, 20, 0)
// ...
)
}

View File

@@ -1,10 +1,9 @@
package net.torvald.terrarum.modulebasegame.gameactors
import com.badlogic.gdx.graphics.Color
import net.torvald.terrarum.gameactors.ActorWBMovable
import net.torvald.terrarum.gameactors.Hitbox
import net.torvald.terrarum.gameactors.Luminous
import net.torvald.terrarum.gameworld.GameWorld
import net.torvald.terrarum.worlddrawer.Cvec
/**
* Created by minjaesong on 2016-04-26.
@@ -21,7 +20,7 @@ class WeaponSwung(val itemID: Int) : ActorWBMovable(RenderOrder.MIDTOP), Luminou
actorValue[AVKey.LUMINOSITY] = value
}
*/
override var color: Color
override var color: Cvec
get() = throw UnsupportedOperationException()
set(value) {
}

View File

@@ -22,6 +22,7 @@ import net.torvald.terrarum.modulebasegame.gameworld.WorldTime
import net.torvald.terrarum.modulebasegame.worldgenerator.WorldGenerator
import net.torvald.terrarum.utils.JsonFetcher
import net.torvald.terrarum.worlddrawer.CreateTileAtlas
import net.torvald.terrarum.worlddrawer.Cvec
import net.torvald.terrarum.worlddrawer.WorldCamera
import java.io.File
import java.util.*
@@ -51,7 +52,8 @@ internal object WeatherMixer : RNGConsumer {
lateinit var mixedWeather: BaseModularWeather
val globalLightNow = Color(0)
var globalLightNow = Cvec()
private set
// Weather indices
const val WEATHER_GENERIC = "generic"
@@ -133,7 +135,7 @@ internal object WeatherMixer : RNGConsumer {
// calculate global light
val globalLight = getGradientColour(skyboxColourMap, 2, timeNow)
globalLightNow.set(globalLight)
globalLightNow = Cvec(globalLight.r, globalLight.g, globalLight.b, globalLight.a)
/* (copied from the shader source)
@@ -178,8 +180,10 @@ internal object WeatherMixer : RNGConsumer {
/**
* Get a GL of specific time
*/
fun getGlobalLightOfTime(timeInSec: Int): Color =
getGradientColour(currentWeather.skyboxGradColourMap, 2, timeInSec)
fun getGlobalLightOfTime(timeInSec: Int): Cvec {
val c = getGradientColour(currentWeather.skyboxGradColourMap, 2, timeInSec)
return Cvec(c.r, c.g, c.b, c.a)
}
fun getGradientColour(colorMap: GdxColorMap, row: Int, timeInSec: Int): Color {
val dataPointDistance = WorldTime.DAY_LENGTH / colorMap.width

View File

@@ -140,10 +140,10 @@ class BasicDebugInfoWindow : UICanvas() {
val mtX = mouseTileX.toString()
val mtY = mouseTileY.toString()
val valRaw = LightmapRenderer.getLight(mouseTileX, mouseTileY)
val rawR = valRaw?.r?.times(100f)?.round()?.div(100f)
val rawG = valRaw?.g?.times(100f)?.round()?.div(100f)
val rawB = valRaw?.b?.times(100f)?.round()?.div(100f)
val rawA = valRaw?.a?.times(100f)?.round()?.div(100f)
val rawR = valRaw?.vec?.lane(0)?.times(100f)?.round()?.div(100f)
val rawG = valRaw?.vec?.lane(1)?.times(100f)?.round()?.div(100f)
val rawB = valRaw?.vec?.lane(2)?.times(100f)?.round()?.div(100f)
val rawA = valRaw?.vec?.lane(3)?.times(100f)?.round()?.div(100f)
lightVal = if (valRaw == null) ""
else "$rawR $rawG $rawB $rawA"

View File

@@ -0,0 +1,75 @@
package net.torvald.terrarum.worlddrawer
import jdk.incubator.vector.FloatVector
import kotlin.math.roundToInt
/**
* Get and compile your OpenJDK-Panama for linux/mac/windows at https://jdk.java.net/panama/
* Or use pre-built ones in https://github.com/minjaesong/openjdk13-vectorintrinsic/tree/master
*
* NOTE: Panama's new vectors are all immutable.
* NOTE2: Class inlining vastly improves the performance
*
* Created by minjaesong on 2019-05-20.
*/
inline class Cvec constructor(val vec: FloatVector) {
/*
Test log
I ran with following jvm args:
-XX:TypeProfileLevel=121 -XX:+UseVectorApiIntrinsics --add-modules=jdk.incubator.vector
and it's slower than the legacy.
This time I removed the TypeProfileLevel part. It now runs slightly faster than the legacy?
-XX:+UseVectorApiIntrinsics --add-modules=jdk.incubator.vector
<<append from here>>
*/
constructor(floatArray: FloatArray) : this(FloatVector.fromArray(SPECIES, floatArray, 0))
constructor(r: Float, g: Float, b: Float, a: Float) : this(FloatVector.scalars(SPECIES, r, g, b, a))
constructor(scalar: Float) : this(FloatVector.scalars(SPECIES, scalar, scalar, scalar, scalar))
constructor() : this(FloatVector.zero(SPECIES))
companion object {
private val EPSILON = 1f / 512f
private val SPECIES = FloatVector.SPECIES_128
}
//fun cpy(): Cvec = Cvec(this.vec)
fun multiply(other: FloatVector) = Cvec(vec.mul(other))
infix operator fun times(other: Cvec) = multiply(other.vec)
infix operator fun times(scalar: Float) = Cvec(vec.mul(scalar))
fun subtract(other: FloatVector) = Cvec(vec.sub(other))
infix operator fun minus(other: Cvec) = subtract(other.vec)
infix operator fun minus(scalar: Float) = Cvec(vec.sub(scalar))
fun addition(other: FloatVector) = Cvec(vec.add(other))
infix operator fun plus(other: Cvec) = addition(other.vec)
infix operator fun plus(scalar: Float) = Cvec(vec.add(scalar))
fun maximum(other: FloatVector): Cvec = Cvec(vec.max(other))
infix fun max(other: Cvec): Cvec = maximum(other.vec)
/**
* true if at least one element in the vector is not zero.
*/
fun nonZero(): Boolean = vec.mulLanes() != 0f
fun toRGBA8888(): Int {
var acc = 0
for (i in 0..3)
acc += vec.lane(i).coerceIn(0f, 1f).times(255f).roundToInt().shl(8 * (3 - i))
return acc
}
/*override fun equals(other: Any?): Boolean {
return this.vec.equal((other as Cvec).vec).allTrue()
}*/
}

View File

@@ -1,19 +1,5 @@
package net.torvald.terrarum.worlddrawer
import com.badlogic.gdx.graphics.Color
import com.badlogic.gdx.graphics.g2d.SpriteBatch
import com.jme3.math.FastMath
import net.torvald.terrarum.Terrarum
import net.torvald.terrarum.blockproperties.Block
import net.torvald.terrarum.blockproperties.BlockCodex
import net.torvald.terrarum.fillRect
import net.torvald.terrarum.floorInt
import net.torvald.terrarum.gameactors.ActorWBMovable
import net.torvald.terrarum.gameactors.Luminous
import net.torvald.terrarum.gameworld.GameWorld
import net.torvald.terrarum.modulebasegame.IngameRenderer
import java.util.*
/**
* Warning: you are not going to store float value to the lightmap -- see RGB_HDR_LUT (beziér)
*
@@ -24,7 +10,7 @@ import java.util.*
// NOTE: no Float16 on this thing: 67 kB of memory footage is totally acceptable
object LightmapRendererOld {
/*object LightmapRendererOld {
lateinit var world: GameWorld
@@ -771,4 +757,4 @@ object LightmapRendererOld {
}
return (1f - scale) * startValue + scale * endValue
}
}
}*/

View File

@@ -82,8 +82,8 @@ object LightmapRenderer {
*/
// it utilises alpha channel to determine brightness of "glow" sprites (so that alpha channel works like UV light)
//private val lightmap: Array<Array<Color>> = Array(LIGHTMAP_HEIGHT) { Array(LIGHTMAP_WIDTH, { Color(0f,0f,0f,0f) }) } // Can't use framebuffer/pixmap -- this is a fvec4 array, whereas they are ivec4.
private var lightmap: Array<Color> = Array(LIGHTMAP_WIDTH * LIGHTMAP_HEIGHT) { Color(0) } // Can't use framebuffer/pixmap -- this is a fvec4 array, whereas they are ivec4.
private val lanternMap = HashMap<BlockAddress, Color>((Terrarum.ingame?.ACTORCONTAINER_INITIAL_SIZE ?: 2) * 4)
private var lightmap: Array<Cvec> = Array(LIGHTMAP_WIDTH * LIGHTMAP_HEIGHT) { Cvec() } // Can't use framebuffer/pixmap -- this is a fvec4 array, whereas they are ivec4.
private val lanternMap = HashMap<BlockAddress, Cvec>((Terrarum.ingame?.ACTORCONTAINER_INITIAL_SIZE ?: 2) * 4)
init {
printdbg(this, "Overscan open: $overscan_open; opaque: $overscan_opaque")
@@ -120,13 +120,13 @@ object LightmapRenderer {
* @param x world tile coord
* @param y world tile coord
*/
internal fun getLight(x: Int, y: Int): Color? {
internal fun getLight(x: Int, y: Int): Cvec? {
val col = getLightInternal(x, y)
if (col == null) {
return null
}
else {
return Color(col.r * MUL_FLOAT, col.g * MUL_FLOAT, col.b * MUL_FLOAT, col.a * MUL_FLOAT)
return col * MUL_FLOAT
}
}
@@ -137,7 +137,7 @@ object LightmapRenderer {
* @param y world tile coord
*/
// TODO in regard of "colour math against integers", return Int?
private fun getLightInternal(x: Int, y: Int): Color? {
private fun getLightInternal(x: Int, y: Int): Cvec? {
if (y - for_y_start + overscan_open in 0 until LIGHTMAP_HEIGHT &&
x - for_x_start + overscan_open in 0 until LIGHTMAP_WIDTH) {
@@ -164,7 +164,7 @@ object LightmapRenderer {
* @param colour Color to write
* @param applyFun A function ```foo(old_colour, given_colour)```
*/
private fun setLightOf(list: Array<Color>, x: Int, y: Int, colour: Color, applyFun: (Color, Color) -> Color = { _, c -> c }) {
private fun setLightOf(list: Array<Cvec>, x: Int, y: Int, colour: Cvec, applyFun: (Cvec, Cvec) -> Cvec = { _, c -> c }) {
if (y - for_y_start + overscan_open in 0 until LIGHTMAP_HEIGHT &&
x - for_x_start + overscan_open in 0 until LIGHTMAP_WIDTH) {
@@ -220,7 +220,7 @@ object LightmapRenderer {
*/
// set sunlight
sunLight.set(world.globalLight); sunLight.mul(DIV_FLOAT)
sunLight = world.globalLight * DIV_FLOAT
// set no-op mask from solidity of the block
AppLoader.measureDebugTime("Renderer.LightNoOpMask") {
@@ -394,15 +394,15 @@ object LightmapRenderer {
//private val ambientAccumulator = Color(0f,0f,0f,0f)
private val lightLevelThis = Color(0)
private var lightLevelThis = Cvec()
private var thisTerrain = 0
private var thisFluid = GameWorld.FluidInfo(Fluid.NULL, 0f)
private val fluidAmountToCol = Color(0)
private var fluidAmountToCol = Cvec()
private var thisWall = 0
private val thisTileLuminosity = Color(0)
private val thisTileOpacity = Color(0)
private val thisTileOpacity2 = Color(0) // thisTileOpacity * sqrt(2)
private val sunLight = Color(0)
private var thisTileLuminosity = Cvec()
private var thisTileOpacity = Cvec()
private var thisTileOpacity2 = Cvec() // thisTileOpacity * sqrt(2)
private var sunLight = Cvec()
/**
* This function will alter following variables:
@@ -416,35 +416,35 @@ object LightmapRenderer {
* - sunlight
*/
private fun getLightsAndShades(x: Int, y: Int) {
lightLevelThis.set(colourNull)
lightLevelThis = colourNull
thisTerrain = world.getTileFromTerrain(x, y) ?: Block.STONE
thisFluid = world.getFluid(x, y)
thisWall = world.getTileFromWall(x, y) ?: Block.STONE
if (thisFluid.type != Fluid.NULL) {
fluidAmountToCol.set(thisFluid.amount, thisFluid.amount, thisFluid.amount, thisFluid.amount)
fluidAmountToCol = Cvec(thisFluid.amount)
thisTileLuminosity.set(BlockCodex[thisTerrain].luminosity)
thisTileLuminosity.maxAndAssign(BlockCodex[thisFluid.type].luminosity mul fluidAmountToCol) // already been div by four
thisTileOpacity.set(BlockCodex[thisTerrain].opacity)
thisTileOpacity.maxAndAssign(BlockCodex[thisFluid.type].opacity mul fluidAmountToCol) // already been div by four
thisTileLuminosity = BlockCodex[thisTerrain].luminosity
thisTileLuminosity = thisTileLuminosity.max(BlockCodex[thisFluid.type].luminosity * fluidAmountToCol) // already been div by four
thisTileOpacity = BlockCodex[thisTerrain].opacity
thisTileOpacity = thisTileOpacity.max(BlockCodex[thisFluid.type].opacity * fluidAmountToCol) // already been div by four
}
else {
thisTileLuminosity.set(BlockCodex[thisTerrain].luminosity)
thisTileOpacity.set(BlockCodex[thisTerrain].opacity)
thisTileLuminosity = BlockCodex[thisTerrain].luminosity
thisTileOpacity = BlockCodex[thisTerrain].opacity
}
thisTileOpacity2.set(thisTileOpacity); thisTileOpacity2.mul(1.41421356f)
thisTileOpacity2 = thisTileOpacity * 1.41421356f
//sunLight.set(world.globalLight); sunLight.mul(DIV_FLOAT) // moved to fireRecalculateEvent()
// open air || luminous tile backed by sunlight
if ((thisTerrain == AIR && thisWall == AIR) || (thisTileLuminosity.nonZero() && thisWall == AIR)) {
lightLevelThis.set(sunLight)
lightLevelThis = sunLight
}
// blend lantern
lightLevelThis.maxAndAssign(thisTileLuminosity).maxAndAssign(lanternMap[LandUtil.getBlockAddr(world, x, y)] ?: colourNull)
lightLevelThis = lightLevelThis.max(thisTileLuminosity).max(lanternMap[LandUtil.getBlockAddr(world, x, y)] ?: colourNull)
}
@@ -499,7 +499,7 @@ object LightmapRenderer {
/**
* Calculates the light simulation, using main lightmap as one of the input.
*/
private fun calculateAndAssign(lightmap: Array<Color>, x: Int, y: Int) {
private fun calculateAndAssign(lightmap: Array<Cvec>, x: Int, y: Int) {
if (inNoopMask(x, y)) return
@@ -517,32 +517,27 @@ object LightmapRenderer {
// will "overwrite" what's there in the lightmap if it's the first pass
// takes about 2 ms on 6700K
/* + */lightLevelThis.maxAndAssign(darkenColoured(getLightInternal(x - 1, y - 1) ?: colourNull, thisTileOpacity2))
/* + */lightLevelThis.maxAndAssign(darkenColoured(getLightInternal(x + 1, y - 1) ?: colourNull, thisTileOpacity2))
/* + */lightLevelThis.maxAndAssign(darkenColoured(getLightInternal(x - 1, y + 1) ?: colourNull, thisTileOpacity2))
/* + */lightLevelThis.maxAndAssign(darkenColoured(getLightInternal(x + 1, y + 1) ?: colourNull, thisTileOpacity2))
/* * */lightLevelThis.maxAndAssign(darkenColoured(getLightInternal(x, y - 1) ?: colourNull, thisTileOpacity))
/* * */lightLevelThis.maxAndAssign(darkenColoured(getLightInternal(x, y + 1) ?: colourNull, thisTileOpacity))
/* * */lightLevelThis.maxAndAssign(darkenColoured(getLightInternal(x - 1, y) ?: colourNull, thisTileOpacity))
/* * */lightLevelThis.maxAndAssign(darkenColoured(getLightInternal(x + 1, y) ?: colourNull, thisTileOpacity))
/* + */lightLevelThis = lightLevelThis.max(darkenColoured(getLightInternal(x - 1, y - 1) ?: colourNull, thisTileOpacity2))
/* + */lightLevelThis = lightLevelThis.max(darkenColoured(getLightInternal(x + 1, y - 1) ?: colourNull, thisTileOpacity2))
/* + */lightLevelThis = lightLevelThis.max(darkenColoured(getLightInternal(x - 1, y + 1) ?: colourNull, thisTileOpacity2))
/* + */lightLevelThis = lightLevelThis.max(darkenColoured(getLightInternal(x + 1, y + 1) ?: colourNull, thisTileOpacity2))
/* * */lightLevelThis = lightLevelThis.max(darkenColoured(getLightInternal(x, y - 1) ?: colourNull, thisTileOpacity))
/* * */lightLevelThis = lightLevelThis.max(darkenColoured(getLightInternal(x, y + 1) ?: colourNull, thisTileOpacity))
/* * */lightLevelThis = lightLevelThis.max(darkenColoured(getLightInternal(x - 1, y) ?: colourNull, thisTileOpacity))
/* * */lightLevelThis = lightLevelThis.max(darkenColoured(getLightInternal(x + 1, y) ?: colourNull, thisTileOpacity))
//return lightLevelThis.cpy() // it HAS to be a cpy(), otherwise all cells gets the same instance
setLightOf(lightmap, x, y, lightLevelThis.cpy())
setLightOf(lightmap, x, y, lightLevelThis)
}
private fun getLightForOpaque(x: Int, y: Int): Color? { // ...so that they wouldn't appear too dark
private fun getLightForOpaque(x: Int, y: Int): Cvec? { // ...so that they wouldn't appear too dark
val l = getLightInternal(x, y)
if (l == null) return null
// brighten if solid
if (BlockCodex[world.getTileFromTerrain(x, y)].isSolid) {
return Color(
(l.r * 1.2f),
(l.g * 1.2f),
(l.b * 1.2f),
(l.a * 1.2f)
)
return l * 1.2f
}
else {
return l
@@ -551,8 +546,7 @@ object LightmapRenderer {
var lightBuffer: Pixmap = Pixmap(1, 1, Pixmap.Format.RGBA8888)
private val colourNull = Color(0)
private val epsilon = 1f/1024f
private val colourNull = Cvec()
private var _lightBufferAsTex: Texture = Texture(1, 1, Pixmap.Format.RGBA8888)
@@ -568,7 +562,7 @@ object LightmapRenderer {
// wipe out beforehand. You DO need this
lightBuffer.blending = Pixmap.Blending.None // gonna overwrite (remove this line causes the world to go bit darker)
lightBuffer.setColor(colourNull)
lightBuffer.setColor(0)
lightBuffer.fill()
@@ -581,12 +575,11 @@ object LightmapRenderer {
for (x in this_x_start..this_x_end) {
val color = (getLightForOpaque(x, y) ?: Color(0f, 0f, 0f, 0f)).normaliseToHDR()
val color: RGBA8888 = (getLightForOpaque(x, y) ?: Cvec()).normaliseToHDR().toRGBA8888()
lightBuffer.setColor(color)
//lightBuffer.drawPixel(x - this_x_start, y - this_y_start)
lightBuffer.drawPixel(x - this_x_start, lightBuffer.height - 1 - y + this_y_start) // flip Y
}
}
@@ -611,6 +604,7 @@ object LightmapRenderer {
}
private val CVEC_ONE = Cvec(1f)
val lightScalingMagic = 8f
/**
@@ -620,15 +614,13 @@ object LightmapRenderer {
* @param darken (0-255) per channel
* @return darkened data (0-255) per channel
*/
fun darkenColoured(data: Color, darken: Color): Color {
fun darkenColoured(data: Cvec, darken: Cvec): Cvec {
// use equation with magic number 8.0
// this function, when done recursively (A_x = darken(A_x-1, C)), draws exponential curve. (R^2 = 1)
return Color(
data.r * (1f - darken.r * lightScalingMagic),//.clampZero(),
data.g * (1f - darken.g * lightScalingMagic),//.clampZero(),
data.b * (1f - darken.b * lightScalingMagic),//.clampZero(),
data.a * (1f - darken.a * lightScalingMagic))
// equation: data * (1 - darken * magic)
return data * (CVEC_ONE - darken * lightScalingMagic)
}
/**
@@ -638,13 +630,13 @@ object LightmapRenderer {
* @param darken (0-255)
* @return
*/
fun darkenUniformInt(data: Color, darken: Float): Color {
/*fun darkenUniformInt(data: Color, darken: Float): Color {
if (darken < 0 || darken > CHANNEL_MAX)
throw IllegalArgumentException("darken: out of range ($darken)")
val darkenColoured = Color(darken, darken, darken, darken)
return darkenColoured(data, darkenColoured)
}
}*/
/**
* Darken or brighten colour by 'brighten' argument
@@ -653,26 +645,12 @@ object LightmapRenderer {
* @param brighten (-1.0 - 1.0) negative means darkening
* @return processed colour
*/
fun alterBrightnessUniform(data: Color, brighten: Float): Color {
return Color(
data.r + brighten,
data.g + brighten,
data.b + brighten,
data.a + brighten
)
fun alterBrightnessUniform(data: Cvec, brighten: Float): Cvec {
// equation = data + brighten
return data + brighten
}
/** infix is removed to clarify the association direction */
fun Color.maxAndAssign(other: Color): Color {
this.set(
if (this.r > other.r) this.r else other.r,
if (this.g > other.g) this.g else other.g,
if (this.b > other.b) this.b else other.b,
if (this.a > other.a) this.a else other.a
)
return this
}
private fun Float.inv() = 1f / this
fun Float.floor() = FastMath.floor(this)
@@ -723,7 +701,7 @@ object LightmapRenderer {
_init = true
}
lightBuffer = Pixmap(tilesInHorizontal, tilesInVertical, Pixmap.Format.RGBA8888)
lightmap = Array<Color>(LIGHTMAP_WIDTH * LIGHTMAP_HEIGHT) { Color(0) }
lightmap = Array<Cvec>(LIGHTMAP_WIDTH * LIGHTMAP_HEIGHT) { Cvec() }
printdbg(this, "Resize event")
@@ -797,14 +775,11 @@ object LightmapRenderer {
1.0000f,1.0000f,1.0000f,1.0000f,1.0000f,1.0000f,1.0000f,1.0000f,1.0000f,1.0000f,1.0000f,1.0000f,1.0000f,1.0000f,1.0000f,1.0000f // isn't it beautiful?
)
/** To eliminated visible edge on the gradient when 255/1023 is exceeded */
internal fun Color.normaliseToHDR() = Color(
hdr(this.r.coerceIn(0f,1f)),
hdr(this.g.coerceIn(0f,1f)),
hdr(this.b.coerceIn(0f,1f)),
hdr(this.a.coerceIn(0f,1f))
)
private fun Color.nonZero() = this.r + this.g + this.b + this.a > epsilon
internal fun Cvec.normaliseToHDR(): Cvec {
// equation: hdr(this.coerceIn)
val arr = this.vec.toArray().map { hdr(it.coerceIn(0f, 1f)) }
return Cvec(arr.toFloatArray())
}
val histogram: Histogram
get() {
@@ -820,10 +795,10 @@ object LightmapRenderer {
try {
//val colour = lightmap[y][x]
val colour = lightmap[y * LIGHTMAP_WIDTH + x]
reds[minOf(CHANNEL_MAX, colour.r.times(MUL).floorInt())] += 1
greens[minOf(CHANNEL_MAX, colour.g.times(MUL).floorInt())] += 1
blues[minOf(CHANNEL_MAX, colour.b.times(MUL).floorInt())] += 1
uvs[minOf(CHANNEL_MAX, colour.a.times(MUL).floorInt())] += 1
reds[minOf(CHANNEL_MAX, colour.vec.lane(0).times(MUL).floorInt())] += 1
greens[minOf(CHANNEL_MAX, colour.vec.lane(1).times(MUL).floorInt())] += 1
blues[minOf(CHANNEL_MAX, colour.vec.lane(2).times(MUL).floorInt())] += 1
uvs[minOf(CHANNEL_MAX, colour.vec.lane(3).times(MUL).floorInt())] += 1
}
catch (e: ArrayIndexOutOfBoundsException) { }
}

View File

@@ -2,10 +2,10 @@
<module type="JAVA_MODULE" version="4">
<component name="FacetManager">
<facet type="kotlin-language" name="Kotlin">
<configuration version="3" platform="JVM 1.8">
<configuration version="3" platform="JVM 9">
<compilerSettings />
<compilerArguments>
<option name="jvmTarget" value="1.8" />
<option name="jvmTarget" value="10" />
</compilerArguments>
</configuration>
</facet>
@@ -16,7 +16,7 @@
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/net/torvald/terrarum/tests" isTestSource="true" />
</content>
<orderEntry type="jdk" jdkName="11" jdkType="JavaSDK" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="KotlinJavaRuntime" level="project" />
<orderEntry type="library" name="lib" level="project" />