better implementation of dithering which relies on external texture and it runs faster

This commit is contained in:
minjaesong
2021-10-10 01:00:58 +09:00
parent d28698b668
commit 35b4b5cfd5
12 changed files with 131 additions and 1395 deletions

View File

@@ -215,6 +215,7 @@ public class App implements ApplicationListener {
public static Texture ditherPattern;
private static ShaderProgram shaderBayerSkyboxFill; // ONLY to be used by the splash screen
public static ShaderProgram shaderHicolour;
public static ShaderProgram shaderPassthruRGB;
@@ -405,6 +406,7 @@ public class App implements ApplicationListener {
CommonResourcePool.INSTANCE.addToLoadingList("title_health2", () -> new Texture(Gdx.files.internal("./assets/graphics/gui/health_distance.tga")));
// set GL graphics constants
ditherPattern = new Texture(Gdx.files.internal("assets/LDR_512_RGBA_0.tga"));
shaderBayerSkyboxFill = loadShaderFromFile("assets/4096.vert", "assets/4096_bayer_skyboxfill.frag");
shaderHicolour = loadShaderFromFile("assets/4096.vert", "assets/hicolour.frag");
shaderPassthruRGB = SpriteBatch.createDefaultShader();
@@ -582,11 +584,13 @@ public class App implements ApplicationListener {
}
private void drawSplash() {
ditherPattern.bind(0);
shaderBayerSkyboxFill.bind();
shaderBayerSkyboxFill.setUniformMatrix("u_projTrans", camera.combined);
shaderBayerSkyboxFill.setUniformi("u_texture", 0);
shaderBayerSkyboxFill.setUniformf("parallax_size", 0f);
shaderBayerSkyboxFill.setUniformf("topColor", gradWhiteTop.r, gradWhiteTop.g, gradWhiteTop.b);
shaderBayerSkyboxFill.setUniformf("bottomColor", gradWhiteBottom.r, gradWhiteBottom.g, gradWhiteBottom.b);
shaderBayerSkyboxFill.setUniformf("topColor", gradWhiteTop.r, gradWhiteTop.g, gradWhiteTop.b, 1f);
shaderBayerSkyboxFill.setUniformf("bottomColor", gradWhiteBottom.r, gradWhiteBottom.g, gradWhiteBottom.b, 1f);
fullscreenQuad.render(shaderBayerSkyboxFill, GL20.GL_TRIANGLES);
@@ -706,6 +710,7 @@ public class App implements ApplicationListener {
//FloatDrawer.INSTANCE.dispose();
ditherPattern.dispose();
shaderBayerSkyboxFill.dispose();
shaderHicolour.dispose();
shaderPassthruRGB.dispose();

View File

@@ -55,6 +55,13 @@ object IngameRenderer : Disposable {
private lateinit var lightTex: TextureRegion
private lateinit var blurTex: TextureRegion
val ditherPattern = App.ditherPattern
init {
ditherPattern.setFilter(Texture.TextureFilter.Nearest, Texture.TextureFilter.Linear)
ditherPattern.setWrap(Texture.TextureWrap.Repeat, Texture.TextureWrap.Repeat)
}
// you must have lightMixed FBO; otherwise you'll be reading from unbaked FBO and it freaks out GPU
inline fun isDither() = App.getConfigBoolean("fx_dither")
@@ -469,6 +476,9 @@ object IngameRenderer : Disposable {
gdxSetBlend()
ditherPattern.bind(1)
Gdx.gl.glActiveTexture(GL20.GL_TEXTURE0) // so that batch that comes next will bind any tex to it
batch.inUse {
blendNormal(batch)
@@ -486,6 +496,7 @@ object IngameRenderer : Disposable {
blendMul(batch)
batch.shader = shaderRGBOnly
batch.shader.setUniformi("u_pattern", 1)
batch.draw(lightTex,
xrem, yrem,
lightTex.regionWidth * lightmapDownsample,
@@ -553,6 +564,9 @@ object IngameRenderer : Disposable {
setCameraPosition(0f, 0f)
val (xrem, yrem) = worldCamToRenderPos()
ditherPattern.bind(1)
Gdx.gl.glActiveTexture(GL20.GL_TEXTURE0) // so that batch that comes next will bind any tex to it
batch.inUse {
// draw world
batch.draw(fboA.colorBufferTexture, 0f, 0f)
@@ -567,6 +581,7 @@ object IngameRenderer : Disposable {
blendMul(batch)
batch.shader = shaderAtoGrey
batch.shader.setUniformi("u_pattern", 1)
batch.draw(lightTex,
xrem, yrem,
lightTex.regionWidth * lightmapDownsample,
@@ -664,12 +679,15 @@ object IngameRenderer : Disposable {
blurWriteBuffer.inAction(camera, batch) {
blurTex.texture = blurReadBuffer.colorBufferTexture
blurTex.texture.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear)
blurTex.texture.bind(0)
ditherPattern.bind(1) // order is important!
shaderBlur.bind()
shaderBlur.setUniformMatrix("u_projTrans", camera.combined)
shaderBlur.setUniformi("u_pattern", 1)
shaderBlur.setUniformi("u_texture", 0)
shaderBlur.setUniformf("iResolution",
blurWriteBuffer.width.toFloat(), blurWriteBuffer.height.toFloat())

View File

@@ -3,20 +3,18 @@ package net.torvald.terrarum.modulebasegame.gameactors
import com.badlogic.gdx.graphics.g2d.SpriteBatch
import net.torvald.gdx.graphics.Cvec
import net.torvald.random.HQRNG
import net.torvald.spriteanimation.SpriteAnimation
import net.torvald.terrarum.*
import net.torvald.terrarum.BlockCodex
import net.torvald.terrarum.CommonResourcePool
import net.torvald.terrarum.ModMgr
import net.torvald.terrarum.Terrarum
import net.torvald.terrarum.blockproperties.Block
import net.torvald.terrarum.blockproperties.BlockCodex
import net.torvald.terrarum.gameactors.AVKey
import net.torvald.terrarum.gameactors.Hitbox
import net.torvald.terrarum.gameactors.Luminous
import net.torvald.terrarum.gameparticles.ParticleVanishingSprite
import net.torvald.terrarum.gameparticles.ParticleVanishingText
import net.torvald.terrarum.langpack.Lang
import net.torvald.terrarum.modulebasegame.TerrarumIngame
import net.torvald.terrarumsansbitmap.gdx.TextureRegionPack
import java.util.*
import kotlin.math.roundToInt
/**
* Created by minjaesong on 2016-06-17.
@@ -45,7 +43,7 @@ internal class FixtureTikiTorch : FixtureBase, Luminous {
TextureRegionPack(ModMgr.getGdxFile("basegame", "sprites/fixtures/tiki_torch.tga"), 16, 32)
}
CommonResourcePool.addToLoadingList("particles-tiki_smoke.tga") {
TextureRegionPack(ModMgr.getGdxFile("basegame", "particles/tiki_smoke.tga"), 10, 10)
TextureRegionPack(ModMgr.getGdxFile("basegame", "particles/bigger_smoke.tga"), 16, 16)
}
CommonResourcePool.loadAll()
@@ -70,7 +68,7 @@ internal class FixtureTikiTorch : FixtureBase, Luminous {
if (spawnTimer >= nextDelay) {
(Terrarum.ingame as TerrarumIngame).addParticle(ParticleVanishingSprite(
CommonResourcePool.getAsTextureRegionPack("particles-tiki_smoke.tga"),
0.25f, true, hitbox.centeredX, hitbox.startY, false, rng.nextInt(256)
25f, true, hitbox.centeredX, hitbox.startY, false, rng.nextInt(256)
))
spawnTimer -= nextDelay

View File

@@ -14,6 +14,7 @@ import net.torvald.terrarum.CommonResourcePool
import net.torvald.terrarum.fillRect
import net.torvald.terrarum.modulebasegame.IngameRenderer
import net.torvald.terrarumsansbitmap.gdx.TextureRegionPack
import org.lwjgl.opengl.GL20
/**
@@ -101,12 +102,20 @@ object Toolkit : Disposable {
}
fun blurEntireScreen(batch: SpriteBatch, camera: OrthographicCamera, blurRadius: Float, x: Int, y: Int, w: Int, h: Int) {
batch.end()
IngameRenderer.ditherPattern.bind(1)
Gdx.gl.glActiveTexture(GL20.GL_TEXTURE0) // so that batch that comes next will bind any tex to it
batch.begin()
for (i in 0 until 6) {
val scalar = blurRadius * (1 shl i.ushr(1))
batch.shader = shaderBlur
shaderBlur.setUniformMatrix("u_projTrans", camera.combined)
shaderBlur.setUniformi("u_texture", 0)
shaderBlur.setUniformi("u_pattern", 1)
shaderBlur.setUniformf("iResolution", w.toFloat(), h.toFloat())
IngameRenderer.shaderBlur.setUniformf("flip", 1f)
if (i % 2 == 0)

View File

@@ -20,7 +20,6 @@ import net.torvald.terrarum.modulebasegame.gameactors.ParticleMegaRain
import net.torvald.terrarum.utils.JsonFetcher
import net.torvald.terrarum.worlddrawer.WorldCamera
import java.io.File
import java.util.*
/**
*
@@ -177,8 +176,17 @@ internal object WeatherMixer : RNGConsumer {
skyboxTexture.dispose()
skyboxTexture = Texture(skyboxPixmap); skyboxTexture.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear)
IngameRenderer.batch.shader = if (App.getConfigBoolean("fx_dither")) IngameRenderer.shaderBayer else null
if (App.getConfigBoolean("fx_dither")) {
IngameRenderer.ditherPattern.bind(1)
Gdx.gl.glActiveTexture(GL20.GL_TEXTURE0) // so that batch that comes next will bind any tex to it
}
batch.inUse {
it.shader = if (App.getConfigBoolean("fx_dither")) IngameRenderer.shaderBayer else null
if (App.getConfigBoolean("fx_dither")) {
it.shader.setUniformi("u_pattern", 1)
}
it.draw(skyboxTexture, 0f, -App.scr.halfhf, App.scr.wf, App.scr.hf * 2f) // because of how the linear filter works, we extend the image by two
}