mirror of
https://github.com/curioustorvald/Terrarum.git
synced 2026-03-14 07:36:06 +09:00
screen blur moved to a single function
This commit is contained in:
@@ -4,6 +4,7 @@ import com.badlogic.gdx.graphics.*
|
||||
import com.badlogic.gdx.graphics.g2d.SpriteBatch
|
||||
import com.badlogic.gdx.graphics.glutils.FrameBuffer
|
||||
import net.torvald.terrarum.modulebasegame.IngameRenderer
|
||||
import net.torvald.terrarum.ui.Toolkit
|
||||
import net.torvald.terrarum.ui.UICanvas
|
||||
|
||||
/**
|
||||
@@ -56,55 +57,11 @@ class UIFakeBlurOverlay : UICanvas() {
|
||||
|
||||
private val shaderBlur = App.loadShaderFromFile("assets/blur.vert", "assets/blur2.frag")
|
||||
|
||||
private val blurRadius = 2f
|
||||
private val darken = Color(0.5f, 0.5f, 0.5f, 1f)
|
||||
private var fbo = FrameBuffer(Pixmap.Format.RGBA8888, width / 4, height / 4, true)
|
||||
|
||||
|
||||
override fun updateUI(delta: Float) {}
|
||||
override fun renderUI(batch: SpriteBatch, camera: Camera) {
|
||||
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.setUniformf("iResolution", width.toFloat(), height.toFloat())
|
||||
IngameRenderer.shaderBlur.setUniformf("flip", 1f)
|
||||
if (i % 2 == 0)
|
||||
IngameRenderer.shaderBlur.setUniformf("direction", scalar, 0f)
|
||||
else
|
||||
IngameRenderer.shaderBlur.setUniformf("direction", 0f, scalar)
|
||||
|
||||
val p = Pixmap.createFromFrameBuffer(0, 0, width, height)
|
||||
val t = Texture(p); t.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear)
|
||||
|
||||
batch.draw(t, 0f, 0f)
|
||||
batch.flush() // so I can safely dispose of the texture
|
||||
|
||||
t.dispose(); p.dispose()
|
||||
}
|
||||
|
||||
// sample blurred but blocky texture, scale it down, and re-scale up to the main screen
|
||||
batch.end()
|
||||
val p = Pixmap.createFromFrameBuffer(0, 0, width, height)
|
||||
val t = Texture(p); t.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear)
|
||||
|
||||
fbo.inAction(camera as OrthographicCamera, batch) {
|
||||
batch.inUse {
|
||||
batch.shader = null
|
||||
batch.draw(t, 0f, 0f, fbo.width.toFloat(), fbo.height.toFloat())
|
||||
}
|
||||
}
|
||||
t.dispose(); p.dispose()
|
||||
|
||||
batch.begin()
|
||||
|
||||
val t2 = fbo.colorBufferTexture
|
||||
t2.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear)
|
||||
blendNormal(batch)
|
||||
batch.draw(t2, 0f, 0f, width.toFloat(), height.toFloat())
|
||||
|
||||
Toolkit.blurEntireScreen(batch, camera as OrthographicCamera, 2f,0, 0, width, height)
|
||||
|
||||
blendMul(batch)
|
||||
batch.color = darken
|
||||
@@ -119,6 +76,5 @@ class UIFakeBlurOverlay : UICanvas() {
|
||||
override fun endClosing(delta: Float) {}
|
||||
override fun dispose() {
|
||||
shaderBlur.dispose()
|
||||
fbo.dispose()
|
||||
}
|
||||
}
|
||||
@@ -180,6 +180,7 @@ class UIInventoryFull(
|
||||
|
||||
override fun renderUI(batch: SpriteBatch, camera: Camera) {
|
||||
|
||||
Toolkit.blurEntireScreen(batch, camera as OrthographicCamera, 1f,0, 0, width, height)
|
||||
|
||||
// background fill
|
||||
batch.end()
|
||||
|
||||
@@ -2,11 +2,13 @@ package net.torvald.terrarum.ui
|
||||
|
||||
import com.badlogic.gdx.Gdx
|
||||
import com.badlogic.gdx.graphics.Color
|
||||
import com.badlogic.gdx.graphics.OrthographicCamera
|
||||
import com.badlogic.gdx.graphics.Pixmap
|
||||
import com.badlogic.gdx.graphics.Texture
|
||||
import com.badlogic.gdx.graphics.g2d.SpriteBatch
|
||||
import net.torvald.terrarum.App
|
||||
import net.torvald.terrarum.CommonResourcePool
|
||||
import net.torvald.terrarum.fillRect
|
||||
import com.badlogic.gdx.graphics.glutils.FrameBuffer
|
||||
import net.torvald.terrarum.*
|
||||
import net.torvald.terrarum.modulebasegame.IngameRenderer
|
||||
import net.torvald.terrarumsansbitmap.gdx.TextureRegionPack
|
||||
|
||||
|
||||
@@ -17,6 +19,8 @@ object Toolkit {
|
||||
|
||||
val DEFAULT_BOX_BORDER_COL = Color(1f, 1f, 1f, 0.2f)
|
||||
|
||||
private val shaderBlur = App.loadShaderFromFile("assets/blur.vert", "assets/blur2.frag")
|
||||
|
||||
init {
|
||||
CommonResourcePool.addToLoadingList("toolkit_box_border") {
|
||||
TextureRegionPack(Gdx.files.internal("./assets/graphics/gui/box_border_flat_tileable.tga"), 1, 1)
|
||||
@@ -68,4 +72,30 @@ object Toolkit {
|
||||
|
||||
}
|
||||
|
||||
fun blurEntireScreen(batch: SpriteBatch, camera: OrthographicCamera, blurRadius: Float, x: Int, y: Int, w: Int, h: Int) {
|
||||
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.setUniformf("iResolution", w.toFloat(), h.toFloat())
|
||||
IngameRenderer.shaderBlur.setUniformf("flip", 1f)
|
||||
if (i % 2 == 0)
|
||||
IngameRenderer.shaderBlur.setUniformf("direction", scalar, 0f)
|
||||
else
|
||||
IngameRenderer.shaderBlur.setUniformf("direction", 0f, scalar)
|
||||
|
||||
val p = Pixmap.createFromFrameBuffer(0, 0, w, h)
|
||||
val t = Texture(p); t.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear)
|
||||
|
||||
batch.draw(t, 0f, 0f)
|
||||
batch.flush() // so I can safely dispose of the texture
|
||||
|
||||
t.dispose(); p.dispose()
|
||||
}
|
||||
|
||||
batch.shader = null
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user