我正在尝试处理片段着色器中的纹理变换。 窗口的分辨率为(640,360),旋转为30度,刻度为vec2(0.5,0.5)。
precision mediump float;
varying vec2 v_texCoord;
uniform sampler2D s_texture;
mat3 makeTranslation(vec2 t) {
mat3 m = mat3(1.0, 0.0, 0.0, 0.0, 1.0, 0.0, t.x, t.y, 1.0);
return m;
}
mat3 makeRotation( float angleInRadians ){
float c = cos(angleInRadians);
float s = sin(angleInRadians);
mat3 m = mat3(c, -s, 0, s, c, 0, 0, 0, 1);
return m;
}
mat3 makeScale(vec2 s) {
mat3 m = mat3( s.x, 0, 0, 0, s.y, 0, 0, 0, 1);
return m;
}
void main(){
vec2 position = vec2(0.0,0.0);
vec2 scale = vec2(0.5,0.5);
float rotation = 30.0;
float r = rotation/180.0*3.14159;
vec2 size = vec2(640.0,480.0);
mat3 mt = makeTranslation( translation );
mat3 mr = makeRotation( r );
mat3 ms = makeScale( 1.0/scale );
//transform
vec3 newCoord = vec3(v_texCoord.xy,1.0);
newCoord = mt*newCoord;
newCoord = mr*ms*vec3(newCoord.x - 0.5, newCoord.y - 0.5,0.0) + vec3(0.5, 0.5, 0.0);
gl_FragColor = texture2D(s_texture, vec2(newCoord.x, newCoord.y) );
}
所以,我将矩形大小的比率应用于texcoord.y:
//transform
float fy = 0.5*(1.0 - size.y*1.0/size.x);
newCoord.y = (newCoord.y-0.5)*size.y/size.x+fy;
newCoord = mt*newCoord; \n"
newCoord = mr*ms*vec3(newCoord.x - 0.5, newCoord.y - 0.5,0.0) + vec3(0.5, 0.5, 0.0);
newCoord.y = (newCoord.y+0.5)*size.x/size.y-fy;
那么,如何获得正确的结果呢? 感谢。
答案 0 :(得分:1)
获得正确的操作顺序非常重要。
当您收到纹理坐标时,它们在[0,1]范围内。但是,在旋转它们之前,需要将它们转换为[-0.5,0.5],以便围绕纹理的中心旋转。然后应用比例,最后应用翻译。