进行亚像素转换时2D纹理失真 [英] 2D Texture gets distorted when making a sub-pixel translation

查看:94
本文介绍了进行亚像素转换时2D纹理失真的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道一个理论推理,为什么这甚至可能:几何图形的平移如何与纹理贴图相关.

I would like to know a theoretical reasoning why is this even possible: how translation of the geometry is related to the texture mapping.

  • 我只能在进行亚像素转换时注意到这种效果,如果整个像素进行转换,纹理看起来就很好.
  • 我正在使用正投影,GL_CLAMP_TO_EDGEGL_NEAREST,片段着色器是highp.
  • 纹理坐标是地图集的一个子区域,但是即使整个地图集都已映射,我也可以看到变形.
  • 使用OpenGL ES(Android和iOS),但是请在重新标记之前,有人可以解释这不是OpenGL中的问题.
  • I can only notice this effect when making a sub-pixel translation, texture looks fine if translated by entire pixel(s).
  • I am using orthographic projection, GL_CLAMP_TO_EDGE, GL_NEAREST, fragment shader is highp.
  • Texture coordinates is a subregion of the atlas, but I can see the distortion even if the entire atlas is mapped.
  • Using OpenGL ES (Android and iOS), but please before retagging could anyone explain that this is not an issue in OpenGL.

我尝试过的事情:

推荐答案

按子像素量移动将始终会更改纹理采样.使用最近的采样会影响纹理的采样方式,但不会影响首先采样的坐标.该坐标是插在多边形上的tex坐标和顶点的函数,顶点决定了如何将多边形栅格化为屏幕像素.

Moving by sub-pixel amounts will always alter the texture sampling. That you use nearest sampling affects how the texture is sampled, but not at what coordinate it is sampled at in the first place. That coordinate is a function of both the tex-coords, interpolated across the polygon, and the vertices, which determine how the polygon is rasterised to screen pixels.

请记住,要获得完美的纹理采样,在像素中心进行评估时,内插的tex坐标必须恰好位于texel中心.

Remember that to get perfect texture sampling the interpolated tex-coords, when evaluated at pixel centres, must exactly lie on texel centres.

因此,将多边形移动一个像素的一小部分会更改从中采样纹理的位置,因为像素中心现在位于多边形内的稍有不同的点上.

Moving the polygon by a fraction of a pixel therefor changes where the texture is sampled from, as the pixel centres now lie at slightly different point within the polygon.

现在,使用最近的采样,生成的坐标将有效地四舍五入到最近的纹理像素,因此,如果您的坐标离中心不远,则可能会起作用.但是,当您的坐标移动了大约半个纹理像素时,它们正好位于两个纹理像素几乎等距的点上,那么在计算精度上只需很小的误差就可以以一种方式微调某些像素,而以另一种方式微调某些像素.

Now, with nearest sampling, the resulting coordinates will effectively be rounded back to the nearest texels, so if your coordinates are not far from the centres, it'll probably work. However when your coordinates have moved by around half a texel, so they lie right at the point where two texels are almost equidistant, then it only takes tiny errors on the precision of the calculations to nudge some pixels one way, and some the other.

这是一张图.我选择显示一个三角形,并映射有一个检查图案,但它同样适用于sprite或其他任何东西.

Here's a diagram. I've chosen to show a triangle, mapped with a check pattern, but it applies equally well to a sprite, or anything else.

在图的顶部,我只是显示三角形如何映射到纹理.如果您在非常高分辨率的屏幕上(或在非常大的尺寸上)渲染了该三角形,那么您会希望看到该复制的检查图完全复制.

In the top part of the diagram, I'm just showing how the triangle is mapped to the texture. If you rendered this triangle on an very high-resolution screen (or at a very large size) you'd expect to see that check pattern exactly replicated.

但是,如果您以更近的比例(例如1:1)进行映射,则采样至关重要.在图的底部显示了这种情况.

However if you're mapping at a closer ratio, such as 1:1, the sampling is critical. In the bottom part of the diagram I show this situation.

要查看的关键是屏幕像素中心在哪里,因为它确定了绘制哪些像素以及纹理的采样位置.在这种情况下,我已经显示了多边形以正确的尺寸进行了映射,但是偏移了半个像素.这样可以将像素中心精确地对齐在相邻纹理像素之间,并且采样将非常抽搐.

The key thing to look at is where the screen-pixel centres are, because that determines which pixels are drawn, and also where the texture is sampled at. In this case I've shown the polygon being mapped at exactly the right size, but offset by half a pixel. That aligns the pixel centres exactly between adjacent texels, and the sampling will be pretty twitchy.

这篇关于进行亚像素转换时2D纹理失真的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆