Skip to content

Commit

Permalink
feat: clean line alias on tile border
Browse files Browse the repository at this point in the history
  • Loading branch information
jmecn committed Apr 13, 2024
1 parent 083f8b7 commit 03d706c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 13 deletions.
24 changes: 22 additions & 2 deletions lib/src/main/resources/Shader/Tiled.frag
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,33 @@ uniform vec4 m_Color;
uniform sampler2D m_ColorMap;
#endif

varying vec2 texCoord;
#ifdef USE_TILESET_IMAGE
uniform vec2 m_ImageSize;
uniform vec4 m_TileSize;//(width, height, margin, space)
varying vec2 v_TilePos;
#endif

varying vec2 v_TexCoord;

vec2 getTileUVClamped(vec2 tilePos, vec2 tileSize, vec2 imageSize) {
vec2 pixel = v_TexCoord * tileSize + tilePos;
vec2 min = vec2(tilePos + 0.5);
vec2 max = vec2(tilePos + tileSize - 0.5);
vec2 uv = clamp(pixel, min, max) / imageSize;
uv.y = 1.0 - uv.y;
return uv;
}

void main(){
vec4 color = vec4(1.0);

#ifdef HAS_COLOR_MAP
vec2 uv = texCoord;
vec2 uv = v_TexCoord;

#ifdef USE_TILESET_IMAGE
uv = getTileUVClamped(v_TilePos, m_TileSize.xy, m_ImageSize.xy);
#endif

color *= texture2D(m_ColorMap, uv);
#endif

Expand Down
16 changes: 6 additions & 10 deletions lib/src/main/resources/Shader/Tiled.vert
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,21 @@

attribute vec3 inPosition;
attribute vec2 inTexCoord;
attribute vec4 inColor;

#ifdef USE_TILESET_IMAGE
// use texcoord2 as tile position
attribute vec3 inTexCoord2;
uniform vec2 m_ImageSize;
uniform vec4 m_TileSize;
// pass it to fragment shader
varying vec2 v_TilePos;
#endif

varying vec2 texCoord;
varying vec2 v_TexCoord;

void main() {
texCoord = inTexCoord;
v_TexCoord = inTexCoord;

#ifdef USE_TILESET_IMAGE
// calculate the UV coordinates for the tileset image
vec2 tilePos = inTexCoord2.xy;
vec2 uv = (texCoord * m_TileSize.xy + tilePos) / m_ImageSize.xy;
uv.y = 1.0 - uv.y;
texCoord = uv;
v_TilePos = inTexCoord2.xy;
#endif

vec3 position = inPosition;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public void simpleInitApp() {

TiledMapAppState tiledMap = stateManager.getState(TiledMapAppState.class);
tiledMap.setMap(map);
tiledMap.setViewColumn(20);
//tiledMap.setViewColumn(20);
}

public static void main(String[] args) {
Expand Down

0 comments on commit 03d706c

Please sign in to comment.