代码之家  ›  专栏  ›  技术社区  ›  Jeff Saremi

错误纹理坐标映射

  •  0
  • Jeff Saremi  · 技术社区  · 6 年前

    我正在尝试55x56纹理,并使用以下例程从逻辑整数偏移映射到TexCoords的uv(st)和back。在这里,一些错误被累积,导致两个随后的偏移量(如54和55)映射到同一个texel。

    下面添加halfTexel的行是在StackOverflow的一个post中找到的(这非常有意义)。在材质球的开头,我还有一行:

    // 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
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   Jeff Saremi    6 年前

    删除了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;
    }