shadows around actors as well as terrain

This commit is contained in:
minjaesong
2024-11-24 17:01:54 +09:00
parent 5bf60cfa82
commit 8cf4b5d9a9
7 changed files with 102 additions and 10 deletions

View File

@@ -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()

View File

@@ -117,6 +117,21 @@ internal object ExportFBO : ConsoleCommand {
fun fborgbterrain(): FrameBuffer {
return IngameRenderer.extortField<Float16FrameBuffer>("fboRGBterrain")!!
}
@ExportFBOCmd("Framebuffer for render-behind actors used for creating shallow shadow effects")
fun fborgbactorsbehindshadow(): FrameBuffer {
return IngameRenderer.extortField<Float16FrameBuffer>("fboRGBactorsBehindShadow")!!
}
@ExportFBOCmd("Framebuffer for render-middle actors used for creating large shadow effects")
fun fborgbactorsmiddleshadow(): FrameBuffer {
return IngameRenderer.extortField<Float16FrameBuffer>("fboRGBactorsMiddleShadow")!!
}
@ExportFBOCmd("Framebuffer for terrain blocks used for creating large shadow effects")
fun fborgbterrainshadow(): FrameBuffer {
return IngameRenderer.extortField<Float16FrameBuffer>("fboRGBterrainShadow")!!
}
}
internal annotation class ExportFBOCmd(val description: String)

View File

@@ -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)

View File

@@ -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)
}

View File

@@ -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)

View File

@@ -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;
}

View File

@@ -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;
}