越野车纹理坐标映射

Buggy texture coordinate mapping

我找不到我的偏移量到坐标映射的问题,反之亦然,但是一旦尺寸变大,就会反复出现一个问题。这在 Chrome.

上运行

我正在尝试 55x56 纹理,我使用以下例程从逻辑整数偏移映射到 TexCoords 的 uv (st) 并返回。有些错误会累积,导致两个后续偏移量(例如 54 和 55)映射到相同的纹素。

下面添加 halfTexel 的行是在一篇 Whosebug 帖子中找到的(它非常有意义)。在我的着色器开始时,我还有这一行:

// const vec2 halfTexel = vec2(${1.0 / (2.0 * xScale)}, ${1.0 / (2.0 * yScale)});
// xscale is the width (55)
// yscale is the height (56)

precision highp float;
...
vec2 offsetToCoords_A(int offset) {
  const vec2 halfTexel = vec2(0.00909090909090909, 0.008928571428571428);
  const float xScale = 55.0;
  const float yScale = 56.0;
  float offsetF = float(offset);
  float s = mod(offsetF, 55.0);
  float t = floor(offsetF / 55.0);
  vec2 coords = vec2(s/xScale, t/yScale) + halfTexel;
  return coords;
}

int coordsToOffset_A(vec2 coords) {
  const float xScale = 55.0;
  const float yScale = 56.0;
  float s = coords.s * xScale;
  float t = coords.t * yScale;
  int offset = int(t) * 55 + int(s);
  return offset;
}

示例结果:

49,50,51,52,53,54,54,56,57,58,59,
...
106,107,108,109,109,111,112,113

删除了 mod 并将 offsetToCoords 更改为以下内容:

vec2 offsetToCoords_A(int offset) {
  const vec2 halfTexel = vec2(0.00909090909090909, 0.008928571428571428);
  const float xScale = 55.0;
  const float yScale = 56.0;
  int t = offset / int(xScale);
  int s = offset - t*int(xScale);
  vec2 coords = vec2(s,t)/vec2(xScale, yScale) + halfTexel;
  return coords;
}