From 8cf4b5d9a9cae62582590f57b58a021378855c44 Mon Sep 17 00:00:00 2001 From: minjaesong Date: Sun, 24 Nov 2024 17:01:54 +0900 Subject: [PATCH] shadows around actors as well as terrain --- .../terrarum/modulebasegame/IngameRenderer.kt | 46 ++++++++++++++++--- .../modulebasegame/console/ExportFBO.kt | 15 ++++++ src/net/torvald/terrarum/ui/BlurMgr.kt | 9 ++++ .../terrarum/worlddrawer/BlocksDrawer.kt | 4 +- .../terrarum/worlddrawer/CreateTileAtlas.kt | 2 +- src/shaders/shadowdeep.frag | 18 ++++++++ src/shaders/shadowshallow.frag | 18 ++++++++ 7 files changed, 102 insertions(+), 10 deletions(-) create mode 100644 src/shaders/shadowdeep.frag create mode 100644 src/shaders/shadowshallow.frag diff --git a/src/net/torvald/terrarum/modulebasegame/IngameRenderer.kt b/src/net/torvald/terrarum/modulebasegame/IngameRenderer.kt index e7313ac2b..ffa40c1dc 100644 --- a/src/net/torvald/terrarum/modulebasegame/IngameRenderer.kt +++ b/src/net/torvald/terrarum/modulebasegame/IngameRenderer.kt @@ -17,7 +17,6 @@ import net.torvald.terrarum.TerrarumAppConfiguration.TILE_SIZE import net.torvald.terrarum.TerrarumAppConfiguration.TILE_SIZED import net.torvald.terrarum.TerrarumAppConfiguration.TILE_SIZEF import net.torvald.terrarum.blockproperties.Block -import net.torvald.terrarum.blockproperties.FluidCodex import net.torvald.terrarum.gameactors.ActorWithBody import net.torvald.terrarum.gameactors.ActorWithBody.Companion.METER import net.torvald.terrarum.gameactors.ActorWithBody.Companion.PHYS_EPSILON_DIST @@ -32,6 +31,7 @@ import net.torvald.terrarum.gameworld.fmod import net.torvald.terrarum.modulebasegame.gameactors.Pocketed import net.torvald.terrarum.modulebasegame.gameitems.ItemThrowable import net.torvald.terrarum.modulebasegame.gameitems.getThrowPosAndVector +import net.torvald.terrarum.ui.BlurMgr import net.torvald.terrarum.ui.Toolkit import net.torvald.terrarum.weather.WeatherMixer import net.torvald.terrarum.worlddrawer.BlocksDrawer @@ -80,6 +80,11 @@ object IngameRenderer : Disposable { private lateinit var fboRGBactorsMiddle: Float16FrameBuffer // for large shadow eff; A channel is for glow effects so they don't get shadow effects private lateinit var fboRGBterrain: Float16FrameBuffer // for large shadow eff; A channel is for glow effects so they don't get shadow effects + private lateinit var fboRGBactorsBehindShadow: Float16FrameBuffer // for small shadow eff; A channel is for glow effects so they don't get shadow effects + private lateinit var fboRGBactorsMiddleShadow: Float16FrameBuffer // for large shadow eff; A channel is for glow effects so they don't get shadow effects + private lateinit var fboRGBterrainShadow: Float16FrameBuffer // for large shadow eff; A channel is for glow effects so they don't get shadow effects + + private lateinit var rgbTex: TextureRegion private lateinit var aTex: TextureRegion private lateinit var mixedOutTex: TextureRegion @@ -109,6 +114,8 @@ object IngameRenderer : Disposable { val shaderBlendGlow: ShaderProgram val shaderBlendGlowTex1Flip: ShaderProgram val shaderForActors: ShaderProgram + val shaderShadowShallow: ShaderProgram + val shaderShadowDeep: ShaderProgram val shaderDemultiply: ShaderProgram val shaderBayerAlpha: ShaderProgram @@ -151,6 +158,8 @@ object IngameRenderer : Disposable { shaderForActors = App.loadShaderFromClasspath("shaders/default.vert", "shaders/actors.frag") + shaderShadowShallow = App.loadShaderFromClasspath("shaders/default.vert", "shaders/shadowshallow.frag") + shaderShadowDeep = App.loadShaderFromClasspath("shaders/default.vert", "shaders/shadowdeep.frag") shaderBlendGlow = App.loadShaderFromClasspath("shaders/blendGlow.vert", "shaders/blendGlow.frag") shaderBlendGlowTex1Flip = App.loadShaderFromClasspath("shaders/blendGlow.vert", "shaders/blendGlowTex1Flip.frag") shaderDemultiply = App.loadShaderFromClasspath("shaders/blendGlow.vert", "shaders/demultiply.frag") @@ -511,9 +520,11 @@ object IngameRenderer : Disposable { batch.shader = shaderForActors batch.color = Color.WHITE moveCameraToWorldCoord() + actorsRenderFarBehind?.forEach { it.drawBody(frameDelta, batch) } actorsRenderBehind?.forEach { it.drawBody(frameDelta, batch) } } } + BlurMgr.makeBlur(fboRGBactorsBehind, fboRGBactorsBehindShadow, 0.25f) fboRGBactorsMiddle.inAction(camera, batch) { clearBuffer() @@ -526,12 +537,14 @@ object IngameRenderer : Disposable { actorsRenderMiddle?.forEach { it.drawBody(frameDelta, batch) } } } + BlurMgr.makeBlur(fboRGBactorsMiddle, fboRGBactorsMiddleShadow, 2.5f) fboRGBterrain.inAction(camera, batch) { clearBuffer() setCameraPosition(0f, 0f) BlocksDrawer.drawTerrain(batch.projectionMatrix, false) } + BlurMgr.makeBlur(fboRGBterrain, fboRGBterrainShadow, 2.5f) fboRGB.inAction(camera, batch) { @@ -539,24 +552,36 @@ object IngameRenderer : Disposable { BlocksDrawer.drawWall(batch.projectionMatrix, false) + // draw actor shadow BEFORE the terrain draw + batch.inUse { + batch.shader = shaderShadowShallow + setCameraPosition(0f, 0f) + batch.drawFlipped(fboRGBactorsBehindShadow.colorBufferTexture, 0f, 0f) + + batch.shader = shaderShadowDeep + setCameraPosition(0f, 0f) + batch.drawFlipped(fboRGBterrainShadow.colorBufferTexture, 0f, 0f) + batch.drawFlipped(fboRGBactorsMiddleShadow.colorBufferTexture, 0f, 0f) + } + + // draw behind actors and particles batch.inUse { batch.shader = shaderForActors batch.color = Color.WHITE - moveCameraToWorldCoord() - actorsRenderFarBehind?.forEach { it.drawBody(frameDelta, batch) } -// actorsRenderBehind?.forEach { it.drawBody(frameDelta, batch) } setCameraPosition(0f, 0f) + batch.color = Color.WHITE batch.drawFlipped(fboRGBactorsBehind.colorBufferTexture, 0f, 0f) moveCameraToWorldCoord() particlesContainer?.forEach { it.drawBody(frameDelta, batch) } } + // draw just the terrain batch.inUse { batch.shader = null - batch.color = Color.WHITE setCameraPosition(0f, 0f) + batch.color = Color.WHITE batch.drawFlipped(fboRGBterrain.colorBufferTexture, 0f, 0f) } @@ -566,9 +591,8 @@ object IngameRenderer : Disposable { ///////////////// // draw actors // ///////////////// -// moveCameraToWorldCoord() -// actorsRenderMiddle?.forEach { it.drawBody(frameDelta, batch) } setCameraPosition(0f, 0f) + batch.color = Color.WHITE batch.drawFlipped(fboRGBactorsMiddle.colorBufferTexture, 0f, 0f) moveCameraToWorldCoord() @@ -1261,6 +1285,9 @@ object IngameRenderer : Disposable { fboRGBactorsBehind = Float16FrameBuffer(width, height, false) fboRGBactorsMiddle = Float16FrameBuffer(width, height, false) fboRGBterrain = Float16FrameBuffer(width, height, false) + fboRGBactorsBehindShadow = Float16FrameBuffer(width, height, false) + fboRGBactorsMiddleShadow = Float16FrameBuffer(width, height, false) + fboRGBterrainShadow = Float16FrameBuffer(width, height, false) lightmapFbo = Float16FrameBuffer( LightmapRenderer.lightBuffer.width * LightmapRenderer.DRAW_TILE_SIZE.toInt(), LightmapRenderer.lightBuffer.height * LightmapRenderer.DRAW_TILE_SIZE.toInt(), @@ -1326,6 +1353,9 @@ object IngameRenderer : Disposable { if (::fboRGBactorsBehind.isInitialized) fboRGBactorsBehind.tryDispose() if (::fboRGBactorsMiddle.isInitialized) fboRGBactorsMiddle.tryDispose() if (::fboRGBterrain.isInitialized) fboRGBterrain.tryDispose() + if (::fboRGBactorsBehindShadow.isInitialized) fboRGBactorsBehindShadow.tryDispose() + if (::fboRGBactorsMiddleShadow.isInitialized) fboRGBactorsMiddleShadow.tryDispose() + if (::fboRGBterrainShadow.isInitialized) fboRGBterrainShadow.tryDispose() blurtex0.tryDispose() @@ -1349,6 +1379,8 @@ object IngameRenderer : Disposable { shaderBlendGlow.dispose() shaderBlendGlowTex1Flip.dispose() shaderForActors.dispose() + shaderShadowShallow.dispose() + shaderShadowDeep.dispose() shaderDemultiply.dispose() shaderBayerAlpha.dispose() diff --git a/src/net/torvald/terrarum/modulebasegame/console/ExportFBO.kt b/src/net/torvald/terrarum/modulebasegame/console/ExportFBO.kt index e3b8fd215..ebb101430 100644 --- a/src/net/torvald/terrarum/modulebasegame/console/ExportFBO.kt +++ b/src/net/torvald/terrarum/modulebasegame/console/ExportFBO.kt @@ -117,6 +117,21 @@ internal object ExportFBO : ConsoleCommand { fun fborgbterrain(): FrameBuffer { return IngameRenderer.extortField("fboRGBterrain")!! } + + @ExportFBOCmd("Framebuffer for render-behind actors used for creating shallow shadow effects") + fun fborgbactorsbehindshadow(): FrameBuffer { + return IngameRenderer.extortField("fboRGBactorsBehindShadow")!! + } + + @ExportFBOCmd("Framebuffer for render-middle actors used for creating large shadow effects") + fun fborgbactorsmiddleshadow(): FrameBuffer { + return IngameRenderer.extortField("fboRGBactorsMiddleShadow")!! + } + + @ExportFBOCmd("Framebuffer for terrain blocks used for creating large shadow effects") + fun fborgbterrainshadow(): FrameBuffer { + return IngameRenderer.extortField("fboRGBterrainShadow")!! + } } internal annotation class ExportFBOCmd(val description: String) \ No newline at end of file diff --git a/src/net/torvald/terrarum/ui/BlurMgr.kt b/src/net/torvald/terrarum/ui/BlurMgr.kt index b3dd4f71c..833ffba58 100644 --- a/src/net/torvald/terrarum/ui/BlurMgr.kt +++ b/src/net/torvald/terrarum/ui/BlurMgr.kt @@ -7,6 +7,7 @@ import com.badlogic.gdx.graphics.glutils.FrameBuffer import com.jme3.math.FastMath import net.torvald.terrarum.App import net.torvald.terrarum.ceilToInt +import net.torvald.terrarum.gdxClearAndEnableBlend import net.torvald.terrarum.inAction /** @@ -97,6 +98,8 @@ object BlurMgr { val radius3 = FastMath.pow(strength / 2, 0.5f)//(blurRadius - 3f) / 8f fbos.half.inAction(fbos.camera, batch) { + gdxClearAndEnableBlend(0f,0f,0f,0f) + blurtex0 = `in`.colorBufferTexture blurtex0.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear) blurtex0.bind(0) @@ -108,6 +111,8 @@ object BlurMgr { } fbos.quarter.inAction(fbos.camera, batch) { + gdxClearAndEnableBlend(0f,0f,0f,0f) + blurtex1 = fbos.half.colorBufferTexture blurtex1.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear) blurtex1.bind(0) @@ -119,6 +124,8 @@ object BlurMgr { } fbos.half.inAction(fbos.camera, batch) { + gdxClearAndEnableBlend(0f,0f,0f,0f) + blurtex2 = fbos.quarter.colorBufferTexture blurtex2.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear) blurtex2.bind(0) @@ -130,6 +137,8 @@ object BlurMgr { } out.inAction(fbos.camera, batch) { + gdxClearAndEnableBlend(0f,0f,0f,0f) + blurtex3 = fbos.half.colorBufferTexture blurtex3.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear) blurtex3.bind(0) diff --git a/src/net/torvald/terrarum/worlddrawer/BlocksDrawer.kt b/src/net/torvald/terrarum/worlddrawer/BlocksDrawer.kt index b14eada01..ac54fe111 100644 --- a/src/net/torvald/terrarum/worlddrawer/BlocksDrawer.kt +++ b/src/net/torvald/terrarum/worlddrawer/BlocksDrawer.kt @@ -278,7 +278,7 @@ internal object BlocksDrawer { fillInTileBuffer(TERRAIN) // regular tiles fillInTileBuffer(ORES) fillInTileBuffer(FLUID) - fillInTileBuffer(OCCLUSION) +// fillInTileBuffer(OCCLUSION) prepareDrawBuffers() } } @@ -289,7 +289,7 @@ internal object BlocksDrawer { renderUsingBuffer(WALL, projectionMatrix, drawGlow, drawEmissive) gdxBlendMul() - renderUsingBuffer(OCCLUSION, projectionMatrix, false, drawEmissive) +// renderUsingBuffer(OCCLUSION, projectionMatrix, false, drawEmissive) } diff --git a/src/net/torvald/terrarum/worlddrawer/CreateTileAtlas.kt b/src/net/torvald/terrarum/worlddrawer/CreateTileAtlas.kt index 1bbd9524d..bbefe9bd2 100644 --- a/src/net/torvald/terrarum/worlddrawer/CreateTileAtlas.kt +++ b/src/net/torvald/terrarum/worlddrawer/CreateTileAtlas.kt @@ -260,7 +260,7 @@ class CreateTileAtlas { } // test print - PixmapIO2.writeTGA(Gdx.files.absolute("${App.defaultDir}/atlas.tga"), atlas, false) +// PixmapIO2.writeTGA(Gdx.files.absolute("${App.defaultDir}/atlas.tga"), atlas, false) // PixmapIO2.writeTGA(Gdx.files.absolute("${AppLoader.defaultDir}/atlasGlow.tga"), atlasGlow, false) // PixmapIO2.writeTGA(Gdx.files.absolute("${App.defaultDir}/atlas_0.tga"), atlasPrevernal, false) diff --git a/src/shaders/shadowdeep.frag b/src/shaders/shadowdeep.frag new file mode 100644 index 000000000..9588c040f --- /dev/null +++ b/src/shaders/shadowdeep.frag @@ -0,0 +1,18 @@ + +#ifdef GL_ES +precision mediump float; +#endif + +in vec4 v_color; +in vec4 v_generic; +in vec2 v_texCoords; +uniform sampler2D u_texture; +out vec4 fragColor; + +vec4 mult = vec4(0.0, 0.0, 0.0, 1.0); + +void main() { + vec4 incol = texture(u_texture, v_texCoords); + vec4 outcol = vec4(incol.rgb, pow(incol.a, 1.4142)); + fragColor = mult * outcol; +} \ No newline at end of file diff --git a/src/shaders/shadowshallow.frag b/src/shaders/shadowshallow.frag new file mode 100644 index 000000000..bcad2de0f --- /dev/null +++ b/src/shaders/shadowshallow.frag @@ -0,0 +1,18 @@ + +#ifdef GL_ES +precision mediump float; +#endif + +in vec4 v_color; +in vec4 v_generic; +in vec2 v_texCoords; +uniform sampler2D u_texture; +out vec4 fragColor; + +vec4 mult = vec4(0.0, 0.0, 0.0, 0.703); + +void main() { + vec4 incol = texture(u_texture, v_texCoords); + vec4 outcol = vec4(incol.rgb, pow(incol.a, 1.4142)); + fragColor = mult * outcol; +} \ No newline at end of file