片段着色器中的 GLSL 转换未按预期工作
GLSL translation in fragment shader not working as expected
我编写了一个简单的 GLSL 着色器,它创建的图案类似于 this page. Then I added code to move the pattern across the screen by following this 示例中显示的彩色方块图案。这是我的代码:
st = vec3(TexCoord.x , TexCoord.y , 0.0);
vec3 translate = vec3(cos(u_time),sin(u_time), 0.0);
st+= translate * 0.1;
st = fract(st * 10);
color = vec3(st.x,st.y,1.0);
一切正常,图案围绕原点做圆周运动。当我想在不使用 u_time 的情况下移动纹理时,问题就出现了,例如。如果我想在 x 方向移动纹理 5 个单位,在 y 方向移动 2 个单位。我尝试了以下代码:
st = vec3(TexCoord.x , TexCoord.y , 0.0);
vec3 translate = vec3(5,2, 0.0); //This line is different
st+= translate * 0.1;
st = fract(st * 10);
color = vec3(st.x,st.y,1.0);
但是当我这样做时纹理根本不会移动,使用更大的值只会拉伸纹理的方向(如果 x = 5000000 纹理在 x 方向拉伸)。
我的问题是为什么会发生这种情况,我该如何纠正?
亲切的问候。
您需要将纹理坐标移动 < 1.0:
vec3 translate = vec3(5,2, 0.0);
vec3 translate = vec3(0.5, 0.2, 0.0);
纹理坐标在 [0.0, 1.0] 范围内。将纹理平移 1 的整数倍与将纹理平移 0.0 具有相同的效果。
请注意,sin
和 cos
函数的结果在 [-1.0, 1.0].
范围内
我编写了一个简单的 GLSL 着色器,它创建的图案类似于 this page. Then I added code to move the pattern across the screen by following this 示例中显示的彩色方块图案。这是我的代码:
st = vec3(TexCoord.x , TexCoord.y , 0.0);
vec3 translate = vec3(cos(u_time),sin(u_time), 0.0);
st+= translate * 0.1;
st = fract(st * 10);
color = vec3(st.x,st.y,1.0);
一切正常,图案围绕原点做圆周运动。当我想在不使用 u_time 的情况下移动纹理时,问题就出现了,例如。如果我想在 x 方向移动纹理 5 个单位,在 y 方向移动 2 个单位。我尝试了以下代码:
st = vec3(TexCoord.x , TexCoord.y , 0.0);
vec3 translate = vec3(5,2, 0.0); //This line is different
st+= translate * 0.1;
st = fract(st * 10);
color = vec3(st.x,st.y,1.0);
但是当我这样做时纹理根本不会移动,使用更大的值只会拉伸纹理的方向(如果 x = 5000000 纹理在 x 方向拉伸)。
我的问题是为什么会发生这种情况,我该如何纠正?
亲切的问候。
您需要将纹理坐标移动 < 1.0:
vec3 translate = vec3(5,2, 0.0);
vec3 translate = vec3(0.5, 0.2, 0.0);
纹理坐标在 [0.0, 1.0] 范围内。将纹理平移 1 的整数倍与将纹理平移 0.0 具有相同的效果。
请注意,sin
和 cos
函数的结果在 [-1.0, 1.0].