gpt4 book ai didi

glsl - 如何降低 GLSL 中 sin() 波的频率?

转载 作者:行者123 更新时间:2023-12-05 04:11:44 25 4
gpt4 key购买 nike

问题引用:

http://thebookofshaders.com/03/

#ifdef GL_ES
precision mediump float;
#endif

uniform float u_time;

void main() {
gl_FragColor = vec4(abs(sin(u_time)),0.0,0.0,1.0);
}

Live Code Example

Now it is time again to play with the above code.

1 - Slow down the frequency until the color change becomes almost imperceptible.

2 - Speed it up until you see a single color without flickering.

3 - Play with the three channels (RGB) in different frequencies to get interesting patterns and behaviors.

对于第一个问题:

1 - Slow down the frequency until the color change becomes almost imperceptible.

我尝试除法/乘法 u_time 产生语法错误。 uniform float u_time/2;//错误,我只是很难理解作者想让我学习的内容。在没有理解的情况下,我很难继续阅读。

也许作者的问题希望我不要使用 abs(sin(u_time)) 而是另一个函数:

The GPU has hardware accelerated angle, trigonometric and exponential functions.

Some of those functions are: sin(), cos(), tan(), asin(), acos(), atan(), pow(), exp(), log(), sqrt(), abs(), sign(), floor(), ceil(), fract(), mod(), min(), max() and clamp().

如果是这样,我将使用哪个函数来完成下面所述的问题?

我还能得到帮助以了解如何解决:2 和 3

2 - Speed it up until you see a single color without flickering.

3 - Play with the three channels (RGB) in different frequencies to get interesting patterns and behaviors.

最佳答案

不了解勿进!实际上,我非常怀疑你是否能做到这一点。它是语言的核心部分!


要在 GLSL 中重新分配一个变量,语法是这样的:

variable = new_value;

并且由于您希望新值依赖于旧值,因此您也只需在那里使用变量:

variable = variable * 2;

但是 GLSL 对它的数字类型很挑剔,所以因为 u_time 是一个 float ,你必须用一个 float 乘(或除,或加,或任何你做的):

u_time = u_time * 2.0f;

现在您将时间乘以 2,这意味着频率翻倍!或者如果你不想改变变量的值,只改变传递给颜色的值,你可以像在 JS 中那样做(我看到你以前有过这方面的经验,所以这就是我正在比较的,但它是跨语言相当标准):

gl_FragColor = vec4(abs(sin(u_time * 2.0f)),0.0,0.0,1.0);

#2 只是将 2 更改为除法,或者使用 0 到 1 之间的数字。

对于 #3,您可以为 vec4 构造函数的其他字段使用非常量值,例如,您可以使用更多正弦波并添加一些偏移量,以制作多种颜色。


关于您的错误的更多技术信息:

语法

modifier type name = value;

保留用于变量声明*,它应该只发生一次(就像 JavaScript 中的 var)。

但是,您可以根据需要更改已声明变量的值,其语法就是

name = value;

value 可以是您想要的任何表达式(函数调用的结果、常量、某些运算符链的结果,如乘法等)。

*等号和初值可选

关于glsl - 如何降低 GLSL 中 sin() 波的频率?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42128408/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com