简体   繁体   English

OpenGL ES纹理图集失真

[英]OpenGL ES texture atlas distortion

I am trying to tile area with texture atlas and specific shaders but I get weird behavior on different devices. 我正在尝试使用纹理图集和特定的着色器平铺区域,但是在不同的设备上却出现了奇怪的行为。

Most of them (Samsung Galaxy Ace, Samsung Galaxy Gio etc) are ok, just bleeding occurs (hope I can fix it with half-pixel correction). 它们中的大多数(三星Galaxy Ace,三星Galaxy Gio等)都可以,只是出现了出血(希望我可以用半像素校正来修复它)。

But Samsung Galaxy S3 gets next with distance: 但是三星Galaxy S3距离遥遥领先:

Screenshots 截图

(jumping from weaker to even worse distortion zone is also seemed, it happens exactly on regular atlas width or height passing) (似乎也从较弱的变形区跳到了甚至更差的变形区,这恰好发生在规则的图集宽度或高度通过时)

  • OpenGL ES 2.0, Android SDK are used. 使用OpenGL ES 2.0,Android SDK。
  • Atlas and texture part for tiling are both POT (1024 for atlas and 64 for texture part). 用于平铺的图集和纹理部分均为POT(图集为1024,纹理部分为64)。
  • Texture filtering doesn't matter: tried both NEAREST and LINEAR 纹理过滤无所谓:同时尝试了NEARESTLINEAR
  • drawable-nodpi resources folder is used 使用drawable-nodpi资源文件夹
  • mat4, vec2, vec4, sampler2D precisions change does nothing mat4,vec2,vec4,sampler2D精度更改无济于事

What am I doing wrong? 我究竟做错了什么? Is it possible to fix it? 有可能修复它吗?

Shaders: 着色器:

Vertex: 顶点:

    uniform mat4 u_viewmatrix;
    uniform mat4 u_modelmatrix;

    attribute vec4 a_position;

    varying vec2 v_texCoord;

    void main()
    {
        mat4 matrix = u_viewmatrix * u_modelmatrix;
        gl_Position = matrix * a_position;

        v_texCoord = a_position.xy;
    }

Fragment: 分段:

    precision highp float;

    uniform sampler2D u_texture;
    uniform float u_texpartx;
    uniform float u_texparty;
    uniform float u_texpartwidth;
    uniform float u_texpartheight;
    uniform float u_texwidth;
    uniform float u_texheight;
    varying vec2 v_texCoord;

    void main()
    {
        vec2 coord;

        coord.x = (u_texpartx + mod(v_texCoord.x, u_texpartwidth))/u_texwidth;
        coord.y = (u_texparty + mod(v_texCoord.y, u_texpartheight))/u_texheight;

        gl_FragColor = texture2D(u_texture, coord);
    }

Place your images in /assets , not in /res/drawable-nodpi . 将图像放置在/assets ,而不是在/res/drawable-nodpi I place all my content in /assets and have no problems with scaled, optimized or distorted in any other way PNGs. 我将所有内容都放在/assets并且以任何其他方式的PNG缩放,优化或失真都没有问题。

Related question: Android app loading images from /drawables-nodpi/ with scaling 相关问题: Android应用程序通过缩放从/ drawables-nodpi /加载图像

You should check dimensions of your bitmap, however. 但是,您应该检查位图的尺寸。 If it is loaded with proper size then problem is not in scaled bitmap. 如果以适当的大小加载,则问题不在缩放的位图中。

Also I'd recommend to update your shader - you should pass offsets to vertex shader and then pass final coordinates to fragment shader. 另外,我建议您更新着色器-您应该将偏移量传递给顶点着色器,然后将最终坐标传递给片段着色器。 They will interpolate correctly but you will have a whole lot less calculations in fragment shader (which possibly might affect precision on certain hardware, too). 它们可以正确插值,但是片段着色器中的计算量要少得多(这也可能会影响某些硬件的精度)。

Ultimately, you shouldn't make such calculations in shader at all - do this math in your Java code instead and just pass final coordinates for sampler into shader. 最终,您根本不应该在着色器中进行此类计算-而是在Java代码中执行此数学运算,而只是将采样器的最终坐标传递到着色器中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM