View Full Version : Tiling a certain texture, on a texture atlas

03-07-2016, 01:48 PM
Hey, I'm working on a small voxel engine, and I'm optimizing the rendering by joining multiple blocks faces to a single quad, but I can't find a way to properly texture it

So I was asking if it was even possible

Does GLSL 'texture()' function allows one to do tiling towards a texture atlas? If so, how can I do it?
My shader currently gives me these informations per vertex:

atlasX : the X offset in the texture atlas (integer)
atlasY : the Y offset in the texture atlas (integer)
quadX : the X offset relative to the quad we are rendering
quadY : the Y offset relative to the quad we are rendering

For example if I'm drawing a quad representing 4x4 blocks top face, (quadX, quadY) will be (0, 0), (4, 0), (0, 4), (4, 4)

Thank you, I hope my english was alright

EDIT: Alright i find a way to reach my goal, I'm basically calculating the UV dynamically (using an offset + a modulus) inside the fragment shader

float uvx = atlasX * uuvx + mod(quadX, 1.0) * uuvx;
float uvy = atlasY * uuvy + mod(quadY , 1.0) * uuvy;

03-09-2016, 12:53 PM
If you're using mipmap, please be careful. Since the modulo will cause 'jumps' in UV coordinates along the tile borders, to the GPU
this will look like a large gradient, thus it'll use a very low res mipmap. You'll notice artifacts along the tile borders.
To get around this, sample the texture using textureGrad(). Supply the new coordinates, but use the gradients from the original texture coordinates
(using dFdx()/dFdy() ).
Alternatively, you can also query the Lod Level, that the GPU would use if you sampled at the original position, and provide the level into textureLod().