效果如图所示:
放大:
缩小:
核心算法那很简单,就不多说了,给个公式:
r = (x^2 + y^2)^0.5
x' = r' * cosΘ = (r / (r+K)) * x/r = x / (r+K);
y' = r' * cosΘ = (r / (r+K)) * y/r = y / (r+K);
注:K是常数,取值范围(0, +∞)
核心代码:
vec2 pos = vec2(uv.x, uv.y);
pos -= vec2(uTransX, uTransY);
pos = 2.*uv - vec2(1., 1.); // 坐标系的转换
float r = length(pos);
vec2 pos2 = pos;
if(r<R)
{
pos2 = vec2(pos * (r+uK)); // 放大
//pos2 = vec2(pos / (r+uK)); // 缩小
}
pos2 = (pos2 + vec2(1., 1.)) * .5; // 坐标系的恢复
vec4 color = texture2D(Texture0, pos2);
gl_FragColor = color;