对使用数组着色的着色器进行动画处理

发布于 2024-12-29 19:45:14 字数 702 浏览 1 评论 0原文

所以我有一种锯齿形图案,如下所示。ZigZag Pattern,它是由以下片段着色器创建的

uniform float time;
varying vec2 texture_coord;
void main()
{
    float wav[10] = float[10](0,.1,.2,.1,0,-.1,-.2,-.1,0,.1);
    //gl_FragColor = gl_Color;
    float mod_time = mod(time, 1);
    float x_pos = mod(texture_coord.x, 1.1);
    float x_pos2 = x_pos * 10;
    int index = int(x_pos2);
    if(texture_coord.y < .5 + wav[index])
        gl_FragColor = vec4(.7,.3,.3,1.0);
    else
        gl_FragColor = vec4(.3,.3,.3,1.0);
}

:我想通过让之字形向上移动来制作动画。

我的问题是,考虑到我正在使用数组来创建与中位数的偏移量,我将如何做到这一点?我不太确定如何调整数组,以便在下一个动画步骤中,数组看起来像 (.1,.2,.1,0,-.1,-.2,-.1,0, .1)?

So I have a sort of zigzag pattern, as shown below.ZigZag Pattern, which is created by the following fragment shader:

uniform float time;
varying vec2 texture_coord;
void main()
{
    float wav[10] = float[10](0,.1,.2,.1,0,-.1,-.2,-.1,0,.1);
    //gl_FragColor = gl_Color;
    float mod_time = mod(time, 1);
    float x_pos = mod(texture_coord.x, 1.1);
    float x_pos2 = x_pos * 10;
    int index = int(x_pos2);
    if(texture_coord.y < .5 + wav[index])
        gl_FragColor = vec4(.7,.3,.3,1.0);
    else
        gl_FragColor = vec4(.3,.3,.3,1.0);
}

which I would like to animate by having the zigzag move upwards.

My question is, how would I do this, considering that I'm using an array to create the offset from the median? I'm not exactly sure how I would adjust the array so that in the next animation step, the array looks like (.1,.2,.1,0,-.1,-.2,-.1,0,.1)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

真心难拥有 2025-01-05 19:45:14

有两种方法可以做到这一点。您可以为数组中的偏移设置动画(可能是最简单的),也可以为数组本身设置动画。您已经传入了一个时间参数,因此您可以使用它,如下所示:

if (texture_coord.y < 0.5 + wav [ (index + (mod_time * 10)) % 10 ]) // Note you may have to calculate the "%" operator yourself
... etc. ...

或者您可以传入数组。要传入数组,您只需获取数组第一个元素的统一位置,然后将其递增以获取后面的值。因此,在源代码中,您可以执行以下操作:

片段着色器:

uniform float wav [ 10 ];
... rest of fragment shader ...

源代码:

wavLoc = glGetUniformLocation (program, "wav");
offset++; // This starts at 0 and is incremented on each frame you want to advance the pattern
for (int i = 0; i < 10; i++)
{
    glUniform1f (wavLoc + i, wavePattern [ (i + offset) % 10 ]);
}

There are 2 ways you could do it. You can either animate the offset into the array (probably the easiest) or you can animate the array itself. You're already passing in a time parameter, so you could use that, like so:

if (texture_coord.y < 0.5 + wav [ (index + (mod_time * 10)) % 10 ]) // Note you may have to calculate the "%" operator yourself
... etc. ...

Or you could pass in the array. To pass in an array, you simply get the uniform location of the first element of the array, and increment it for later values. So in your source code, you could do this:

Fragment Shader:

uniform float wav [ 10 ];
... rest of fragment shader ...

Source Code:

wavLoc = glGetUniformLocation (program, "wav");
offset++; // This starts at 0 and is incremented on each frame you want to advance the pattern
for (int i = 0; i < 10; i++)
{
    glUniform1f (wavLoc + i, wavePattern [ (i + offset) % 10 ]);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文