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_SIZED
import net.torvald.terrarum.TerrarumAppConfiguration.TILE_SIZEF import net.torvald.terrarum.TerrarumAppConfiguration.TILE_SIZEF
import net.torvald.terrarum.blockproperties.Block import net.torvald.terrarum.blockproperties.Block
import net.torvald.terrarum.blockproperties.FluidCodex
import net.torvald.terrarum.gameactors.ActorWithBody import net.torvald.terrarum.gameactors.ActorWithBody
import net.torvald.terrarum.gameactors.ActorWithBody.Companion.METER import net.torvald.terrarum.gameactors.ActorWithBody.Companion.METER
import net.torvald.terrarum.gameactors.ActorWithBody.Companion.PHYS_EPSILON_DIST 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.gameactors.Pocketed
import net.torvald.terrarum.modulebasegame.gameitems.ItemThrowable import net.torvald.terrarum.modulebasegame.gameitems.ItemThrowable
import net.torvald.terrarum.modulebasegame.gameitems.getThrowPosAndVector import net.torvald.terrarum.modulebasegame.gameitems.getThrowPosAndVector
import net.torvald.terrarum.ui.BlurMgr
import net.torvald.terrarum.ui.Toolkit import net.torvald.terrarum.ui.Toolkit
import net.torvald.terrarum.weather.WeatherMixer import net.torvald.terrarum.weather.WeatherMixer
import net.torvald.terrarum.worlddrawer.BlocksDrawer 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 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 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 rgbTex: TextureRegion
private lateinit var aTex: TextureRegion private lateinit var aTex: TextureRegion
private lateinit var mixedOutTex: TextureRegion private lateinit var mixedOutTex: TextureRegion
@@ -109,6 +114,8 @@ object IngameRenderer : Disposable {
val shaderBlendGlow: ShaderProgram val shaderBlendGlow: ShaderProgram
val shaderBlendGlowTex1Flip: ShaderProgram val shaderBlendGlowTex1Flip: ShaderProgram
val shaderForActors: ShaderProgram val shaderForActors: ShaderProgram
val shaderShadowShallow: ShaderProgram
val shaderShadowDeep: ShaderProgram
val shaderDemultiply: ShaderProgram val shaderDemultiply: ShaderProgram
val shaderBayerAlpha: ShaderProgram val shaderBayerAlpha: ShaderProgram
@@ -151,6 +158,8 @@ object IngameRenderer : Disposable {
shaderForActors = App.loadShaderFromClasspath("shaders/default.vert", "shaders/actors.frag") 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") shaderBlendGlow = App.loadShaderFromClasspath("shaders/blendGlow.vert", "shaders/blendGlow.frag")
shaderBlendGlowTex1Flip = App.loadShaderFromClasspath("shaders/blendGlow.vert", "shaders/blendGlowTex1Flip.frag") shaderBlendGlowTex1Flip = App.loadShaderFromClasspath("shaders/blendGlow.vert", "shaders/blendGlowTex1Flip.frag")
shaderDemultiply = App.loadShaderFromClasspath("shaders/blendGlow.vert", "shaders/demultiply.frag") shaderDemultiply = App.loadShaderFromClasspath("shaders/blendGlow.vert", "shaders/demultiply.frag")
@@ -511,9 +520,11 @@ object IngameRenderer : Disposable {
batch.shader = shaderForActors batch.shader = shaderForActors
batch.color = Color.WHITE batch.color = Color.WHITE
moveCameraToWorldCoord() moveCameraToWorldCoord()
actorsRenderFarBehind?.forEach { it.drawBody(frameDelta, batch) }
actorsRenderBehind?.forEach { it.drawBody(frameDelta, batch) } actorsRenderBehind?.forEach { it.drawBody(frameDelta, batch) }
} }
} }
BlurMgr.makeBlur(fboRGBactorsBehind, fboRGBactorsBehindShadow, 0.25f)
fboRGBactorsMiddle.inAction(camera, batch) { fboRGBactorsMiddle.inAction(camera, batch) {
clearBuffer() clearBuffer()
@@ -526,12 +537,14 @@ object IngameRenderer : Disposable {
actorsRenderMiddle?.forEach { it.drawBody(frameDelta, batch) } actorsRenderMiddle?.forEach { it.drawBody(frameDelta, batch) }
} }
} }
BlurMgr.makeBlur(fboRGBactorsMiddle, fboRGBactorsMiddleShadow, 2.5f)
fboRGBterrain.inAction(camera, batch) { fboRGBterrain.inAction(camera, batch) {
clearBuffer() clearBuffer()
setCameraPosition(0f, 0f) setCameraPosition(0f, 0f)
BlocksDrawer.drawTerrain(batch.projectionMatrix, false) BlocksDrawer.drawTerrain(batch.projectionMatrix, false)
} }
BlurMgr.makeBlur(fboRGBterrain, fboRGBterrainShadow, 2.5f)
fboRGB.inAction(camera, batch) { fboRGB.inAction(camera, batch) {
@@ -539,24 +552,36 @@ object IngameRenderer : Disposable {
BlocksDrawer.drawWall(batch.projectionMatrix, false) 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.inUse {
batch.shader = shaderForActors batch.shader = shaderForActors
batch.color = Color.WHITE batch.color = Color.WHITE
moveCameraToWorldCoord()
actorsRenderFarBehind?.forEach { it.drawBody(frameDelta, batch) }
// actorsRenderBehind?.forEach { it.drawBody(frameDelta, batch) }
setCameraPosition(0f, 0f) setCameraPosition(0f, 0f)
batch.color = Color.WHITE
batch.drawFlipped(fboRGBactorsBehind.colorBufferTexture, 0f, 0f) batch.drawFlipped(fboRGBactorsBehind.colorBufferTexture, 0f, 0f)
moveCameraToWorldCoord() moveCameraToWorldCoord()
particlesContainer?.forEach { it.drawBody(frameDelta, batch) } particlesContainer?.forEach { it.drawBody(frameDelta, batch) }
} }
// draw just the terrain
batch.inUse { batch.inUse {
batch.shader = null batch.shader = null
batch.color = Color.WHITE
setCameraPosition(0f, 0f) setCameraPosition(0f, 0f)
batch.color = Color.WHITE
batch.drawFlipped(fboRGBterrain.colorBufferTexture, 0f, 0f) batch.drawFlipped(fboRGBterrain.colorBufferTexture, 0f, 0f)
} }
@@ -566,9 +591,8 @@ object IngameRenderer : Disposable {
///////////////// /////////////////
// draw actors // // draw actors //
///////////////// /////////////////
// moveCameraToWorldCoord()
// actorsRenderMiddle?.forEach { it.drawBody(frameDelta, batch) }
setCameraPosition(0f, 0f) setCameraPosition(0f, 0f)
batch.color = Color.WHITE
batch.drawFlipped(fboRGBactorsMiddle.colorBufferTexture, 0f, 0f) batch.drawFlipped(fboRGBactorsMiddle.colorBufferTexture, 0f, 0f)
moveCameraToWorldCoord() moveCameraToWorldCoord()
@@ -1261,6 +1285,9 @@ object IngameRenderer : Disposable {
fboRGBactorsBehind = Float16FrameBuffer(width, height, false) fboRGBactorsBehind = Float16FrameBuffer(width, height, false)
fboRGBactorsMiddle = Float16FrameBuffer(width, height, false) fboRGBactorsMiddle = Float16FrameBuffer(width, height, false)
fboRGBterrain = 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( lightmapFbo = Float16FrameBuffer(
LightmapRenderer.lightBuffer.width * LightmapRenderer.DRAW_TILE_SIZE.toInt(), LightmapRenderer.lightBuffer.width * LightmapRenderer.DRAW_TILE_SIZE.toInt(),
LightmapRenderer.lightBuffer.height * 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 (::fboRGBactorsBehind.isInitialized) fboRGBactorsBehind.tryDispose()
if (::fboRGBactorsMiddle.isInitialized) fboRGBactorsMiddle.tryDispose() if (::fboRGBactorsMiddle.isInitialized) fboRGBactorsMiddle.tryDispose()
if (::fboRGBterrain.isInitialized) fboRGBterrain.tryDispose() if (::fboRGBterrain.isInitialized) fboRGBterrain.tryDispose()
if (::fboRGBactorsBehindShadow.isInitialized) fboRGBactorsBehindShadow.tryDispose()
if (::fboRGBactorsMiddleShadow.isInitialized) fboRGBactorsMiddleShadow.tryDispose()
if (::fboRGBterrainShadow.isInitialized) fboRGBterrainShadow.tryDispose()
blurtex0.tryDispose() blurtex0.tryDispose()
@@ -1349,6 +1379,8 @@ object IngameRenderer : Disposable {
shaderBlendGlow.dispose() shaderBlendGlow.dispose()
shaderBlendGlowTex1Flip.dispose() shaderBlendGlowTex1Flip.dispose()
shaderForActors.dispose() shaderForActors.dispose()
shaderShadowShallow.dispose()
shaderShadowDeep.dispose()
shaderDemultiply.dispose() shaderDemultiply.dispose()
shaderBayerAlpha.dispose() shaderBayerAlpha.dispose()

View File

@@ -117,6 +117,21 @@ internal object ExportFBO : ConsoleCommand {
fun fborgbterrain(): FrameBuffer { fun fborgbterrain(): FrameBuffer {
return IngameRenderer.extortField<Float16FrameBuffer>("fboRGBterrain")!! 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) 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 com.jme3.math.FastMath
import net.torvald.terrarum.App import net.torvald.terrarum.App
import net.torvald.terrarum.ceilToInt import net.torvald.terrarum.ceilToInt
import net.torvald.terrarum.gdxClearAndEnableBlend
import net.torvald.terrarum.inAction import net.torvald.terrarum.inAction
/** /**
@@ -97,6 +98,8 @@ object BlurMgr {
val radius3 = FastMath.pow(strength / 2, 0.5f)//(blurRadius - 3f) / 8f val radius3 = FastMath.pow(strength / 2, 0.5f)//(blurRadius - 3f) / 8f
fbos.half.inAction(fbos.camera, batch) { fbos.half.inAction(fbos.camera, batch) {
gdxClearAndEnableBlend(0f,0f,0f,0f)
blurtex0 = `in`.colorBufferTexture blurtex0 = `in`.colorBufferTexture
blurtex0.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear) blurtex0.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear)
blurtex0.bind(0) blurtex0.bind(0)
@@ -108,6 +111,8 @@ object BlurMgr {
} }
fbos.quarter.inAction(fbos.camera, batch) { fbos.quarter.inAction(fbos.camera, batch) {
gdxClearAndEnableBlend(0f,0f,0f,0f)
blurtex1 = fbos.half.colorBufferTexture blurtex1 = fbos.half.colorBufferTexture
blurtex1.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear) blurtex1.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear)
blurtex1.bind(0) blurtex1.bind(0)
@@ -119,6 +124,8 @@ object BlurMgr {
} }
fbos.half.inAction(fbos.camera, batch) { fbos.half.inAction(fbos.camera, batch) {
gdxClearAndEnableBlend(0f,0f,0f,0f)
blurtex2 = fbos.quarter.colorBufferTexture blurtex2 = fbos.quarter.colorBufferTexture
blurtex2.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear) blurtex2.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear)
blurtex2.bind(0) blurtex2.bind(0)
@@ -130,6 +137,8 @@ object BlurMgr {
} }
out.inAction(fbos.camera, batch) { out.inAction(fbos.camera, batch) {
gdxClearAndEnableBlend(0f,0f,0f,0f)
blurtex3 = fbos.half.colorBufferTexture blurtex3 = fbos.half.colorBufferTexture
blurtex3.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear) blurtex3.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear)
blurtex3.bind(0) blurtex3.bind(0)

View File

@@ -278,7 +278,7 @@ internal object BlocksDrawer {
fillInTileBuffer(TERRAIN) // regular tiles fillInTileBuffer(TERRAIN) // regular tiles
fillInTileBuffer(ORES) fillInTileBuffer(ORES)
fillInTileBuffer(FLUID) fillInTileBuffer(FLUID)
fillInTileBuffer(OCCLUSION) // fillInTileBuffer(OCCLUSION)
prepareDrawBuffers() prepareDrawBuffers()
} }
} }
@@ -289,7 +289,7 @@ internal object BlocksDrawer {
renderUsingBuffer(WALL, projectionMatrix, drawGlow, drawEmissive) renderUsingBuffer(WALL, projectionMatrix, drawGlow, drawEmissive)
gdxBlendMul() gdxBlendMul()
renderUsingBuffer(OCCLUSION, projectionMatrix, false, drawEmissive) // renderUsingBuffer(OCCLUSION, projectionMatrix, false, drawEmissive)
} }

View File

@@ -260,7 +260,7 @@ class CreateTileAtlas {
} }
// test print // 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("${AppLoader.defaultDir}/atlasGlow.tga"), atlasGlow, false)
// PixmapIO2.writeTGA(Gdx.files.absolute("${App.defaultDir}/atlas_0.tga"), atlasPrevernal, 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;
}