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 { class Cvec {
/** the red, green, blue and alpha components */ /** the red, green, blue and alpha components */
var r: Float = 0f @kotlin.jvm.JvmField var r: Float = 0f
var g: Float = 0f @kotlin.jvm.JvmField var g: Float = 0f
var b: Float = 0f @kotlin.jvm.JvmField var b: Float = 0f
var a: 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. */ /** Constructs a new Cvec with all components set to 0. */
constructor() {} constructor() {}

View File

@@ -3,13 +3,14 @@ package net.torvald.terrarum
import com.badlogic.gdx.graphics.Texture import com.badlogic.gdx.graphics.Texture
import com.badlogic.gdx.graphics.g2d.SpriteBatch import com.badlogic.gdx.graphics.g2d.SpriteBatch
import com.badlogic.gdx.graphics.g2d.TextureRegion import com.badlogic.gdx.graphics.g2d.TextureRegion
import com.badlogic.gdx.graphics.g2d.UnpackedColourSpriteBatch
/** /**
* Don't flip the assets! Flip the draw command instead! * Don't flip the assets! Flip the draw command instead!
* *
* Created by minjaesong on 2021-12-13. * 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 * 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) draw(region.texture, x, y, region.regionWidth.toFloat(), region.regionHeight.toFloat(), region.u, region.v2, region.u2, region.v)
/** /**
* NOTE TO SELF: * NOTE TO SELF:
* *

View File

@@ -4,6 +4,7 @@ import com.badlogic.gdx.Gdx
import com.badlogic.gdx.graphics.* import com.badlogic.gdx.graphics.*
import com.badlogic.gdx.graphics.g2d.SpriteBatch import com.badlogic.gdx.graphics.g2d.SpriteBatch
import com.badlogic.gdx.graphics.g2d.TextureRegion 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.Vector2
import com.badlogic.gdx.math.Vector3 import com.badlogic.gdx.math.Vector3
import com.badlogic.gdx.utils.JsonValue 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 batch.shader.setUniformf("shadeCol", 0.06f, 0.07f, 0.08f, 1f) // TODO temporary value
clouds.forEach { 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.Color
import com.badlogic.gdx.graphics.g2d.SpriteBatch import com.badlogic.gdx.graphics.g2d.SpriteBatch
import com.badlogic.gdx.graphics.g2d.TextureRegion import com.badlogic.gdx.graphics.g2d.TextureRegion
import com.badlogic.gdx.graphics.g2d.UnpackedColourSpriteBatch
import com.badlogic.gdx.math.Vector3 import com.badlogic.gdx.math.Vector3
import com.jme3.math.FastMath import com.jme3.math.FastMath
import com.jme3.math.FastMath.PI import com.jme3.math.FastMath.PI
@@ -77,28 +78,13 @@ class WeatherObjectCloud(
private val vecMult = Vector3(1f, 1f, 1f / (4f * H)) private val vecMult = Vector3(1f, 1f, 1f / (4f * H))
private fun roundRgbGamma(x: Float): Int { fun render(batch: UnpackedColourSpriteBatch, cloudDrawColour: Color) {
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) {
val sc = screenCoord val sc = screenCoord
val rgbGammaIndex = roundRgbGamma(rgbGamma)
val aGammaIndex = roundAgamma(aGamma)
// printdbg(this, "gamma: (${rgbGamma}, ${aGamma}) index: ($rgbGammaIndex, $aGammaIndex)") // printdbg(this, "gamma: (${rgbGamma}, ${aGamma}) index: ($rgbGammaIndex, $aGammaIndex)")
val lightBits = cloudDrawColour.toIntBits() batch.color = cloudDrawColour
val rbits = lightBits.ushr( 0).and(252) or rgbGammaIndex.ushr(2).and(3) batch.generic.set(rgbGamma, aGamma, 0f, 0f)
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)
if (flipW) if (flipW)
batch.draw(texture, sc.x + texture.regionWidth / posZ, sc.y, -texture.regionWidth * sc.z, texture.regionHeight * sc.z) 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 CLOUD_STAGE_DEPTH = 256f
const val OLD_AGE_DECAY = 5000f const val OLD_AGE_DECAY = 5000f
const val NEWBORN_GROWTH_TIME = 1000f 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 #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; in vec2 v_texCoords;
uniform sampler2D u_texture; uniform sampler2D u_texture;
out vec4 fragColor; out vec4 fragColor;
const vec2 boolean = vec2(0.0, 1.0); const vec2 boolean = vec2(0.0, 1.0);
uniform LOWP vec4 shadeCol; uniform 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
);
void main() { void main() {
// vertex colour format: // vertex colour format:
@@ -54,15 +20,10 @@ void main() {
// bbbbbb: 6-bit blue component // bbbbbb: 6-bit blue component
// MMLL: index to the rgbGammas // MMLL: index to the rgbGammas
// AA: index to the aGammas // AA: index to the aGammas
vec4 cloudCol = vec4( vec4 cloudCol = v_color;
(int(v_color.r * 255) >> 2) * 4.0 / 255.0, float rgbGamma = v_generic.x;
(int(v_color.g * 255) >> 2) * 4.0 / 255.0, float aGamma = v_generic.y;
(int(v_color.b * 255) >> 2) * 4.0 / 255.0, vec4 gamma = v_generic.xxxy;
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 range = vec4(vec3(min(rgbGamma, 1.0 / rgbGamma)), 1.0); vec4 range = vec4(vec3(min(rgbGamma, 1.0 / rgbGamma)), 1.0);
vec4 offset = vec4(vec3(max(0.0, 1.0 - rgbGamma)), 0.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_position;
in vec4 a_color; in vec4 a_color;
in vec4 a_generic;
in vec2 a_texCoord0; in vec2 a_texCoord0;
uniform mat4 u_projTrans; uniform mat4 u_projTrans;
out vec4 v_color; out vec4 v_color;
out vec2 v_texCoords; out vec2 v_texCoords;
out vec4 v_generic;
void main() { void main() {
v_color = a_color; 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_texCoords = a_texCoord0;
v_generic = a_generic;
gl_Position = u_projTrans * a_position; gl_Position = u_projTrans * a_position;
} }

View File

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

View File

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