From 0200fa8803cdf72c39380526c5af4be0ff34ca2b Mon Sep 17 00:00:00 2001 From: minjaesong Date: Mon, 13 Apr 2020 02:13:24 +0900 Subject: [PATCH] simplified the tiling shader a bit --- .gitignore | 2 ++ assets/tiling.frag | 33 +++++++------------ src/net/torvald/terrarum/AppLoader.java | 20 ++++++++--- src/net/torvald/terrarum/GlslTilingTest.kt | 4 +-- .../terrarum/debuggerapp/CSVEditor.java | 7 ++-- .../terrarum/modulebasegame/IngameRenderer.kt | 12 +++---- .../terrarum/worlddrawer/BlocksDrawerNew.kt | 7 ++-- 7 files changed, 43 insertions(+), 42 deletions(-) diff --git a/.gitignore b/.gitignore index ac72da34e..e6e7ba5d5 100644 --- a/.gitignore +++ b/.gitignore @@ -13,6 +13,8 @@ replay_pid* Thumbs.db .DS_Store desktop.ini +.directory +*/.directory # Resources that should not be tracked assets/mods/basegame/demoworld diff --git a/assets/tiling.frag b/assets/tiling.frag index 131bd32af..f79d8a38c 100644 --- a/assets/tiling.frag +++ b/assets/tiling.frag @@ -19,25 +19,22 @@ uniform sampler2D u_texture; uniform vec2 screenDimension; uniform vec2 tilesInAxes; // basically a screen dimension; vec2(tiles_in_horizontal, tiles_in_vertical) -uniform ivec2 tilemapDimension; uniform sampler2D tilemap; // RGBA8888 uniform sampler2D tilesAtlas; // terrain, wire, fluids, etc. uniform sampler2D tilesBlendAtlas; // alternative terrain for the weather mix (e.g. yellowed grass) uniform float tilesBlend = 0.0; // percentage of blending [0f..1f]. 0: draws tilesAtlas, 1: draws tilesBlendAtlas -uniform ivec2 tilesInAtlas = ivec2(256, 256); -uniform ivec2 atlasTexSize = ivec2(4096, 4096); -ivec2 tileSizeInPx = atlasTexSize / tilesInAtlas; // should be like ivec2(16, 16) +uniform vec2 tilesInAtlas = ivec2(256.0, 256.0); +uniform vec2 atlasTexSize = ivec2(4096.0, 4096.0); +vec2 tileSizeInPx = atlasTexSize / tilesInAtlas; // should be like ivec2(16.0, 16.0) uniform vec4 colourFilter = vec4(1, 1, 1, 1); // used by WALL to darken it -uniform ivec2 cameraTranslation = ivec2(0, 0); // used to offset the drawing +uniform ivec2 cameraTranslation = ivec2(0, 0); // used to offset the drawing; it's integer because we want the drawing to be pixel-aligned uniform float drawBreakage = 1.0; // set it to 0f to not draw breakage, 1f to draw it; NEVER set to any other values. -uniform float zoom = 1.0; - ivec2 getTileXY(int tileNumber) { return ivec2(tileNumber % int(tilesInAtlas.x), tileNumber / int(tilesInAtlas.x)); @@ -70,10 +67,8 @@ void main() { // default gl_FragCoord takes half-integer (represeting centre of the pixel) -- could be useful for phys solver? // This one, however, takes exact integer by rounding down. // - vec2 overscannedScreenDimension = tilesInAxes * tileSizeInPx; // how many tiles will fit into a screen; one used by the tileFromMap + vec2 overscannedScreenDimension = tilesInAxes * tileSizeInPx; // how many tiles will fit into a screen; one used by the tileFromMap; we need this because screen size is not integer multiple of the tile size vec2 flippedFragCoord = vec2(gl_FragCoord.x, screenDimension.y - gl_FragCoord.y) + cameraTranslation; // NO IVEC2!!; this flips Y - //vec2 pxCoord = flippedFragCoord.xy; // TODO do I actually need 'pxCoord'? - vec2 zoomVec = vec2(zoom); // get required tile numbers // @@ -83,23 +78,17 @@ void main() { ivec2 tileXY = getTileXY(tile); ivec2 breakageXY = getTileXY(breakage + 5); // +5 is hard-coded constant that depends on the atlas - // cauculate the UV coord value for texture sampling // - - //vec2 coordInTile = mod(pxCoord, tileSizeInPx) / tileSizeInPx; // 0..1 regardless of tile position in atlas // TODO do I actually need 'pxCoord'? - vec2 coordInTile = mod(flippedFragCoord, tileSizeInPx) / tileSizeInPx; // 0..1 regardless of tile position in atlas + // calculate the UV coord value for texture sampling // // don't really need highp here; read the GLES spec - vec2 singleTileSizeInUV = vec2(1) / tilesInAtlas; // constant 0.00390625 for unmodified default uniforms - - vec2 uvCoordForTile = coordInTile * singleTileSizeInUV; // 0..0.00390625 regardless of tile position in atlas - - vec2 uvCoordOffsetTile = tileXY * singleTileSizeInUV; // where the tile starts in the atlas, using uv coord (0..1) - vec2 uvCoordOffsetBreakage = breakageXY * singleTileSizeInUV; + vec2 uvCoordForTile = (mod(flippedFragCoord, tileSizeInPx) / tileSizeInPx) / tilesInAtlas; // 0..0.00390625 regardless of tile position in atlas + vec2 uvCoordOffsetTile = tileXY / tilesInAtlas; // where the tile starts in the atlas, using uv coord (0..1) + vec2 uvCoordOffsetBreakage = breakageXY / tilesInAtlas; // get final UV coord for the actual sampling // - vec2 finalUVCoordForTile = (uvCoordForTile + uvCoordOffsetTile);// where we should be actually looking for in atlas, using UV coord (0..1) - vec2 finalUVCoordForBreakage = (uvCoordForTile + uvCoordOffsetBreakage); + vec2 finalUVCoordForTile = uvCoordForTile + uvCoordOffsetTile;// where we should be actually looking for in atlas, using UV coord (0..1) + vec2 finalUVCoordForBreakage = uvCoordForTile + uvCoordOffsetBreakage; // blending a breakage tex with main tex // diff --git a/src/net/torvald/terrarum/AppLoader.java b/src/net/torvald/terrarum/AppLoader.java index cd115c16b..f5e7bc5d5 100644 --- a/src/net/torvald/terrarum/AppLoader.java +++ b/src/net/torvald/terrarum/AppLoader.java @@ -396,11 +396,11 @@ public class AppLoader implements ApplicationListener { CommonResourcePool.INSTANCE.addToLoadingList("title_health2", () -> new Texture(Gdx.files.internal("./assets/graphics/gui/health_distance.tga"))); // set GL graphics constants - shaderBayerSkyboxFill = loadShader("assets/4096.vert", "assets/4096_bayer_skyboxfill.frag"); - shaderHicolour = loadShader("assets/4096.vert", "assets/hicolour.frag"); + shaderBayerSkyboxFill = loadShaderFromFile("assets/4096.vert", "assets/4096_bayer_skyboxfill.frag"); + shaderHicolour = loadShaderFromFile("assets/4096.vert", "assets/hicolour.frag"); shaderPassthruRGB = SpriteBatch.createDefaultShader(); - shaderColLUT = loadShader("assets/4096.vert", "assets/passthrurgb.frag"); - shaderReflect = loadShader("assets/4096.vert", "assets/reflect.frag"); + shaderColLUT = loadShaderFromFile("assets/4096.vert", "assets/passthrurgb.frag"); + shaderReflect = loadShaderFromFile("assets/4096.vert", "assets/reflect.frag"); fullscreenQuad = new Mesh( true, 4, 6, @@ -1182,7 +1182,7 @@ public class AppLoader implements ApplicationListener { System.out.println("[" + out + "] " + message.toString()); } - public static ShaderProgram loadShader(String vert, String frag) { + public static ShaderProgram loadShaderFromFile(String vert, String frag) { ShaderProgram s = new ShaderProgram(Gdx.files.internal(vert), Gdx.files.internal(frag)); if (s.getLog().toLowerCase().contains("error")) { @@ -1192,6 +1192,16 @@ public class AppLoader implements ApplicationListener { return s; } + public static ShaderProgram loadShaderInline(String vert, String frag) { + ShaderProgram s = new ShaderProgram(vert, frag); + + if (s.getLog().toLowerCase().contains("error")) { + throw new Error(String.format("Shader program loaded with %s, %s failed:\n%s", vert, frag, s.getLog())); + } + + return s; + } + public static void measureDebugTime(String name, kotlin.jvm.functions.Function0 block) { if (IS_DEVELOPMENT_BUILD) { //debugTimers.put(name, kotlin.system.TimingKt.measureNanoTime(block)); diff --git a/src/net/torvald/terrarum/GlslTilingTest.kt b/src/net/torvald/terrarum/GlslTilingTest.kt index 0269df5d2..3303ff711 100644 --- a/src/net/torvald/terrarum/GlslTilingTest.kt +++ b/src/net/torvald/terrarum/GlslTilingTest.kt @@ -152,8 +152,8 @@ object GlslTilingTest : ApplicationAdapter() { shader.setUniformf("tilesInAxes", tilesInHorizontal, tilesInVertical) shader.setUniformf("cameraTranslation", cameraX, cameraY) shader.setUniformi("tileSizeInPx", 16) - shader.setUniformi("tilesInAtlas", 256, 256) //depends on the tile atlas - shader.setUniformi("atlasTexSize", 4096, 4096) //depends on the tile atlas + shader.setUniformf("tilesInAtlas", 256f, 256f) //depends on the tile atlas + shader.setUniformf("atlasTexSize", 4096f, 4096f) //depends on the tile atlas tilesQuad.render(shader, GL20.GL_TRIANGLES) shader.end() tilesBufferAsTex.dispose() diff --git a/src/net/torvald/terrarum/debuggerapp/CSVEditor.java b/src/net/torvald/terrarum/debuggerapp/CSVEditor.java index a47a1bcef..0f1651384 100644 --- a/src/net/torvald/terrarum/debuggerapp/CSVEditor.java +++ b/src/net/torvald/terrarum/debuggerapp/CSVEditor.java @@ -28,12 +28,12 @@ import java.util.Properties; public class CSVEditor extends JFrame { /** Default columns. When you open existing csv, it should overwrite this. */ - private String[] columns = new String[]{"id", "drop", "name", "shdr", "shdg", "shdb", "shduv", "str", "dsty", "mate", "solid", "plat", "wall", "grav", "dlfn", "fv", "fr", "lumr", "lumg", "lumb", "lumuv", "colour", "vscs"}; + private String[] columns = new String[]{"id", "drop", "name", "shdr", "shdg", "shdb", "shduv", "str", "dsty", "mate", "solid", "plat", "wall", "grav", "dlfn", "fv", "fr", "lumr", "lumg", "lumb", "lumuv", "colour", "vscs", "refl"}; private final int FOUR_DIGIT = 42; private final int SIX_DIGIT = 50; private final int TWO_DIGIT = 30; private final int ARBITRARY = 240; - private int[] colWidth = new int[]{FOUR_DIGIT, FOUR_DIGIT, ARBITRARY, SIX_DIGIT, SIX_DIGIT, SIX_DIGIT, SIX_DIGIT, TWO_DIGIT, FOUR_DIGIT, FOUR_DIGIT, TWO_DIGIT, TWO_DIGIT, TWO_DIGIT, TWO_DIGIT, TWO_DIGIT, TWO_DIGIT, TWO_DIGIT, SIX_DIGIT, SIX_DIGIT, SIX_DIGIT, SIX_DIGIT, FOUR_DIGIT * 2, TWO_DIGIT}; + private int[] colWidth = new int[]{FOUR_DIGIT, FOUR_DIGIT, ARBITRARY, SIX_DIGIT, SIX_DIGIT, SIX_DIGIT, SIX_DIGIT, TWO_DIGIT, FOUR_DIGIT, FOUR_DIGIT, TWO_DIGIT, TWO_DIGIT, TWO_DIGIT, TWO_DIGIT, TWO_DIGIT, TWO_DIGIT, TWO_DIGIT, SIX_DIGIT, SIX_DIGIT, SIX_DIGIT, SIX_DIGIT, FOUR_DIGIT * 2, TWO_DIGIT, SIX_DIGIT}; private final int UNDO_BUFFER_SIZE = 10; @@ -501,7 +501,8 @@ public class CSVEditor extends JFrame { "fv=Vertical friction when player slide on the cliff. 0 means not slide-able\n" + "fr=Horizontal friction. <16:slippery 16:regular >16:sticky\n" + "colour=[Fluids] Colour of the block in hexadecimal RGBA.\n" + - "vscs=[Fluids] Viscocity of the block. 16 for water.\n"; + "vscs=[Fluids] Viscocity of the block. 16 for water.\n" + + "refl=[NOT Fluids] Reflectance of the block, used by the light calculation. Valid range 0.0-1.0\n"; /** * ยค is used as a \n marker diff --git a/src/net/torvald/terrarum/modulebasegame/IngameRenderer.kt b/src/net/torvald/terrarum/modulebasegame/IngameRenderer.kt index ff589fbd6..52633044d 100644 --- a/src/net/torvald/terrarum/modulebasegame/IngameRenderer.kt +++ b/src/net/torvald/terrarum/modulebasegame/IngameRenderer.kt @@ -80,11 +80,11 @@ object IngameRenderer : Disposable { // these codes will run regardless of the invocation of the "initialise()" function // the "initialise()" function will also be called init { - shaderBlur = AppLoader.loadShader("assets/blur.vert", "assets/blur.frag") + shaderBlur = AppLoader.loadShaderFromFile("assets/blur.vert", "assets/blur.frag") if (AppLoader.getConfigBoolean("fxdither")) { - shaderBayer = AppLoader.loadShader("assets/4096.vert", "assets/4096_bayer.frag") + shaderBayer = AppLoader.loadShaderFromFile("assets/4096.vert", "assets/4096_bayer.frag") shaderBayer.begin() shaderBayer.setUniformf("rcount", 64f) shaderBayer.setUniformf("gcount", 64f) @@ -92,14 +92,14 @@ object IngameRenderer : Disposable { shaderBayer.end() } else { - shaderBayer = AppLoader.loadShader("assets/4096.vert", "assets/passthrurgb.frag") + shaderBayer = AppLoader.loadShaderFromFile("assets/4096.vert", "assets/passthrurgb.frag") } - shaderBlendGlow = AppLoader.loadShader("assets/blendGlow.vert", "assets/blendGlow.frag") + shaderBlendGlow = AppLoader.loadShaderFromFile("assets/blendGlow.vert", "assets/blendGlow.frag") - shaderRGBOnly = AppLoader.loadShader("assets/4096.vert", "assets/rgbonly.frag") - shaderAtoGrey = AppLoader.loadShader("assets/4096.vert", "assets/aonly.frag") + shaderRGBOnly = AppLoader.loadShaderFromFile("assets/4096.vert", "assets/rgbonly.frag") + shaderAtoGrey = AppLoader.loadShaderFromFile("assets/4096.vert", "assets/aonly.frag") if (!shaderBlendGlow.isCompiled) { diff --git a/src/net/torvald/terrarum/worlddrawer/BlocksDrawerNew.kt b/src/net/torvald/terrarum/worlddrawer/BlocksDrawerNew.kt index d7a2efa1a..8a2af6edf 100644 --- a/src/net/torvald/terrarum/worlddrawer/BlocksDrawerNew.kt +++ b/src/net/torvald/terrarum/worlddrawer/BlocksDrawerNew.kt @@ -86,7 +86,7 @@ internal object BlocksDrawer { private lateinit var tilesQuad: Mesh - private val shader = AppLoader.loadShader("assets/4096.vert", "assets/tiling.frag") + private val shader = AppLoader.loadShaderFromFile("assets/4096.vert", "assets/tiling.frag") init { @@ -637,8 +637,8 @@ internal object BlocksDrawer { shader.setUniformi("tilemapDimension", tilesBuffer.width, tilesBuffer.height) shader.setUniformf("tilesInAxes", tilesInHorizontal.toFloat(), tilesInVertical.toFloat()) shader.setUniformi("cameraTranslation", WorldCamera.x fmod TILE_SIZE, WorldCamera.y fmod TILE_SIZE) // usage of 'fmod' and '%' were depend on the for_x_start, which I can't just do naive int div - /*shader hard-code*/shader.setUniformi("tilesInAtlas", tileAtlas.horizontalCount, tileAtlas.verticalCount) //depends on the tile atlas - /*shader hard-code*/shader.setUniformi("atlasTexSize", tileAtlas.texture.width, tileAtlas.texture.height) //depends on the tile atlas + shader.setUniformf("tilesInAtlas", tileAtlas.horizontalCount.toFloat(), tileAtlas.verticalCount.toFloat()) //depends on the tile atlas + shader.setUniformf("atlasTexSize", tileAtlas.texture.width.toFloat(), tileAtlas.texture.height.toFloat()) //depends on the tile atlas // set the blend value as world's time progresses, in linear fashion shader.setUniformf("tilesBlend", if (world is GameWorldExtension && (mode == TERRAIN || mode == WALL)) drawTIME_T.fmod(SECONDS_IN_MONTH) / SECONDS_IN_MONTH.toFloat() @@ -646,7 +646,6 @@ internal object BlocksDrawer { 0f ) //shader.setUniformf("drawBreakage", if (mode == WIRE) 0f else 1f) - shader.setUniformf("zoom", Terrarum.ingame?.screenZoom ?: 1f) tilesQuad.render(shader, GL20.GL_TRIANGLES) shader.end()