我正在尝试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