From 79892f25258d6ed9b8a67ad209e5c7d078249a9e Mon Sep 17 00:00:00 2001 From: minjaesong Date: Wed, 13 Sep 2023 15:09:31 +0900 Subject: [PATCH] vibrancy is applied to the world only, as it should be --- src/net/torvald/random/HQRNG.java | 21 +++++++ .../torvald/terrarum/TerrarumPostProcessor.kt | 8 --- .../torvald/terrarum/gameworld/GameWorld.kt | 1 + .../terrarum/modulebasegame/IngameRenderer.kt | 27 ++++++--- src/net/torvald/terrarum/ui/ConsoleWindow.kt | 9 +-- src/shaders/postproc_dither.frag | 30 +--------- src/shaders/postproc_nodither.frag | 30 +--------- src/shaders/vibrancy.frag | 55 +++++++++++++++++++ 8 files changed, 102 insertions(+), 79 deletions(-) create mode 100644 src/shaders/vibrancy.frag diff --git a/src/net/torvald/random/HQRNG.java b/src/net/torvald/random/HQRNG.java index b958cc6ab..219496ebd 100644 --- a/src/net/torvald/random/HQRNG.java +++ b/src/net/torvald/random/HQRNG.java @@ -191,4 +191,25 @@ public class HQRNG extends Random { state0 = 1L; } + /** + * Returns a random value whose probability is distributed in triangular manner, of which 0.5 is the most and 0.0 and 1.0 are the least likely. + */ + public double nextTriangular() { + return (nextDouble() + nextDouble()) / 2.0; + } + + /** + * Returns a random value whose probability is distributed in triangular manner, of which 0.0 is the most and 1.0 is the least likely. + */ + public double nextBrown() { + return Math.abs(nextDouble() + nextDouble() - 1); + } + + /** + * Returns a random value whose probability is distributed in triangular manner, of which 1.0 is the most and 0.0 is the least likely. + */ + public double nextBlue() { + return 1.0 - nextBrown(); + } + } diff --git a/src/net/torvald/terrarum/TerrarumPostProcessor.kt b/src/net/torvald/terrarum/TerrarumPostProcessor.kt index fbf1e6656..6bdc63a29 100644 --- a/src/net/torvald/terrarum/TerrarumPostProcessor.kt +++ b/src/net/torvald/terrarum/TerrarumPostProcessor.kt @@ -237,13 +237,6 @@ object TerrarumPostProcessor : Disposable { else shaderPostNoDither - val (vo, vg) = INGAME.world.weatherbox.let { - if (it.currentWeather.identifier == "titlescreen") - 1f to 1f - else - it.currentVibrancy.x to it.currentVibrancy.y - } - App.getCurrentDitherTex().bind(1) fbo.colorBufferTexture.bind(0) @@ -253,7 +246,6 @@ object TerrarumPostProcessor : Disposable { shader.setUniformi("rnd", rng.nextInt(8192), rng.nextInt(8192)) shader.setUniformi("u_pattern", 1) shader.setUniformf("quant", shaderQuant[App.getConfigInt("displaycolourdepth")] ?: 255f) - shader.setUniformf("vibrancy", 1f, vo, vg, 1f) shader.setUniformMatrix4fv("swizzler", swizzler, rng.nextInt(24), 16*4) App.fullscreenQuad.render(shader, GL20.GL_TRIANGLE_FAN) diff --git a/src/net/torvald/terrarum/gameworld/GameWorld.kt b/src/net/torvald/terrarum/gameworld/GameWorld.kt index eefbab08c..61f9d9459 100644 --- a/src/net/torvald/terrarum/gameworld/GameWorld.kt +++ b/src/net/torvald/terrarum/gameworld/GameWorld.kt @@ -7,6 +7,7 @@ import net.torvald.terrarum.* import net.torvald.terrarum.App.printdbg import net.torvald.terrarum.blockproperties.Block import net.torvald.terrarum.blockproperties.Fluid +import net.torvald.terrarum.concurrent.ThreadExecutor import net.torvald.terrarum.gameactors.ActorID import net.torvald.terrarum.gameitems.ItemID import net.torvald.terrarum.itemproperties.ItemRemapTable diff --git a/src/net/torvald/terrarum/modulebasegame/IngameRenderer.kt b/src/net/torvald/terrarum/modulebasegame/IngameRenderer.kt index b2b6335f3..7afb61a84 100644 --- a/src/net/torvald/terrarum/modulebasegame/IngameRenderer.kt +++ b/src/net/torvald/terrarum/modulebasegame/IngameRenderer.kt @@ -88,6 +88,8 @@ object IngameRenderer : Disposable { val shaderForActors: ShaderProgram val shaderDemultiply: ShaderProgram + val shaderVibrancy: ShaderProgram + private val WIDTH = App.scr.width private val HEIGHT = App.scr.height private val WIDTHF = WIDTH.toFloat() @@ -131,6 +133,8 @@ object IngameRenderer : Disposable { shaderKawaseDown = App.loadShaderFromClasspath("shaders/default.vert", "shaders/kawasedown.frag") shaderKawaseUp = App.loadShaderFromClasspath("shaders/default.vert", "shaders/kawaseup.frag") + shaderVibrancy = App.loadShaderFromClasspath("shaders/default.vert", "shaders/vibrancy.frag") + if (!shaderBlendGlow.isCompiled) { Gdx.app.log("shaderBlendGlow", shaderBlendGlow.log) exitProcess(1) @@ -352,17 +356,20 @@ object IngameRenderer : Disposable { blendNormalStraightAlpha(batch) - batch.inUse { - // it's no use applying dithering here: colours are no longer "floats" once they're written to the FBO - // proof: disable dithering on skybox and enable dither here -- banding is still visible - // it would work if GDX supported HDR, or GL_RGBA32F as a texture format, but alas. - // but mixedOutTex is still needed for the screen capturing - - //batch.shader = if (App.getConfigBoolean("fx_dither")) IngameRenderer.shaderBayer else null - batch.shader = null - batch.drawFlipped(mixedOutTex, 0f, 0f) + val (vo, vg) = world.weatherbox.let { + if (it.currentWeather.identifier == "titlescreen") + 1f to 1f + else + it.currentVibrancy.x to it.currentVibrancy.y } + mixedOutTex.texture.bind(0) + shaderVibrancy.bind() + shaderVibrancy.setUniformMatrix("u_projTrans", camera.combined) + shaderVibrancy.setUniformi("u_texture", 0) + shaderVibrancy.setUniformf("vibrancy", 1f, vo, vg, 1f) + fullscreenQuad.render(shaderVibrancy, GL20.GL_TRIANGLE_FAN) + /////////////////////////////////////////////////////////////////////// @@ -899,6 +906,8 @@ object IngameRenderer : Disposable { shaderForActors.dispose() shaderDemultiply.dispose() + shaderVibrancy.dispose() + if (::fboRGBexport.isInitialized) fboRGBexport.tryDispose() } diff --git a/src/net/torvald/terrarum/ui/ConsoleWindow.kt b/src/net/torvald/terrarum/ui/ConsoleWindow.kt index ae280e7af..8c2749d29 100644 --- a/src/net/torvald/terrarum/ui/ConsoleWindow.kt +++ b/src/net/torvald/terrarum/ui/ConsoleWindow.kt @@ -7,6 +7,7 @@ import com.badlogic.gdx.graphics.Color import com.badlogic.gdx.graphics.OrthographicCamera import com.badlogic.gdx.graphics.g2d.SpriteBatch import net.torvald.terrarum.App +import net.torvald.terrarum.App.printdbg import net.torvald.terrarum.Terrarum import net.torvald.terrarum.TerrarumAppConfiguration import net.torvald.terrarum.ccE @@ -249,7 +250,7 @@ class ConsoleWindow : UICanvas() { override fun doOpening(delta: Float) { Terrarum.ingame?.let { - println("Game was paused beforehand: ${it.paused}") +// printdbg(this, "Game was paused beforehand: ${it.paused}") if (!it.paused) { iMadeTheGameToPause = true it.pause() @@ -257,7 +258,7 @@ class ConsoleWindow : UICanvas() { else { iMadeTheGameToPause = false } - println("I made the game to pause: $iMadeTheGameToPause") +// printdbg(this, "I made the game to pause: $iMadeTheGameToPause") } /*openingTimeCounter += delta drawOffY = MovementInterpolator.fastPullOut(openingTimeCounter.toFloat() / openCloseTime.toFloat(), @@ -282,10 +283,10 @@ class ConsoleWindow : UICanvas() { } override fun endClosing(delta: Float) { - println("Close -- I made the game to pause: $iMadeTheGameToPause") +// printdbg(this, "Close -- I made the game to pause: $iMadeTheGameToPause") if (iMadeTheGameToPause) { Terrarum.ingame?.resume() - println("Close -- resume game") +// printdbg(this, "Close -- resume game") } iMadeTheGameToPause = false Terrarum.ingame?.setTooltipMessage(null) diff --git a/src/shaders/postproc_dither.frag b/src/shaders/postproc_dither.frag index f9b73b208..3cc26d3f5 100644 --- a/src/shaders/postproc_dither.frag +++ b/src/shaders/postproc_dither.frag @@ -41,20 +41,6 @@ const vec4 matrixNormaliser = vec4(0.5 / 256.0); const vec2 patternsize = vec2(1.0/512.0, 1.0/512.0); -const mat4 rgb_to_ycocg = mat4( - 0.25, 1.0, -0.5, 0.0, - 0.5, 0.0, 1.0, 0.0, - 0.25, -1.0, -0.5, 0.0, - 0.0, 0.0, 0.0, 1.0 -); - -const mat4 ycocg_to_rgb = mat4( - 1.0, 1.0, 1.0, 0.0, - 0.5, 0.0, -0.5, 0.0, - -0.5, 0.5, -0.5, 0.0, - 0.0, 0.0, 0.0, 1.0 -); - vec4 nearestColour(vec4 inColor) { return floor(quantVec * inColor) * invQuant; @@ -65,23 +51,9 @@ vec4 getDitherredDot(vec4 inColor) { return nearestColour(fma(bayerThreshold, invQuant, inColor)); } - -uniform vec4 vibrancy = vec4(1.0);//vec4(1.0, 1.4, 1.2, 1.0); - void main(void) { - // convert input RGB into YCoCg vec4 incolour = texture(u_texture, v_texCoords); - vec4 yog = rgb_to_ycocg * incolour; // vec4(Y, Co, Cg, A) where Y,A=[0,1]; Co,Cg=[-1,1] - - // Do colour-grading magic - vec4 sgn = sign(yog); - vec4 absval = abs(yog); - vec4 raised = pow(absval, boolean.yyyy / vibrancy); - vec4 newColour = sgn * raised; - - // Dither the output - vec4 graded = ycocg_to_rgb * newColour; - vec4 selvec = getDitherredDot(graded); + vec4 selvec = getDitherredDot(incolour); vec4 outcol = fma(selvec, boolean.yyyx, boolean.xxxy); // use quantised RGB but not the A fragColor = outcol; diff --git a/src/shaders/postproc_nodither.frag b/src/shaders/postproc_nodither.frag index be4152967..e62c9a504 100644 --- a/src/shaders/postproc_nodither.frag +++ b/src/shaders/postproc_nodither.frag @@ -28,37 +28,9 @@ uniform float quant = 255.0; // 64 steps -> 63.0; 256 steps -> 255.0 const vec2 boolean = vec2(0.0, 1.0); -const mat4 rgb_to_ycocg = mat4( - 0.25, 1.0, -0.5, 0.0, - 0.5, 0.0, 1.0, 0.0, - 0.25, -1.0, -0.5, 0.0, - 0.0, 0.0, 0.0, 1.0 -); - -const mat4 ycocg_to_rgb = mat4( - 1.0, 1.0, 1.0, 0.0, - 0.5, 0.0, -0.5, 0.0, - -0.5, 0.5, -0.5, 0.0, - 0.0, 0.0, 0.0, 1.0 -); - - -uniform vec4 vibrancy = vec4(1.0);//vec4(1.0, 1.4, 1.2, 1.0); - out vec4 fragColor; void main(void) { - // convert input RGB into YCoCg vec4 incolour = texture(u_texture, v_texCoords); - vec4 yog = rgb_to_ycocg * incolour; // vec4(Y, Co, Cg, A) where Y,A=[0,1]; Co,Cg=[-1,1] - - // Do colour-grading magic - vec4 sgn = sign(yog); - vec4 absval = abs(yog); - vec4 raised = pow(absval, boolean.yyyy / vibrancy); - vec4 newColour = sgn * raised; - - // Dither the output - vec4 graded = ycocg_to_rgb * newColour; - fragColor = fma(graded, boolean.yyyx, boolean.xxxy); // use quantised RGB but not the A + fragColor = fma(incolour, boolean.yyyx, boolean.xxxy); // use quantised RGB but not the A } \ No newline at end of file diff --git a/src/shaders/vibrancy.frag b/src/shaders/vibrancy.frag new file mode 100644 index 000000000..8c2da0646 --- /dev/null +++ b/src/shaders/vibrancy.frag @@ -0,0 +1,55 @@ +#version 400 +#ifdef GL_ES +precision mediump float; +#endif + + +vec4 gammaIn(vec4 col) { + return pow(col, vec4(2.2)); +} + +vec4 gammaOut(vec4 col) { + return pow(col, vec4(1.0 / 2.2)); +} + +in vec4 v_color; // unused! +in vec2 v_texCoords; + +uniform sampler2D u_texture; + +const vec2 boolean = vec2(0.0, 1.0); + +const mat4 rgb_to_ycocg = mat4( +0.25, 1.0, -0.5, 0.0, +0.5, 0.0, 1.0, 0.0, +0.25, -1.0, -0.5, 0.0, +0.0, 0.0, 0.0, 1.0 +); + +const mat4 ycocg_to_rgb = mat4( +1.0, 1.0, 1.0, 0.0, +0.5, 0.0, -0.5, 0.0, +-0.5, 0.5, -0.5, 0.0, +0.0, 0.0, 0.0, 1.0 +); + + +uniform vec4 vibrancy = vec4(1.0);//vec4(1.0, 1.4, 1.2, 1.0); + +out vec4 fragColor; + +void main(void) { + // convert input RGB into YCoCg + vec4 incolour = texture(u_texture, v_texCoords); + vec4 yog = rgb_to_ycocg * incolour; // vec4(Y, Co, Cg, A) where Y,A=[0,1]; Co,Cg=[-1,1] + + // Do colour-grading magic + vec4 sgn = sign(yog); + vec4 absval = abs(yog); + vec4 raised = pow(absval, boolean.yyyy / vibrancy); + vec4 newColour = sgn * raised; + + // Dither the output + vec4 graded = ycocg_to_rgb * newColour; + fragColor = fma(graded, boolean.yyyx, boolean.xxxy); // use quantised RGB but not the A +} \ No newline at end of file