Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (791)
Games in Android Showcase (234)
games submitted by our members
Games in WIP (864)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Strange shader problem.  (Read 2167 times)
0 Members and 1 Guest are viewing this topic.
Offline LiquidNitrogen
« Posted 2018-12-07 04:47:08 »

I'm trying to make a shader which can do texture edge wrapping for arbitrary texture regions within an atlas. Generally it works quite well, although there is a strange jittering artifact which I cant figure out how to correct.

These 4 triangles are each drawn with a different sized region of the same texture.

Looking closely, the pixels are misplaced, it is easiest to see on the yellow lines, which should be straight lines.
(there are also some artifacts from edge bleeding, and nearest-neighbour filtering, which dont seem relevant to the problem)

*The problem seems to be only visible when using nearest texture filtering, linear filtering probably hides it enough to be invisible.
*The problem appears and dissappears depending on camera zoom. Specially noticeable when zoom = 1.
*If I offset the texture coords, some values make the problem dissappear. eg: + 0.01 or, + 100.0
*Avoiding using negative texture coord values doesnt help.
*changing precision qualifiers in the shader doesnt seem to make any difference.

Im assuming it is some sort of floating point precision error, which the vertex shaders interpolation naturally accounts for, but my fragment shader code cant account for?

The issue might not be a problem for linear filtered textures, but the intention is to use it for low resolution pixel art with nearest texture filtering, and currently it is ugly.

fragment shader code:
uniform sampler2D u_texture;
varying vec4 v_atlasRegion; // {u1, v1, u2, v2}
varying vec2 v_texCoord;
const float TILE_SIZE = 2048.0 / 32.0; //this should be called howManySingleWorldUnitTilesFitOnTheTexture!

void main(){
   vec2 start = v_atlasRegion.xy;
   vec2 end =;
   vec2 size = end - start;

   vec2 sampleCoord = mod(v_texCoord , size * TILE_SIZE) / TILE_SIZE + start;

   gl_FragColor = texture2D(u_texture, sampleCoord);


The texCoord attribute values in the mesh have the same values as the position attribute, so they should line up nicely.
Offline LiquidNitrogen
« Reply #1 - Posted 2018-12-07 05:36:47 »

Another part of the mystery! There doesnt appear to be any standard egde bleeding present..
I've circled the source tile, added a red rectangle around the exterior of the tiles region, but what is drawn to the screen appears to be some weird sampling from the pink/green area around that (as if its sampling from a different mipmap level there).

perhaps im doing something wrong with the mod function?

Pages: [1]
  ignore  |  Print  

hadezbladez (2847 views)
2018-11-16 13:46:03

hadezbladez (1029 views)
2018-11-16 13:41:33

hadezbladez (2814 views)
2018-11-16 13:35:35

hadezbladez (552 views)
2018-11-16 13:32:03

EgonOlsen (3825 views)
2018-06-10 19:43:48

EgonOlsen (4238 views)
2018-06-10 19:43:44

EgonOlsen (2544 views)
2018-06-10 19:43:20

DesertCoockie (3356 views)
2018-05-13 18:23:11

nelsongames (3470 views)
2018-04-24 18:15:36

nelsongames (4469 views)
2018-04-24 18:14:32
Java Gaming Resources
by philfrei
2019-05-14 16:15:13

Deployment and Packaging
by philfrei
2019-05-08 15:15:36

Deployment and Packaging
by philfrei
2019-05-08 15:13:34

Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!