spritebatch with hdr colour

This commit is contained in:
minjaesong
2023-09-14 23:17:37 +09:00
parent f8b74f2445
commit d6fea9889e
9 changed files with 1410 additions and 103 deletions

View File

@@ -34,10 +34,15 @@ import com.badlogic.gdx.utils.NumberUtils
class Cvec {
/** the red, green, blue and alpha components */
var r: Float = 0f
var g: Float = 0f
var b: Float = 0f
var a: Float = 0f
@kotlin.jvm.JvmField var r: Float = 0f
@kotlin.jvm.JvmField var g: Float = 0f
@kotlin.jvm.JvmField var b: Float = 0f
@kotlin.jvm.JvmField var a: Float = 0f
var x: Float; get() = r; set(value) { r = value }
var y: Float; get() = g; set(value) { g = value }
var z: Float; get() = b; set(value) { b = value }
var w: Float; get() = a; set(value) { a = value }
/** Constructs a new Cvec with all components set to 0. */
constructor() {}

View File

@@ -3,13 +3,14 @@ package net.torvald.terrarum
import com.badlogic.gdx.graphics.Texture
import com.badlogic.gdx.graphics.g2d.SpriteBatch
import com.badlogic.gdx.graphics.g2d.TextureRegion
import com.badlogic.gdx.graphics.g2d.UnpackedColourSpriteBatch
/**
* Don't flip the assets! Flip the draw command instead!
*
* Created by minjaesong on 2021-12-13.
*/
class FlippingSpriteBatch(size: Int = 1000) : SpriteBatch(size, DefaultGL32Shaders.createSpriteBatchShader()) {
class FlippingSpriteBatch(size: Int = 1000) : UnpackedColourSpriteBatch(size, DefaultGL32Shaders.createSpriteBatchShader()) {
/**
* This function draws the flipped version of the image by giving flipped uv-coord to the SpriteBatch
@@ -42,6 +43,7 @@ class FlippingSpriteBatch(size: Int = 1000) : SpriteBatch(size, DefaultGL32Shade
draw(region.texture, x, y, region.regionWidth.toFloat(), region.regionHeight.toFloat(), region.u, region.v2, region.u2, region.v)
/**
* NOTE TO SELF:
*

View File

@@ -4,6 +4,7 @@ import com.badlogic.gdx.Gdx
import com.badlogic.gdx.graphics.*
import com.badlogic.gdx.graphics.g2d.SpriteBatch
import com.badlogic.gdx.graphics.g2d.TextureRegion
import com.badlogic.gdx.graphics.g2d.UnpackedColourSpriteBatch
import com.badlogic.gdx.math.Vector2
import com.badlogic.gdx.math.Vector3
import com.badlogic.gdx.utils.JsonValue
@@ -518,7 +519,7 @@ internal object WeatherMixer : RNGConsumer {
batch.shader.setUniformf("shadeCol", 0.06f, 0.07f, 0.08f, 1f) // TODO temporary value
clouds.forEach {
it.render(batch, cloudDrawColour)
it.render(batch as UnpackedColourSpriteBatch, cloudDrawColour)
}
}
}

View File

@@ -3,6 +3,7 @@ package net.torvald.terrarum.weather
import com.badlogic.gdx.graphics.Color
import com.badlogic.gdx.graphics.g2d.SpriteBatch
import com.badlogic.gdx.graphics.g2d.TextureRegion
import com.badlogic.gdx.graphics.g2d.UnpackedColourSpriteBatch
import com.badlogic.gdx.math.Vector3
import com.jme3.math.FastMath
import com.jme3.math.FastMath.PI
@@ -77,28 +78,13 @@ class WeatherObjectCloud(
private val vecMult = Vector3(1f, 1f, 1f / (4f * H))
private fun roundRgbGamma(x: Float): Int {
return RGB_GAMMA_TABLE.mapIndexed { i, f -> (f - x).absoluteValue to i }.minBy { it.first }.second
}
private fun roundAgamma(x: Float): Int {
return A_GAMMA_TABLE.mapIndexed { i, f -> (f - x).absoluteValue to i }.minBy { it.first }.second
}
fun render(batch: SpriteBatch, cloudDrawColour: Color) {
fun render(batch: UnpackedColourSpriteBatch, cloudDrawColour: Color) {
val sc = screenCoord
val rgbGammaIndex = roundRgbGamma(rgbGamma)
val aGammaIndex = roundAgamma(aGamma)
// printdbg(this, "gamma: (${rgbGamma}, ${aGamma}) index: ($rgbGammaIndex, $aGammaIndex)")
val lightBits = cloudDrawColour.toIntBits()
val rbits = lightBits.ushr( 0).and(252) or rgbGammaIndex.ushr(2).and(3)
val gbits = lightBits.ushr( 8).and(252) or rgbGammaIndex.ushr(0).and(3)
val bbits = lightBits.ushr(16).and(252) or aGammaIndex
val abits = (alpha * 255).toInt()
batch.color = Color(rbits.shl(24) or gbits.shl(16) or bbits.shl(8) or abits)
batch.color = cloudDrawColour
batch.generic.set(rgbGamma, aGamma, 0f, 0f)
if (flipW)
batch.draw(texture, sc.x + texture.regionWidth / posZ, sc.y, -texture.regionWidth * sc.z, texture.regionHeight * sc.z)
@@ -182,31 +168,5 @@ class WeatherObjectCloud(
const val CLOUD_STAGE_DEPTH = 256f
const val OLD_AGE_DECAY = 5000f
const val NEWBORN_GROWTH_TIME = 1000f
val RGB_GAMMA_TABLE = floatArrayOf(
0.2f,
0.3f,
0.4f,
0.5f,
0.6f,
0.7f,
0.8f,
0.9f,
1.0f,
1.25f,
1.5f,
1.75f,
2.0f,
2.5f,
3.0f,
3.5f
)
val A_GAMMA_TABLE = floatArrayOf(
1.6f,
2.0f,
2.4f,
2.8f
)
}
}