我找不到用偏移量进行坐标转换的问题,反之亦然,但是,一旦尺寸稍微变大,就会出现非常反复的问题。这可以在Chrome上运行。
我正在尝试使用55x56纹理,并且我使用以下例程将逻辑整数偏移量映射到TexCoords的uv(st)并返回。在某处累积了一些错误,从而导致两个后续偏移(例如54和55)映射到同一纹理像素。
下面的HalfTexel添加行在StackOverflow帖子之一中找到(这非常有意义)。在着色器的开头,我也有以下一行:
// 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
答案 0 :(得分:0)
删除了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;
}