Need help with mipmaps(SOLVED)

I am trying to add mipmaps to my voxel prototype. But i get some strange artifacts on borders.
Any ideas?
No mipmaps


Mipmaps

I use mesh for chunk(15x15). For that mesh i set texture atlas. Uv i am calculate in shader.

This is my atlas.

This is my shader. Set all data when generate mesh. uvIdx can be [0,3] this is uv coord of one of image corners

attribute highp float data1;
attribute highp float data2;

uniform highp mat4 mtx_worldview;
uniform highp mat4 mtx_world;
uniform highp mat4 mtx_proj;
uniform highp mat4 mtx_normal;
uniform highp vec4 chunks_uv[256*2]; //vec4 is 2 points of uv. Every block have 4 points.

varying highp vec3 var_position;
varying mediump vec3 var_normal;
varying mediump vec2 var_texcoord0;
varying lowp float var_light_power;

vec3 NORMALS[8] = vec3[](
    vec3(0, 1, 0),
    vec3(0, -1, 0),
    vec3(1, 0, 0),
    vec3(-1, 0, 0),
    vec3(0, 0, 1),
    vec3(0, 0, -1),

    vec3(0, 0, 0),
    vec3(0, 0, 0)
);
float LIGHT_POWER[8] = float[8](
    1.0,
    0.4,
    0.8,
    0.75,
    0.6,
    0.55,
    0.0,
    0.0
);

float AO_POWER[4] = float[4](
    0.5,
    0.7,
    0.80,
    1.0
);



void main()
{
    int data = int(data1);
    int data2 = int(data2);

    int x = data2 & 15;
    int z = (data2>>4) & 15;
    int y = (data2>>8) & 255;

    vec3 pos = vec3(x, y, z);


    vec4 p = mtx_worldview * vec4(pos, 1.0);
    var_position = p.xyz;


    int chunkIdx = data & 255;
    int uvIdx = (data>>8) & 3; //0,1,2,3
    int arrayIdx = chunkIdx*2+uvIdx/2;
    highp vec4 uv = chunks_uv[arrayIdx];
    float second = mod(float(uvIdx),2.0);
    float u = uv.x * mod(second+1.0,2.0) + uv.z * second;
    float v = uv.y * mod(second+1.0,2.0) + uv.w * second;
    var_texcoord0 = vec2(u,v);

    int side = (data>>10) & 7;
    int ao = (data>>13) & 3;

    var_normal = NORMALS[side];
    var_light_power = LIGHT_POWER[side]*AO_POWER[ao];

    gl_Position = mtx_proj * p;
}

Thanks.

I understnad problem. Looks like it mipmap bleeding. So in lower mipmap levels near tiles bleed on each other.
When i set extrude borders in atlas at 16. I get less artifacts.

I know all uv points. So i find center of image.In fp i pass uv and uvoffset to center.
Then depend on mipmap level i add delta offset.

    // convert normalized texture coordinates to texel units before calling mip_map_level
    float mipmapLevel = mip_map_level(var_texcoord0 * textureSize(texture0, 0));
    mipmapLevel = clamp(mipmapLevel-1,0,3);
    vec4 overlay_color = texture2D(texture0, var_texcoord0.xy+var_uvToCenter*mipmapLevel/3);
    //vec4 overlay_color = texture2D(texture0, var_texcoord0.xy);

No mipmaps


mipmaps

Fixed mipmaps

5 Likes