From ebce90aa4bdf7711e8a19df31e7521a4f003c752 Mon Sep 17 00:00:00 2001 From: minjaesong Date: Thu, 17 Jan 2019 16:05:48 +0900 Subject: [PATCH] light: lantern lookup should be faster --- src/net/torvald/terrarum/Point2d.kt | 2 ++ .../terrarum/ui/BasicDebugInfoWindow.kt | 18 ++++++++++++- .../worlddrawer/LightmapRendererNew.kt | 25 ++++++------------- 3 files changed, 26 insertions(+), 19 deletions(-) diff --git a/src/net/torvald/terrarum/Point2d.kt b/src/net/torvald/terrarum/Point2d.kt index e2182bc7b..029610840 100644 --- a/src/net/torvald/terrarum/Point2d.kt +++ b/src/net/torvald/terrarum/Point2d.kt @@ -53,3 +53,5 @@ data class Point2d(var x: Double, var y: Double) : Cloneable { fun distSqr(other: Point2d) = ((this.x - other.x).sqr() + (this.y - other.y).sqr()) } + +data class Point2i(val x: Int, val y: Int) diff --git a/src/net/torvald/terrarum/ui/BasicDebugInfoWindow.kt b/src/net/torvald/terrarum/ui/BasicDebugInfoWindow.kt index 932259a39..01f6307a4 100644 --- a/src/net/torvald/terrarum/ui/BasicDebugInfoWindow.kt +++ b/src/net/torvald/terrarum/ui/BasicDebugInfoWindow.kt @@ -49,6 +49,21 @@ class BasicDebugInfoWindow : UICanvas() { } } + private fun formatNanoTime(l: Long?): String { + if (l == null) return "null" + + val sb = StringBuilder() + + l.toString().reversed().forEachIndexed { index, c -> + if (index > 0 && index % 3 == 0) + sb.append(' ') + + sb.append(c) + } + + return sb.reverse().toString() + } + override fun renderUI(batch: SpriteBatch, camera: Camera) { val player = ingame.actorNowPlaying @@ -136,9 +151,10 @@ class BasicDebugInfoWindow : UICanvas() { printLine(batch, 10, "fluid@cursor ${ccY}Type $ccM${fluid.type.value} ${ccY}Fill $ccG${fluid.amount}f") + // print time var dbgCnt = 12 AppLoader.debugTimers.forEach { t, u -> - printLine(batch, dbgCnt, "$ccM$t $ccG$u$ccY ns") + printLine(batch, dbgCnt, "$ccM$t $ccG${formatNanoTime(u as? Long)}$ccY ns") dbgCnt++ } diff --git a/src/net/torvald/terrarum/worlddrawer/LightmapRendererNew.kt b/src/net/torvald/terrarum/worlddrawer/LightmapRendererNew.kt index 8f93ce45f..5bffec770 100644 --- a/src/net/torvald/terrarum/worlddrawer/LightmapRendererNew.kt +++ b/src/net/torvald/terrarum/worlddrawer/LightmapRendererNew.kt @@ -7,15 +7,12 @@ import com.badlogic.gdx.graphics.Pixmap import com.badlogic.gdx.graphics.Texture import com.badlogic.gdx.graphics.g2d.SpriteBatch import com.jme3.math.FastMath -import net.torvald.terrarum.AppLoader +import net.torvald.terrarum.* import net.torvald.terrarum.AppLoader.printdbg -import net.torvald.terrarum.Terrarum import net.torvald.terrarum.blockproperties.Block import net.torvald.terrarum.blockproperties.BlockCodex -import net.torvald.terrarum.ceilInt import net.torvald.terrarum.concurrent.ParallelUtils.sliceEvenly import net.torvald.terrarum.concurrent.ThreadParallel -import net.torvald.terrarum.floorInt import net.torvald.terrarum.gameactors.ActorWBMovable import net.torvald.terrarum.gameactors.Luminous import net.torvald.terrarum.gameworld.GameWorld @@ -86,7 +83,7 @@ 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(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 = Array(LIGHTMAP_WIDTH * LIGHTMAP_HEIGHT) { Color(0f,0f,0f,0f) } // Can't use framebuffer/pixmap -- this is a fvec4 array, whereas they are ivec4. - private val lanternMap = ArrayList((Terrarum.ingame?.ACTORCONTAINER_INITIAL_SIZE ?: 2) * 4) + private val lanternMap = HashMap((Terrarum.ingame?.ACTORCONTAINER_INITIAL_SIZE ?: 2) * 4) private val AIR = Block.AIR @@ -402,10 +399,10 @@ object LightmapRenderer { val normalisedColor = it.color.cpy().mul(DIV_FLOAT) - lanternMap.add(Lantern(x, y, normalisedColor)) + lanternMap[Point2i(x, y)] = normalisedColor // Q&D fix for Roundworld anomaly - lanternMap.add(Lantern(x + world.width, y, normalisedColor)) - lanternMap.add(Lantern(x - world.width, y, normalisedColor)) + lanternMap[Point2i(x + world.width, y)] = normalisedColor + lanternMap[Point2i(x - world.width, y)] = normalisedColor } } } @@ -459,11 +456,8 @@ object LightmapRenderer { } // END MIX TILE - for (i in 0 until lanternMap.size) { - val lmap = lanternMap[i] - if (lmap.posX == x && lmap.posY == y) - lightLevelThis.set(lightLevelThis maxBlend lmap.color) // maximise to not exceed 1.0 with normal (<= 1.0) light - } + // blend lantern + lightLevelThis maxBlend (lanternMap[Point2i(x, y)] ?: colourNull) // calculate ambient /* + * + @@ -863,11 +857,6 @@ object LightmapRenderer { hdr(this.a) ) - /** - * color values are normalised -- 0.0 to 1.0 for 0..1023 - */ - data class Lantern(val posX: Int, val posY: Int, val color: Color) - private fun Color.nonZero() = this.r != 0f || this.g != 0f || this.b != 0f || this.a != 0f val histogram: Histogram