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

File diff suppressed because it is too large Load Diff

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
)
}
}

View File

@@ -1,49 +1,15 @@
#version 400
#ifdef GL_ES
#define LOWP lowp
precision mediump float;
#else
#define LOWP
#endif
in LOWP vec4 v_color; // lightCol.rgb + cloud's alpha
in vec4 v_color; // lightCol
in vec4 v_generic; // gamma values
in vec2 v_texCoords;
uniform sampler2D u_texture;
out vec4 fragColor;
const vec2 boolean = vec2(0.0, 1.0);
uniform LOWP vec4 shadeCol;
const float rgbGammas[16] = float[](
0.2,
0.3,
0.4,
0.5,
0.6,
0.7,
0.8,
0.9,
1.0,
1.25,
1.5,
1.75,
2.0,
2.5,
3.0,
3.5
);
const float aGammas[4] = float[](
1.6,
2.0,
2.4,
2.8
);
uniform vec4 shadeCol;
void main() {
// vertex colour format:
@@ -54,15 +20,10 @@ void main() {
// bbbbbb: 6-bit blue component
// MMLL: index to the rgbGammas
// AA: index to the aGammas
vec4 cloudCol = vec4(
(int(v_color.r * 255) >> 2) * 4.0 / 255.0,
(int(v_color.g * 255) >> 2) * 4.0 / 255.0,
(int(v_color.b * 255) >> 2) * 4.0 / 255.0,
v_color.a
);
float rgbGamma = rgbGammas[((int(v_color.r * 255) & 3) << 2) | (int(v_color.g * 255) & 3)];
float aGamma = aGammas[int(v_color.b * 255) & 3];
vec4 gamma = vec4(rgbGamma, rgbGamma, rgbGamma, aGamma);
vec4 cloudCol = v_color;
float rgbGamma = v_generic.x;
float aGamma = v_generic.y;
vec4 gamma = v_generic.xxxy;
vec4 range = vec4(vec3(min(rgbGamma, 1.0 / rgbGamma)), 1.0);
vec4 offset = vec4(vec3(max(0.0, 1.0 - rgbGamma)), 0.0);

View File

@@ -2,16 +2,18 @@
in vec4 a_position;
in vec4 a_color;
in vec4 a_generic;
in vec2 a_texCoord0;
uniform mat4 u_projTrans;
out vec4 v_color;
out vec2 v_texCoords;
out vec4 v_generic;
void main() {
v_color = a_color;
v_color.a = v_color.a * (255.0/254.0); // GDX will crush the alpha value to 0,2,4,6,8,10,...,254; see com.badlogic.gdx.utils.NumberUtils.intToFloatColor
v_texCoords = a_texCoord0;
v_generic = a_generic;
gl_Position = u_projTrans * a_position;
}

View File

@@ -1,13 +1,11 @@
#version 150
#ifdef GL_ES
#define LOWP lowp
precision mediump float;
#else
#define LOWP
#endif
in LOWP vec4 v_color;
in vec4 v_color;
in vec4 v_generic;
in vec2 v_texCoords;
uniform sampler2D u_texture;
out vec4 fragColor;

View File

@@ -2,16 +2,18 @@
in vec4 a_position;
in vec4 a_color;
in vec4 a_generic;
in vec2 a_texCoord0;
uniform mat4 u_projTrans;
out vec4 v_color;
out vec2 v_texCoords;
out vec4 v_generic;
void main() {
v_color = a_color;
v_color.a = v_color.a * (255.0/254.0);
v_texCoords = a_texCoord0;
v_generic = a_generic;
gl_Position = u_projTrans * a_position;
}
}